1 |
|
%% @doc Caches info about non-anonymous users using a queue of ets tables |
2 |
|
-module(mod_cache_users). |
3 |
|
|
4 |
|
-include("mongoose_config_spec.hrl"). |
5 |
|
|
6 |
|
-behaviour(gen_mod). |
7 |
|
|
8 |
|
%% gen_mod API |
9 |
|
-export([start/2, stop/1, hooks/1, config_spec/0, supported_features/0]). |
10 |
|
|
11 |
|
%% Hooks. |
12 |
|
-export([does_cached_user_exist/3, maybe_put_user_into_cache/3, |
13 |
|
remove_user/3, remove_domain/3]). |
14 |
|
|
15 |
|
%%==================================================================== |
16 |
|
%% gen_mod callbacks |
17 |
|
%%==================================================================== |
18 |
|
|
19 |
|
-spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok. |
20 |
|
start(HostType, Opts) -> |
21 |
187 |
mongoose_user_cache:start_new_cache(HostType, ?MODULE, Opts), |
22 |
187 |
ok. |
23 |
|
|
24 |
|
-spec stop(mongooseim:host_type()) -> ok. |
25 |
|
stop(HostType) -> |
26 |
185 |
mongoose_user_cache:stop_cache(HostType, ?MODULE), |
27 |
185 |
ok. |
28 |
|
|
29 |
|
-spec config_spec() -> mongoose_config_spec:config_section(). |
30 |
|
config_spec() -> |
31 |
208 |
#section{items = Items, |
32 |
|
defaults = Defaults} = Sec = mongoose_user_cache:config_spec(), |
33 |
208 |
Sec#section{items = maps:remove(<<"module">>, Items), |
34 |
|
defaults = maps:remove(<<"module">>, Defaults)}. |
35 |
|
|
36 |
|
-spec supported_features() -> [atom()]. |
37 |
|
supported_features() -> |
38 |
90 |
[dynamic_domains]. |
39 |
|
|
40 |
|
hooks(HostType) -> |
41 |
372 |
[ |
42 |
|
%% These hooks must run before and after the ejabberd_auth does_user_exist hook |
43 |
|
{does_user_exist, HostType, fun ?MODULE:does_cached_user_exist/3, #{}, 30}, |
44 |
|
{does_user_exist, HostType, fun ?MODULE:maybe_put_user_into_cache/3, #{}, 70}, |
45 |
|
%% It is important that these two handlers happen _after_ ejabberd_auth |
46 |
|
%% but _before_ all other modules |
47 |
|
{remove_user, HostType, fun ?MODULE:remove_user/3, #{}, 10}, |
48 |
|
{remove_domain, HostType, fun ?MODULE:remove_domain/3, #{}, 20} |
49 |
|
]. |
50 |
|
|
51 |
|
%%==================================================================== |
52 |
|
%% Hooks |
53 |
|
%%==================================================================== |
54 |
|
|
55 |
|
-spec remove_user(Acc, Params, Extra) -> {ok, Acc} when |
56 |
|
Acc :: mongoose_acc:t(), |
57 |
|
Params :: #{jid := jid:jid()}, |
58 |
|
Extra :: #{host_type := mongooseim:host_type()}. |
59 |
|
remove_user(Acc, #{jid := Jid}, #{host_type := HostType}) -> |
60 |
6035 |
mongoose_user_cache:delete_user(HostType, ?MODULE, Jid), |
61 |
6035 |
{ok, Acc}. |
62 |
|
|
63 |
|
-spec remove_domain(Acc, Params, Extra) -> {ok , Acc} when |
64 |
|
Acc :: mongoose_domain_api:remove_domain_acc(), |
65 |
|
Params :: #{domain := jid:lserver()}, |
66 |
|
Extra :: #{host_type := mongooseim:host_type()}. |
67 |
|
remove_domain(Acc, #{domain := Domain}, #{host_type := HostType}) -> |
68 |
16 |
mongoose_user_cache:delete_domain(HostType, ?MODULE, Domain), |
69 |
16 |
{ok, Acc}. |
70 |
|
|
71 |
|
-spec does_cached_user_exist(Acc, Params, Extra) -> {stop | ok , Acc} when |
72 |
|
Acc :: boolean(), |
73 |
|
Params :: #{jid := jid:jid(), request_type := ejabberd_auth:exist_type()}, |
74 |
|
Extra :: #{host_type := mongooseim:host_type()}. |
75 |
|
does_cached_user_exist(false, |
76 |
|
#{jid := Jid, request_type := stored}, |
77 |
|
#{host_type := HostType}) -> |
78 |
14277 |
case mongoose_user_cache:is_member(HostType, ?MODULE, Jid) of |
79 |
11510 |
true -> {stop, true}; |
80 |
2767 |
false -> {ok, false} |
81 |
|
end; |
82 |
|
does_cached_user_exist(Acc, _, _) -> |
83 |
:-( |
{ok, Acc}. |
84 |
|
|
85 |
|
-spec maybe_put_user_into_cache(Acc, Params, Extra) -> {ok , Acc} when |
86 |
|
Acc :: boolean(), |
87 |
|
Params :: #{jid := jid:jid(), request_type := ejabberd_auth:exist_type()}, |
88 |
|
Extra :: #{host_type := mongooseim:host_type()}. |
89 |
|
maybe_put_user_into_cache(true, |
90 |
|
#{jid := Jid, request_type := stored}, |
91 |
|
#{host_type := HostType}) -> |
92 |
2179 |
mongoose_user_cache:merge_entry(HostType, ?MODULE, Jid, #{}), |
93 |
2179 |
{ok, true}; |
94 |
|
maybe_put_user_into_cache(Acc, _, _) -> |
95 |
587 |
{ok, Acc}. |