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

73 lines
2.3 KiB
Erlang
Raw Normal View History

%%%-------------------------------------------------------------------
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% @copyright (C) 2016, Evgeny Khramtsov
%%% @doc
%%%
%%% @end
%%% Created : 13 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
%%%-------------------------------------------------------------------
-module(mod_last_sql).
-behaviour(mod_last).
2016-05-04 20:01:05 +02:00
-compile([{parse_transform, ejabberd_sql_pt}]).
%% API
-export([init/2, get_last/2, store_last_info/4, remove_user/2,
import/1, 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) ->
2016-04-20 11:27:32 +02:00
case catch sql_queries:get_last(LServer, LUser) of
{selected, []} ->
not_found;
{selected, [{TimeStamp, Status}]} ->
{ok, TimeStamp, Status};
Reason ->
?ERROR_MSG("failed to get last for user ~s@~s: ~p",
[LUser, LServer, Reason]),
{error, {invalid_result, Reason}}
end.
store_last_info(LUser, LServer, TimeStamp, Status) ->
2016-04-20 11:27:32 +02:00
sql_queries:set_last_t(LServer, LUser, TimeStamp, Status).
remove_user(LUser, LServer) ->
2016-04-20 11:27:32 +02:00
sql_queries:del_last(LServer, LUser).
import(_LServer, _LA) ->
pass.
export(_Server) ->
[{last_activity,
fun(Host, #last_activity{us = {LUser, LServer},
timestamp = TimeStamp, status = Status})
when LServer == Host ->
2016-05-04 20:01:05 +02:00
[?SQL("delete from last where username=%(LUser)s;"),
?SQL("insert into last(username, seconds, state)"
" values (%(LUser)s, %(TimeStamp)d, %(Status)s);")];
(_Host, _R) ->
[]
end}].
import(LServer) ->
[{<<"select username, seconds, state from last">>,
fun([LUser, TimeStamp, State]) ->
#last_activity{us = {LUser, LServer},
timestamp = jlib:binary_to_integer(
TimeStamp),
status = State}
end}].
%%%===================================================================
%%% Internal functions
%%%===================================================================