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

mod_push_sql: Check 'max_user_sessions' limit

Remove the oldest push session(s) of a user if the number of enabled
sessions exceeds the 'max_user_sessions' limit.
This commit is contained in:
Holger Weiss 2018-05-23 20:02:52 +02:00
parent 97f7d99007
commit 508f3ef88d

View File

@ -48,6 +48,8 @@ store_session(LUser, LServer, NowTS, PushJID, Node, XData) ->
TS = misc:now_to_usec(NowTS),
PushLJID = jid:tolower(PushJID),
Service = jid:encode(PushLJID),
MaxSessions = ejabberd_sm:get_max_user_sessions(LUser, LServer),
enforce_max_sessions(LUser, LServer, MaxSessions),
case ?SQL_UPSERT(LServer, "push_session",
["!username=%(LUser)s",
"!server_host=%(LServer)s",
@ -207,6 +209,24 @@ export(_Server) ->
%%%===================================================================
%%% Internal functions
%%%===================================================================
enforce_max_sessions(_LUser, _LServer, infinity) ->
ok;
enforce_max_sessions(LUser, LServer, MaxSessions) ->
case lookup_sessions(LUser, LServer) of
{ok, Sessions} when length(Sessions) >= MaxSessions ->
?INFO_MSG("Disabling old push session(s) of ~s@~s",
[LUser, LServer]),
Sessions1 = lists:sort(fun({TS1, _, _, _}, {TS2, _, _, _}) ->
TS1 >= TS2
end, Sessions),
OldSessions = lists:nthtail(MaxSessions - 1, Sessions1),
lists:foreach(fun({TS, _, _, _}) ->
delete_session(LUser, LServer, TS)
end, OldSessions);
_ ->
ok
end.
decode_xdata(<<>>, _LUser, _LServer) ->
undefined;
decode_xdata(XML, LUser, LServer) ->