./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 41 start_cache(HostType, Opts),
34 41 ejabberd_hooks:add(hooks(HostType, Opts)),
35 41 ok.
36
37 -spec stop(HostType :: mongooseim:host_type()) -> ok.
38 stop(HostType) ->
39 41 ejabberd_hooks:delete(hooks(HostType)),
40 41 stop_cache(HostType),
41 41 ok.
42
43 -spec supported_features() -> [atom()].
44 supported_features() ->
45 41 [dynamic_domains].
46
47 -spec hooks(mongooseim:host_type()) -> [ejabberd_hooks:hook()].
48 hooks(HostType) ->
49 41 Opts = gen_mod:get_module_opts(HostType, ?MODULE),
50 41 hooks(HostType, Opts).
51
52 -spec hooks(mongooseim:host_type(), gen_mod:module_opts()) -> any().
53 hooks(HostType, Opts) ->
54 82 PM = gen_mod:get_opt(pm, Opts, false),
55 82 MUC = gen_mod:get_opt(muc, Opts, false),
56 82 maybe_pm_hooks(PM, HostType) ++ maybe_muc_hooks(MUC, HostType).
57
58 68 maybe_pm_hooks(true, HostType) -> pm_hooks(HostType);
59 14 maybe_pm_hooks(false, _HostType) -> [].
60
61 30 maybe_muc_hooks(true, HostType) -> muc_hooks(HostType);
62 52 maybe_muc_hooks(false, _HostType) -> [].
63
64 pm_hooks(HostType) ->
65 68 [{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 30 [{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 3463 case mongoose_user_cache:get_entry(HostType, ?MODULE, ArcJid) of
81 2626 #{id := ArchId} -> ArchId;
82 _ ->
83 837 put(mam_not_cached_flag, true),
84 837 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 3463 case erase(mam_not_cached_flag) of
91 undefined ->
92 2626 ArchId;
93 true ->
94 837 mongoose_user_cache:merge_entry(HostType, ?MODULE, ArcJid, #{id => ArchId}),
95 837 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 315 mongoose_user_cache:delete_user(HostType, ?MODULE, ArcJid),
102 315 Acc.
103
104 %%====================================================================
105 %% internal
106 %%====================================================================
107 -spec start_cache(mongooseim:host_type(), gen_mod:module_opts()) -> any().
108 start_cache(HostType, Opts) ->
109 41 mongoose_user_cache:start_new_cache(HostType, ?MODULE, Opts).
110
111 -spec stop_cache(mongooseim:host_type()) -> any().
112 stop_cache(HostType) ->
113 41 mongoose_user_cache:stop_cache(HostType, ?MODULE).
Line Hits Source