From a7d71c0cd71c4b2ded877a554cda34e8204bc2d5 Mon Sep 17 00:00:00 2001 From: Nelson Vides Date: Wed, 21 Jul 2021 00:54:26 +0200 Subject: [PATCH] Make mod_blocking multitenancy ready --- big_tests/dynamic_domains.spec | 2 ++ rel/files/mongooseim.toml | 4 +-- rel/mim1.vars-toml.config | 1 + src/mod_blocking.erl | 47 +++++++++++++++++++++++----------- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/big_tests/dynamic_domains.spec b/big_tests/dynamic_domains.spec index e42ad3d837..83cb34b76d 100644 --- a/big_tests/dynamic_domains.spec +++ b/big_tests/dynamic_domains.spec @@ -23,6 +23,8 @@ {suites, "tests", mam_SUITE}. +{suites, "tests", mod_blocking_SUITE}. + {suites, "tests", mod_ping_SUITE}. {suites, "tests", muc_SUITE}. diff --git a/rel/files/mongooseim.toml b/rel/files/mongooseim.toml index 135aa91d93..164ae3f346 100644 --- a/rel/files/mongooseim.toml +++ b/rel/files/mongooseim.toml @@ -242,11 +242,9 @@ {{#mod_privacy}} [modules.mod_privacy] {{{mod_privacy}}} +[modules.mod_blocking] {{/mod_privacy}} -{{#mod_blocking}} -{{{mod_blocking}}} -{{/mod_blocking}} {{#mod_private}} {{{mod_private}}} diff --git a/rel/mim1.vars-toml.config b/rel/mim1.vars-toml.config index 4d05c8ea57..b5ba83cb89 100644 --- a/rel/mim1.vars-toml.config +++ b/rel/mim1.vars-toml.config @@ -43,6 +43,7 @@ {{#mod_privacy}} [host_config.modules.mod_privacy] {{{mod_privacy}}} + [host_config.modules.mod_blocking] {{/mod_privacy}}"}. {password_format, "password.format = \"scram\" password.hash = [\"sha256\"]"}. diff --git a/src/mod_blocking.erl b/src/mod_blocking.erl index 597f02d82b..4dbddfed9a 100644 --- a/src/mod_blocking.erl +++ b/src/mod_blocking.erl @@ -12,10 +12,15 @@ -behaviour(gen_mod). -behaviour(mongoose_module_metrics). --export([start/2, +-export([start/2]). +-export([stop/1]). +-export([deps/2]). +-export([supported_features/0]). +-export([config_spec/0]). + +-export([ process_iq_get/5, process_iq_set/4, - stop/1, disco_local_features/1 ]). @@ -27,16 +32,26 @@ -type listitem() :: #listitem{}. -start(Host, _Opts) -> - ejabberd_hooks:add(hooks(Host)). +start(HostType, _Opts) -> + ejabberd_hooks:add(hooks(HostType)). + +stop(HostType) -> + ejabberd_hooks:delete(hooks(HostType)). + +deps(_HostType, Opts) -> + [{mod_privacy, Opts, hard}]. -stop(Host) -> - ejabberd_hooks:delete(hooks(Host)). +-spec supported_features() -> [atom()]. +supported_features() -> + [dynamic_domains]. -hooks(Host) -> - [{disco_local_features, Host, ?MODULE, disco_local_features, 99}, - {privacy_iq_get, Host, ?MODULE, process_iq_get, 50}, - {privacy_iq_set, Host, ?MODULE, process_iq_set, 50}]. +config_spec() -> + mod_privacy:config_spec(). + +hooks(HostType) -> + [{disco_local_features, HostType, ?MODULE, disco_local_features, 99}, + {privacy_iq_get, HostType, ?MODULE, process_iq_get, 50}, + {privacy_iq_set, HostType, ?MODULE, process_iq_set, 50}]. -spec disco_local_features(mongoose_disco:feature_acc()) -> mongoose_disco:feature_acc(). disco_local_features(Acc = #{node := <<>>}) -> @@ -44,10 +59,10 @@ disco_local_features(Acc = #{node := <<>>}) -> disco_local_features(Acc) -> Acc. - process_iq_get(Acc, _From = #jid{luser = LUser, lserver = LServer}, _, #iq{xmlns = ?NS_BLOCKING}, _) -> - Res = case mod_privacy_backend:get_privacy_list(LUser, LServer, <<"blocking">>) of + HostType = mongoose_acc:host_type(Acc), + Res = case mod_privacy_backend:get_privacy_list(HostType, LUser, LServer, <<"blocking">>) of {error, not_found} -> {ok, []}; {ok, L} -> @@ -67,11 +82,12 @@ process_iq_get(Val, _, _, _, _) -> process_iq_set(Acc, From, _To, #iq{xmlns = ?NS_BLOCKING, sub_el = SubEl}) -> %% collect needed data + HostType = mongoose_acc:host_type(Acc), #jid{luser = LUser, lserver = LServer} = From, #xmlel{name = BType} = SubEl, Type = parse_command_type(BType), Usrs = exml_query:paths(SubEl, [{element, <<"item">>}, {attr, <<"jid">>}]), - CurrList = case mod_privacy_backend:get_privacy_list(LUser, LServer, <<"blocking">>) of + CurrList = case mod_privacy_backend:get_privacy_list(HostType, LUser, LServer, <<"blocking">>) of {ok, List} -> List; {error, not_found} -> @@ -111,12 +127,13 @@ process_blocking_iq_set(block, Acc, _, _, _, []) -> {Acc, {error, mongoose_xmpp_errors:bad_request()}}; process_blocking_iq_set(Type, Acc, LUser, LServer, CurrList, Usrs) -> %% check who is being added / removed + HostType = mongoose_acc:host_type(Acc), {NType, Changed, NewList} = blocking_list_modify(Type, Usrs, CurrList), - case mod_privacy_backend:replace_privacy_list(LUser, LServer, <<"blocking">>, NewList) of + case mod_privacy_backend:replace_privacy_list(HostType, LUser, LServer, <<"blocking">>, NewList) of {error, E} -> {error, E}; ok -> - case mod_privacy_backend:set_default_list(LUser, LServer, <<"blocking">>) of + case mod_privacy_backend:set_default_list(HostType, LUser, LServer, <<"blocking">>) of ok -> {Acc, {ok, Changed, NewList, NType}}; {error, not_found} ->