]> granicus.if.org Git - ejabberd/commitdiff
Limit number of rooms that we return for disco_items from muc
authorPaweł Chmielowski <pchmielowski@process-one.net>
Fri, 5 Jul 2019 09:08:32 +0000 (11:08 +0200)
committerPaweł Chmielowski <pchmielowski@process-one.net>
Fri, 5 Jul 2019 09:08:32 +0000 (11:08 +0200)
We will limit it to max_room_discoitems and return rsm that would allow
fetching more

src/mod_muc.erl

index c696812616c9c106fbd3cf0fef9d6b41a74cd127..30ecdc6cd83e2ff9ab7b0da73c4fdd0203bdc0f8 100644 (file)
@@ -829,15 +829,27 @@ iq_disco_items(ServerHost, Host, From, Lang, MaxRoomsDiscoItems, Node, RSM)
               true ->
                    {get_disco_item, all, From, Lang}
            end,
-    Items = lists:flatmap(
-             fun(R) ->
-                     case get_room_disco_item(R, Query) of
-                         {ok, Item} -> [Item];
-                         {error, _} -> []
-                     end
-             end, get_online_rooms(ServerHost, Host, RSM)),
+    MaxItems = case RSM of
+                  undefined ->
+                      MaxRoomsDiscoItems;
+                  #rsm_set{max = undefined} ->
+                      MaxRoomsDiscoItems;
+                  #rsm_set{max = Max} when Max > MaxRoomsDiscoItems ->
+                      MaxRoomsDiscoItems;
+                  #rsm_set{max = Max} ->
+                      Max
+              end,
+    {Items, HitMax} = lists:foldr(
+       fun(_, {Acc, _}) when length(Acc) >= MaxItems ->
+           {Acc, true};
+          (R, {Acc, _}) ->
+           case get_room_disco_item(R, Query) of
+               {ok, Item} -> {[Item | Acc], false};
+               {error, _} -> {Acc, false}
+           end
+       end, {[], false}, get_online_rooms(ServerHost, Host, RSM)),
     ResRSM = case Items of
-                [_|_] when RSM /= undefined ->
+                [_|_] when RSM /= undefined; HitMax ->
                     #disco_item{jid = #jid{luser = First}} = hd(Items),
                     #disco_item{jid = #jid{luser = Last}} = lists:last(Items),
                     #rsm_set{first = #rsm_first{data = First},