From 2b363c7aee23ec0d4a446ded312b5356deeeefb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chmielowski?= Date: Mon, 30 Oct 2017 12:05:18 +0100 Subject: [PATCH] Add mucsub event for subscribers list changes --- rebar.config | 2 +- src/mod_muc_room.erl | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/rebar.config b/rebar.config index 85f99849a..1932be6cd 100644 --- a/rebar.config +++ b/rebar.config @@ -25,7 +25,7 @@ {fast_tls, ".*", {git, "https://github.com/processone/fast_tls", {tag, "1.0.16"}}}, {stringprep, ".*", {git, "https://github.com/processone/stringprep", {tag, "1.0.10"}}}, {fast_xml, ".*", {git, "https://github.com/processone/fast_xml", {tag, "1.1.24"}}}, - {xmpp, ".*", {git, "https://github.com/processone/xmpp", "6c8f891"}}, + {xmpp, ".*", {git, "https://github.com/processone/xmpp", "3289a6b4e3f3d82e5be513524d2293c0c2b3e7fd"}}, {fast_yaml, ".*", {git, "https://github.com/processone/fast_yaml", {tag, "1.0.11"}}}, {jiffy, ".*", {git, "https://github.com/davisp/jiffy", {tag, "0.14.8"}}}, {p1_oauth2, ".*", {git, "https://github.com/processone/p1_oauth2", {tag, "0.6.2"}}}, diff --git a/src/mod_muc_room.erl b/src/mod_muc_room.erl index 6e83a374f..891807209 100644 --- a/src/mod_muc_room.erl +++ b/src/mod_muc_room.erl @@ -1625,6 +1625,13 @@ set_subscriber(JID, Nick, Nodes, StateData) -> NewStateData = StateData#state{subscribers = Subscribers, subscriber_nicks = Nicks}, store_room(NewStateData), + case not ?DICT:is_key(LBareJID, StateData#state.subscribers) of + true -> + send_subscriptions_change_notifications(jid:replace_resource(StateData#state.jid, Nick), + Nick, subscribe, NewStateData); + _ -> + ok + end, NewStateData. -spec add_online_user(jid(), binary(), role(), state()) -> state(). @@ -3795,6 +3802,8 @@ process_iq_mucsub(From, #iq{type = set, sub_els = [#muc_unsubscribe{}]}, NewStateData = StateData#state{subscribers = Subscribers, subscriber_nicks = Nicks}, store_room(NewStateData), + send_subscriptions_change_notifications(jid:replace_resource(StateData#state.jid, Nick), + Nick, unsubscribe, StateData), NewStateData2 = case close_room_if_temporary_and_empty(NewStateData) of {stop, normal, _} -> stop; {next_state, normal_state, SD} -> SD @@ -3839,7 +3848,8 @@ get_subscription_nodes(#iq{sub_els = [#muc_subscribe{events = Nodes}]}) -> ?NS_MUCSUB_NODES_AFFILIATIONS, ?NS_MUCSUB_NODES_SUBJECT, ?NS_MUCSUB_NODES_CONFIG, - ?NS_MUCSUB_NODES_PARTICIPANTS]) + ?NS_MUCSUB_NODES_PARTICIPANTS, + ?NS_MUCSUB_NODES_SUBSCRIBERS]) end, Nodes); get_subscription_nodes(_) -> []. @@ -4068,6 +4078,34 @@ store_room(StateData) -> ok end. +-spec send_subscriptions_change_notifications(jid(), binary(), subscribe|unsubscribe, state()) -> ok. +send_subscriptions_change_notifications(From, Nick, Type, State) -> + ?DICT:fold(fun(_, #subscriber{nodes = Nodes, jid = JID}, _) -> + case lists:member(?NS_MUCSUB_NODES_SUBSCRIBERS, Nodes) of + true -> + ShowJid = case (State#state.config)#config.anonymous == false orelse + get_role(JID, State) == moderator orelse + get_default_role(get_affiliation(JID, State), State) == moderator of + true -> true; + _ -> false + end, + Packet = case {Type, ShowJid} of + {subscribe, true} -> + #muc_subscribe{jid = From, nick = Nick}; + {subscribe, _} -> + #muc_subscribe{nick = Nick}; + {unsubscribe, true} -> + #muc_unsubscribe{jid = From, nick = Nick}; + {unsubscribe, _} -> + #muc_unsubscribe{nick = Nick} + end, + NewPacket = wrap(From, JID, Packet, ?NS_MUCSUB_NODES_SUBSCRIBERS), + ejabberd_router:route(xmpp:set_from_to(NewPacket, From, JID)); + false -> + ok + end + end, ok, State#state.subscribers). + -spec send_wrapped(jid(), jid(), stanza(), binary(), state()) -> ok. send_wrapped(From, To, Packet, Node, State) -> LTo = jid:tolower(To),