2018-02-12 23:45:48 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2018-02-14 23:04:26 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
2018-02-12 23:45:48 +01:00
|
|
|
{
|
2018-02-15 22:06:49 +01:00
|
|
|
"Client 8.2 :Profiles"
|
2018-02-12 23:45:48 +01:00
|
|
|
};
|
|
|
|
|
2018-02-14 23:04:26 +01:00
|
|
|
ircd::resource
|
|
|
|
profile_resource
|
2018-02-12 23:45:48 +01:00
|
|
|
{
|
2018-02-14 23:04:26 +01:00
|
|
|
"/_matrix/client/r0/profile/",
|
2018-02-12 23:45:48 +01:00
|
|
|
{
|
2018-02-14 23:04:26 +01:00
|
|
|
"(8.2) Profiles",
|
2018-02-12 23:45:48 +01:00
|
|
|
resource::DIRECTORY,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-23 00:25:45 +01:00
|
|
|
extern "C" void
|
|
|
|
profile_get(const m::user &user,
|
|
|
|
const string_view &key,
|
|
|
|
const m::user::profile_closure &closure);
|
|
|
|
|
|
|
|
extern "C" m::event::id::buf
|
|
|
|
profile_set(const m::user &user,
|
|
|
|
const m::user &sender,
|
|
|
|
const string_view &key,
|
|
|
|
const string_view &value);
|
|
|
|
|
2018-02-14 23:04:26 +01:00
|
|
|
static resource::response
|
2018-03-23 00:27:42 +01:00
|
|
|
get__profile_full(client &,
|
|
|
|
const resource::request &,
|
|
|
|
const m::user &);
|
2018-02-14 23:04:26 +01:00
|
|
|
|
2018-02-25 09:29:44 +01:00
|
|
|
static resource::response
|
2018-03-23 00:27:42 +01:00
|
|
|
get__profile_remote(client &,
|
|
|
|
const resource::request &,
|
|
|
|
const m::user &);
|
2018-02-14 23:04:26 +01:00
|
|
|
|
2018-02-12 23:45:48 +01:00
|
|
|
resource::response
|
2018-02-14 23:04:26 +01:00
|
|
|
get__profile(client &client,
|
2018-02-12 23:45:48 +01:00
|
|
|
const resource::request &request)
|
|
|
|
{
|
2018-02-14 23:04:26 +01:00
|
|
|
if(request.parv.size() < 1)
|
2018-02-15 06:52:19 +01:00
|
|
|
throw m::NEED_MORE_PARAMS
|
2018-02-14 23:04:26 +01:00
|
|
|
{
|
|
|
|
"user_id path parameter required"
|
|
|
|
};
|
|
|
|
|
|
|
|
m::user::id::buf user_id
|
|
|
|
{
|
|
|
|
url::decode(request.parv[0], user_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user user
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
2018-03-23 00:27:42 +01:00
|
|
|
if(!my(user) && !exists(user))
|
2018-02-25 09:29:44 +01:00
|
|
|
return get__profile_remote(client, request, user);
|
|
|
|
|
2018-02-14 23:04:26 +01:00
|
|
|
if(request.parv.size() < 2)
|
2018-03-23 00:27:42 +01:00
|
|
|
return get__profile_full(client, request, user);
|
2018-02-14 23:04:26 +01:00
|
|
|
|
2018-03-23 00:27:42 +01:00
|
|
|
char parambuf[128];
|
2018-02-14 23:04:26 +01:00
|
|
|
const string_view ¶m
|
|
|
|
{
|
2018-03-23 00:27:42 +01:00
|
|
|
url::decode(request.parv[1], parambuf)
|
2018-02-14 23:04:26 +01:00
|
|
|
};
|
|
|
|
|
2018-03-23 00:27:42 +01:00
|
|
|
profile_get(user, param, [¶m, &client]
|
|
|
|
(const string_view &value)
|
2018-02-14 23:04:26 +01:00
|
|
|
{
|
|
|
|
resource::response
|
|
|
|
{
|
|
|
|
client, json::members
|
|
|
|
{
|
|
|
|
{ param, value }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return {}; // responded from closure
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::method
|
|
|
|
method_get
|
|
|
|
{
|
|
|
|
profile_resource, "GET", get__profile
|
|
|
|
};
|
|
|
|
|
2018-02-25 09:29:44 +01:00
|
|
|
resource::response
|
|
|
|
get__profile_full(client &client,
|
|
|
|
const resource::request &request,
|
2018-03-23 00:27:42 +01:00
|
|
|
const m::user &user)
|
2018-02-25 09:29:44 +01:00
|
|
|
{
|
2018-03-23 00:27:42 +01:00
|
|
|
const m::user::room user_room{user};
|
|
|
|
const m::room::state state{user_room};
|
2018-02-25 09:29:44 +01:00
|
|
|
|
|
|
|
std::vector<json::member> members;
|
|
|
|
state.for_each("ircd.profile", [&members]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &key
|
|
|
|
{
|
|
|
|
at<"state_key"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &value
|
|
|
|
{
|
|
|
|
at<"content"_>(event).at("text")
|
|
|
|
};
|
|
|
|
|
|
|
|
members.emplace_back(key, value);
|
|
|
|
});
|
|
|
|
|
|
|
|
const json::strung response
|
|
|
|
{
|
|
|
|
data(members), data(members) + size(members)
|
|
|
|
};
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, json::object
|
|
|
|
{
|
|
|
|
response
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
get__profile_remote(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const m::user &user)
|
|
|
|
{
|
|
|
|
//TODO: XXX cache strat user's room
|
|
|
|
|
2018-03-23 00:27:42 +01:00
|
|
|
char parambuf[128];
|
|
|
|
const string_view ¶m
|
2018-02-25 09:29:44 +01:00
|
|
|
{
|
2018-03-23 00:27:42 +01:00
|
|
|
url::decode(request.parv[1], parambuf)
|
2018-02-25 09:29:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
64_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
m::v1::query::profile federation_request
|
|
|
|
{
|
2018-03-23 00:27:42 +01:00
|
|
|
user.user_id, param, buf
|
2018-02-25 09:29:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: conf
|
2018-04-06 06:19:16 +02:00
|
|
|
if(!federation_request.wait(seconds(8), std::nothrow))
|
2018-02-27 11:44:32 +01:00
|
|
|
throw http::error
|
2018-02-25 09:29:44 +01:00
|
|
|
{
|
2018-02-27 11:44:32 +01:00
|
|
|
http::REQUEST_TIMEOUT
|
2018-02-25 09:29:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const http::code &code
|
|
|
|
{
|
|
|
|
federation_request.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &response
|
|
|
|
{
|
|
|
|
federation_request
|
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: cache into a room with ttl
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, response
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-14 23:04:26 +01:00
|
|
|
resource::response
|
|
|
|
put__profile(client &client,
|
|
|
|
const resource::request &request)
|
|
|
|
{
|
|
|
|
if(request.parv.size() < 1)
|
2018-02-15 06:52:19 +01:00
|
|
|
throw m::NEED_MORE_PARAMS
|
2018-02-14 23:04:26 +01:00
|
|
|
{
|
|
|
|
"user_id path parameter required"
|
|
|
|
};
|
|
|
|
|
|
|
|
if(request.parv.size() < 2)
|
2018-02-15 06:52:19 +01:00
|
|
|
throw m::NEED_MORE_PARAMS
|
2018-02-14 23:04:26 +01:00
|
|
|
{
|
|
|
|
"profile property path parameter required"
|
|
|
|
};
|
|
|
|
|
|
|
|
m::user::id::buf user_id
|
|
|
|
{
|
|
|
|
url::decode(request.parv[0], user_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(user_id != request.user_id)
|
|
|
|
throw m::FORBIDDEN
|
|
|
|
{
|
|
|
|
"Trying to set profile for '%s' but you are '%s'",
|
|
|
|
user_id,
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user user
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
2018-03-23 00:27:42 +01:00
|
|
|
char parambuf[128];
|
|
|
|
const string_view ¶m
|
2018-02-14 23:04:26 +01:00
|
|
|
{
|
2018-03-23 00:27:42 +01:00
|
|
|
url::decode(request.parv[1], parambuf)
|
2018-02-14 23:04:26 +01:00
|
|
|
};
|
|
|
|
|
2018-03-23 00:27:42 +01:00
|
|
|
const string_view &value
|
2018-02-14 23:04:26 +01:00
|
|
|
{
|
2018-03-23 00:27:42 +01:00
|
|
|
unquote(request.at(param))
|
2018-02-14 23:04:26 +01:00
|
|
|
};
|
|
|
|
|
2018-03-23 00:27:42 +01:00
|
|
|
const m::user::id &sender
|
2018-02-14 23:04:26 +01:00
|
|
|
{
|
2018-03-23 00:27:42 +01:00
|
|
|
request.user_id
|
2018-02-14 23:04:26 +01:00
|
|
|
};
|
|
|
|
|
2018-04-26 12:35:38 +02:00
|
|
|
bool modified{true};
|
|
|
|
user.profile(std::nothrow, param, [&value, &modified]
|
2018-04-22 02:28:48 +02:00
|
|
|
(const string_view &existing)
|
|
|
|
{
|
2018-04-26 12:35:38 +02:00
|
|
|
modified = existing != value;
|
2018-04-22 02:28:48 +02:00
|
|
|
});
|
|
|
|
|
2018-04-26 12:35:38 +02:00
|
|
|
if(!modified)
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
|
2018-03-23 00:27:42 +01:00
|
|
|
const auto eid
|
2018-02-14 23:04:26 +01:00
|
|
|
{
|
2018-03-23 00:27:42 +01:00
|
|
|
profile_set(user, sender, param, value)
|
2018-02-14 23:04:26 +01:00
|
|
|
};
|
|
|
|
|
2018-02-12 23:45:48 +01:00
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-14 23:04:26 +01:00
|
|
|
resource::method
|
|
|
|
method_put
|
2018-02-12 23:45:48 +01:00
|
|
|
{
|
2018-02-14 23:04:26 +01:00
|
|
|
profile_resource, "PUT", put__profile,
|
2018-02-12 23:45:48 +01:00
|
|
|
{
|
2018-04-22 02:28:48 +02:00
|
|
|
method_put.REQUIRES_AUTH,
|
|
|
|
60s
|
2018-02-12 23:45:48 +01:00
|
|
|
}
|
|
|
|
};
|
2018-03-23 00:25:45 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
profile_get(const m::user &user,
|
|
|
|
const string_view &key,
|
|
|
|
const m::user::profile_closure &closure)
|
2018-04-03 07:12:25 +02:00
|
|
|
try
|
2018-03-23 00:25:45 +01:00
|
|
|
{
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user
|
|
|
|
};
|
|
|
|
|
|
|
|
user_room.get("ircd.profile", key, [&closure]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const string_view &value
|
|
|
|
{
|
|
|
|
unquote(at<"content"_>(event).at("text"))
|
|
|
|
};
|
|
|
|
|
|
|
|
closure(value);
|
|
|
|
});
|
|
|
|
}
|
2018-04-03 07:12:25 +02:00
|
|
|
catch(const m::NOT_FOUND &e)
|
|
|
|
{
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Nothing about '%s' and/or profile for '%s'",
|
|
|
|
key,
|
|
|
|
string_view{user.user_id}
|
|
|
|
};
|
|
|
|
}
|
2018-03-23 00:25:45 +01:00
|
|
|
|
|
|
|
m::event::id::buf
|
|
|
|
profile_set(const m::user &user,
|
|
|
|
const m::user &sender,
|
|
|
|
const string_view &key,
|
|
|
|
const string_view &value)
|
|
|
|
{
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user
|
|
|
|
};
|
|
|
|
|
|
|
|
return send(user_room, sender, "ircd.profile", key,
|
|
|
|
{
|
|
|
|
{ "text", value }
|
|
|
|
});
|
|
|
|
}
|
2018-04-22 00:21:50 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
_rejoin_room(const m::room &room,
|
|
|
|
const m::user &user)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m::join(room, user);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"Failed to rejoin '%s' to room '%s' to update profile",
|
|
|
|
string_view{user.user_id},
|
|
|
|
string_view{room.room_id}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_rejoin_rooms(const m::user::id &user_id)
|
|
|
|
{
|
|
|
|
assert(my(user_id));
|
|
|
|
const m::user::rooms &rooms
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
rooms.for_each("join", [&user_id]
|
|
|
|
(const m::room &room, const string_view &)
|
|
|
|
{
|
|
|
|
_rejoin_room(room, user_id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_my_profile_changed__displayname(const m::event &event)
|
|
|
|
{
|
|
|
|
_rejoin_rooms(at<"sender"_>(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_my_profile_changed__avatar_url(const m::event &event)
|
|
|
|
{
|
|
|
|
_rejoin_rooms(at<"sender"_>(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_my_profile_changed(const m::event &event)
|
|
|
|
{
|
|
|
|
if(!my(event))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const m::user::id &user_id
|
|
|
|
{
|
|
|
|
json::get<"sender"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
// The event has to be an ircd.profile in the user's room, not just a
|
|
|
|
// random ircd.profile typed event in some other room...
|
|
|
|
const m::user::room user_room{user_id};
|
|
|
|
if(json::get<"room_id"_>(event) != user_room.room_id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(json::get<"state_key"_>(event) == "displayname")
|
|
|
|
return handle_my_profile_changed__displayname(event);
|
|
|
|
|
|
|
|
if(json::get<"state_key"_>(event) == "avatar_url")
|
|
|
|
return handle_my_profile_changed__avatar_url(event);
|
|
|
|
}
|
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
const m::hookfn<>
|
2018-04-22 00:21:50 +02:00
|
|
|
my_profile_changed
|
|
|
|
{
|
|
|
|
handle_my_profile_changed,
|
|
|
|
{
|
|
|
|
{ "_site", "vm.notify" },
|
|
|
|
{ "type", "ircd.profile" },
|
|
|
|
}
|
|
|
|
};
|