Skip to content

Commit

Permalink
Use atoms as keys for defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
arcusfelis committed Mar 8, 2022
1 parent 9f49539 commit 21a76c8
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 77 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 @@ -94,7 +94,7 @@ group_to_modules(muc_light_removal) ->
group_to_modules(muc_removal) ->
MucHost = subhost_pattern(muc_helper:muc_host_pattern()),
Opts = #{backend => rdbms, host => MucHost},
[{mod_muc, config_parser_helper:mod_config(mod_muc, Opts)}];
[{mod_muc, muc_helper:make_opts(Opts)}];
group_to_modules(inbox_removal) ->
[{mod_inbox, inbox_helper:inbox_opts()}];
group_to_modules(private_removal) ->
Expand Down
6 changes: 2 additions & 4 deletions big_tests/tests/gdpr_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -371,19 +371,17 @@ mam_required_modules(retrieve_mam_pm_and_muc_light_interfere, Backend) ->
{mod_muc_light, [{host, HostPattern}]}];
mam_required_modules(CN, Backend) when CN =:= retrieve_mam_muc_private_msg;
CN =:= retrieve_mam_muc ->
HostType = domain_helper:host_type(),
HostPattern = subhost_pattern(muc_helper:muc_host_pattern()),
[{mod_mam_meta, mam_helper:config_opts(#{backend => Backend,
pm => #{},
muc => #{host => HostPattern}})},
{mod_muc, dynamic_modules:start(HostType, mod_muc, #{host => HostPattern})}];
{mod_muc, muc_helper:make_opts(#{host => HostPattern})}];
mam_required_modules(retrieve_mam_muc_store_pm, Backend) ->
HostType = domain_helper:host_type(),
HostPattern = subhost_pattern(muc_helper:muc_host_pattern()),
[{mod_mam_meta, mam_helper:config_opts(#{backend => Backend,
pm => #{archive_groupchats => true},
muc => #{host => HostPattern}})},
{mod_muc, dynamic_modules:start(HostType, mod_muc, #{host => HostPattern})}].
{mod_muc, muc_helper:make_opts(#{host => HostPattern})}].

pick_enabled_backend() ->
BackendsList = [
Expand Down
4 changes: 2 additions & 2 deletions big_tests/tests/muc_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,13 @@ init_per_group(_GroupName, Config) ->

required_modules(http_auth) ->
MucHostPattern = ct:get_config({hosts, mim, muc_service_pattern}),
DefRoomOpts = rpc(mim(), mod_muc, default_room_defaults, []),
DefRoomOpts = rpc(mim(), mod_muc, default_room_opts, []),
Opts = #{host => subhost_pattern(MucHostPattern),
access => muc,
access_create => muc_create,
http_auth_pool => muc_http_auth_test,
default_room => DefRoomOpts#{password_protected => true}},
[{mod_muc, config_parser_helper:mod_config(mod_muc, Opts)}].
[{mod_muc, muc_helper:make_opts(Opts)}].

handle_http_auth(Req) ->
Qs = cowboy_req:parse_qs(Req),
Expand Down
13 changes: 11 additions & 2 deletions big_tests/tests/muc_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@

-export_type([verify_fun/0]).

%% Extend default opts with new ExtraOpts
make_opts(ExtraOpts) ->
Opts = rpc(mim(), mod_muc, default_opts, []),
maps:merge(Opts, ExtraOpts).

make_log_opts(ExtraOpts) ->
Opts = rpc(mim(), mod_muc_log, default_opts, []),
maps:merge(Opts, ExtraOpts).

-spec foreach_occupant(
Users :: [escalus:client()], Stanza :: #xmlel{}, VerifyFun :: verify_fun()) -> ok.
foreach_occupant(Users, Stanza, VerifyFun) ->
Expand Down Expand Up @@ -56,8 +65,8 @@ load_muc() ->
hibernated_room_timeout => 2000,
access => muc, access_create => muc_create},
LogOpts = #{outdir => "/tmp/muclogs", access_log => muc},
dynamic_modules:start(HostType, mod_muc, config_parser_helper:mod_config(mod_muc, Opts)),
dynamic_modules:start(HostType, mod_muc_log, config_parser_helper:mod_config(mod_muc_log, LogOpts)).
dynamic_modules:start(HostType, mod_muc, make_opts(Opts)),
dynamic_modules:start(HostType, mod_muc_log, make_log_opts(LogOpts)).

unload_muc() ->
HostType = domain_helper:host_type(),
Expand Down
4 changes: 4 additions & 0 deletions src/config/mongoose_config_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
-module(mongoose_config_utils).
-export([exit_or_halt/1]).
-export([section_to_defaults/1]).
-export([keys_are_binaries/1]).
-ignore_xref([section_to_defaults/1]).

-include("mongoose_config_spec.hrl").
Expand All @@ -21,3 +22,6 @@ exit_or_halt(ExitText) ->

section_to_defaults(#section{defaults = Defaults}) ->
Defaults.

keys_are_binaries(KV) ->
maps:from_list([{atom_to_binary(K, latin1), V} || {K, V} <- maps:to_list(KV)]).
2 changes: 1 addition & 1 deletion src/inbox/mod_inbox.erl
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ muclight_dep(List) ->

muc_dep(List) ->
case lists:member(muc, List) of
true -> [{mod_muc, #{}, hard}];
true -> [{mod_muc, mod_muc:default_opts(), hard}];
false -> []
end.

Expand Down
64 changes: 31 additions & 33 deletions src/mod_muc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@
-export([hibernated_rooms_number/0]).

-export([config_metrics/1]).
-export([default_room_defaults/0]).
-export([default_opts/0]).
-export([default_room_opts/0]).

-ignore_xref([
can_access_identity/4, can_access_room/4, acc_room_affiliations/2, create_instant_room/6,
disco_local_items/1, hibernated_rooms_number/0, is_muc_room_owner/4, remove_domain/3,
online_rooms_number/0, register_room/4, restore_room/3, start_link/2,
default_room_defaults/0
default_room_opts/0
]).

-include("mongoose.hrl").
Expand Down Expand Up @@ -250,35 +251,35 @@ config_spec() ->
validate = non_negative},
<<"default_room">> => default_room_config_spec()
},
defaults = defaults()
defaults = mongoose_config_utils:keys_are_binaries(default_opts())
}.

defaults() ->
default_opts() ->
#{
<<"backend">> => mnesia,
<<"host">> => default_host(),
<<"access">> => all,
<<"access_create">> => all,
<<"access_admin">> => none,
<<"access_persistent">> => all,
<<"history_size">> => 20,
<<"room_shaper">> => none,
<<"max_room_id">> => infinity,
<<"max_room_name">> => infinity,
<<"max_room_desc">> => infinity,
<<"min_message_interval">> => 0,
<<"min_presence_interval">> => 0,
<<"max_users">> => ?MAX_USERS_DEFAULT,
<<"max_users_admin_threshold">> => 5,
<<"user_message_shaper">> => none,
<<"user_presence_shaper">> => none,
<<"max_user_conferences">> => 10,
<<"http_auth_pool">> => none,
<<"load_permanent_rooms_at_startup">> => false,
<<"hibernate_timeout">> => timer:seconds(90),
<<"hibernated_room_check_interval">> => infinity,
<<"hibernated_room_timeout">> => infinity,
<<"default_room">> => default_room_defaults()
backend => mnesia,
host => default_host(),
access => all,
access_create => all,
access_admin => none,
access_persistent => all,
history_size => 20,
room_shaper => none,
max_room_id => infinity,
max_room_name => infinity,
max_room_desc => infinity,
min_message_interval => 0,
min_presence_interval => 0,
max_users => ?MAX_USERS_DEFAULT,
max_users_admin_threshold => 5,
user_message_shaper => none,
user_presence_shaper => none,
max_user_conferences => 10,
http_auth_pool => none,
load_permanent_rooms_at_startup => false,
hibernate_timeout => timer:seconds(90),
hibernated_room_check_interval => infinity,
hibernated_room_timeout => infinity,
default_room => default_room_opts()
}.

default_room_config_spec() ->
Expand Down Expand Up @@ -311,13 +312,10 @@ default_room_config_spec() ->
<<"subject">> => #option{type = binary},
<<"subject_author">> => #option{type = binary}
},
defaults = keys_are_binaries(default_room_defaults())
defaults = mongoose_config_utils:keys_are_binaries(default_room_opts())
}.

keys_are_binaries(KV) ->
maps:from_list([{atom_to_binary(K, latin1), V} || {K, V} <- maps:to_list(KV)]).

default_room_defaults() ->
default_room_opts() ->
X = #config{},
#{
title => X#config.title,
Expand Down
25 changes: 14 additions & 11 deletions src/mod_muc_log.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@
-export([config_spec/0,
process_top_link/1]).

-export([default_opts/0]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).

-ignore_xref([start_link/2]).
-ignore_xref([default_opts/2]).

-include("mongoose.hrl").
-include("jlib.hrl").
Expand Down Expand Up @@ -147,20 +150,20 @@ config_spec() ->
<<"top_link">> => top_link_config_spec(),
<<"spam_prevention">> => #option{type = boolean}
},
defaults = defaults()
defaults = mongoose_config_utils:keys_are_binaries(default_opts())
}.

defaults() ->
default_opts() ->
#{
<<"outdir">> => "www/muc",
<<"access_log">> => muc_admin,
<<"dirtype">> => subdirs,
<<"dirname">> => room_jid,
<<"file_format">> => html,
<<"css_file">> => false,
<<"timezone">> => local,
<<"top_link">> => {"/", "Home"},
<<"spam_prevention">> => true
outdir => "www/muc",
access_log => muc_admin,
dirtype => subdirs,
dirname => room_jid,
file_format => html,
css_file => false,
timezone => local,
top_link => {"/", "Home"},
spam_prevention => true
}.

top_link_config_spec() ->
Expand Down
24 changes: 1 addition & 23 deletions test/common/config_parser_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -890,11 +890,7 @@ default_mod_config(mod_mam_rdbms_arch) ->
default_mod_config(mod_mam_muc_rdbms_arch) ->
#{no_writer => false,
db_message_format => mam_message_compressed_eterm,
db_jid_format => mam_jid_rfc};
default_mod_config(mod_muc) ->
defaults_from_spec(mod_muc);
default_mod_config(mod_muc_log) ->
defaults_from_spec(mod_muc_log).
db_jid_format => mam_jid_rfc}.

default_config([modules, M]) ->
default_mod_config(M);
Expand Down Expand Up @@ -975,21 +971,3 @@ common_mam_config() ->

config(Path, Opts) ->
maps:merge(default_config(Path), Opts).

defaults_from_spec(Module) ->
keys_are_atoms(defaults_from_spec2(Module)).

defaults_from_spec2(Module) ->
try
%% Call from small tests
mongoose_config_utils:section_to_defaults(Module:config_spec())
catch _:_ ->
%% Call from big tests
Node = distributed_helper:mim(),
Spec = distributed_helper:rpc(Node, Module, config_spec, []),
distributed_helper:rpc(Node, mongoose_config_utils,
section_to_defaults, [Spec])
end.

keys_are_atoms(KV) ->
maps:from_list([{binary_to_atom(K, latin1), V} || {K, V} <- maps:to_list(KV)]).

0 comments on commit 21a76c8

Please sign in to comment.