From 9eb2685f90f28b53c5219e8b188dc97721d5beb9 Mon Sep 17 00:00:00 2001 From: Evgeniy Khramtsov Date: Thu, 26 Oct 2017 21:05:09 +0300 Subject: [PATCH] Make it possible to export push_session table to SQL --- include/mod_push.hrl | 24 ++++++++++++++++++++++++ src/ejd2sql.erl | 1 + src/mod_push_mnesia.erl | 8 +------- src/mod_push_sql.erl | 35 ++++++++++++++++++++++++++++++----- 4 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 include/mod_push.hrl diff --git a/include/mod_push.hrl b/include/mod_push.hrl new file mode 100644 index 000000000..6b6e898c2 --- /dev/null +++ b/include/mod_push.hrl @@ -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()}). diff --git a/src/ejd2sql.erl b/src/ejd2sql.erl index 7b98d2494..048787c08 100644 --- a/src/ejd2sql.erl +++ b/src/ejd2sql.erl @@ -59,6 +59,7 @@ modules() -> mod_privacy, mod_private, mod_pubsub, + mod_push, mod_roster, mod_shared_roster, mod_vcard]. diff --git a/src/mod_push_mnesia.erl b/src/mod_push_mnesia.erl index ea707dbf6..82021b8bd 100644 --- a/src/mod_push_mnesia.erl +++ b/src/mod_push_mnesia.erl @@ -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 diff --git a/src/mod_push_sql.erl b/src/mod_push_sql.erl index 866d51ed4..522469083 100644 --- a/src/mod_push_sql.erl +++ b/src/mod_push_sql.erl @@ -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)).