1 |
|
%%%---------------------------------------------------------------------- |
2 |
|
%%% File : mod_last.erl |
3 |
|
%%% Author : MichaĆ Piotrowski <michal.piotrowski@erlang-solutions.com> |
4 |
|
%%% Purpose : mod_last mnesia backend (XEP-0012) |
5 |
|
%%% |
6 |
|
%%% |
7 |
|
%%% ejabberd, Copyright (C) 2002-2014 ProcessOne |
8 |
|
%%% MongooseIM, Copyright (C) 2014 Erlang Solutions Ltd. |
9 |
|
%%% |
10 |
|
%%%---------------------------------------------------------------------- |
11 |
|
|
12 |
|
|
13 |
|
-module(mod_last_mnesia). |
14 |
|
|
15 |
|
-behaviour(mod_last_backend). |
16 |
|
|
17 |
|
-include("mod_last.hrl"). |
18 |
|
-include("mongoose.hrl"). |
19 |
|
|
20 |
|
%% API |
21 |
|
-export([init/2, |
22 |
|
get_last/3, |
23 |
|
count_active_users/3, |
24 |
|
set_last_info/5, |
25 |
|
session_cleanup/5, |
26 |
|
remove_user/3, |
27 |
|
remove_domain/2, |
28 |
|
sessions_cleanup/2]). |
29 |
|
|
30 |
|
-type host_type() :: mongooseim:host_type(). |
31 |
|
|
32 |
|
-spec init(host_type(), gen_mod:module_opts()) -> ok. |
33 |
|
init(_HostType, _Opts) -> |
34 |
10 |
mongoose_mnesia:create_table(last_activity, |
35 |
|
[{disc_copies, [node()]}, |
36 |
|
{attributes, record_info(fields, last_activity)}]), |
37 |
10 |
ok. |
38 |
|
|
39 |
|
-spec get_last(host_type(), jid:luser(), jid:lserver()) -> |
40 |
|
{ok, mod_last:timestamp(), mod_last:status()} | {error, term()} | not_found. |
41 |
|
get_last(_HostType, LUser, LServer) -> |
42 |
9 |
case catch mnesia:dirty_read(last_activity, {LUser, LServer}) of |
43 |
:-( |
{'EXIT', Reason} -> {error, Reason}; |
44 |
2 |
[] -> not_found; |
45 |
|
[#last_activity{timestamp = TimeStamp, |
46 |
|
status = Status}] -> |
47 |
7 |
{ok, TimeStamp, Status} |
48 |
|
end. |
49 |
|
|
50 |
|
-spec count_active_users(host_type(), jid:lserver(), mod_last:timestamp()) -> non_neg_integer(). |
51 |
|
count_active_users(_HostType, LServer, TimeStamp) -> |
52 |
6 |
MS = [{{last_activity, {'_', LServer}, '$1', '_'}, |
53 |
|
[{'>', '$1', TimeStamp}], |
54 |
|
[true]}], |
55 |
6 |
ets:select_count(last_activity, MS). |
56 |
|
|
57 |
|
-spec set_last_info(host_type(), jid:luser(), jid:lserver(), |
58 |
|
mod_last:timestamp(), mod_last:status()) -> ok | {error, term()}. |
59 |
|
set_last_info(_HostType, LUser, LServer, TimeStamp, Status) -> |
60 |
387 |
US = {LUser, LServer}, |
61 |
387 |
F = fun() -> |
62 |
387 |
mnesia:write(#last_activity{us = US, |
63 |
|
timestamp = TimeStamp, |
64 |
|
status = Status}) |
65 |
|
end, |
66 |
387 |
wrap_transaction_result(mnesia:transaction(F)). |
67 |
|
|
68 |
|
-spec session_cleanup(host_type(), jid:luser(), jid:lserver(), |
69 |
|
mod_last:timestamp(), mod_last:status()) -> ok | {error, term()}. |
70 |
|
session_cleanup(HostType, LUser, LServer, TimeStamp, Status) -> |
71 |
345 |
set_last_info(HostType, LUser, LServer, TimeStamp, Status). |
72 |
|
|
73 |
|
-spec remove_user(host_type(), jid:luser(), jid:lserver()) -> ok. |
74 |
|
remove_user(_HostType, LUser, LServer) -> |
75 |
22 |
US = {LUser, LServer}, |
76 |
22 |
F = fun() -> mnesia:delete({last_activity, US}) end, |
77 |
22 |
wrap_transaction_result(mnesia:transaction(F)). |
78 |
|
|
79 |
|
% Implementation only for RDBMS backends |
80 |
|
-spec remove_domain(host_type(), jid:lserver()) -> ok. |
81 |
|
remove_domain(_HostType, _Domain) -> |
82 |
:-( |
ok. |
83 |
|
|
84 |
|
-spec sessions_cleanup(mongooseim:host_type(), [ejabberd_sm:session()]) -> ok. |
85 |
|
sessions_cleanup(_HostType, _Sessions) -> |
86 |
1 |
ok. |
87 |
|
|
88 |
|
-spec wrap_transaction_result({atomic, ok | term()} | term()) -> ok | {error, term()}. |
89 |
409 |
wrap_transaction_result({atomic, ok}) -> ok; |
90 |
:-( |
wrap_transaction_result({atomic, Error}) -> {error, Error}; |
91 |
:-( |
wrap_transaction_result(Error) -> {error, Error}. |
92 |
|
|
93 |
|
|