./ct_report/coverage/mod_mam_cache_user.COVER.html

1 %%%-------------------------------------------------------------------
2 %%% @doc Stores mam user ids in cache.
3 %%% This module is a proxy for `mod_mam_rdbms_user' (it should be started).
4 %%%
5 %%% There are 2 hooks for `mam_archive_id':
6 %%% `cached_archive_id/3' and `store_archive_id/3'.
7 %%%
8 %%% This module supports several hosts.
9 %%%
10 %%% @end
11 %%%-------------------------------------------------------------------
12 -module(mod_mam_cache_user).
13
14 -behaviour(mongoose_module_metrics).
15
16 %% gen_mod handlers
17 -export([start/2, stop/1, supported_features/0]).
18
19 %% ejabberd handlers
20 -export([cached_archive_id/3,
21 store_archive_id/3,
22 remove_archive/4]).
23
24 -ignore_xref([start/2, stop/1, supported_features/0,
25 cached_archive_id/3, store_archive_id/3, remove_archive/4]).
26
27 %%====================================================================
28 %% gen_mod callbacks
29 %%====================================================================
30
31 -spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
32 start(HostType, Opts) ->
33
:-(
start_cache(HostType, Opts),
34
:-(
ejabberd_hooks:add(hooks(HostType, Opts)),
35
:-(
ok.
36
37 -spec stop(HostType :: mongooseim:host_type()) -> ok.
38 stop(HostType) ->
39
:-(
ejabberd_hooks:delete(hooks(HostType)),
40
:-(
stop_cache(HostType),
41
:-(
ok.
42
43 -spec supported_features() -> [atom()].
44 supported_features() ->
45
:-(
[dynamic_domains].
46
47 -spec hooks(mongooseim:host_type()) -> [ejabberd_hooks:hook()].
48 hooks(HostType) ->
49
:-(
Opts = gen_mod:get_module_opts(HostType, ?MODULE),
50
:-(
hooks(HostType, Opts).
51
52 -spec hooks(mongooseim:host_type(), gen_mod:module_opts()) -> any().
53 hooks(HostType, Opts) ->
54
:-(
PM = gen_mod:get_opt(pm, Opts, false),
55
:-(
MUC = gen_mod:get_opt(muc, Opts, false),
56
:-(
maybe_pm_hooks(PM, HostType) ++ maybe_muc_hooks(MUC, HostType).
57
58
:-(
maybe_pm_hooks(true, HostType) -> pm_hooks(HostType);
59
:-(
maybe_pm_hooks(false, _HostType) -> [].
60
61
:-(
maybe_muc_hooks(true, HostType) -> muc_hooks(HostType);
62
:-(
maybe_muc_hooks(false, _HostType) -> [].
63
64 pm_hooks(HostType) ->
65
:-(
[{mam_archive_id, HostType, ?MODULE, cached_archive_id, 30},
66 {mam_archive_id, HostType, ?MODULE, store_archive_id, 70},
67 {mam_remove_archive, HostType, ?MODULE, remove_archive, 100}].
68
69 muc_hooks(HostType) ->
70
:-(
[{mam_muc_archive_id, HostType, ?MODULE, cached_archive_id, 30},
71 {mam_muc_archive_id, HostType, ?MODULE, store_archive_id, 70},
72 {mam_muc_remove_archive, HostType, ?MODULE, remove_archive, 100}].
73
74 %%====================================================================
75 %% API
76 %%====================================================================
77 -spec cached_archive_id(undefined, mongooseim:host_type(), jid:jid()) ->
78 mod_mam:archive_id().
79 cached_archive_id(undefined, HostType, ArcJid) ->
80
:-(
case mongoose_user_cache:get_entry(HostType, ?MODULE, ArcJid) of
81
:-(
#{id := ArchId} -> ArchId;
82 _ ->
83
:-(
put(mam_not_cached_flag, true),
84
:-(
undefined
85 end.
86
87 -spec store_archive_id(mod_mam:archive_id(), mongooseim:host_type(), jid:jid())
88 -> mod_mam:archive_id().
89 store_archive_id(ArchId, HostType, ArcJid) ->
90
:-(
case erase(mam_not_cached_flag) of
91 undefined ->
92
:-(
ArchId;
93 true ->
94
:-(
mongoose_user_cache:merge_entry(HostType, ?MODULE, ArcJid, #{id => ArchId}),
95
:-(
ArchId
96 end.
97
98 -spec remove_archive(Acc :: map(), HostType :: mongooseim:host_type(),
99 ArchId :: mod_mam:archive_id(), ArcJid :: jid:jid()) -> map().
100 remove_archive(Acc, HostType, _UserID, ArcJid) ->
101
:-(
mongoose_user_cache:delete_user(HostType, ?MODULE, ArcJid),
102
:-(
Acc.
103
104 %%====================================================================
105 %% internal
106 %%====================================================================
107 -spec start_cache(mongooseim:host_type(), gen_mod:module_opts()) -> any().
108 start_cache(HostType, Opts) ->
109
:-(
mongoose_user_cache:start_new_cache(HostType, ?MODULE, Opts).
110
111 -spec stop_cache(mongooseim:host_type()) -> any().
112 stop_cache(HostType) ->
113
:-(
mongoose_user_cache:stop_cache(HostType, ?MODULE).
Line Hits Source