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

mod_push: Rename "timestamp" to "push session ID"

The fact that mod_push uses a timestamp as a push session ID is just an
implementation detail.  Rename things accordingly to avoid confusion.

However, stick to "timestamp" in the storage modules, as it corresponds
to the database column names/types, there.
This commit is contained in:
Holger Weiss 2022-05-01 15:49:22 +02:00
parent 11574f0a43
commit a30d5875ed

View File

@ -57,19 +57,19 @@
-define(PUSH_CACHE, push_cache). -define(PUSH_CACHE, push_cache).
-type c2s_state() :: ejabberd_c2s:state(). -type c2s_state() :: ejabberd_c2s:state().
-type timestamp() :: erlang:timestamp(). -type push_session_id() :: erlang:timestamp().
-type push_session() :: {timestamp(), ljid(), binary(), xdata()}. -type push_session() :: {push_session_id(), ljid(), binary(), xdata()}.
-type err_reason() :: notfound | db_failure. -type err_reason() :: notfound | db_failure.
-type direction() :: send | recv | undefined. -type direction() :: send | recv | undefined.
-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(), push_session_id(), jid(), binary(),
xdata()) xdata())
-> {ok, push_session()} | {error, err_reason()}. -> {ok, push_session()} | {error, err_reason()}.
-callback lookup_session(binary(), binary(), jid(), binary()) -callback lookup_session(binary(), binary(), jid(), binary())
-> {ok, push_session()} | {error, err_reason()}. -> {ok, push_session()} | {error, err_reason()}.
-callback lookup_session(binary(), binary(), timestamp()) -callback lookup_session(binary(), binary(), push_session_id())
-> {ok, push_session()} | {error, err_reason()}. -> {ok, push_session()} | {error, err_reason()}.
-callback lookup_sessions(binary(), binary(), jid()) -callback lookup_sessions(binary(), binary(), jid())
-> {ok, [push_session()]} | {error, err_reason()}. -> {ok, [push_session()]} | {error, err_reason()}.
@ -77,7 +77,7 @@
-> {ok, [push_session()]} | {error, err_reason()}. -> {ok, [push_session()]} | {error, err_reason()}.
-callback lookup_sessions(binary()) -callback lookup_sessions(binary())
-> {ok, [push_session()]} | {error, err_reason()}. -> {ok, [push_session()]} | {error, err_reason()}.
-callback delete_session(binary(), binary(), timestamp()) -callback delete_session(binary(), binary(), push_session_id())
-> ok | {error, err_reason()}. -> ok | {error, err_reason()}.
-callback delete_old_sessions(binary() | global, erlang:timestamp()) -callback delete_old_sessions(binary() | global, erlang:timestamp())
-> ok | {error, err_reason()}. -> ok | {error, err_reason()}.
@ -361,14 +361,14 @@ process_iq(IQ) ->
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
{TS, PID} -> {ID, PID} ->
case store_session(LUser, LServer, TS, PushJID, Node, XData) of case store_session(LUser, LServer, ID, PushJID, Node, XData) of
{ok, _} -> {ok, _} ->
?INFO_MSG("Enabling push notifications for ~ts", ?INFO_MSG("Enabling push notifications for ~ts",
[jid:encode(JID)]), [jid:encode(JID)]),
ejabberd_c2s:cast(PID, {push_enable, TS}), ejabberd_c2s:cast(PID, {push_enable, ID}),
ejabberd_sm:set_user_info(LUser, LServer, LResource, ejabberd_sm:set_user_info(LUser, LServer, LResource,
push_id, TS); push_id, ID);
{error, _} = Err -> {error, _} = Err ->
?ERROR_MSG("Cannot enable push for ~ts: database error", ?ERROR_MSG("Cannot enable push for ~ts: database error",
[jid:encode(JID)]), [jid:encode(JID)]),
@ -383,8 +383,8 @@ enable(#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID,
-spec disable(jid(), jid(), binary() | undefined) -> ok | {error, err_reason()}. -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_pid(LUser, LServer, LResource) of
{_TS, PID} -> PID when is_pid(PID) ->
?INFO_MSG("Disabling push notifications for ~ts", ?INFO_MSG("Disabling push notifications for ~ts",
[jid:encode(JID)]), [jid:encode(JID)]),
ejabberd_sm:del_user_info(LUser, LServer, LResource, push_id), ejabberd_sm:del_user_info(LUser, LServer, LResource, push_id),
@ -405,7 +405,7 @@ disable(#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID,
-spec c2s_stanza(c2s_state(), xmpp_element() | xmlel(), term()) -> c2s_state(). -spec c2s_stanza(c2s_state(), xmpp_element() | xmlel(), term()) -> c2s_state().
c2s_stanza(State, #stream_error{}, _SendResult) -> c2s_stanza(State, #stream_error{}, _SendResult) ->
State; State;
c2s_stanza(#{push_session_id := _TS, mgmt_state := pending} = State, c2s_stanza(#{push_session_id := _ID, mgmt_state := pending} = State,
Pkt, _SendResult) -> Pkt, _SendResult) ->
?DEBUG("Notifying client of stanza", []), ?DEBUG("Notifying client of stanza", []),
notify(State, Pkt, get_direction(Pkt)), notify(State, Pkt, get_direction(Pkt)),
@ -450,7 +450,7 @@ offline_message(Acc) ->
Acc. Acc.
-spec c2s_session_pending(c2s_state()) -> c2s_state(). -spec c2s_session_pending(c2s_state()) -> c2s_state().
c2s_session_pending(#{push_session_id := _TS, mgmt_queue := Queue} = State) -> c2s_session_pending(#{push_session_id := _ID, mgmt_queue := Queue} = State) ->
case p1_queue:len(Queue) of case p1_queue:len(Queue) of
Len when Len > 0 -> Len when Len > 0 ->
?DEBUG("Notifying client of unacknowledged stanza(s)", []), ?DEBUG("Notifying client of unacknowledged stanza(s)", []),
@ -468,14 +468,14 @@ c2s_session_pending(State) ->
State. State.
-spec c2s_copy_session(c2s_state(), c2s_state()) -> c2s_state(). -spec c2s_copy_session(c2s_state(), c2s_state()) -> c2s_state().
c2s_copy_session(State, #{push_session_id := TS}) -> c2s_copy_session(State, #{push_session_id := ID}) ->
State#{push_session_id => TS}; State#{push_session_id => ID};
c2s_copy_session(State, _) -> c2s_copy_session(State, _) ->
State. State.
-spec c2s_handle_cast(c2s_state(), any()) -> c2s_state() | {stop, c2s_state()}. -spec c2s_handle_cast(c2s_state(), any()) -> c2s_state() | {stop, c2s_state()}.
c2s_handle_cast(State, {push_enable, TS}) -> c2s_handle_cast(State, {push_enable, ID}) ->
{stop, State#{push_session_id => TS}}; {stop, State#{push_session_id => ID}};
c2s_handle_cast(State, push_disable) -> c2s_handle_cast(State, push_disable) ->
{stop, maps:remove(push_session_id, State)}; {stop, maps:remove(push_session_id, State)};
c2s_handle_cast(State, _Msg) -> c2s_handle_cast(State, _Msg) ->
@ -504,7 +504,7 @@ notify(#{jid := #jid{luser = LUser, lserver = LServer}} = State, Pkt, Dir) ->
xmpp_element() | xmlel() | none, direction()) -> ok. xmpp_element() | xmlel() | none, direction()) -> ok.
notify(LUser, LServer, Clients, Pkt, Dir) -> notify(LUser, LServer, Clients, Pkt, Dir) ->
lists:foreach( lists:foreach(
fun({TS, PushLJID, Node, XData}) -> fun({ID, PushLJID, Node, XData}) ->
HandleResponse = HandleResponse =
fun(#iq{type = result}) -> fun(#iq{type = result}) ->
?DEBUG("~ts accepted notification for ~ts@~ts (~ts)", ?DEBUG("~ts accepted notification for ~ts@~ts (~ts)",
@ -518,7 +518,7 @@ notify(LUser, LServer, Clients, Pkt, Dir) ->
LServer, Node, Reason]); LServer, Node, Reason]);
{Type, Reason} -> {Type, Reason} ->
spawn(?MODULE, delete_session, spawn(?MODULE, delete_session,
[LUser, LServer, TS]), [LUser, LServer, ID]),
?WARNING_MSG("~ts rejected notification for " ?WARNING_MSG("~ts rejected notification for "
"~ts@~ts (~ts), disabling push: ~ts " "~ts@~ts (~ts), disabling push: ~ts "
"(~ts)", "(~ts)",
@ -566,9 +566,9 @@ is_incoming_chat_msg(_Stanza) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Internal functions. %% Internal functions.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec store_session(binary(), binary(), timestamp(), jid(), binary(), xdata()) -spec store_session(binary(), binary(), push_session_id(), jid(), binary(),
-> {ok, push_session()} | {error, err_reason()}. xdata()) -> {ok, push_session()} | {error, err_reason()}.
store_session(LUser, LServer, TS, PushJID, Node, XData) -> store_session(LUser, LServer, ID, 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),
case use_cache(Mod, LServer) of case use_cache(Mod, LServer) of
@ -577,26 +577,26 @@ store_session(LUser, LServer, TS, PushJID, Node, XData) ->
cache_nodes(Mod, LServer)), cache_nodes(Mod, LServer)),
ets_cache:update( ets_cache:update(
?PUSH_CACHE, ?PUSH_CACHE,
{LUser, LServer, TS}, {ok, {TS, PushJID, Node, XData}}, {LUser, LServer, ID}, {ok, {ID, PushJID, Node, XData}},
fun() -> fun() ->
Mod:store_session(LUser, LServer, TS, PushJID, Node, Mod:store_session(LUser, LServer, ID, PushJID, Node,
XData) XData)
end, cache_nodes(Mod, LServer)); end, cache_nodes(Mod, LServer));
false -> false ->
Mod:store_session(LUser, LServer, TS, PushJID, Node, XData) Mod:store_session(LUser, LServer, ID, PushJID, Node, XData)
end. end.
-spec lookup_session(binary(), binary(), c2s_state()) -spec lookup_session(binary(), binary(), c2s_state())
-> {ok, push_session()} | error | {error, err_reason()}. -> {ok, push_session()} | error | {error, err_reason()}.
lookup_session(LUser, LServer, #{push_session_id := TS}) -> lookup_session(LUser, LServer, #{push_session_id := ID}) ->
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
true -> true ->
ets_cache:lookup( ets_cache:lookup(
?PUSH_CACHE, {LUser, LServer, TS}, ?PUSH_CACHE, {LUser, LServer, ID},
fun() -> Mod:lookup_session(LUser, LServer, TS) end); fun() -> Mod:lookup_session(LUser, LServer, ID) end);
false -> false ->
Mod:lookup_session(LUser, LServer, TS) Mod:lookup_session(LUser, LServer, ID)
end. end.
-spec lookup_sessions(binary(), binary()) -> {ok, [push_session()]} | {error, err_reason()}. -spec lookup_sessions(binary(), binary()) -> {ok, [push_session()]} | {error, err_reason()}.
@ -611,16 +611,17 @@ lookup_sessions(LUser, LServer) ->
Mod:lookup_sessions(LUser, LServer) Mod:lookup_sessions(LUser, LServer)
end. end.
-spec delete_session(binary(), binary(), timestamp()) -> ok | {error, db_failure}. -spec delete_session(binary(), binary(), push_session_id())
delete_session(LUser, LServer, TS) -> -> ok | {error, db_failure}.
delete_session(LUser, LServer, ID) ->
Mod = gen_mod:db_mod(LServer, ?MODULE), Mod = gen_mod:db_mod(LServer, ?MODULE),
case Mod:delete_session(LUser, LServer, TS) of case Mod:delete_session(LUser, LServer, ID) of
ok -> ok ->
case use_cache(Mod, LServer) of case use_cache(Mod, LServer) of
true -> true ->
ets_cache:delete(?PUSH_CACHE, {LUser, LServer}, ets_cache:delete(?PUSH_CACHE, {LUser, LServer},
cache_nodes(Mod, LServer)), cache_nodes(Mod, LServer)),
ets_cache:delete(?PUSH_CACHE, {LUser, LServer, TS}, ets_cache:delete(?PUSH_CACHE, {LUser, LServer, ID},
cache_nodes(Mod, LServer)); cache_nodes(Mod, LServer));
false -> false ->
ok ok
@ -633,8 +634,8 @@ delete_session(LUser, LServer, TS) ->
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
{ok, {TS, _, _, _}} -> {ok, {ID, _, _, _}} ->
delete_session(LUser, LServer, TS); delete_session(LUser, LServer, ID);
error -> error ->
{error, notfound}; {error, notfound};
{error, _} = Err -> {error, _} = Err ->
@ -662,12 +663,12 @@ delete_sessions(LUser, LServer, LookupFun, Mod) ->
ok ok
end, end,
lists:foreach( lists:foreach(
fun({TS, _, _, _}) -> fun({ID, _, _, _}) ->
ok = Mod:delete_session(LUser, LServer, TS), ok = Mod:delete_session(LUser, LServer, ID),
case use_cache(Mod, LServer) of case use_cache(Mod, LServer) of
true -> true ->
ets_cache:delete(?PUSH_CACHE, ets_cache:delete(?PUSH_CACHE,
{LUser, LServer, TS}, {LUser, LServer, ID},
cache_nodes(Mod, LServer)); cache_nodes(Mod, LServer));
false -> false ->
ok ok
@ -680,7 +681,7 @@ delete_sessions(LUser, LServer, LookupFun, Mod) ->
-spec drop_online_sessions(binary(), binary(), [push_session()]) -spec drop_online_sessions(binary(), binary(), [push_session()])
-> [push_session()]. -> [push_session()].
drop_online_sessions(LUser, LServer, Clients) -> drop_online_sessions(LUser, LServer, Clients) ->
OnlineTSs = lists:filtermap( OnlineIDs = lists:filtermap(
fun({_, Info}) -> fun({_, Info}) ->
case proplists:get_value(push_id, Info) of case proplists:get_value(push_id, Info) of
OnlineID = {_, _, _} -> OnlineID = {_, _, _} ->
@ -689,8 +690,8 @@ drop_online_sessions(LUser, LServer, Clients) ->
false false
end end
end, ejabberd_sm:get_user_info(LUser, LServer)), end, ejabberd_sm:get_user_info(LUser, LServer)),
[Client || {TS, _, _, _} = Client <- Clients, [Client || {ID, _, _, _} = Client <- Clients,
not lists:member(TS, OnlineTSs)]. not lists:member(ID, OnlineIDs)].
-spec make_summary(binary(), xmpp_element() | xmlel() | none, direction()) -spec make_summary(binary(), xmpp_element() | xmlel() | none, direction())
-> xdata() | undefined. -> xdata() | undefined.