25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

Simplify backend interface for mod_push

This commit is contained in:
Evgeniy Khramtsov 2017-10-27 10:55:48 +03:00
parent 568ab526e2
commit 484e5de072
4 changed files with 35 additions and 32 deletions

View File

@ -21,7 +21,7 @@
{deps, [{lager, ".*", {git, "https://github.com/erlang-lager/lager", {deps, [{lager, ".*", {git, "https://github.com/erlang-lager/lager",
{tag, {if_version_above, "17", "3.4.2", "3.2.1"}}}}, {tag, {if_version_above, "17", "3.4.2", "3.2.1"}}}},
{p1_utils, ".*", {git, "https://github.com/processone/p1_utils", {tag, "1.0.10"}}}, {p1_utils, ".*", {git, "https://github.com/processone/p1_utils", {tag, "1.0.10"}}},
{cache_tab, ".*", {git, "https://github.com/processone/cache_tab", {tag, "1.0.11"}}}, {cache_tab, ".*", {git, "https://github.com/processone/cache_tab", "938610ad3"}},
{fast_tls, ".*", {git, "https://github.com/processone/fast_tls", {tag, "1.0.16"}}}, {fast_tls, ".*", {git, "https://github.com/processone/fast_tls", {tag, "1.0.16"}}},
{stringprep, ".*", {git, "https://github.com/processone/stringprep", {tag, "1.0.10"}}}, {stringprep, ".*", {git, "https://github.com/processone/stringprep", {tag, "1.0.10"}}},
{fast_xml, ".*", {git, "https://github.com/processone/fast_xml", {tag, "1.1.24"}}}, {fast_xml, ".*", {git, "https://github.com/processone/fast_xml", {tag, "1.1.24"}}},

View File

@ -56,26 +56,27 @@
-type c2s_state() :: ejabberd_c2s:state(). -type c2s_state() :: ejabberd_c2s:state().
-type timestamp() :: erlang:timestamp(). -type timestamp() :: erlang:timestamp().
-type push_session() :: {timestamp(), ljid(), binary(), xdata()}. -type push_session() :: {timestamp(), ljid(), binary(), xdata()}.
-type err_reason() :: notfound | db_failure.
-callback init(binary(), gen_mod:opts()) -callback init(binary(), gen_mod:opts())
-> any(). -> any().
-callback store_session(binary(), binary(), timestamp(), jid(), binary(), -callback store_session(binary(), binary(), timestamp(), jid(), binary(),
xdata()) xdata())
-> {ok, push_session()} | {error, any()}. -> {ok, push_session()} | {error, err_reason()}.
-callback lookup_session(binary(), binary(), jid(), binary()) -callback lookup_session(binary(), binary(), jid(), binary())
-> {ok, push_session()} | error | {error, any()}. -> {ok, push_session()} | {error, err_reason()}.
-callback lookup_session(binary(), binary(), timestamp()) -callback lookup_session(binary(), binary(), timestamp())
-> {ok, push_session()} | error | {error, any()}. -> {ok, push_session()} | {error, err_reason()}.
-callback lookup_sessions(binary(), binary(), jid()) -callback lookup_sessions(binary(), binary(), jid())
-> {ok, [push_session()]} | {error, any()}. -> {ok, [push_session()]} | {error, err_reason()}.
-callback lookup_sessions(binary(), binary()) -callback lookup_sessions(binary(), binary())
-> {ok, [push_session()]} | {error, any()}. -> {ok, [push_session()]} | {error, err_reason()}.
-callback lookup_sessions(binary()) -callback lookup_sessions(binary())
-> {ok, [push_session()]} | {error, any()}. -> {ok, [push_session()]} | {error, err_reason()}.
-callback delete_session(binary(), binary(), timestamp()) -callback delete_session(binary(), binary(), timestamp())
-> ok | {error, any()}. -> ok | {error, err_reason()}.
-callback delete_old_sessions(binary() | global, erlang:timestamp()) -callback delete_old_sessions(binary() | global, erlang:timestamp())
-> ok | {error, any()}. -> ok | {error, err_reason()}.
-callback use_cache(binary()) -callback use_cache(binary())
-> boolean(). -> boolean().
-callback cache_nodes(binary()) -callback cache_nodes(binary())
@ -285,7 +286,7 @@ process_iq(#iq{from = #jid{lserver = LServer} = JID,
process_iq(IQ) -> process_iq(IQ) ->
xmpp:make_error(IQ, xmpp:err_not_allowed()). xmpp:make_error(IQ, xmpp:err_not_allowed()).
-spec enable(jid(), jid(), binary(), xdata()) -> ok | {error, notfound | db_failure}. -spec enable(jid(), jid(), binary(), xdata()) -> ok | {error, err_reason()}.
enable(#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID, enable(#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID,
PushJID, Node, XData) -> PushJID, Node, XData) ->
case ejabberd_sm:get_session_sid(LUser, LServer, LResource) of case ejabberd_sm:get_session_sid(LUser, LServer, LResource) of
@ -295,10 +296,10 @@ enable(#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID,
?INFO_MSG("Enabling push notifications for ~s", ?INFO_MSG("Enabling push notifications for ~s",
[jid:encode(JID)]), [jid:encode(JID)]),
ejabberd_c2s:cast(PID, push_enable); ejabberd_c2s:cast(PID, push_enable);
{error, _} -> {error, _} = Err ->
?ERROR_MSG("Cannot enable push for ~s: database error", ?ERROR_MSG("Cannot enable push for ~s: database error",
[jid:encode(JID)]), [jid:encode(JID)]),
{error, db_failure} Err
end; end;
none -> none ->
?WARNING_MSG("Cannot enable push for ~s: session not found", ?WARNING_MSG("Cannot enable push for ~s: session not found",
@ -306,7 +307,7 @@ enable(#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID,
{error, notfound} {error, notfound}
end. end.
-spec disable(jid(), jid(), binary() | undefined) -> ok | {error, notfound | db_failure}. -spec disable(jid(), jid(), binary() | undefined) -> ok | {error, err_reason()}.
disable(#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID, disable(#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID,
PushJID, Node) -> PushJID, Node) ->
case ejabberd_sm:get_session_sid(LUser, LServer, LResource) of case ejabberd_sm:get_session_sid(LUser, LServer, LResource) of
@ -398,7 +399,7 @@ c2s_handle_cast(State, push_disable) ->
c2s_handle_cast(State, _Msg) -> c2s_handle_cast(State, _Msg) ->
State. State.
-spec remove_user(binary(), binary()) -> ok | {error, any()}. -spec remove_user(binary(), binary()) -> ok | {error, err_reason()}.
remove_user(LUser, LServer) -> remove_user(LUser, LServer) ->
?INFO_MSG("Removing any push sessions of ~s@~s", [LUser, LServer]), ?INFO_MSG("Removing any push sessions of ~s@~s", [LUser, LServer]),
Mod = gen_mod:db_mod(LServer, ?MODULE), Mod = gen_mod:db_mod(LServer, ?MODULE),
@ -450,7 +451,7 @@ notify(LServer, PushLJID, Node, XData, HandleResponse) ->
%% Internal functions. %% Internal functions.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec store_session(binary(), binary(), timestamp(), jid(), binary(), xdata()) -spec store_session(binary(), binary(), timestamp(), jid(), binary(), xdata())
-> {ok, push_session()} | {error, any()}. -> {ok, push_session()} | {error, err_reason()}.
store_session(LUser, LServer, TS, PushJID, Node, XData) -> store_session(LUser, LServer, TS, PushJID, Node, XData) ->
Mod = gen_mod:db_mod(LServer, ?MODULE), Mod = gen_mod:db_mod(LServer, ?MODULE),
delete_session(LUser, LServer, PushJID, Node), delete_session(LUser, LServer, PushJID, Node),
@ -470,7 +471,7 @@ store_session(LUser, LServer, TS, PushJID, Node, XData) ->
end. end.
-spec lookup_session(binary(), binary(), timestamp()) -spec lookup_session(binary(), binary(), timestamp())
-> {ok, push_session()} | error | {error, any()}. -> {ok, push_session()} | error | {error, err_reason()}.
lookup_session(LUser, LServer, TS) -> lookup_session(LUser, LServer, TS) ->
Mod = gen_mod:db_mod(LServer, ?MODULE), Mod = gen_mod:db_mod(LServer, ?MODULE),
case use_cache(Mod, LServer) of case use_cache(Mod, LServer) of
@ -482,7 +483,7 @@ lookup_session(LUser, LServer, TS) ->
Mod:lookup_session(LUser, LServer, TS) Mod:lookup_session(LUser, LServer, TS)
end. end.
-spec lookup_sessions(binary(), binary()) -> {ok, [push_session()]} | {error, any()}. -spec lookup_sessions(binary(), binary()) -> {ok, [push_session()]} | {error, err_reason()}.
lookup_sessions(LUser, LServer) -> lookup_sessions(LUser, LServer) ->
Mod = gen_mod:db_mod(LServer, ?MODULE), Mod = gen_mod:db_mod(LServer, ?MODULE),
case use_cache(Mod, LServer) of case use_cache(Mod, LServer) of
@ -508,11 +509,11 @@ delete_session(LUser, LServer, TS) ->
false -> false ->
ok ok
end; end;
{error, _} -> {error, _} = Err ->
{error, db_failure} Err
end. end.
-spec delete_session(binary(), binary(), jid(), binary()) -> ok | {error, notfound | db_failure}. -spec delete_session(binary(), binary(), jid(), binary()) -> ok | {error, err_reason()}.
delete_session(LUser, LServer, PushJID, Node) -> delete_session(LUser, LServer, PushJID, Node) ->
Mod = gen_mod:db_mod(LServer, ?MODULE), Mod = gen_mod:db_mod(LServer, ?MODULE),
case Mod:lookup_session(LUser, LServer, PushJID, Node) of case Mod:lookup_session(LUser, LServer, PushJID, Node) of
@ -520,18 +521,18 @@ delete_session(LUser, LServer, PushJID, Node) ->
delete_session(LUser, LServer, TS); delete_session(LUser, LServer, TS);
error -> error ->
{error, notfound}; {error, notfound};
{error, _} -> {error, _} = Err ->
{error, db_failure} Err
end. end.
-spec delete_sessions(binary(), binary(), jid()) -> ok | {error, notfound | db_failure}. -spec delete_sessions(binary(), binary(), jid()) -> ok | {error, err_reason()}.
delete_sessions(LUser, LServer, PushJID) -> delete_sessions(LUser, LServer, PushJID) ->
Mod = gen_mod:db_mod(LServer, ?MODULE), Mod = gen_mod:db_mod(LServer, ?MODULE),
LookupFun = fun() -> Mod:lookup_sessions(LUser, LServer, PushJID) end, LookupFun = fun() -> Mod:lookup_sessions(LUser, LServer, PushJID) end,
delete_sessions(LUser, LServer, LookupFun, Mod). delete_sessions(LUser, LServer, LookupFun, Mod).
-spec delete_sessions(binary(), binary(), fun(() -> any()), module()) -spec delete_sessions(binary(), binary(), fun(() -> any()), module())
-> ok | {error, _}. -> ok | {error, err_reason()}.
delete_sessions(LUser, LServer, LookupFun, Mod) -> delete_sessions(LUser, LServer, LookupFun, Mod) ->
case LookupFun() of case LookupFun() of
{ok, []} -> {ok, []} ->
@ -556,8 +557,8 @@ delete_sessions(LUser, LServer, LookupFun, Mod) ->
ok ok
end end
end, Clients); end, Clients);
{error, _} -> {error, _} = Err ->
{error, db_failure} Err
end. end.
-spec drop_online_sessions(binary(), binary(), [push_session()]) -spec drop_online_sessions(binary(), binary(), [push_session()])

View File

@ -88,7 +88,7 @@ lookup_session(LUser, LServer, PushJID, Node) ->
[] -> [] ->
?DEBUG("No push session found for ~s@~s (~p, ~s)", ?DEBUG("No push session found for ~s@~s (~p, ~s)",
[LUser, LServer, PushJID, Node]), [LUser, LServer, PushJID, Node]),
error {error, notfound}
end. end.
lookup_session(LUser, LServer, TS) -> lookup_session(LUser, LServer, TS) ->
@ -105,17 +105,19 @@ lookup_session(LUser, LServer, TS) ->
[] -> [] ->
?DEBUG("No push session found for ~s@~s (~p)", ?DEBUG("No push session found for ~s@~s (~p)",
[LUser, LServer, TS]), [LUser, LServer, TS]),
error {error, notfound}
end. end.
lookup_sessions(LUser, LServer, PushJID) -> lookup_sessions(LUser, LServer, PushJID) ->
PushLJID = jid:tolower(PushJID), PushLJID = jid:tolower(PushJID),
MatchSpec = ets:fun2ms( MatchSpec = ets:fun2ms(
fun(#push_session{us = {U, S}, service = P, node = N} = Rec) fun(#push_session{us = {U, S}, service = P,
node = Node, timestamp = TS,
xdata = XData} = Rec)
when U == LUser, when U == LUser,
S == LServer, S == LServer,
P == PushLJID -> P == PushLJID ->
Rec {TS, PushLJID, Node, XData}
end), end),
{ok, mnesia:dirty_select(push_session, MatchSpec)}. {ok, mnesia:dirty_select(push_session, MatchSpec)}.

View File

@ -67,7 +67,7 @@ lookup_session(LUser, LServer, PushJID, Node) ->
XData = decode_xdata(XML, LUser, LServer), XData = decode_xdata(XML, LUser, LServer),
{ok, {NowTS, PushLJID, Node, XData}}; {ok, {NowTS, PushLJID, Node, XData}};
{selected, []} -> {selected, []} ->
error; {error, notfound};
Err -> Err ->
?ERROR_MSG("Failed to select from 'push_session' table: ~p", [Err]), ?ERROR_MSG("Failed to select from 'push_session' table: ~p", [Err]),
{error, db_failure} {error, db_failure}
@ -85,7 +85,7 @@ lookup_session(LUser, LServer, NowTS) ->
XData = decode_xdata(XML, LUser, LServer), XData = decode_xdata(XML, LUser, LServer),
{ok, {NowTS, PushLJID, Node, XData}}; {ok, {NowTS, PushLJID, Node, XData}};
{selected, []} -> {selected, []} ->
error; {error, notfound};
Err -> Err ->
?ERROR_MSG("Failed to select from 'push_session' table: ~p", [Err]), ?ERROR_MSG("Failed to select from 'push_session' table: ~p", [Err]),
{error, db_failure} {error, db_failure}