From 150543ae91773f07084e5b08edbaec5f338eca60 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 2 Mar 2018 07:25:37 -0800 Subject: [PATCH] ircd::m: Add linkage for user::presence; modules/client/presence: reorg for linkage; minor cleanup. --- include/ircd/m/user.h | 2 + ircd/m/m.cc | 19 +++++ modules/client/presence.cc | 147 +++++++++++++++++++++++-------------- 3 files changed, 111 insertions(+), 57 deletions(-) diff --git a/include/ircd/m/user.h b/include/ircd/m/user.h index 3d4bb7f07..71701acc5 100644 --- a/include/ircd/m/user.h +++ b/include/ircd/m/user.h @@ -39,6 +39,8 @@ struct ircd::m::user bool is_active() const; bool is_password(const string_view &password) const noexcept; + event::id::buf presence(const string_view &, const string_view &status = {}); + void password(const string_view &password); void activate(const json::members &contents = {}); void deactivate(const json::members &contents = {}); diff --git a/ircd/m/m.cc b/ircd/m/m.cc index 83fe32c44..f752dfc52 100644 --- a/ircd/m/m.cc +++ b/ircd/m/m.cc @@ -266,6 +266,25 @@ ircd::m::leave_ircd_room() leave(my_room, me.user_id); } +/////////////////////////////////////////////////////////////////////////////// +// +// m/user.h +// + +ircd::m::event::id::buf +ircd::m::user::presence(const string_view &presence, + const string_view &status_msg) +{ + using prototype = event::id::buf (const id &, const string_view &, const string_view &); + + static import function + { + "client_presence", "set__user_presence_status" + }; + + return function(user_id, presence, status_msg); +} + /////////////////////////////////////////////////////////////////////////////// // // m/room.h diff --git a/modules/client/presence.cc b/modules/client/presence.cc index 23bede2b5..fa1a8df3f 100644 --- a/modules/client/presence.cc +++ b/modules/client/presence.cc @@ -26,6 +26,10 @@ presence_resource } }; +// +// put +// + const string_view valid_states[] { @@ -33,63 +37,30 @@ valid_states[] }; static bool -valid_state(const string_view &state) -{ - return std::any_of(begin(valid_states), end(valid_states), [&state] - (const string_view &valid) - { - return state == valid; - }); -} +valid_state(const string_view &state); + +extern "C" m::event::id::buf +set__user_presence_status(const m::user::id &user_id, + const string_view &presence, + const string_view &status_msg); static resource::response put__presence_status(client &client, const resource::request &request, - const m::user::id &user_id) + const m::user::id &user_id); + +static resource::response +put__presence(client &client, + const resource::request &request); + +resource::method +method_put { - const string_view &presence + presence_resource, "PUT", put__presence, { - unquote(request.at("presence")) - }; - - if(!valid_state(presence)) - throw m::UNSUPPORTED - { - "That presence state is not supported" - }; - - const string_view &status_msg - { - trunc(unquote(request["status_msg"]), 390) - }; - - json::iov content; - const json::iov::push _presence[] - { - { content, { "presence", presence } }, - { content, { "user_id", user_id } }, - }; - - const json::iov::set_if _status_msg - { - content, !empty(status_msg), - { - "status_msg", status_msg - } - }; - - const m::user::room user_room - { - user_id - }; - - send(user_room, user_id, "m.presence", content); - - return resource::response - { - client, http::OK - }; -} + method_put.REQUIRES_AUTH + } +}; resource::response put__presence(client &client, @@ -134,14 +105,76 @@ put__presence(client &client, }; } -resource::method -method_put +resource::response +put__presence_status(client &client, + const resource::request &request, + const m::user::id &user_id) { - presence_resource, "PUT", put__presence, + const string_view &presence { - method_put.REQUIRES_AUTH - } -}; + unquote(request.at("presence")) + }; + + const string_view &status_msg + { + trunc(unquote(request["status_msg"]), 390) + }; + + set__user_presence_status(request.user_id, presence, status_msg); + + return resource::response + { + client, http::OK + }; +} + +m::event::id::buf +set__user_presence_status(const m::user::id &user_id, + const string_view &presence, + const string_view &status_msg) +{ + if(!valid_state(presence)) + throw m::UNSUPPORTED + { + "That presence state is not supported" + }; + + json::iov content; + const json::iov::push _presence[] + { + { content, { "presence", presence } }, + { content, { "user_id", user_id } }, + }; + + const json::iov::set_if _status_msg + { + content, !empty(status_msg), + { + "status_msg", status_msg + } + }; + + const m::user::room user_room + { + user_id + }; + + return send(user_room, user_id, "m.presence", content); +} + +bool +valid_state(const string_view &state) +{ + return std::any_of(begin(valid_states), end(valid_states), [&state] + (const string_view &valid) + { + return state == valid; + }); +} + +// +// get +// static resource::response get__presence_status(client &client,