Skip to content

Commit

Permalink
Make mod_blocking multitenancy ready
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Jul 21, 2021
1 parent 1d1149d commit a7d71c0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
2 changes: 2 additions & 0 deletions big_tests/dynamic_domains.spec
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

{suites, "tests", mam_SUITE}.

{suites, "tests", mod_blocking_SUITE}.

{suites, "tests", mod_ping_SUITE}.

{suites, "tests", muc_SUITE}.
Expand Down
4 changes: 1 addition & 3 deletions rel/files/mongooseim.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}

Expand Down
1 change: 1 addition & 0 deletions rel/mim1.vars-toml.config
Original file line number Diff line number Diff line change
Expand Up @@ -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\"]"}.
Expand Down
47 changes: 32 additions & 15 deletions src/mod_blocking.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
]).

Expand All @@ -27,27 +32,37 @@

-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 := <<>>}) ->
mongoose_disco:add_features([?NS_BLOCKING], Acc);
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} ->
Expand All @@ -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} ->
Expand Down Expand Up @@ -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} ->
Expand Down

0 comments on commit a7d71c0

Please sign in to comment.