24
1
mirror of https://github.com/processone/ejabberd.git synced 2024-06-02 21:17:12 +02:00

Change mucsub API for database backends

This commit is contained in:
Evgeny Khramtsov 2019-04-03 14:20:37 +03:00
parent 4e591a73c5
commit e66f594901
5 changed files with 50 additions and 34 deletions

View File

@ -1121,11 +1121,14 @@ select_with_mucsub(LServer, JidRequestor, JidArchive, Query, RSM) ->
end, end,
SubRooms = case mod_muc_admin:find_hosts(LServer) of SubRooms = case mod_muc_admin:find_hosts(LServer) of
[First|_] -> [First|_] ->
mod_muc:get_subscribed_rooms(First, JidRequestor); case mod_muc:get_subscribed_rooms(First, JidRequestor) of
{ok, L} -> L;
{error, _} -> []
end;
_ -> _ ->
[] []
end, end,
SubRoomJids = [Jid || #muc_subscription{jid = Jid} <- SubRooms], SubRoomJids = [Jid || {Jid, _} <- SubRooms],
{E2, A2, C2} = lists:foldl( {E2, A2, C2} = lists:foldl(
fun(MucJid, {E0, A0, C0}) -> fun(MucJid, {E0, A0, C0}) ->
case select(LServer, JidRequestor, MucJid, Query, RSM, case select(LServer, JidRequestor, MucJid, Query, RSM,

View File

@ -194,11 +194,14 @@ select_with_mucsub(LServer, JidRequestor, #jid{luser = LUser} = JidArchive,
_ -> _ ->
SubRooms = case mod_muc_admin:find_hosts(LServer) of SubRooms = case mod_muc_admin:find_hosts(LServer) of
[First|_] -> [First|_] ->
mod_muc:get_subscribed_rooms(First, JidRequestor); case mod_muc:get_subscribed_rooms(First, JidRequestor) of
{ok, L} -> L;
{error, _} -> []
end;
_ -> _ ->
[] []
end, end,
[jid:encode(Jid) || #muc_subscription{jid = Jid} <- SubRooms] [jid:encode(Jid) || {Jid, _} <- SubRooms]
end, end,
{Query, CountQuery} = make_sql_query(LUser, LServer, MAMQuery, RSM, Extra), {Query, CountQuery} = make_sql_query(LUser, LServer, MAMQuery, RSM, Extra),
do_select_query(LServer, JidRequestor, JidArchive, RSM, chat, Query, CountQuery). do_select_query(LServer, JidRequestor, JidArchive, RSM, chat, Query, CountQuery).

View File

@ -107,7 +107,10 @@
-callback unregister_online_user(binary(), ljid(), binary(), binary()) -> any(). -callback unregister_online_user(binary(), ljid(), binary(), binary()) -> any().
-callback count_online_rooms_by_user(binary(), binary(), binary()) -> non_neg_integer(). -callback count_online_rooms_by_user(binary(), binary(), binary()) -> non_neg_integer().
-callback get_online_rooms_by_user(binary(), binary(), binary()) -> [{binary(), binary()}]. -callback get_online_rooms_by_user(binary(), binary(), binary()) -> [{binary(), binary()}].
-callback get_subscribed_rooms(binary(), binary(), jid()) -> [{ljid(), [binary()]}] | []. -callback get_subscribed_rooms(binary(), binary(), jid()) ->
{ok, [{jid(), [binary()]}]} | {error, db_failure}.
-optional_callbacks([get_subscribed_rooms/3]).
%%==================================================================== %%====================================================================
%% API %% API
@ -584,12 +587,19 @@ process_muc_unique(#iq{from = From, type = get,
process_mucsub(#iq{type = set, lang = Lang} = IQ) -> process_mucsub(#iq{type = set, lang = Lang} = IQ) ->
Txt = <<"Value 'set' of 'type' attribute is not allowed">>, Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang)); xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
process_mucsub(#iq{type = get, from = From, to = To, process_mucsub(#iq{type = get, from = From, to = To, lang = Lang,
sub_els = [#muc_subscriptions{}]} = IQ) -> sub_els = [#muc_subscriptions{}]} = IQ) ->
Host = To#jid.lserver, Host = To#jid.lserver,
ServerHost = ejabberd_router:host_of_route(Host), ServerHost = ejabberd_router:host_of_route(Host),
Subs = get_subscribed_rooms(ServerHost, Host, From), case get_subscribed_rooms(ServerHost, Host, From) of
xmpp:make_iq_result(IQ, #muc_subscriptions{list = Subs}); {ok, Subs} ->
List = [#muc_subscription{jid = JID, events = Nodes}
|| {JID, Nodes} <- Subs],
xmpp:make_iq_result(IQ, #muc_subscriptions{list = List});
{error, _} ->
Txt = <<"Database failure">>,
xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang))
end;
process_mucsub(#iq{lang = Lang} = IQ) -> process_mucsub(#iq{lang = Lang} = IQ) ->
Txt = <<"No module is handling this query">>, Txt = <<"No module is handling this query">>,
xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang)). xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang)).
@ -728,30 +738,33 @@ get_room_disco_item({Name, Host, Pid}, Query) ->
{error, notfound} {error, notfound}
end. end.
-spec get_subscribed_rooms(binary(), jid()) -> [#muc_subscription{}]. -spec get_subscribed_rooms(binary(), jid()) -> {ok, [{jid(), [binary()]}]} | {error, any()}.
get_subscribed_rooms(Host, User) -> get_subscribed_rooms(Host, User) ->
ServerHost = ejabberd_router:host_of_route(Host), ServerHost = ejabberd_router:host_of_route(Host),
get_subscribed_rooms(ServerHost, Host, User). get_subscribed_rooms(ServerHost, Host, User).
-spec get_subscribed_rooms(binary(), binary(), jid()) ->
{ok, [{jid(), [binary()]}]} | {error, any()}.
get_subscribed_rooms(ServerHost, Host, From) -> get_subscribed_rooms(ServerHost, Host, From) ->
LServer = jid:nameprep(ServerHost), LServer = jid:nameprep(ServerHost),
Mod = gen_mod:db_mod(LServer, ?MODULE), Mod = gen_mod:db_mod(LServer, ?MODULE),
BareFrom = jid:remove_resource(From), BareFrom = jid:remove_resource(From),
case Mod:get_subscribed_rooms(LServer, Host, BareFrom) of case erlang:function_exported(Mod, get_subscribed_rooms, 3) of
not_implemented -> false ->
Rooms = get_online_rooms(ServerHost, Host), Rooms = get_online_rooms(ServerHost, Host),
lists:flatmap( {ok, lists:flatmap(
fun({Name, _, Pid}) -> fun({Name, _, Pid}) ->
case p1_fsm:sync_send_all_state_event(Pid, {is_subscribed, BareFrom}) of case p1_fsm:sync_send_all_state_event(
Pid, {is_subscribed, BareFrom}) of
{true, Nodes} -> {true, Nodes} ->
[#muc_subscription{jid = jid:make(Name, Host), events = Nodes}]; [{jid:make(Name, Host), Nodes}];
false -> [] false -> []
end; end;
(_) -> (_) ->
[] []
end, Rooms); end, Rooms)};
V -> true ->
[#muc_subscription{jid = Jid, events = Nodes} || {Jid, Nodes} <- V] Mod:get_subscribed_rooms(LServer, Host, BareFrom)
end. end.
get_nick(ServerHost, Host, From) -> get_nick(ServerHost, Host, From) ->

View File

@ -33,8 +33,7 @@
-export([register_online_room/4, unregister_online_room/4, find_online_room/3, -export([register_online_room/4, unregister_online_room/4, find_online_room/3,
get_online_rooms/3, count_online_rooms/2, rsm_supported/0, get_online_rooms/3, count_online_rooms/2, rsm_supported/0,
register_online_user/4, unregister_online_user/4, register_online_user/4, unregister_online_user/4,
count_online_rooms_by_user/3, get_online_rooms_by_user/3, count_online_rooms_by_user/3, get_online_rooms_by_user/3]).
get_subscribed_rooms/3]).
-export([set_affiliation/6, set_affiliations/4, get_affiliation/5, -export([set_affiliation/6, set_affiliations/4, get_affiliation/5,
get_affiliations/3, search_affiliation/4]). get_affiliations/3, search_affiliation/4]).
%% gen_server callbacks %% gen_server callbacks
@ -401,6 +400,3 @@ transform(#muc_registered{us_host = {{U, S}, H}, nick = Nick} = R) ->
R#muc_registered{us_host = {{iolist_to_binary(U), iolist_to_binary(S)}, R#muc_registered{us_host = {{iolist_to_binary(U), iolist_to_binary(S)},
iolist_to_binary(H)}, iolist_to_binary(H)},
nick = iolist_to_binary(Nick)}. nick = iolist_to_binary(Nick)}.
get_subscribed_rooms(_, _, _) ->
not_implemented.

View File

@ -409,14 +409,15 @@ import(_, _, _) ->
get_subscribed_rooms(LServer, Host, Jid) -> get_subscribed_rooms(LServer, Host, Jid) ->
JidS = jid:encode(Jid), JidS = jid:encode(Jid),
case catch ejabberd_sql:sql_query( case ejabberd_sql:sql_query(
LServer, LServer,
?SQL("select @(room)s, @(nodes)s from muc_room_subscribers where jid=%(JidS)s" ?SQL("select @(room)s, @(nodes)s from muc_room_subscribers "
" and host=%(Host)s")) of "where jid=%(JidS)s and host=%(Host)s")) of
{selected, Subs} -> {selected, Subs} ->
[{jid:make(Room, Host, <<>>), ejabberd_sql:decode_term(Nodes)} || {Room, Nodes} <- Subs]; {ok, [{jid:make(Room, Host), ejabberd_sql:decode_term(Nodes)}
|| {Room, Nodes} <- Subs]};
_Error -> _Error ->
[] {error, db_failure}
end. end.
%%%=================================================================== %%%===================================================================