1 |
|
-module(mod_muc_light_cache). |
2 |
|
|
3 |
|
-include("mongoose_config_spec.hrl"). |
4 |
|
|
5 |
|
-behaviour(gen_mod). |
6 |
|
|
7 |
|
%% gen_mod callbacks |
8 |
|
-export([start/2, stop/1, hooks/1, config_spec/0, supported_features/0]). |
9 |
|
|
10 |
|
%% Hook handlers |
11 |
|
-export([pre_acc_room_affiliations/3, post_acc_room_affiliations/3, |
12 |
|
pre_room_exists/3, post_room_exists/3, |
13 |
|
forget_room/3, remove_domain/3, room_new_affiliations/3]). |
14 |
|
|
15 |
|
%% For tests |
16 |
|
-export([force_clear/1]). |
17 |
|
|
18 |
|
-spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok. |
19 |
|
start(HostType, Opts) -> |
20 |
1 |
start_cache(HostType, Opts), |
21 |
1 |
ok. |
22 |
|
|
23 |
|
-spec stop(mongooseim:host_type()) -> ok. |
24 |
|
stop(HostType) -> |
25 |
1 |
stop_cache(HostType), |
26 |
1 |
ok. |
27 |
|
|
28 |
|
-spec config_spec() -> mongoose_config_spec:config_section(). |
29 |
|
config_spec() -> |
30 |
106 |
Section = #section{defaults = Defaults} = mongoose_user_cache:config_spec(), |
31 |
106 |
Section#section{defaults = Defaults#{<<"time_to_live">> := 2}}. |
32 |
|
|
33 |
|
-spec supported_features() -> [atom()]. |
34 |
|
supported_features() -> |
35 |
:-( |
[dynamic_domains]. |
36 |
|
|
37 |
|
-spec hooks(mongooseim:host_type()) -> gen_hook:hook_list(). |
38 |
|
hooks(HostType) -> |
39 |
2 |
[ |
40 |
|
{acc_room_affiliations, HostType, fun ?MODULE:pre_acc_room_affiliations/3, #{}, 40}, |
41 |
|
{acc_room_affiliations, HostType, fun ?MODULE:post_acc_room_affiliations/3, #{}, 60}, |
42 |
|
{room_exists, HostType, fun ?MODULE:pre_room_exists/3, #{}, 40}, |
43 |
|
{room_exists, HostType, fun ?MODULE:post_room_exists/3, #{}, 60}, |
44 |
|
{forget_room, HostType, fun ?MODULE:forget_room/3, #{}, 80}, |
45 |
|
{remove_domain, HostType, fun ?MODULE:remove_domain/3, #{}, 20}, |
46 |
|
{room_new_affiliations, HostType, fun ?MODULE:room_new_affiliations/3, #{}, 50} |
47 |
|
]. |
48 |
|
|
49 |
|
-spec pre_acc_room_affiliations(Acc, Params, Extra) -> {ok | stop, Acc} when |
50 |
|
Acc :: mongoose_acc:t(), |
51 |
|
Params :: map(), |
52 |
|
Extra :: gen_hook:extra(). |
53 |
|
pre_acc_room_affiliations(Acc, #{room := RoomJid}, #{host_type := HostType}) -> |
54 |
58 |
case mod_muc_light:get_room_affs_from_acc(Acc, RoomJid) of |
55 |
|
{error, _} -> |
56 |
58 |
case mongoose_user_cache:get_entry(HostType, ?MODULE, RoomJid) of |
57 |
|
#{affs := Res} -> |
58 |
30 |
{ok, mod_muc_light:set_room_affs_from_acc(Acc, RoomJid, Res)}; |
59 |
|
_ -> |
60 |
28 |
{ok, Acc} |
61 |
|
end; |
62 |
|
_Res -> |
63 |
:-( |
{stop, Acc} |
64 |
|
end. |
65 |
|
|
66 |
|
-spec post_acc_room_affiliations(Acc, Params, Extra) -> {ok, Acc} when |
67 |
|
Acc :: mongoose_acc:t(), |
68 |
|
Params :: map(), |
69 |
|
Extra :: gen_hook:extra(). |
70 |
|
post_acc_room_affiliations(Acc, #{room := RoomJid}, #{host_type := HostType}) -> |
71 |
58 |
case mod_muc_light:get_room_affs_from_acc(Acc, RoomJid) of |
72 |
|
{error, _} -> |
73 |
:-( |
{ok, Acc}; |
74 |
|
Res -> |
75 |
58 |
mongoose_user_cache:merge_entry(HostType, ?MODULE, RoomJid, #{affs => Res}), |
76 |
58 |
{ok, Acc} |
77 |
|
end. |
78 |
|
|
79 |
|
-spec pre_room_exists(Acc, Params, Extra) -> {ok | stop, Acc} when |
80 |
|
Acc :: boolean(), |
81 |
|
Params :: map(), |
82 |
|
Extra :: gen_hook:extra(). |
83 |
|
pre_room_exists(false, #{room := RoomJid}, #{host_type := HostType}) -> |
84 |
59 |
case mongoose_user_cache:is_member(HostType, ?MODULE, RoomJid) of |
85 |
30 |
true -> {stop, true}; |
86 |
29 |
false -> {ok, false} |
87 |
|
end; |
88 |
|
pre_room_exists(Status, _, _) -> |
89 |
:-( |
{ok, Status}. |
90 |
|
|
91 |
|
-spec post_room_exists(Acc, Params, Extra) -> {ok, Acc} when |
92 |
|
Acc :: boolean(), |
93 |
|
Params :: map(), |
94 |
|
Extra :: gen_hook:extra(). |
95 |
|
post_room_exists(true, #{room := RoomJid}, #{host_Type := HostType}) -> |
96 |
:-( |
mongoose_user_cache:merge_entry(HostType, ?MODULE, RoomJid, #{}), |
97 |
:-( |
{ok, true}; |
98 |
|
post_room_exists(Status, _, _) -> |
99 |
29 |
{ok, Status}. |
100 |
|
|
101 |
|
-spec forget_room(Acc, Params, Extra) -> {ok, Acc} when |
102 |
|
Acc :: mongoose_acc:t(), |
103 |
|
Params :: #{muc_host := jid:server(), room := jid:luser()}, |
104 |
|
Extra :: gen_hook:extra(). |
105 |
|
forget_room(Acc, #{muc_host := RoomS, room := RoomU}, #{host_type := HostType}) -> |
106 |
5 |
mongoose_user_cache:delete_user(HostType, ?MODULE, jid:make_noprep(RoomU, RoomS, <<>>)), |
107 |
5 |
{ok, Acc}. |
108 |
|
|
109 |
|
-spec remove_domain(Acc, Params, Extra) -> {ok, Acc} when |
110 |
|
Acc :: mongoose_domain_api:remove_domain_acc(), |
111 |
|
Params :: map(), |
112 |
|
Extra :: gen_hook:extra(). |
113 |
|
remove_domain(Acc, #{domain := Domain}, #{host_type := HostType}) -> |
114 |
:-( |
MUCHost = mod_muc_light:server_host_to_muc_host(HostType, Domain), |
115 |
:-( |
mongoose_user_cache:delete_domain(HostType, ?MODULE, MUCHost), |
116 |
:-( |
{ok, Acc}. |
117 |
|
|
118 |
|
-spec room_new_affiliations(Acc, Params, Extra) -> {ok, Acc} when |
119 |
|
Acc :: mongoose_acc:t(), |
120 |
|
Params :: map(), |
121 |
|
Extra :: gen_hook:extra(). |
122 |
|
room_new_affiliations(Acc, #{room := RoomJid, new_affs := NewAffs, version := NewVersion}, _Extra) -> |
123 |
10 |
HostType = mod_muc_light_utils:acc_to_host_type(Acc), |
124 |
|
% make sure other nodes forget about stale values |
125 |
10 |
mongoose_user_cache:delete_user(HostType, ?MODULE, RoomJid), |
126 |
10 |
mongoose_user_cache:merge_entry(HostType, ?MODULE, RoomJid, #{affs => {ok, NewAffs, NewVersion}}), |
127 |
10 |
{ok, Acc}. |
128 |
|
|
129 |
|
-spec force_clear(mongooseim:host_type()) -> ok. |
130 |
|
force_clear(HostType) -> |
131 |
108 |
CacheName = gen_mod:get_module_proc(HostType, ?MODULE), |
132 |
108 |
segmented_cache:delete_pattern(CacheName, {'_', '_'}), |
133 |
89 |
ok. |
134 |
|
|
135 |
|
%%==================================================================== |
136 |
|
%% internal |
137 |
|
%%==================================================================== |
138 |
|
-spec start_cache(mongooseim:host_type(), gen_mod:module_opts()) -> any(). |
139 |
|
start_cache(HostType, Opts) -> |
140 |
1 |
mongoose_user_cache:start_new_cache(HostType, ?MODULE, Opts). |
141 |
|
|
142 |
|
-spec stop_cache(mongooseim:host_type()) -> any(). |
143 |
|
stop_cache(HostType) -> |
144 |
1 |
mongoose_user_cache:stop_cache(HostType, ?MODULE). |