From 17b9dc6035eab8b847ef746ce82e11f00e6f8287 Mon Sep 17 00:00:00 2001 From: Evgeny Khramtsov Date: Wed, 3 Apr 2019 14:50:56 +0300 Subject: [PATCH] Decrease ugliness of the ugly code --- src/mod_mam.erl | 101 +++++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 48 deletions(-) diff --git a/src/mod_mam.erl b/src/mod_mam.erl index 63f089b93..3e02c5946 100644 --- a/src/mod_mam.erl +++ b/src/mod_mam.erl @@ -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. -- 2.40.0