./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 -behaviour(gen_mod).
16
17 %% gen_mod handlers
18 -export([start/2, stop/1, supported_features/0]).
19
20 %% ejabberd handlers
21 -export([cached_archive_id/3,
22 store_archive_id/3,
23 remove_archive/3]).
24
25 %%====================================================================
26 %% gen_mod callbacks
27 %%====================================================================
28
29 -spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
30 start(HostType, Opts) ->
31 64 start_cache(HostType, Opts),
32 64 gen_hook:add_handlers(hooks(HostType, Opts)),
33 64 ok.
34
35 -spec stop(HostType :: mongooseim:host_type()) -> ok.
36 stop(HostType) ->
37 64 gen_hook:delete_handlers(hooks(HostType)),
38 64 stop_cache(HostType),
39 64 ok.
40
41 -spec supported_features() -> [atom()].
42 supported_features() ->
43 1 [dynamic_domains].
44
45 -spec hooks(mongooseim:host_type()) -> gen_hook:hook_list().
46 hooks(HostType) ->
47 64 Opts = gen_mod:get_module_opts(HostType, ?MODULE),
48 64 hooks(HostType, Opts).
49
50 -spec hooks(mongooseim:host_type(), gen_mod:module_opts()) -> any().
51 hooks(HostType, Opts) ->
52 128 PM = gen_mod:get_opt(pm, Opts, false),
53 128 MUC = gen_mod:get_opt(muc, Opts, false),
54 128 maybe_pm_hooks(PM, HostType) ++ maybe_muc_hooks(MUC, HostType).
55
56 96 maybe_pm_hooks(true, HostType) -> pm_hooks(HostType);
57 32 maybe_pm_hooks(false, _HostType) -> [].
58
59 64 maybe_muc_hooks(true, HostType) -> muc_hooks(HostType);
60 64 maybe_muc_hooks(false, _HostType) -> [].
61
62 pm_hooks(HostType) ->
63 96 [{mam_archive_id, HostType, fun ?MODULE:cached_archive_id/3, #{}, 30},
64 {mam_archive_id, HostType, fun ?MODULE:store_archive_id/3, #{}, 70},
65 {mam_remove_archive, HostType, fun ?MODULE:remove_archive/3, #{}, 100}].
66
67 muc_hooks(HostType) ->
68 64 [{mam_muc_archive_id, HostType, fun ?MODULE:cached_archive_id/3, #{}, 30},
69 {mam_muc_archive_id, HostType, fun ?MODULE:store_archive_id/3, #{}, 70},
70 {mam_muc_remove_archive, HostType, fun ?MODULE:remove_archive/3, #{}, 100}].
71
72 %%====================================================================
73 %% API
74 %%====================================================================
75 -spec cached_archive_id(Acc, Params, Extra) -> {ok, Acc} when
76 Acc :: mod_mam:archive_id() | undefined,
77 Params :: #{owner := jid:jid()},
78 Extra :: gen_hook:extra().
79 cached_archive_id(undefined, #{owner := ArcJid}, #{host_type := HostType}) ->
80 5268 case mongoose_user_cache:get_entry(HostType, ?MODULE, ArcJid) of
81 #{id := ArchId} ->
82 3873 {ok, ArchId};
83 _ ->
84 1395 put(mam_not_cached_flag, true),
85 1395 {ok, undefined}
86 end.
87
88 -spec store_archive_id(Acc, Params, Extra) -> {ok, Acc} when
89 Acc :: mod_mam:archive_id() | undefined,
90 Params :: #{owner := jid:jid()},
91 Extra :: gen_hook:extra().
92 store_archive_id(ArchId, #{owner := ArcJid}, #{host_type := HostType}) ->
93 5268 case erase(mam_not_cached_flag) of
94 undefined ->
95 3873 {ok, ArchId};
96 true ->
97 1395 mongoose_user_cache:merge_entry(HostType, ?MODULE, ArcJid, #{id => ArchId}),
98 1395 {ok, ArchId}
99 end.
100
101 -spec remove_archive(Acc, Params, Extra) -> {ok, Acc} when
102 Acc :: term(),
103 Params :: #{archive_id := mod_mam:archive_id() | undefined, owner => jid:jid(), room => jid:jid()},
104 Extra :: gen_hook:extra().
105 remove_archive(Acc, #{owner := ArcJid}, #{host_type := HostType}) ->
106 347 mongoose_user_cache:delete_user(HostType, ?MODULE, ArcJid),
107 347 {ok, Acc};
108 remove_archive(Acc, #{room := ArcJid}, #{host_type := HostType}) ->
109 284 mongoose_user_cache:delete_user(HostType, ?MODULE, ArcJid),
110 284 {ok, Acc}.
111
112 %%====================================================================
113 %% internal
114 %%====================================================================
115 -spec start_cache(mongooseim:host_type(), gen_mod:module_opts()) -> any().
116 start_cache(HostType, Opts) ->
117 64 mongoose_user_cache:start_new_cache(HostType, ?MODULE, Opts).
118
119 -spec stop_cache(mongooseim:host_type()) -> any().
120 stop_cache(HostType) ->
121 64 mongoose_user_cache:stop_cache(HostType, ?MODULE).
Line Hits Source