./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 57 start_cache(HostType, Opts),
32 57 gen_hook:add_handlers(hooks(HostType, Opts)),
33 57 ok.
34
35 -spec stop(HostType :: mongooseim:host_type()) -> ok.
36 stop(HostType) ->
37 57 gen_hook:delete_handlers(hooks(HostType)),
38 57 stop_cache(HostType),
39 57 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 57 Opts = gen_mod:get_module_opts(HostType, ?MODULE),
48 57 hooks(HostType, Opts).
49
50 -spec hooks(mongooseim:host_type(), gen_mod:module_opts()) -> any().
51 hooks(HostType, Opts) ->
52 114 PM = gen_mod:get_opt(pm, Opts, false),
53 114 MUC = gen_mod:get_opt(muc, Opts, false),
54 114 maybe_pm_hooks(PM, HostType) ++ maybe_muc_hooks(MUC, HostType).
55
56 84 maybe_pm_hooks(true, HostType) -> pm_hooks(HostType);
57 30 maybe_pm_hooks(false, _HostType) -> [].
58
59 64 maybe_muc_hooks(true, HostType) -> muc_hooks(HostType);
60 50 maybe_muc_hooks(false, _HostType) -> [].
61
62 pm_hooks(HostType) ->
63 84 [{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 4236 case mongoose_user_cache:get_entry(HostType, ?MODULE, ArcJid) of
81 #{id := ArchId} ->
82 3154 {ok, ArchId};
83 _ ->
84 1082 put(mam_not_cached_flag, true),
85 1082 {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 4236 case erase(mam_not_cached_flag) of
94 undefined ->
95 3154 {ok, ArchId};
96 true ->
97 1082 mongoose_user_cache:merge_entry(HostType, ?MODULE, ArcJid, #{id => ArchId}),
98 1082 {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 310 mongoose_user_cache:delete_user(HostType, ?MODULE, ArcJid),
107 310 {ok, Acc};
108 remove_archive(Acc, #{room := ArcJid}, #{host_type := HostType}) ->
109 206 mongoose_user_cache:delete_user(HostType, ?MODULE, ArcJid),
110 206 {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 57 mongoose_user_cache:start_new_cache(HostType, ?MODULE, Opts).
118
119 -spec stop_cache(mongooseim:host_type()) -> any().
120 stop_cache(HostType) ->
121 57 mongoose_user_cache:stop_cache(HostType, ?MODULE).
Line Hits Source