Skip to content

Commit

Permalink
Merge pull request #3549 from esl/module-opts-map-private
Browse files Browse the repository at this point in the history
Put mod_private opts in a map with defaults and updating tests
  • Loading branch information
chrzaszcz authored Feb 21, 2022
2 parents c431f16 + 246a677 commit d23ac7a
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 21 deletions.
2 changes: 1 addition & 1 deletion big_tests/tests/domain_removal_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ group_to_modules(muc_removal) ->
group_to_modules(inbox_removal) ->
[{mod_inbox, inbox_helper:inbox_opts()}];
group_to_modules(private_removal) ->
[{mod_private, [{backend, rdbms}]}];
[{mod_private, #{iqdisc => one_queue, backend => rdbms}}];
group_to_modules(roster_removal) ->
[{mod_roster, [{backend, rdbms}]}];
group_to_modules(offline_removal) ->
Expand Down
7 changes: 6 additions & 1 deletion big_tests/tests/gdpr_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,12 @@ offline_started() ->
dynamic_modules:ensure_modules(host_type(), offline_required_modules()).

private_required_modules() ->
[{mod_private, [{backend, pick_enabled_backend()}]}].
[{mod_private, create_private_config(pick_enabled_backend())}].

create_private_config(riak) ->
config_parser_helper:mod_config(mod_private, #{backend => riak, riak => #{bucket_type => <<"private">>}});
create_private_config(Backend) ->
config_parser_helper:mod_config(mod_private, #{backend => Backend}).

private_started() ->
dynamic_modules:ensure_modules(host_type(), private_required_modules()).
Expand Down
4 changes: 3 additions & 1 deletion big_tests/tests/mongooseimctl_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ init_per_group(basic, Config) ->
dynamic_modules:ensure_modules(domain_helper:host_type(), [{mod_offline, []}]),
Config;
init_per_group(private, Config) ->
dynamic_modules:ensure_modules(domain_helper:host_type(), [{mod_private, []}]),
dynamic_modules:ensure_modules(domain_helper:host_type(),
[{mod_private, #{iqdisc => one_queue}}]
),
Config;
init_per_group(vcard, Config) ->
case rpc(mim(), gen_mod, get_module_opt, [host_type(), mod_vcard, backend, mnesia]) of
Expand Down
9 changes: 8 additions & 1 deletion big_tests/tests/private_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,17 @@ init_per_suite(Config0) ->
HostType = domain_helper:host_type(),
Config1 = dynamic_modules:save_modules(HostType, Config0),
Backend = mongoose_helper:get_backend_mnesia_rdbms_riak(HostType),
ModConfig = mongoose_helper:backend_for_module(mod_private, Backend),
ModConfig = create_config(Backend),
dynamic_modules:ensure_modules(HostType, ModConfig),
escalus:init_per_suite([{backend, Backend} | Config1]).

create_config(riak) ->
[{mod_private, #{backend => riak,
iqdisc => one_queue,
riak => #{bucket_type => <<"private">>}}}];
create_config(Backend) ->
[{mod_private, #{backend => Backend, iqdisc => one_queue}}].

end_per_suite(Config) ->
dynamic_modules:restore_modules(Config),
escalus:end_per_suite(Config).
Expand Down
23 changes: 17 additions & 6 deletions src/mod_private.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
config_spec/0,
process_iq/5,
remove_user/3,
remove_domain/3]).
remove_domain/3,
remove_unused_backend_opts/1]).

-export([get_personal_data/3]).

Expand Down Expand Up @@ -69,13 +70,14 @@ get_personal_data(Acc, HostType, #jid{ luser = LUser, lserver = LServer }) ->
%% ------------------------------------------------------------------
%% gen_mod callbacks

start(HostType, Opts) ->
-spec start(HostType :: mongooseim:host_type(), Opts :: gen_mod:module_opts()) -> ok | {error, atom()}.
start(HostType, #{iqdisc := IQDisc} = Opts) ->
mod_private_backend:init(HostType, Opts),
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
ejabberd_hooks:add(hooks(HostType)),
gen_iq_handler:add_iq_handler_for_domain(HostType, ?NS_PRIVATE, ejabberd_sm,
fun ?MODULE:process_iq/5, #{}, IQDisc).

-spec stop(HostType :: mongooseim:host_type()) -> ok | {error, not_registered}.
stop(HostType) ->
ejabberd_hooks:delete(hooks(HostType)),
gen_iq_handler:remove_iq_handler_for_domain(HostType, ?NS_PRIVATE, ejabberd_sm).
Expand All @@ -93,15 +95,24 @@ config_spec() ->
items = #{<<"iqdisc">> => mongoose_config_spec:iqdisc(),
<<"backend">> => #option{type = atom,
validate = {module, mod_private}},
<<"riak">> => riak_config_spec()}
}.
<<"riak">> => riak_config_spec()},
defaults = #{<<"iqdisc">> => one_queue,
<<"backend">> => rdbms},
format_items = map,
process = fun ?MODULE:remove_unused_backend_opts/1
}.

remove_unused_backend_opts(Opts = #{backend := riak}) -> Opts;
remove_unused_backend_opts(Opts) -> maps:remove(riak, Opts).

riak_config_spec() ->
#section{
items = #{<<"bucket_type">> => #option{type = binary,
validate = non_empty}
},
wrap = none
defaults = #{<<"bucket_type">> => <<"private">>},
include = always,
format_items = map
}.

%% ------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/mod_private_backend.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

-callback init(HostType, Opts) -> ok when
HostType :: mongooseim:host_type(),
Opts :: list().
Opts :: gen_mod:module_opts().

-callback multi_set_data(HostType, LUser, LServer, NS2XML) -> Result when
HostType :: mongooseim:host_type(),
Expand Down Expand Up @@ -58,7 +58,7 @@

-spec init(HostType, Opts) -> ok when
HostType :: mongooseim:host_type(),
Opts :: list().
Opts :: gen_mod:module_opts().
init(HostType, Opts) ->
TrackedFuns = [multi_get_data, multi_set_data],
mongoose_backend:init(HostType, ?MAIN_MODULE, TrackedFuns, Opts),
Expand Down
2 changes: 1 addition & 1 deletion src/mod_private_riak.erl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ get_private_data(LUser, LServer, NS, Default) ->
end.

bucket_type(LServer) ->
{gen_mod:get_module_opt(LServer, mod_private, bucket_type, <<"private">>), LServer}.
{gen_mod:get_module_opt(LServer, mod_private, [riak, bucket_type]), LServer}.

key(LUser, NS) ->
<<LUser/binary, "/", NS/binary>>.
4 changes: 3 additions & 1 deletion test/common/config_parser_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ pgsql_modules() ->
mod_muc_commands => [], mod_muc_light_commands => [],
mod_offline => [{backend, rdbms}],
mod_privacy => [{backend, rdbms}],
mod_private => [{backend, rdbms}],
mod_private => default_mod_config(mod_private),
mod_register =>
[{access, register},
{ip_access, [{allow, "127.0.0.0/8"}, {deny, "0.0.0.0/0"}]},
Expand Down Expand Up @@ -833,5 +833,7 @@ default_mod_config(mod_inbox) ->
remove_on_kicked => true,
reset_markers => [<<"displayed">>],
iqdisc => no_queue};
default_mod_config(mod_private) ->
#{iqdisc => one_queue, backend => rdbms};
default_mod_config(mod_version) ->
#{iqdisc => no_queue, os_info => false}.
15 changes: 8 additions & 7 deletions test/config_parser_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2587,15 +2587,16 @@ mod_privacy(_Config) ->
?errh(T(#{<<"riak">> => #{<<"bucket_type">> => 1}})).

mod_private(_Config) ->
check_iqdisc_map(mod_private),
check_module_defaults(mod_private),
T = fun(Opts) -> #{<<"modules">> => #{<<"mod_private">> => Opts}} end,
M = fun(Cfg) -> modopts(mod_private, Cfg) end,
?cfgh(M([{backend, riak}]),
T(#{<<"backend">> => <<"riak">>})),
?cfgh(M([{bucket_type, <<"private_stuff">>}]),
T(#{<<"riak">> => #{<<"bucket_type">> => <<"private_stuff">>}})),
P = [modules, mod_private],
?cfgh(P ++ [backend], riak, T(#{<<"backend">> => <<"riak">>})),
?cfgh(P ++ [riak, bucket_type], <<"private">>, T(#{<<"backend">> => <<"riak">>})),
?cfgh(P ++ [riak, bucket_type], <<"private_stuff">>, T(#{<<"backend">> => <<"riak">>,
<<"riak">> => #{<<"bucket_type">> => <<"private_stuff">>}})),
?errh(T(#{<<"backend">> => <<"mssql">>})),
?errh(T(#{<<"riak">> => #{<<"bucket_type">> => 1}})),
check_iqdisc(mod_private).
?errh(T(#{<<"riak">> => #{<<"bucket_type">> => 1}})).

mod_pubsub(_Config) ->
check_iqdisc(mod_pubsub),
Expand Down

0 comments on commit d23ac7a

Please sign in to comment.