diff --git a/ircd/m.cc b/ircd/m.cc index e8935ccc0..80b837caa 100644 --- a/ircd/m.cc +++ b/ircd/m.cc @@ -244,6 +244,7 @@ ircd::m::module_names "m_command", "m_control", "m_create", + "m_profile", "m_event_horizon", "m_event_pretty", "conf", diff --git a/modules/m_profile.cc b/modules/m_profile.cc new file mode 100644 index 000000000..2adcae25a --- /dev/null +++ b/modules/m_profile.cc @@ -0,0 +1,104 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2019 Jason Volk +// +// 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; + +static void _rejoin_room(const m::room &room, const m::user &user); +static void _rejoin_rooms(const m::user::id &user_id); +static void handle_my_profile_changed__displayname(const m::event &event); +static void handle_my_profile_changed__avatar_url(const m::event &event); +static void handle_my_profile_changed(const m::event &, m::vm::eval &); + +mapi::header +IRCD_MODULE +{ + "Matrix profile." +}; + +m::hookfn +my_profile_changed +{ + handle_my_profile_changed, + { + { "_site", "vm.effect" }, + { "type", "ircd.profile" }, + { "origin", my_host() }, + } +}; + +void +handle_my_profile_changed(const m::event &event, + m::vm::eval &eval) +{ + const m::user::id &user_id + { + json::get<"sender"_>(event) + }; + + if(!my(event) || !my(user_id)) + return; + + // 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); +} + +void +handle_my_profile_changed__avatar_url(const m::event &event) +{ + _rejoin_rooms(at<"sender"_>(event)); +} + +void +handle_my_profile_changed__displayname(const m::event &event) +{ + _rejoin_rooms(at<"sender"_>(event)); +} + +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); + }); +} + +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} + }; +} diff --git a/modules/m_user_profile.cc b/modules/m_user_profile.cc index b56d7957d..e79aaf285 100644 --- a/modules/m_user_profile.cc +++ b/modules/m_user_profile.cc @@ -1,114 +1,32 @@ // Matrix Construct // // Copyright (C) Matrix Construct Developers, Authors & Contributors -// Copyright (C) 2016-2018 Jason Volk +// Copyright (C) 2016-2019 Jason Volk // // 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; +namespace ircd::m +{ + extern conf::item remote_request_timeout; +} -static void _rejoin_room(const m::room &room, const m::user &user); -static void _rejoin_rooms(const m::user::id &user_id); -static void handle_my_profile_changed__displayname(const m::event &event); -static void handle_my_profile_changed__avatar_url(const m::event &event); -static void handle_my_profile_changed(const m::event &, m::vm::eval &); - -mapi::header +ircd::mapi::header IRCD_MODULE { "Matrix user profiles." }; -m::hookfn -my_profile_changed +decltype(ircd::m::remote_request_timeout) +ircd::m::remote_request_timeout { - handle_my_profile_changed, - { - { "_site", "vm.effect" }, - { "type", "ircd.profile" }, - { "origin", my_host() }, - } + { "name", "ircd.m.user.profile.remote_request.timeout" }, + { "default", 10L } }; -void -handle_my_profile_changed(const m::event &event, - m::vm::eval &eval) -{ - const m::user::id &user_id - { - json::get<"sender"_>(event) - }; - - if(!my(event) || !my(user_id)) - return; - - // 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); -} - -void -handle_my_profile_changed__avatar_url(const m::event &event) -{ - _rejoin_rooms(at<"sender"_>(event)); -} - -void -handle_my_profile_changed__displayname(const m::event &event) -{ - _rejoin_rooms(at<"sender"_>(event)); -} - -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); - }); -} - -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} - }; -} - -/////////////////////////////////////////////////////////////////////////////// -// -// ircd/m/user/profile.h -// - -m::event::id::buf +ircd::m::event::id::buf IRCD_MODULE_EXPORT ircd::m::user::profile::set(const string_view &key, const string_view &val) @@ -208,13 +126,6 @@ const }); } -conf::item -remote_request_timeout -{ - { "name", "ircd.m.user.profile.remote_request.timeout" }, - { "default", 10L } -}; - void IRCD_MODULE_EXPORT ircd::m::user::profile::fetch(const m::user &user,