From 0f2b272d5adc4edc57c9618e3b805b9acef926d1 Mon Sep 17 00:00:00 2001 From: Mikhail Uvarov Date: Thu, 24 Mar 2022 17:27:29 +0100 Subject: [PATCH 1/2] Make mod_offline_chatmarkers configurable --- big_tests/tests/offline_SUITE.erl | 4 +++- src/config/mongoose_config_parser_toml.erl | 2 +- src/config/mongoose_config_spec.erl | 1 + src/offline/mod_offline_chatmarkers.erl | 21 ++++++++++++++++++--- test/common/config_parser_helper.erl | 3 +++ test/config_parser_SUITE.erl | 16 ++++++++++++++-- 6 files changed, 40 insertions(+), 7 deletions(-) diff --git a/big_tests/tests/offline_SUITE.erl b/big_tests/tests/offline_SUITE.erl index a214bcd3e9..294877cdcb 100644 --- a/big_tests/tests/offline_SUITE.erl +++ b/big_tests/tests/offline_SUITE.erl @@ -109,7 +109,9 @@ config_with_groupchat_modules(Backend) -> chatmarkers_modules() -> [{mod_smart_markers, config_parser_helper:default_mod_config(mod_smart_markers)}, {mod_offline, config_with_groupchat_modules(rdbms)}, - {mod_offline_chatmarkers, [{store_groupchat_messages, true}]}, + {mod_offline_chatmarkers, + mod_config(mod_offline_chatmarkers, + #{store_groupchat_messages => true})}, {mod_muc_light, mod_config(mod_muc_light, #{backend => rdbms})}]. end_per_group(Group, C) when Group =:= chatmarkers; diff --git a/src/config/mongoose_config_parser_toml.erl b/src/config/mongoose_config_parser_toml.erl index 49c3a90d34..dbf61f7a40 100644 --- a/src/config/mongoose_config_parser_toml.erl +++ b/src/config/mongoose_config_parser_toml.erl @@ -110,7 +110,7 @@ get_spec_for_key(Key, Items) -> false -> case maps:find(default, Items) of {ok, Spec} -> Spec; - error -> error(#{what => unexpected_key, key => Key}) + error -> error(#{what => unexpected_key, key => Key, items => Items}) end end. diff --git a/src/config/mongoose_config_spec.erl b/src/config/mongoose_config_spec.erl index 6f7794002a..6a90e729f6 100644 --- a/src/config/mongoose_config_spec.erl +++ b/src/config/mongoose_config_spec.erl @@ -821,6 +821,7 @@ configurable_modules() -> mod_muc_light, mod_muc_log, mod_offline, + mod_offline_chatmarkers, mod_ping, mod_privacy, mod_private, diff --git a/src/offline/mod_offline_chatmarkers.erl b/src/offline/mod_offline_chatmarkers.erl index 3f866b6fd4..a9e7555a8f 100644 --- a/src/offline/mod_offline_chatmarkers.erl +++ b/src/offline/mod_offline_chatmarkers.erl @@ -40,6 +40,7 @@ -export([stop/1]). -export([deps/2]). -export([supported_features/0]). +-export([config_spec/0]). %% Hook handlers -export([inspect_packet/4, @@ -52,6 +53,7 @@ -include("jlib.hrl"). -include_lib("exml/include/exml.hrl"). +-include("mongoose_config_spec.hrl"). %% gen_mod callbacks %% ------------------------------------------------------------------ @@ -61,7 +63,7 @@ supported_features() -> [dynamic_domains]. -spec deps(mongooseim:host_type(), gen_mod:module_opts()) -> gen_mod_deps:deps(). -deps(_,_)-> +deps(_, _)-> []. %% TODO: this need to be marked as required-to-be-configured % [{mod_smart_markers, [], hard}]. @@ -80,7 +82,7 @@ hooks(HostType) -> {resend_offline_messages_hook, HostType, ?MODULE, pop_offline_messages, 60}, {remove_user, HostType, ?MODULE, remove_user, 50} ], - case gen_mod:get_module_opt(HostType, ?MODULE, store_groupchat_messages, false) of + case gen_mod:get_module_opt(HostType, ?MODULE, store_groupchat_messages) of true -> GroupChatHook = {offline_groupchat_message_hook, HostType, ?MODULE, inspect_packet, 40}, @@ -88,9 +90,22 @@ hooks(HostType) -> _ -> DefaultHooks end. +-spec config_spec() -> mongoose_config_spec:config_section(). +config_spec() -> + #section{ + items = #{<<"backend">> => #option{type = atom, + validate = {module, ?MODULE}}, + <<"store_groupchat_messages">> => #option{type = boolean} + }, + defaults = #{<<"store_groupchat_messages">> => false, + <<"backend">> => rdbms + }, + format_items = map + }. + remove_user(Acc, User, Server) -> HostType = mongoose_acc:host_type(Acc), - mod_offline_chatmarkers_backend:remove_user(HostType, jid:make(User, Server, <<"">>)), + mod_offline_chatmarkers_backend:remove_user(HostType, jid:make(User, Server, <<>>)), Acc. pop_offline_messages(Acc, JID) -> diff --git a/test/common/config_parser_helper.erl b/test/common/config_parser_helper.erl index af9824f983..6dc1c618d0 100644 --- a/test/common/config_parser_helper.erl +++ b/test/common/config_parser_helper.erl @@ -957,6 +957,9 @@ default_mod_config(mod_offline) -> #{backend => mnesia, access_max_user_messages => max_user_offline_messages, store_groupchat_messages => false}; +default_mod_config(mod_offline_chatmarkers) -> + #{backend => rdbms, + store_groupchat_messages => false}; default_mod_config(mod_privacy) -> #{backend => mnesia}; default_mod_config(mod_private) -> diff --git a/test/config_parser_SUITE.erl b/test/config_parser_SUITE.erl index cffca71446..0fecaad5d2 100644 --- a/test/config_parser_SUITE.erl +++ b/test/config_parser_SUITE.erl @@ -233,6 +233,7 @@ groups() -> mod_muc_light, mod_muc_light_config_schema, mod_offline, + mod_offline_chatmarkers, mod_ping, mod_privacy, mod_private, @@ -2534,6 +2535,17 @@ mod_offline(_Config) -> ?errh(T(#{<<"riak">> => #{<<"bucket_type">> => 1}})), ?errh(T(#{<<"riak">> => #{<<"bucket">> => <<"leaky">>}})). +mod_offline_chatmarkers(_Config) -> + check_module_defaults(mod_offline_chatmarkers), + T = fun(Opts) -> #{<<"modules">> => #{<<"mod_offline_chatmarkers">> => Opts}} end, + P = [modules, mod_offline_chatmarkers], + ?cfgh(P ++ [backend], rdbms, + T(#{<<"backend">> => <<"rdbms">>})), + ?cfgh(P ++ [store_groupchat_messages], true, + T(#{<<"store_groupchat_messages">> => true})), + ?errh(T(#{<<"store_groupchat_messages">> => 1})), + ?errh(T(#{<<"backend">> => <<"riak_is_the_best">>})). + mod_ping(_Config) -> T = fun(Opts) -> #{<<"modules">> => #{<<"mod_ping">> => Opts}} end, P = [modules, mod_ping], @@ -3382,10 +3394,10 @@ handle_module_options(V1, V2) -> ?eq(V1, V2). %% Generic assertions, use the 'F' handler for any custom cases -compare_unordered_lists(L1, L2) -> +compare_unordered_lists(L1, L2) when is_list(L1), is_list(L2) -> compare_unordered_lists(L1, L2, fun(V1, V2) -> ?eq(V1, V2) end). -compare_unordered_lists(L1, L2, F) -> +compare_unordered_lists(L1, L2, F) when is_list(L1), is_list(L2) -> SL1 = lists:sort(L1), SL2 = lists:sort(L2), compare_ordered_lists(SL1, SL2, F). From 167478a65fcf40555f1bdb9413b078a0cf472701 Mon Sep 17 00:00:00 2001 From: Mikhail Uvarov Date: Tue, 29 Mar 2022 14:24:13 +0200 Subject: [PATCH 2/2] Remove mod_offline_chatmarkers:add_default_backend function --- src/offline/mod_offline_chatmarkers.erl | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/offline/mod_offline_chatmarkers.erl b/src/offline/mod_offline_chatmarkers.erl index a9e7555a8f..67b5685d4f 100644 --- a/src/offline/mod_offline_chatmarkers.erl +++ b/src/offline/mod_offline_chatmarkers.erl @@ -67,11 +67,13 @@ deps(_, _)-> []. %% TODO: this need to be marked as required-to-be-configured % [{mod_smart_markers, [], hard}]. +-spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok. start(HostType, Opts) -> - mod_offline_chatmarkers_backend:init(HostType, add_default_backend(Opts)), + mod_offline_chatmarkers_backend:init(HostType, Opts), ejabberd_hooks:add(hooks(HostType)), ok. +-spec stop(mongooseim:host_type()) -> ok. stop(HostType) -> ejabberd_hooks:delete(hooks(HostType)), ok. @@ -192,11 +194,3 @@ thread(undefined) -> []; thread(Thread) -> [#xmlel{name = <<"thread">>, attrs = [], children = [#xmlcdata{content = Thread}]}]. - -add_default_backend(Opts) -> - case lists:keyfind(backend, 2, Opts) of - false -> - [{backend, rdbms} | Opts]; - _ -> - Opts - end.