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

Make it possible to export push_session table to SQL

This commit is contained in:
Evgeniy Khramtsov 2017-10-26 21:05:09 +03:00
parent db57394399
commit 9eb2685f90
4 changed files with 56 additions and 12 deletions

24
include/mod_push.hrl Normal file
View File

@ -0,0 +1,24 @@
%%%----------------------------------------------------------------------
%%% ejabberd, Copyright (C) 2017 ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License along
%%% with this program; if not, write to the Free Software Foundation, Inc.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%%
%%%----------------------------------------------------------------------
-record(push_session,
{us = {<<"">>, <<"">>} :: {binary(), binary()},
timestamp = p1_time_compat:timestamp() :: erlang:timestamp(),
service = {<<"">>, <<"">>, <<"">>} :: ljid(),
node = <<"">> :: binary(),
xdata = #xdata{} :: xdata()}).

View File

@ -59,6 +59,7 @@ modules() ->
mod_privacy,
mod_private,
mod_pubsub,
mod_push,
mod_roster,
mod_shared_roster,
mod_vcard].

View File

@ -36,13 +36,7 @@
-include_lib("stdlib/include/ms_transform.hrl").
-include("logger.hrl").
-include("xmpp.hrl").
-record(push_session,
{us = {<<"">>, <<"">>} :: {binary(), binary()},
timestamp = p1_time_compat:timestamp() :: erlang:timestamp(),
service = {<<"">>, <<"">>, <<"">>} :: ljid(),
node = <<"">> :: binary(),
xdata = #xdata{} :: xdata()}).
-include("mod_push.hrl").
%%%-------------------------------------------------------------------
%%% API

View File

@ -23,11 +23,12 @@
%% API
-export([init/2, store_session/6, lookup_session/4, lookup_session/3,
lookup_sessions/3, lookup_sessions/2, lookup_sessions/1,
delete_session/3, delete_old_sessions/2]).
delete_session/3, delete_old_sessions/2, export/1]).
-include("xmpp.hrl").
-include("logger.hrl").
-include("ejabberd_sql_pt.hrl").
-include("mod_push.hrl").
%%%===================================================================
%%% API
@ -36,10 +37,7 @@ init(_Host, _Opts) ->
ok.
store_session(LUser, LServer, NowTS, PushJID, Node, XData) ->
XML = case XData of
undefined -> <<>>;
_ -> fxml:element_to_binary(xmpp:encode(XData))
end,
XML = encode_xdata(XData),
TS = misc:now_to_usec(NowTS),
PushLJID = jid:tolower(PushJID),
Service = jid:encode(PushLJID),
@ -173,6 +171,28 @@ delete_old_sessions(LServer, Time) ->
{error, db_failure}
end.
export(_Server) ->
[{push_session,
fun(Host, #push_session{us = {LUser, LServer},
timestamp = NowTS,
service = PushLJID,
node = Node,
xdata = XData})
when LServer == Host ->
TS = misc:now_to_usec(NowTS),
Service = jid:encode(PushLJID),
XML = encode_xdata(XData),
[?SQL("delete from push_session where "
"username=%(LUser)s and timestamp=%(TS)d and "
"service=%(Service)s and node=%(Node)s and "
"xml=%(XML)s;"),
?SQL("insert into push_session(username, timestamp, "
"service, node, xml) values ("
"%(LUser)s, %(TS)d, %(Service)s, %(Node)s, %(XML)s);")];
(_Host, _R) ->
[]
end}].
%%%===================================================================
%%% Internal functions
%%%===================================================================
@ -194,3 +214,8 @@ decode_xdata(XML, LUser, LServer) ->
[XML, LUser, LServer, Err]),
undefined
end.
encode_xdata(undefined) ->
<<>>;
encode_xdata(XData) ->
fxml:element_to_binary(xmpp:encode(XData)).