25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-22 17:28:25 +01:00

Refactor code to reduce calls to get_affiliation and get_role (thanks to Steam)(EJAB-1038)

This commit is contained in:
Badlop 2010-03-29 20:49:52 +02:00
parent 584fa98564
commit fa28c8baf6

View File

@ -593,36 +593,21 @@ handle_event(_Event, StateName, StateData) ->
%% {stop, Reason, Reply, NewStateData} %% {stop, Reason, Reply, NewStateData}
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
handle_sync_event({get_disco_item, JID, Lang}, _From, StateName, StateData) -> handle_sync_event({get_disco_item, JID, Lang}, _From, StateName, StateData) ->
FAffiliation = get_affiliation(JID, StateData), case ((StateData#state.config)#config.public_list == true) of
FRole = get_role(JID, StateData), true ->
Tail = get_roomdesc_reply(StateData, StateName, get_roomdesc_tail(StateData, Lang));
case ((StateData#state.config)#config.public_list == true) orelse _ ->
(FRole /= none) orelse Affiliation = get_affiliation(JID, StateData),
(FAffiliation == admin) orelse Role = get_role(JID, StateData),
(FAffiliation == owner) of case (Role /= none) orelse
true -> (Affiliation == admin) orelse
Desc = case (StateData#state.config)#config.public of (Affiliation == owner) of
true -> true ->
""; get_roomdesc_reply(StateData, StateName, get_roomdesc_tail(StateData, Lang));
_ -> _ ->
translate:translate(Lang, "private, ") {reply, false, StateName, StateData}
end, end
Len = ?DICT:fold(fun(_, _, Acc) -> Acc + 1 end, 0, end;
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};
handle_sync_event(get_config, _From, StateName, StateData) -> handle_sync_event(get_config, _From, StateName, StateData) ->
{reply, {ok, StateData#state.config}, StateName, StateData}; {reply, {ok, StateData#state.config}, StateName, StateData};
handle_sync_event(get_state, _From, 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}; {error, ?ERR_NOT_ALLOWED};
process_iq_disco_items(From, get, _Lang, StateData) -> process_iq_disco_items(From, get, _Lang, StateData) ->
FAffiliation = get_affiliation(From, StateData), case ((StateData#state.config)#config.public_list == true) of
FRole = get_role(From, StateData), true ->
case ((StateData#state.config)#config.public_list == true) orelse {result, get_mucroom_disco_items(StateData), StateData};
(FRole /= none) orelse _ ->
(FAffiliation == admin) orelse FAffiliation = get_affiliation(From, StateData),
(FAffiliation == owner) of FRole = get_role(From, StateData),
true -> case (FRole /= none) orelse
UList = (FAffiliation == admin) orelse
lists:map( (FAffiliation == owner) of
fun({_LJID, Info}) -> true ->
Nick = Info#user.nick, {result, get_mucroom_disco_items(StateData), StateData};
{xmlelement, "item", _ ->
[{"jid", jlib:jid_to_string( {error, ?ERR_FORBIDDEN}
{StateData#state.room, end
StateData#state.host, end.
Nick})},
{"name", Nick}], []}
end,
?DICT:to_list(StateData#state.users)),
{result, UList, StateData};
_ ->
{error, ?ERR_FORBIDDEN}
end.
process_iq_captcha(_From, get, _Lang, _SubEl, _StateData) -> process_iq_captcha(_From, get, _Lang, _SubEl, _StateData) ->
{error, ?ERR_NOT_ALLOWED}; {error, ?ERR_NOT_ALLOWED};
@ -3372,6 +3349,36 @@ get_title(StateData) ->
Name Name
end. 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 % Invitation support