From fa28c8baf66872378c66abbf946dcbe36047e481 Mon Sep 17 00:00:00 2001 From: Badlop Date: Mon, 29 Mar 2010 20:49:52 +0200 Subject: [PATCH] Refactor code to reduce calls to get_affiliation and get_role (thanks to Steam)(EJAB-1038) --- src/mod_muc/mod_muc_room.erl | 113 +++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 53 deletions(-) diff --git a/src/mod_muc/mod_muc_room.erl b/src/mod_muc/mod_muc_room.erl index 7caa93f7a..63c5a4338 100644 --- a/src/mod_muc/mod_muc_room.erl +++ b/src/mod_muc/mod_muc_room.erl @@ -593,36 +593,21 @@ handle_event(_Event, StateName, StateData) -> %% {stop, Reason, Reply, NewStateData} %%---------------------------------------------------------------------- handle_sync_event({get_disco_item, JID, Lang}, _From, StateName, StateData) -> - FAffiliation = get_affiliation(JID, StateData), - FRole = get_role(JID, StateData), - Tail = - case ((StateData#state.config)#config.public_list == true) orelse - (FRole /= none) orelse - (FAffiliation == admin) orelse - (FAffiliation == owner) of - true -> - Desc = case (StateData#state.config)#config.public of - true -> - ""; - _ -> - translate:translate(Lang, "private, ") - end, - Len = ?DICT:fold(fun(_, _, Acc) -> Acc + 1 end, 0, - StateData#state.users), - " (" ++ Desc ++ integer_to_list(Len) ++ ")"; - _ -> - " (n/a)" - end, - Reply = case ((StateData#state.config)#config.public == true) orelse - (FRole /= none) orelse - (FAffiliation == admin) orelse - (FAffiliation == owner) of - true -> - {item, get_title(StateData) ++ Tail}; - _ -> - false - end, - {reply, Reply, StateName, StateData}; + case ((StateData#state.config)#config.public_list == true) of + true -> + get_roomdesc_reply(StateData, StateName, get_roomdesc_tail(StateData, Lang)); + _ -> + Affiliation = get_affiliation(JID, StateData), + Role = get_role(JID, StateData), + case (Role /= none) orelse + (Affiliation == admin) orelse + (Affiliation == owner) of + true -> + get_roomdesc_reply(StateData, StateName, get_roomdesc_tail(StateData, Lang)); + _ -> + {reply, false, StateName, StateData} + end + end; handle_sync_event(get_config, _From, StateName, StateData) -> {reply, {ok, StateData#state.config}, StateName, StateData}; handle_sync_event(get_state, _From, StateName, StateData) -> @@ -3329,29 +3314,21 @@ process_iq_disco_items(_From, set, _Lang, _StateData) -> {error, ?ERR_NOT_ALLOWED}; process_iq_disco_items(From, get, _Lang, StateData) -> - FAffiliation = get_affiliation(From, StateData), - FRole = get_role(From, StateData), - case ((StateData#state.config)#config.public_list == true) orelse - (FRole /= none) orelse - (FAffiliation == admin) orelse - (FAffiliation == owner) of - true -> - UList = - lists:map( - fun({_LJID, Info}) -> - Nick = Info#user.nick, - {xmlelement, "item", - [{"jid", jlib:jid_to_string( - {StateData#state.room, - StateData#state.host, - Nick})}, - {"name", Nick}], []} - end, - ?DICT:to_list(StateData#state.users)), - {result, UList, StateData}; - _ -> - {error, ?ERR_FORBIDDEN} - end. + case ((StateData#state.config)#config.public_list == true) of + true -> + {result, get_mucroom_disco_items(StateData), StateData}; + _ -> + FAffiliation = get_affiliation(From, StateData), + FRole = get_role(From, StateData), + case (FRole /= none) orelse + (FAffiliation == admin) orelse + (FAffiliation == owner) of + true -> + {result, get_mucroom_disco_items(StateData), StateData}; + _ -> + {error, ?ERR_FORBIDDEN} + end + end. process_iq_captcha(_From, get, _Lang, _SubEl, _StateData) -> {error, ?ERR_NOT_ALLOWED}; @@ -3372,6 +3349,36 @@ get_title(StateData) -> Name end. +get_roomdesc_reply(StateData, StateName, Tail) -> + Reply = + case ((StateData#state.config)#config.public == true) of + true -> + {item, get_title(StateData) ++ Tail}; + _ -> + false + end, + {reply, Reply, StateName, StateData}. + +get_roomdesc_tail(StateData, Lang) -> + Desc = + case (StateData#state.config)#config.public of + true -> + ""; + _ -> + translate:translate(Lang, "private, ") + end, + Len = ?DICT:fold(fun(_, _, Acc) -> Acc + 1 end, 0, StateData#state.users)," (" ++ Desc ++ integer_to_list(Len) ++ ")", + Len. + +get_mucroom_disco_items(StateData) -> + lists:map( + fun({_LJID, Info}) -> + Nick = Info#user.nick, + {xmlelement, "item", + [{"jid", jlib:jid_to_string({StateData#state.room, StateData#state.host,Nick})}, + {"name", Nick}], []} + end, + ?DICT:to_list(StateData#state.users)). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Invitation support