./ct_report/coverage/mod_cache_users.COVER.html

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 174 mongoose_user_cache:start_new_cache(HostType, ?MODULE, Opts),
22 174 ok.
23
24 -spec stop(mongooseim:host_type()) -> ok.
25 stop(HostType) ->
26 174 mongoose_user_cache:stop_cache(HostType, ?MODULE),
27 174 ok.
28
29 -spec config_spec() -> mongoose_config_spec:config_section().
30 config_spec() ->
31 186 #section{items = Items,
32 defaults = Defaults} = Sec = mongoose_user_cache:config_spec(),
33 186 Sec#section{items = maps:remove(<<"module">>, Items),
34 defaults = maps:remove(<<"module">>, Defaults)}.
35
36 -spec supported_features() -> [atom()].
37 supported_features() ->
38 88 [dynamic_domains].
39
40 hooks(HostType) ->
41 348 [
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 5529 mongoose_user_cache:delete_user(HostType, ?MODULE, Jid),
61 5529 {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 18 mongoose_user_cache:delete_domain(HostType, ?MODULE, Domain),
69 18 {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 13993 case mongoose_user_cache:is_member(HostType, ?MODULE, Jid) of
79 11475 true -> {stop, true};
80 2518 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 2040 mongoose_user_cache:merge_entry(HostType, ?MODULE, Jid, #{}),
93 2040 {ok, true};
94 maybe_put_user_into_cache(Acc, _, _) ->
95 477 {ok, Acc}.
Line Hits Source