]> granicus.if.org Git - ejabberd/commitdiff
Make mod_admin_extra add/delete_rosteritem reuse code from mod_roster
authorPaweł Chmielowski <pchmielowski@process-one.net>
Fri, 24 May 2019 12:02:10 +0000 (14:02 +0200)
committerPaweł Chmielowski <pchmielowski@process-one.net>
Fri, 24 May 2019 12:02:17 +0000 (14:02 +0200)
src/mod_admin_extra.erl
src/mod_roster.erl

index d86e50258858ea838064257ac96c5b176b749a4b..f61699a75061c9dff674851468be4ba27a29376c 100644 (file)
@@ -1233,34 +1233,25 @@ update_vcard_els(Data, ContentList, Els1) ->
 %%%
 
 add_rosteritem(LocalUser, LocalServer, User, Server, Nick, Group, Subs) ->
-    case add_rosteritem(LocalUser, LocalServer, User, Server, Nick, Group, Subs, []) of
-       {atomic, ok} ->
-           push_roster_item(LocalUser, LocalServer, User, Server, {add, Nick, Subs, Group}),
-           ok;
-       _ ->
-           error
+    Jid = jid:make(LocalUser, LocalServer),
+    RosterItem = build_roster_item(User, Server, {add, Nick, Subs, Group}),
+    case mod_roster:set_item_and_notify_clients(Jid, RosterItem, true) of
+       ok -> ok;
+       _ -> error
     end.
 
-add_rosteritem(LU, LS, User, Server, Nick, Group, Subscription, Xattrs) ->
-    subscribe(LU, LS, User, Server, Nick, Group, Subscription, Xattrs).
-
 subscribe(LU, LS, User, Server, Nick, Group, Subscription, _Xattrs) ->
     ItemEl = build_roster_item(User, Server, {add, Nick, Subscription, Group}),
     mod_roster:set_items(LU, LS, #roster_query{items = [ItemEl]}).
 
 delete_rosteritem(LocalUser, LocalServer, User, Server) ->
-    case unsubscribe(LocalUser, LocalServer, User, Server) of
-       {atomic, ok} ->
-           push_roster_item(LocalUser, LocalServer, User, Server, remove),
-           ok;
-       _  ->
-           error
+    Jid = jid:make(LocalUser, LocalServer),
+    RosterItem = build_roster_item(User, Server, remove),
+    case mod_roster:set_item_and_notify_clients(Jid, RosterItem, true) of
+       ok -> ok;
+       _ -> error
     end.
 
-unsubscribe(LU, LS, User, Server) ->
-    ItemEl = build_roster_item(User, Server, remove),
-    mod_roster:set_items(LU, LS, #roster_query{items = [ItemEl]}).
-
 %% -----------------------------
 %% Get Roster
 %% -----------------------------
index 6d8e45512395b5bdf8b1ac78da5847ec3ef05568..a9bafd4feea4feaf0b947a1931e2009256666f1f 100644 (file)
@@ -51,7 +51,7 @@
         roster_versioning_enabled/1, roster_version/2,
         mod_opt_type/1, mod_options/1, set_roster/1, del_roster/3,
         process_rosteritems/5,
-        depends/2]).
+        depends/2, set_item_and_notify_clients/3]).
 
 -include("logger.hrl").
 -include("xmpp.hrl").
@@ -441,23 +441,34 @@ decode_item(Item, R, Managed) ->
 
 process_iq_set(#iq{from = _From, to = To,
                   sub_els = [#roster_query{items = [QueryItem]}]} = IQ) ->
+    case set_item_and_notify_clients(To, QueryItem, false) of
+       ok ->
+           xmpp:make_iq_result(IQ);
+       E ->
+           ?ERROR_MSG("roster set failed:~nIQ = ~s~nError = ~p",
+                      [xmpp:pp(IQ), E]),
+           xmpp:make_error(IQ, xmpp:err_internal_server_error())
+    end.
+
+-spec set_item_and_notify_clients(jid(), #roster_item{}, boolean()) -> ok | error.
+set_item_and_notify_clients(To, #roster_item{jid = LJID} = RosterItem,
+                           OverrideSubscription) ->
     #jid{luser = LUser, lserver = LServer} = To,
-    LJID = jid:tolower(QueryItem#roster_item.jid),
     F = fun () ->
-               Item = get_roster_item(LUser, LServer, LJID),
-               Item2 = decode_item(QueryItem, Item, false),
-               Item3 = ejabberd_hooks:run_fold(roster_process_item,
-                                               LServer, Item2,
-                                               [LServer]),
-               case Item3#roster.subscription of
-                   remove -> del_roster_t(LUser, LServer, LJID);
-                   _ -> update_roster_t(LUser, LServer, LJID, Item3)
-               end,
-               case roster_version_on_db(LServer) of
-                   true -> write_roster_version_t(LUser, LServer);
-                   false -> ok
-               end,
-               {Item, Item3}
+       Item = get_roster_item(LUser, LServer, LJID),
+       Item2 = decode_item(RosterItem, Item, OverrideSubscription),
+       Item3 = ejabberd_hooks:run_fold(roster_process_item,
+                                       LServer, Item2,
+                                       [LServer]),
+       case Item3#roster.subscription of
+           remove -> del_roster_t(LUser, LServer, LJID);
+           _ -> update_roster_t(LUser, LServer, LJID, Item3)
+       end,
+       case roster_version_on_db(LServer) of
+           true -> write_roster_version_t(LUser, LServer);
+           false -> ok
+       end,
+       {Item, Item3}
        end,
     case transaction(LUser, LServer, [LJID], F) of
        {atomic, {OldItem, Item}} ->
@@ -468,11 +479,9 @@ process_iq_set(#iq{from = _From, to = To,
                _ ->
                    ok
            end,
-           xmpp:make_iq_result(IQ);
+           ok;
        E ->
-           ?ERROR_MSG("roster set failed:~nIQ = ~s~nError = ~p",
-                      [xmpp:pp(IQ), E]),
-           xmpp:make_error(IQ, xmpp:err_internal_server_error())
+           E
     end.
 
 push_item(To, OldItem, NewItem) ->