xmpp.chapril.org-ejabberd/src/mod_last_sql.erl

93 lines
2.9 KiB
Erlang
Raw Normal View History

%%%-------------------------------------------------------------------
2016-12-27 10:44:07 +01:00
%%% File : mod_last_sql.erl
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% Created : 13 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
2016-12-27 10:44:07 +01:00
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2021 ProcessOne
2016-12-27 10:44:07 +01:00
%%%
%%% 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.
%%%
%%%----------------------------------------------------------------------
-module(mod_last_sql).
2016-12-27 10:44:07 +01:00
-behaviour(mod_last).
2016-05-04 20:01:05 +02:00
%% API
-export([init/2, get_last/2, store_last_info/4, remove_user/2,
2016-11-22 14:48:01 +01:00
import/2, export/1]).
-include("mod_last.hrl").
-include("logger.hrl").
2016-05-04 20:01:05 +02:00
-include("ejabberd_sql_pt.hrl").
%%%===================================================================
%%% API
%%%===================================================================
init(_Host, _Opts) ->
ok.
get_last(LUser, LServer) ->
2017-05-23 11:25:13 +02:00
case ejabberd_sql:sql_query(
LServer,
?SQL("select @(seconds)d, @(state)s from last"
" where username=%(LUser)s and %(LServer)H")) of
{selected, []} ->
2017-05-18 12:21:17 +02:00
error;
{selected, [{TimeStamp, Status}]} ->
2017-05-18 12:21:17 +02:00
{ok, {TimeStamp, Status}};
_Reason ->
2017-05-18 12:21:17 +02:00
{error, db_failure}
end.
store_last_info(LUser, LServer, TimeStamp, Status) ->
TS = integer_to_binary(TimeStamp),
2017-05-23 11:25:13 +02:00
case ?SQL_UPSERT(LServer, "last",
["!username=%(LUser)s",
"!server_host=%(LServer)s",
"seconds=%(TS)s",
2017-05-23 11:25:13 +02:00
"state=%(Status)s"]) of
2017-05-18 12:21:17 +02:00
ok ->
ok;
_Err ->
2017-05-18 12:21:17 +02:00
{error, db_failure}
end.
remove_user(LUser, LServer) ->
2017-05-23 11:25:13 +02:00
ejabberd_sql:sql_query(
LServer,
?SQL("delete from last where username=%(LUser)s and %(LServer)H")).
export(_Server) ->
[{last_activity,
fun(Host, #last_activity{us = {LUser, LServer},
timestamp = TimeStamp, status = Status})
when LServer == Host ->
TS = integer_to_binary(TimeStamp),
[?SQL("delete from last where username=%(LUser)s and %(LServer)H;"),
?SQL_INSERT("last",
["username=%(LUser)s",
"server_host=%(LServer)s",
"seconds=%(TS)s",
"state=%(Status)s"])];
(_Host, _R) ->
[]
end}].
2016-11-22 14:48:01 +01:00
import(_LServer, _LA) ->
pass.