]> granicus.if.org Git - ejabberd/commitdiff
Decrease ugliness of the ugly code
authorEvgeny Khramtsov <ekhramtsov@process-one.net>
Wed, 3 Apr 2019 11:50:56 +0000 (14:50 +0300)
committerEvgeny Khramtsov <ekhramtsov@process-one.net>
Wed, 3 Apr 2019 11:50:56 +0000 (14:50 +0300)
src/mod_mam.erl

index 63f089b93b096cd9ef117c0a8232f430edff85af..3e02c5946df6a54875a7591454e4b9afc07288ac 100644 (file)
@@ -1107,54 +1107,59 @@ select_with_mucsub(LServer, JidRequestor, JidArchive, Query, RSM) ->
                true ->
                    Mod:select_with_mucsub(LServer, JidRequestor, JidArchive, Query, RSM);
                false ->
-                   case Mod:select(LServer, JidRequestor, JidArchive, Query, RSM, chat) of
-                       {error, _} = Err ->
-                           Err;
-                       {Entries, All, Count} ->
-                           {Dir, Max} = case RSM of
-                                            #rsm_set{max = M, before = V} when is_binary(V) ->
-                                                {desc, M};
-                                            #rsm_set{max = M} ->
-                                                {asc, M};
-                                            _ ->
-                                                {asc, undefined}
-                                        end,
-                           SubRooms = case mod_muc_admin:find_hosts(LServer) of
-                                          [First|_] ->
-                                              case mod_muc:get_subscribed_rooms(First, JidRequestor) of
-                                                  {ok, L} -> L;
-                                                  {error, _} -> []
-                                              end;
-                                          _ ->
-                                              []
-                                      end,
-                           SubRoomJids = [Jid || {Jid, _} <- SubRooms],
-                           {E2, A2, C2} = lists:foldl(
-                               fun(MucJid, {E0, A0, C0}) ->
-                                   case select(LServer, JidRequestor, MucJid, Query, RSM,
-                                               {groupchat, member, #state{config = #config{mam = true}}}) of
-                                       {error, _} ->
-                                           {E0, A0, C0};
-                                       {E, A, C} ->
-                                           {lists:keymerge(2, E0, wrap_as_mucsub(E, JidRequestor)),
-                                            A0 andalso A, C0 + C}
-                                   end
-                               end, {Entries, All, Count}, SubRoomJids),
-                           case {Dir, Max} of
-                               {_, undefined} ->
-                                   {E2, A2, C2};
-                               {desc, _} ->
-                                   Start = case length(E2) of
-                                               Len when Len < Max -> 1;
-                                               Len -> Len - Max + 1
-                                           end,
-                                   Sub = lists:sublist(E2, Start, Max),
-                                   {Sub, if Sub == E2 -> A2; true -> false end, C2};
-                               _ ->
-                                   Sub = lists:sublist(E2, 1, Max),
-                                   {Sub, if Sub == E2 -> A2; true -> false end, C2}
-                           end
-                   end
+                   select_with_mucsub_fallback(LServer, JidRequestor, JidArchive, Query, RSM)
+           end
+    end.
+
+select_with_mucsub_fallback(LServer, JidRequestor, JidArchive, Query, RSM) ->
+    Mod = gen_mod:db_mod(LServer, ?MODULE),
+    case Mod:select(LServer, JidRequestor, JidArchive, Query, RSM, chat) of
+       {error, _} = Err ->
+           Err;
+       {Entries, All, Count} ->
+           {Dir, Max} = case RSM of
+                            #rsm_set{max = M, before = V} when is_binary(V) ->
+                                {desc, M};
+                            #rsm_set{max = M} ->
+                                {asc, M};
+                            _ ->
+                                {asc, undefined}
+                        end,
+           SubRooms = case mod_muc_admin:find_hosts(LServer) of
+                          [First|_] ->
+                              case mod_muc:get_subscribed_rooms(First, JidRequestor) of
+                                  {ok, L} -> L;
+                                  {error, _} -> []
+                              end;
+                          _ ->
+                              []
+                      end,
+           SubRoomJids = [Jid || {Jid, _} <- SubRooms],
+           {E2, A2, C2} =
+               lists:foldl(
+                 fun(MucJid, {E0, A0, C0}) ->
+                         case select(LServer, JidRequestor, MucJid, Query, RSM,
+                                     {groupchat, member, #state{config = #config{mam = true}}}) of
+                             {error, _} ->
+                                 {E0, A0, C0};
+                             {E, A, C} ->
+                                 {lists:keymerge(2, E0, wrap_as_mucsub(E, JidRequestor)),
+                                  A0 andalso A, C0 + C}
+                         end
+                 end, {Entries, All, Count}, SubRoomJids),
+           case {Dir, Max} of
+               {_, undefined} ->
+                   {E2, A2, C2};
+               {desc, _} ->
+                   Start = case length(E2) of
+                               Len when Len < Max -> 1;
+                               Len -> Len - Max + 1
+                           end,
+                   Sub = lists:sublist(E2, Start, Max),
+                   {Sub, if Sub == E2 -> A2; true -> false end, C2};
+               _ ->
+                   Sub = lists:sublist(E2, 1, Max),
+                   {Sub, if Sub == E2 -> A2; true -> false end, C2}
            end
     end.