25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-20 16:15:59 +01:00
xmpp.chapril.org-ejabberd/src/mod_last_sql.erl

110 lines
3.6 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
%%%
%%%
2024-01-22 16:40:01 +01:00
%%% ejabberd, Copyright (C) 2002-2024 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]).
-export([sql_schemas/0]).
-include("mod_last.hrl").
-include("logger.hrl").
2016-05-04 20:01:05 +02:00
-include("ejabberd_sql_pt.hrl").
%%%===================================================================
%%% API
%%%===================================================================
init(Host, _Opts) ->
ejabberd_sql_schema:update_schema(Host, ?MODULE, sql_schemas()),
ok.
sql_schemas() ->
[#sql_schema{
version = 1,
tables =
[#sql_table{
name = <<"last">>,
columns =
[#sql_column{name = <<"username">>, type = text},
#sql_column{name = <<"server_host">>, type = text},
#sql_column{name = <<"seconds">>, type = text},
#sql_column{name = <<"state">>, type = text}],
indices = [#sql_index{
columns = [<<"server_host">>, <<"username">>],
unique = true}]}]}].
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.