]> granicus.if.org Git - ejabberd/commitdiff
Make it possible to export push_session table to SQL
authorEvgeniy Khramtsov <ekhramtsov@process-one.net>
Thu, 26 Oct 2017 18:05:09 +0000 (21:05 +0300)
committerEvgeniy Khramtsov <ekhramtsov@process-one.net>
Thu, 26 Oct 2017 18:05:09 +0000 (21:05 +0300)
include/mod_push.hrl [new file with mode: 0644]
src/ejd2sql.erl
src/mod_push_mnesia.erl
src/mod_push_sql.erl

diff --git a/include/mod_push.hrl b/include/mod_push.hrl
new file mode 100644 (file)
index 0000000..6b6e898
--- /dev/null
@@ -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()}).
index 7b98d2494911b861e49e213a65894b5673efecec..048787c08c239c5e68d97ebd48179a4205e669f9 100644 (file)
@@ -59,6 +59,7 @@ modules() ->
      mod_privacy,
      mod_private,
      mod_pubsub,
+     mod_push,
      mod_roster,
      mod_shared_roster,
      mod_vcard].
index ea707dbf644a1bdd8aa795fe0adfe91086ee96c5..82021b8bd87fa6ae04d2613c1661d23ec943dd4b 100644 (file)
 -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
index 866d51ed42be2e8dd34f51e7b1b8bcf72c2fa75f..522469083bd3f6fca8f6de85416dc8db6761ec9c 100644 (file)
 %% 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)).