From 4a952df6ba726368d2ad1768df09021b0a9726f0 Mon Sep 17 00:00:00 2001 From: Kamil Waz Date: Thu, 3 Nov 2022 14:22:22 +0100 Subject: [PATCH] Convert mod_mam to gen_hook --- big_tests/tests/amp_big_SUITE.erl | 7 +- .../mam_send_message_example.erl | 17 +- src/mam/ejabberd_gen_mam_archive.erl | 50 ++-- src/mam/ejabberd_gen_mam_prefs.erl | 26 +- src/mam/mod_mam_cache_user.erl | 59 +++-- src/mam/mod_mam_cassandra_arch.erl | 96 ++++--- src/mam/mod_mam_cassandra_prefs.erl | 85 +++--- src/mam/mod_mam_elasticsearch_arch.erl | 97 ++++--- src/mam/mod_mam_mnesia_prefs.erl | 101 +++---- src/mam/mod_mam_muc.erl | 73 +++--- src/mam/mod_mam_muc_cassandra_arch.erl | 97 ++++--- src/mam/mod_mam_muc_elasticsearch_arch.erl | 109 ++++---- src/mam/mod_mam_muc_rdbms_arch.erl | 97 +++---- src/mam/mod_mam_muc_rdbms_arch_async.erl | 39 ++- src/mam/mod_mam_pm.erl | 8 +- src/mam/mod_mam_rdbms_arch.erl | 103 ++++---- src/mam/mod_mam_rdbms_arch_async.erl | 38 ++- src/mam/mod_mam_rdbms_prefs.erl | 86 +++--- src/mam/mod_mam_rdbms_user.erl | 53 ++-- src/mam/mod_mam_riak_timed_arch_yz.erl | 151 +++++++---- src/metrics/mongoose_metrics_mam_hooks.erl | 185 +++++++------ src/mod_dynamic_domains_test.erl | 3 +- src/mongoose_hooks.erl | 248 +++++++++++------- 23 files changed, 1041 insertions(+), 787 deletions(-) diff --git a/big_tests/tests/amp_big_SUITE.erl b/big_tests/tests/amp_big_SUITE.erl index 59b35b1865..7af4c914bb 100644 --- a/big_tests/tests/amp_big_SUITE.erl +++ b/big_tests/tests/amp_big_SUITE.erl @@ -152,9 +152,9 @@ setup_meck(suite) -> ok = rpc(mim(), meck, new, [ejabberd_socket, [passthrough, no_link]]), ok = rpc(mim(), amp_test_helper, setup_meck, []); setup_meck(mam_failure) -> - ok = rpc(mim(), meck, expect, [mod_mam_rdbms_arch, archive_message, 3, {error, simulated}]); + ok = rpc(mim(), meck, expect, [mod_mam_rdbms_arch, archive_message, 3, {ok, {error, simulated}}]); setup_meck(offline_failure) -> - ok = rpc(mim(), meck, expect, [mod_offline_mnesia, write_messages, 4, {error, simulated}]); +ok = rpc(mim(), meck, expect, [mod_offline_mnesia, write_messages, 4, {error, simulated}]); setup_meck(_) -> ok. save_offline_status(mam_success, Config) -> [{offline_storage, mam} | Config]; @@ -371,6 +371,7 @@ notify_deliver_to_offline_user_test(Config) -> false -> none end, notify}, Rules = rules(Config, [Rule]), + io:format("~p~n", [Rules]), BobJid = escalus_users:get_jid(FreshConfig, bob), Msg = amp_message_to(BobJid, Rules, <<"A message in a bottle...">>), @@ -382,12 +383,14 @@ notify_deliver_to_offline_user_test(Config) -> true -> client_receives_notification(Alice, BobJid, Rule); false -> ok end, + io:format("~p~n", [?config(offline_storage, Config)]), case ?config(offline_storage, Config) of offline_failure -> client_receives_generic_error(Alice, <<"500">>, <<"wait">>); _ -> client_receives_nothing(Alice) end end), wait_until_no_session(FreshConfig, alice), + io:format("no session~n"), case is_offline_storage_working(Config) of true -> user_has_incoming_offline_message(FreshConfig, bob, <<"A message in a bottle...">>); false -> user_has_no_incoming_offline_messages(FreshConfig, bob) diff --git a/big_tests/tests/mam_send_message_SUITE_data/mam_send_message_example.erl b/big_tests/tests/mam_send_message_SUITE_data/mam_send_message_example.erl index ea536cdc78..ca93ddb8ee 100644 --- a/big_tests/tests/mam_send_message_SUITE_data/mam_send_message_example.erl +++ b/big_tests/tests/mam_send_message_SUITE_data/mam_send_message_example.erl @@ -16,25 +16,26 @@ start(HostType, _Opts) -> - ejabberd_hooks:add(hooks(HostType)). + gen_hook:add_handlers(hooks(HostType)). stop(HostType) -> - ejabberd_hooks:delete(hooks(HostType)). + gen_hook:delete_handlers(hooks(HostType)). supported_features() -> [dynamic_domains]. hooks(HostType) -> - [{mam_lookup_messages, HostType, ?MODULE, lookup_messages, 60}, - {mam_muc_lookup_messages, HostType, ?MODULE, lookup_messages, 60}]. + [{mam_lookup_messages, HostType, fun ?MODULE:lookup_messages/3, #{}, 60}, + {mam_muc_lookup_messages, HostType, fun ?MODULE:lookup_messages/3, #{}, 60}]. %% caller_jid could be used for privacy checking or per-user customization -lookup_messages({error, _Reason} = Result, _Host, _Params) -> - Result; +lookup_messages({error, _Reason} = Result, _Params, _Extra) -> + {ok, Result}; lookup_messages({ok, {TotalCount, Offset, MessageRows}}, - Host, _Params = #{owner_jid := ArcJID, caller_jid := _CallerJID}) -> + #{params := #{owner_jid := ArcJID, caller_jid := _CallerJID} = _Params}, + #{host_type := Host}) -> MessageRows2 = [extend_message(Host, ArcJID, Row) || Row <- MessageRows], - {ok, {TotalCount, Offset, MessageRows2}}. + {ok, {ok, {TotalCount, Offset, MessageRows2}}}. extend_message(_Host, _ArcJID, Row = #{}) -> %% Extend a message with a new field diff --git a/src/mam/ejabberd_gen_mam_archive.erl b/src/mam/ejabberd_gen_mam_archive.erl index a02dbcc873..ba043996ed 100644 --- a/src/mam/ejabberd_gen_mam_archive.erl +++ b/src/mam/ejabberd_gen_mam_archive.erl @@ -1,30 +1,40 @@ -module(ejabberd_gen_mam_archive). --ignore_xref([behaviour_info/1]). - --callback archive_size(Size :: integer(), Host :: jid:server(), - ArchiveID :: mod_mam:archive_id(), ArchiveJID :: jid:jid()) - -> integer(). - --callback archive_message(Result :: any(), Host :: jid:server(), - Params :: mod_mam:archive_message_params()) -> ok | {error, timeout}. - --callback lookup_messages(Result :: any(), Host :: jid:server(), - Params :: map()) -> Result when - Result :: {ok, mod_mam:lookup_result()} | {error, 'policy-violation'}. - --callback get_mam_pm_gdpr_data(mam_pm_gdpr_data(), mongooseim:host_type(), jid:jid()) -> mam_pm_gdpr_data(). - --callback get_mam_muc_gdpr_data(mam_muc_gdpr_data(), mongooseim:host_type(), jid:jid()) -> mam_muc_gdpr_data(). +-callback archive_size(Acc, Params, Extra) -> {ok | stop, Acc} when + Acc :: integer(), + Params :: map(), + Extra :: map(). + +-callback archive_message(Acc, Params, Extra) -> {ok | stop, Acc} when + Acc :: ok | {error, term()}, + Params :: map(), + Extra :: map(). + +-callback lookup_messages(Acc, Params, Extra) -> {ok | stop, Acc} when + Acc :: {ok, mod_mam:lookup_result()} | {error, term()}, + Params :: map(), + Extra :: map(). + +-callback get_mam_pm_gdpr_data(Acc, Params, Extra) -> {ok | stop, Acc} when + Acc :: mam_pm_gdpr_data(), + Params :: map(), + Extra :: map(). + +-callback get_mam_muc_gdpr_data(Acc, Params, Extra) -> {ok | stop, Acc} when + Acc :: mam_muc_gdpr_data(), + Params :: map(), + Extra :: map(). -optional_callbacks([get_mam_pm_gdpr_data/3, get_mam_muc_gdpr_data/3, - archive_size/4, + archive_size/3, lookup_messages/3]). --type mam_pm_gdpr_data() :: [{MessageID :: bitstring(), FromJID :: bitstring(), Message :: bitstring()}]. +-type mam_pm_gdpr_data() :: [{MessageID :: bitstring(), + FromJID :: bitstring(), + Message :: bitstring()}]. --type mam_muc_gdpr_data() :: [{MessageID :: bitstring(), Message :: bitstring()}]. +-type mam_muc_gdpr_data() :: [{MessageID :: bitstring(), + Message :: bitstring()}]. -export_type([mam_pm_gdpr_data/0, mam_muc_gdpr_data/0]). - diff --git a/src/mam/ejabberd_gen_mam_prefs.erl b/src/mam/ejabberd_gen_mam_prefs.erl index d0fafb2a5f..65652ddd04 100644 --- a/src/mam/ejabberd_gen_mam_prefs.erl +++ b/src/mam/ejabberd_gen_mam_prefs.erl @@ -1,18 +1,16 @@ -module(ejabberd_gen_mam_prefs). --ignore_xref([behaviour_info/1]). +-callback get_behaviour(Acc, Params, Extra) -> {ok | stop, Acc} when + Acc :: mod_mam:archive_behaviour(), + Params :: map(), + Extra :: map(). --callback get_behaviour(Default :: mod_mam:archive_behaviour(), - Host :: jid:server(), ArcID :: mod_mam:archive_id(), - LocJID :: jid:jid(), RemJID :: jid:jid()) -> any(). - --callback set_prefs(Result :: any(), Host :: jid:server(), - ArcID :: mod_mam:archive_id(), ArcJID :: jid:jid(), - DefaultMode :: mod_mam:archive_behaviour(), - AlwaysJIDs :: [jid:literal_jid()], - NeverJIDs :: [jid:literal_jid()]) -> any(). - --callback get_prefs(mod_mam:preference(), _Host :: jid:server(), - ArcId :: mod_mam:archive_id(), ArcJID :: jid:jid()) - -> mod_mam:preference(). +-callback set_prefs(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +-callback get_prefs(Acc, Params, Extra) -> {ok, Acc} when + Acc :: mod_mam:preference() | {error, Reason :: term()}, + Params :: map(), + Extra :: map(). diff --git a/src/mam/mod_mam_cache_user.erl b/src/mam/mod_mam_cache_user.erl index c0b7b28ef9..c88742898d 100644 --- a/src/mam/mod_mam_cache_user.erl +++ b/src/mam/mod_mam_cache_user.erl @@ -12,6 +12,7 @@ -module(mod_mam_cache_user). -behaviour(mongoose_module_metrics). +-behaviour(gen_mod). %% gen_mod handlers -export([start/2, stop/1, supported_features/0]). @@ -19,10 +20,7 @@ %% ejabberd handlers -export([cached_archive_id/3, store_archive_id/3, - remove_archive/4]). - --ignore_xref([start/2, stop/1, supported_features/0, - cached_archive_id/3, store_archive_id/3, remove_archive/4]). + remove_archive/3]). %%==================================================================== %% gen_mod callbacks @@ -31,12 +29,12 @@ -spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok. start(HostType, Opts) -> start_cache(HostType, Opts), - ejabberd_hooks:add(hooks(HostType, Opts)), + gen_hook:add_handlers(hooks(HostType, Opts)), ok. -spec stop(HostType :: mongooseim:host_type()) -> ok. stop(HostType) -> - ejabberd_hooks:delete(hooks(HostType)), + gen_hook:delete_handlers(hooks(HostType)), stop_cache(HostType), ok. @@ -44,7 +42,7 @@ stop(HostType) -> supported_features() -> [dynamic_domains]. --spec hooks(mongooseim:host_type()) -> [ejabberd_hooks:hook()]. +-spec hooks(mongooseim:host_type()) -> gen_hook:hook_list(). hooks(HostType) -> Opts = gen_mod:get_module_opts(HostType, ?MODULE), hooks(HostType, Opts). @@ -62,44 +60,51 @@ maybe_muc_hooks(true, HostType) -> muc_hooks(HostType); maybe_muc_hooks(false, _HostType) -> []. pm_hooks(HostType) -> - [{mam_archive_id, HostType, ?MODULE, cached_archive_id, 30}, - {mam_archive_id, HostType, ?MODULE, store_archive_id, 70}, - {mam_remove_archive, HostType, ?MODULE, remove_archive, 100}]. + [{mam_archive_id, HostType, fun ?MODULE:cached_archive_id/3, #{}, 30}, + {mam_archive_id, HostType, fun ?MODULE:store_archive_id/3, #{}, 70}, + {mam_remove_archive, HostType, fun ?MODULE:remove_archive/3, #{}, 100}]. muc_hooks(HostType) -> - [{mam_muc_archive_id, HostType, ?MODULE, cached_archive_id, 30}, - {mam_muc_archive_id, HostType, ?MODULE, store_archive_id, 70}, - {mam_muc_remove_archive, HostType, ?MODULE, remove_archive, 100}]. + [{mam_muc_archive_id, HostType, fun ?MODULE:cached_archive_id/3, #{}, 30}, + {mam_muc_archive_id, HostType, fun ?MODULE:store_archive_id/3, #{}, 70}, + {mam_muc_remove_archive, HostType, fun ?MODULE:remove_archive/3, #{}, 100}]. %%==================================================================== %% API %%==================================================================== --spec cached_archive_id(undefined, mongooseim:host_type(), jid:jid()) -> - mod_mam:archive_id(). -cached_archive_id(undefined, HostType, ArcJid) -> +-spec cached_archive_id(Acc, Params, Extra) -> {ok, Acc} when + Acc :: mod_mam:archive_id() | undefined, + Params :: map(), + Extra :: map(). +cached_archive_id(undefined, #{owner := ArcJid}, #{host_type := HostType}) -> case mongoose_user_cache:get_entry(HostType, ?MODULE, ArcJid) of - #{id := ArchId} -> ArchId; + #{id := ArchId} -> + {ok, ArchId}; _ -> put(mam_not_cached_flag, true), - undefined + {ok, undefined} end. --spec store_archive_id(mod_mam:archive_id(), mongooseim:host_type(), jid:jid()) - -> mod_mam:archive_id(). -store_archive_id(ArchId, HostType, ArcJid) -> +-spec store_archive_id(Acc, Params, Extra) -> {ok, Acc} when + Acc :: mod_mam:archive_id() | undefined, + Params :: map(), + Extra :: map(). +store_archive_id(ArchId, #{owner := ArcJid}, #{host_type := HostType}) -> case erase(mam_not_cached_flag) of undefined -> - ArchId; + {ok, ArchId}; true -> mongoose_user_cache:merge_entry(HostType, ?MODULE, ArcJid, #{id => ArchId}), - ArchId + {ok, ArchId} end. --spec remove_archive(Acc :: map(), HostType :: mongooseim:host_type(), - ArchId :: mod_mam:archive_id(), ArcJid :: jid:jid()) -> map(). -remove_archive(Acc, HostType, _UserID, ArcJid) -> +-spec remove_archive(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +remove_archive(Acc, #{owner := ArcJid}, #{host_type := HostType}) -> mongoose_user_cache:delete_user(HostType, ?MODULE, ArcJid), - Acc. + {ok, Acc}. %%==================================================================== %% internal diff --git a/src/mam/mod_mam_cassandra_arch.erl b/src/mam/mod_mam_cassandra_arch.erl index f228a820b6..87673abbc9 100644 --- a/src/mam/mod_mam_cassandra_arch.erl +++ b/src/mam/mod_mam_cassandra_arch.erl @@ -17,10 +17,10 @@ -export([start/2, stop/1]). %% MAM hook handlers --export([archive_size/4, +-export([archive_size/3, archive_message/3, lookup_messages/3, - remove_archive/4]). + remove_archive/3]). %% mongoose_cassandra callbacks -export([prepared_queries/0]). @@ -28,10 +28,6 @@ %gdpr -export([get_mam_pm_gdpr_data/3]). --ignore_xref([ - behaviour_info/1, remove_archive/4 -]). - %% ---------------------------------------------------------------------- %% Imports @@ -79,22 +75,22 @@ -spec start(host_type(), gen_mod:module_opts()) -> ok. start(HostType, _Opts) -> - ejabberd_hooks:add(hooks(HostType)). + gen_hook:add_handlers(hooks(HostType)). -spec stop(host_type()) -> ok. stop(HostType) -> - ejabberd_hooks:delete(hooks(HostType)). + gen_hook:delete_handlers(hooks(HostType)). %% ---------------------------------------------------------------------- %% Add hooks for mod_mam_pm --spec hooks(host_type()) -> [ejabberd_hooks:hook()]. +-spec hooks(mongooseim:host_type()) -> gen_hook:hook_list(). hooks(HostType) -> - [{mam_archive_message, HostType, ?MODULE, archive_message, 50}, - {mam_archive_size, HostType, ?MODULE, archive_size, 50}, - {mam_lookup_messages, HostType, ?MODULE, lookup_messages, 50}, - {mam_remove_archive, HostType, ?MODULE, remove_archive, 50}, - {get_mam_pm_gdpr_data, HostType, ?MODULE, get_mam_pm_gdpr_data, 50}]. + [{mam_archive_message, HostType, fun ?MODULE:archive_message/3, #{}, 50}, + {mam_archive_size, HostType, fun ?MODULE:archive_size/3, #{}, 50}, + {mam_lookup_messages, HostType, fun ?MODULE:lookup_messages/3, #{}, 50}, + {mam_remove_archive, HostType, fun ?MODULE:remove_archive/3, #{}, 50}, + {get_mam_pm_gdpr_data, HostType, fun ?MODULE:get_mam_pm_gdpr_data/3, #{}, 50}]. %% ---------------------------------------------------------------------- %% mongoose_cassandra_worker callbacks @@ -117,10 +113,14 @@ prepared_queries() -> %% ---------------------------------------------------------------------- %% Internal functions and callbacks -archive_size(Size, HostType, _UserID, UserJID) when is_integer(Size) -> +-spec archive_size(Acc, Params, Extra) -> {ok, Acc} when + Acc :: integer(), + Params :: map(), + Extra :: map(). +archive_size(Size, #{owner := UserJID}, #{host_type := HostType}) when is_integer(Size) -> Borders = Start = End = WithJID = undefined, Filter = prepare_filter(UserJID, Borders, Start, End, WithJID), - calc_count(pool_name(HostType), UserJID, HostType, Filter). + {ok, calc_count(pool_name(HostType), UserJID, HostType, Filter)}. %% ---------------------------------------------------------------------- @@ -131,11 +131,15 @@ insert_query_cql() -> "(id, user_jid, from_jid, remote_jid, with_jid, message) " "VALUES (?, ?, ?, ?, ?, ?)". -archive_message(_Result, HostType, Params) -> +-spec archive_message(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ok, + Params :: map(), + Extra :: map(). +archive_message(_Result, #{params := Params}, #{host_type := HostType}) -> try - archive_message2(Params, HostType) + {ok, archive_message2(Params, HostType)} catch _Type:Reason -> - {error, Reason} + {ok, {error, Reason}} end. archive_message2(#{message_id := MessID, @@ -187,9 +191,13 @@ remove_archive_offsets_query_cql() -> select_for_removal_query_cql() -> "SELECT DISTINCT user_jid, with_jid FROM mam_message WHERE user_jid = ?". -remove_archive(Acc, HostType, _UserID, UserJID) -> +-spec remove_archive(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +remove_archive(Acc, #{owner := UserJID}, #{host_type := HostType}) -> remove_archive(HostType, UserJID), - Acc. + {ok, Acc}. remove_archive(HostType, UserJID) -> PoolName = pool_name(HostType), @@ -209,24 +217,27 @@ remove_archive(HostType, UserJID) -> %% ---------------------------------------------------------------------- %% SELECT MESSAGES --spec lookup_messages(Result :: any(), HostType :: host_type(), Params :: map()) -> - {ok, mod_mam:lookup_result()}. -lookup_messages({error, _Reason} = Result, _HostType, _Params) -> - Result; -lookup_messages(_Result, _HostType, #{search_text := <<_/binary>>}) -> - {error, 'not-supported'}; -lookup_messages(_Result, HostType, - #{owner_jid := UserJID, rsm := RSM, borders := Borders, - start_ts := Start, end_ts := End, with_jid := WithJID, - search_text := undefined, page_size := PageSize, - is_simple := IsSimple}) -> +-spec lookup_messages(Acc, Params, Extra) -> {ok, Acc} when + Acc :: {ok, mod_mam:lookup_result()} | {error, term()}, + Params :: map(), + Extra :: map(). +lookup_messages({error, _Reason} = Result, _Params, _Extra) -> + {ok, Result}; +lookup_messages(_Result, #{params := #{search_text := <<_/binary>>}}, _Extra) -> + {ok, {error, 'not-supported'}}; +lookup_messages(_Result, + #{params := #{owner_jid := UserJID, rsm := RSM, borders := Borders, + start_ts := Start, end_ts := End, with_jid := WithJID, + search_text := undefined, page_size := PageSize, + is_simple := IsSimple}}, + #{host_type := HostType}) -> try - lookup_messages2(pool_name(HostType), HostType, - UserJID, RSM, Borders, - Start, End, WithJID, - PageSize, IsSimple) + {ok, lookup_messages2(pool_name(HostType), HostType, + UserJID, RSM, Borders, + Start, End, WithJID, + PageSize, IsSimple)} catch _Type:Reason:S -> - {error, {Reason, S}} + {ok, {error, {Reason, S}}} end. lookup_messages2(PoolName, HostType, @@ -415,15 +426,16 @@ row_to_uniform_format(HostType, #{from_jid := FromJID, message := Msg, id := Msg row_to_message_id(#{id := MsgID}) -> MsgID. --spec get_mam_pm_gdpr_data(ejabberd_gen_mam_archive:mam_pm_gdpr_data(), - host_type(), jid:jid()) -> - ejabberd_gen_mam_archive:mam_pm_gdpr_data(). -get_mam_pm_gdpr_data(Acc, HostType, JID) -> +-spec get_mam_pm_gdpr_data(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ejabberd_gen_mam_archive:mam_pm_gdpr_data(), + Params :: map(), + Extra :: map(). +get_mam_pm_gdpr_data(Acc, #{jid := JID}, #{host_type := HostType}) -> BinJID = jid:to_binary(jid:to_lower(JID)), FilterMap = #{user_jid => BinJID, with_jid => <<"">>}, Rows = fetch_user_messages(pool_name(HostType), JID, FilterMap), Messages = [rows_to_gdpr_mam_message(HostType, Row) || Row <- Rows], - Messages ++ Acc. + {ok, Messages ++ Acc}. rows_to_gdpr_mam_message(HostType, #{message := Data, id:= Id, from_jid:=FromJid}) -> {Id, FromJid, exml:to_binary(stored_binary_to_packet(HostType, Data))}. diff --git a/src/mam/mod_mam_cassandra_prefs.erl b/src/mam/mod_mam_cassandra_prefs.erl index a3ec8e119a..77c90b6df8 100644 --- a/src/mam/mod_mam_cassandra_prefs.erl +++ b/src/mam/mod_mam_cassandra_prefs.erl @@ -6,6 +6,7 @@ %%%------------------------------------------------------------------- -module(mod_mam_cassandra_prefs). -behaviour(mongoose_cassandra). +-behaviour(gen_mod). %% ---------------------------------------------------------------------- %% Exports @@ -15,15 +16,13 @@ %% MAM hook handlers -behaviour(ejabberd_gen_mam_prefs). --export([get_behaviour/5, - get_prefs/4, - set_prefs/7, - remove_archive/4]). +-export([get_behaviour/3, + get_prefs/3, + set_prefs/3, + remove_archive/3]). -export([prepared_queries/0]). --ignore_xref([remove_archive/4, start/2, stop/1]). - -include("mongoose.hrl"). -include("jlib.hrl"). -include_lib("exml/include/exml.hrl"). @@ -36,12 +35,12 @@ -spec start(host_type(), gen_mod:module_opts()) -> ok. start(HostType, Opts) -> - ejabberd_hooks:add(hooks(HostType, Opts)). + gen_hook:add_handlers(hooks(HostType, Opts)). -spec stop(host_type()) -> ok. stop(HostType) -> Opts = gen_mod:get_loaded_module_opts(HostType, ?MODULE), - ejabberd_hooks:delete(hooks(HostType, Opts)). + gen_hook:delete_handlers(hooks(HostType, Opts)). %% ---------------------------------------------------------------------- %% Hooks @@ -50,15 +49,15 @@ hooks(HostType, Opts) -> lists:flatmap(fun(Type) -> hooks(HostType, Type, Opts) end, [pm, muc]). hooks(HostType, pm, #{pm := true}) -> - [{mam_get_behaviour, HostType, ?MODULE, get_behaviour, 50}, - {mam_get_prefs, HostType, ?MODULE, get_prefs, 50}, - {mam_set_prefs, HostType, ?MODULE, set_prefs, 50}, - {mam_remove_archive, HostType, ?MODULE, remove_archive, 50}]; + [{mam_get_behaviour, HostType, fun ?MODULE:get_behaviour/3, #{}, 50}, + {mam_get_prefs, HostType, fun ?MODULE:get_prefs/3, #{}, 50}, + {mam_set_prefs, HostType, fun ?MODULE:set_prefs/3, #{}, 50}, + {mam_remove_archive, HostType, fun ?MODULE:remove_archive/3, #{}, 50}]; hooks(HostType, muc, #{muc := true}) -> - [{mam_muc_get_behaviour, HostType, ?MODULE, get_behaviour, 50}, - {mam_muc_get_prefs, HostType, ?MODULE, get_prefs, 50}, - {mam_muc_set_prefs, HostType, ?MODULE, set_prefs, 50}, - {mam_muc_remove_archive, HostType, ?MODULE, remove_archive, 50}]; + [{mam_muc_get_behaviour, HostType, fun ?MODULE:get_behaviour/3, #{}, 50}, + {mam_muc_get_prefs, HostType, fun ?MODULE:get_prefs/3, #{}, 50}, + {mam_muc_set_prefs, HostType, fun ?MODULE:set_prefs/3, #{}, 50}, + {mam_muc_remove_archive, HostType, fun ?MODULE:remove_archive/3, #{}, 50}]; hooks(_HostType, _Opt, _Opts) -> []. @@ -82,16 +81,19 @@ prepared_queries() -> %% ---------------------------------------------------------------------- %% Internal functions and callbacks --spec get_behaviour(Default :: mod_mam:archive_behaviour(), - HostType :: host_type(), ArchiveID :: mod_mam:archive_id(), - LocJID :: jid:jid(), RemJID :: jid:jid()) -> any(). -get_behaviour(DefaultBehaviour, HostType, _UserID, LocJID, RemJID) -> +-spec get_behaviour(Acc, Params, Extra) -> {ok, Acc} when + Acc :: mod_mam:archive_behaviour(), + Params :: map(), + Extra :: map(). +get_behaviour(DefaultBehaviour, + #{owner := LocJID, remote := RemJID}, + #{host_type := HostType}) -> BUserJID = mod_mam_utils:bare_jid(LocJID), BRemBareJID = mod_mam_utils:bare_jid(RemJID), BRemJID = mod_mam_utils:full_jid(RemJID), case query_behaviour(HostType, LocJID, BUserJID, BRemJID, BRemBareJID) of {ok, []} -> - DefaultBehaviour; + {ok, DefaultBehaviour}; {ok, [_ | _] = Rows} -> %% After sort <<>>, <<"a">>, <<"a/b">> SortedRows = lists:sort( @@ -100,24 +102,26 @@ get_behaviour(DefaultBehaviour, HostType, _UserID, LocJID, RemJID) -> {JID1, B1} < {JID2, B2} end, Rows), #{behaviour := Behaviour} = lists:last(SortedRows), - decode_behaviour(Behaviour) + {ok, decode_behaviour(Behaviour)} end. --spec set_prefs(Result :: any(), HostType :: host_type(), - ArchiveID :: mod_mam:archive_id(), ArchiveJID :: jid:jid(), - DefaultMode :: mod_mam:archive_behaviour(), - AlwaysJIDs :: [jid:literal_jid()], - NeverJIDs :: [jid:literal_jid()]) -> any(). -set_prefs(_Result, HostType, _UserID, UserJID, DefaultMode, AlwaysJIDs, NeverJIDs) -> +-spec set_prefs(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +set_prefs(_Result, + #{owner := UserJID, default_mode := DefaultMode, always_jids := AlwaysJIDs, + never_jids := NeverJIDs}, + #{host_type := HostType}) -> try - set_prefs1(HostType, UserJID, DefaultMode, AlwaysJIDs, NeverJIDs) + {ok, set_prefs1(HostType, UserJID, DefaultMode, AlwaysJIDs, NeverJIDs)} catch Type:Error:StackTrace -> ?LOG_ERROR(#{what => mam_set_prefs_failed, user_jid => UserJID, default_mode => DefaultMode, always_jids => AlwaysJIDs, never_jids => NeverJIDs, class => Type, reason => Error, stacktrace => StackTrace}), - {error, Error} + {ok, {error, Error}} end. set_prefs1(HostType, UserJID, DefaultMode, AlwaysJIDs, NeverJIDs) -> @@ -144,22 +148,25 @@ encode_row(BUserJID, BRemoteJID, Behaviour, Timestamp) -> behaviour => Behaviour, '[timestamp]' => Timestamp}. --spec get_prefs(mod_mam:preference(), _HostType :: host_type(), - ArchiveID :: mod_mam:archive_id(), ArchiveJID :: jid:jid()) - -> mod_mam:preference(). -get_prefs({GlobalDefaultMode, _, _}, HostType, _UserID, UserJID) -> +-spec get_prefs(Acc, Params, Extra) -> {ok, Acc} when + Acc :: mod_mam:preference(), + Params :: map(), + Extra :: map(). +get_prefs({GlobalDefaultMode, _, _}, #{owner := UserJID}, #{host_type := HostType}) -> BUserJID = mod_mam_utils:bare_jid(UserJID), Params = #{user_jid => BUserJID}, {ok, Rows} = mongoose_cassandra:cql_read(pool_name(HostType), UserJID, ?MODULE, get_prefs_query, Params), - decode_prefs_rows(Rows, GlobalDefaultMode, [], []). + {ok, decode_prefs_rows(Rows, GlobalDefaultMode, [], [])}. --spec remove_archive(mongoose_acc:t(), host_type(), mod_mam:archive_id(), jid:jid()) -> - mongoose_acc:t(). -remove_archive(Acc, HostType, _UserID, UserJID) -> +-spec remove_archive(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +remove_archive(Acc, #{owner := UserJID}, #{host_type := HostType}) -> remove_archive(HostType, UserJID), - Acc. + {ok, Acc}. remove_archive(HostType, UserJID) -> BUserJID = mod_mam_utils:bare_jid(UserJID), diff --git a/src/mam/mod_mam_elasticsearch_arch.erl b/src/mam/mod_mam_elasticsearch_arch.erl index 85a83b3a80..d54700e39a 100644 --- a/src/mam/mod_mam_elasticsearch_arch.erl +++ b/src/mam/mod_mam_elasticsearch_arch.erl @@ -29,13 +29,10 @@ %% ejabberd_gen_mam_archive callbacks -export([archive_message/3]). -export([lookup_messages/3]). --export([remove_archive/4]). --export([archive_size/4]). - +-export([remove_archive/3]). +-export([archive_size/3]). -export([get_mam_pm_gdpr_data/3]). --ignore_xref([remove_archive/4]). - -include("mongoose.hrl"). -include("mongoose_rsm.hrl"). -include("mod_mam.hrl"). @@ -50,18 +47,19 @@ -spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok. start(HostType, _Opts) -> - ejabberd_hooks:add(hooks(HostType)), + gen_hook:add_handlers(hooks(HostType)), ok. -spec stop(mongooseim:host_type()) -> ok. stop(HostType) -> - ejabberd_hooks:delete(hooks(HostType)), + gen_hook:delete_handlers(hooks(HostType)), ok. --spec get_mam_pm_gdpr_data(ejabberd_gen_mam_archive:mam_pm_gdpr_data(), - mongooseim:host_type(), jid:jid()) -> - ejabberd_gen_mam_archive:mam_pm_gdpr_data(). -get_mam_pm_gdpr_data(Acc, _HostType, Owner) -> +-spec get_mam_pm_gdpr_data(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ejabberd_gen_mam_archive:mam_pm_gdpr_data(), + Params :: map(), + Extra :: map(). +get_mam_pm_gdpr_data(Acc, #{jid := Owner}, _Extra) -> BinOwner = mod_mam_utils:bare_jid(Owner), Filter = #{term => #{owner => BinOwner}}, Sorting = #{mam_id => #{order => asc}}, @@ -69,17 +67,22 @@ get_mam_pm_gdpr_data(Acc, _HostType, Owner) -> {ok, #{<<"hits">> := #{<<"hits">> := Hits}}} = mongoose_elasticsearch:search(?INDEX_NAME, ?TYPE_NAME, SearchQuery), Messages = lists:map(fun hit_to_gdpr_mam_message/1, Hits), - Messages ++ Acc. + {ok, Messages ++ Acc}. %%------------------------------------------------------------------- %% ejabberd_gen_mam_archive callbacks %%------------------------------------------------------------------- -archive_message(_Result, Host, #{message_id := MessageId, - local_jid := LocalJid, - remote_jid := RemoteJid, - source_jid := SourceJid, - packet := Packet}) -> +-spec archive_message(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ok | {error, term()}, + Params :: map(), + Extra :: map(). +archive_message(_Result, #{params := Params}, #{host_type := Host}) -> + #{message_id := MessageId, + local_jid := LocalJid, + remote_jid := RemoteJid, + source_jid := SourceJid, + packet := Packet} = Params, Owner = mod_mam_utils:bare_jid(LocalJid), Remote = mod_mam_utils:bare_jid(RemoteJid), SourceBinJid = mod_mam_utils:full_jid(SourceJid), @@ -87,23 +90,31 @@ archive_message(_Result, Host, #{message_id := MessageId, Doc = make_document(MessageId, Owner, Remote, SourceBinJid, Packet), case mongoose_elasticsearch:insert_document(?INDEX_NAME, ?TYPE_NAME, DocId, Doc) of {ok, _} -> - ok; + {ok, ok}; {error, Reason} = Err -> ?LOG_ERROR(#{what => archive_message_failed, user => Owner, server => Host, remote => Remote, message_id => MessageId, reason => Reason}), mongoose_metrics:update(Host, modMamDropped, 1), - Err + {ok, Err} end. -lookup_messages(Result, Host, #{rsm := #rsm_in{direction = before, id = ID} = RSM} = Params) +-spec lookup_messages(Acc, Params, Extra) -> {ok, Acc} when + Acc :: {ok, mod_mam:lookup_result()} | {error, term()}, + Params :: map(), + Extra :: map(). +lookup_messages(Result, + #{params := #{rsm := #rsm_in{direction = before, id = ID} = RSM} = Params}, + #{host_type := Host}) when ID =/= undefined -> - lookup_message_page(Result, Host, RSM, Params); -lookup_messages(Result, Host, #{rsm := #rsm_in{direction = aft, id = ID} = RSM} = Params) + {ok, lookup_message_page(Result, Host, RSM, Params)}; +lookup_messages(Result, + #{params := #{rsm := #rsm_in{direction = aft, id = ID} = RSM} = Params}, + #{host_type := Host}) when ID =/= undefined -> - lookup_message_page(Result, Host, RSM, Params); -lookup_messages(Result, Host, Params) -> - do_lookup_messages(Result, Host, Params). + {ok, lookup_message_page(Result, Host, RSM, Params)}; +lookup_messages(Result, #{params := Params}, #{host_type := Host}) -> + {ok, do_lookup_messages(Result, Host, Params)}. lookup_message_page(AccResult, Host, #rsm_in{id = _ID} = RSM, Params) -> PageSize = maps:get(page_size, Params), @@ -130,21 +141,21 @@ do_lookup_messages(_Result, Host, Params) -> Err end. --spec archive_size(Size :: integer(), - Host :: jid:server(), - _ArchiveId, - OwnerJid :: jid:jid()) -> non_neg_integer(). -archive_size(_Size, _Host, _ArchiveId, OwnerJid) -> +-spec archive_size(Acc, Params, Extra) -> {ok, Acc} when + Acc :: integer(), + Params :: map(), + Extra :: map(). +archive_size(_Size, #{owner := OwnerJid}, _Extra)-> SearchQuery = build_search_query(#{owner_jid => OwnerJid}), - archive_size(SearchQuery). + {ok, archive_size(SearchQuery)}. --spec remove_archive(Acc :: mongoose_acc:t(), - Host :: jid:server(), - ArchiveId :: mod_mam:archive_id(), - OwnerJid :: jid:jid()) -> Acc when Acc :: map(). -remove_archive(Acc, Host, _ArchiveId, OwnerJid) -> +-spec remove_archive(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +remove_archive(Acc, #{owner := OwnerJid}, #{host_type := Host}) -> remove_archive(Host, OwnerJid), - Acc. + {ok, Acc}. remove_archive(Host, OwnerJid) -> SearchQuery = build_search_query(#{owner_jid => OwnerJid}), @@ -161,13 +172,13 @@ remove_archive(Host, OwnerJid) -> %% Helpers %%------------------------------------------------------------------- --spec hooks(jid:lserver()) -> [ejabberd_hooks:hook()]. +-spec hooks(mongooseim:host_type()) -> gen_hook:hook_list(). hooks(Host) -> - [{mam_archive_message, Host, ?MODULE, archive_message, 50}, - {mam_lookup_messages, Host, ?MODULE, lookup_messages, 50}, - {mam_archive_size, Host, ?MODULE, archive_size, 50}, - {mam_remove_archive, Host, ?MODULE, remove_archive, 50}, - {get_mam_pm_gdpr_data, Host, ?MODULE, get_mam_pm_gdpr_data, 50}]. + [{mam_archive_message, Host, fun ?MODULE:archive_message/3, #{}, 50}, + {mam_lookup_messages, Host, fun ?MODULE:lookup_messages/3, #{}, 50}, + {mam_archive_size, Host, fun ?MODULE:archive_size/3, #{}, 50}, + {mam_remove_archive, Host, fun ?MODULE:remove_archive/3, #{}, 50}, + {get_mam_pm_gdpr_data, Host, fun ?MODULE:get_mam_pm_gdpr_data/3, #{}, 50}]. -spec make_document_id(binary(), mod_mam:message_id()) -> binary(). make_document_id(Owner, MessageId) -> diff --git a/src/mam/mod_mam_mnesia_prefs.erl b/src/mam/mod_mam_mnesia_prefs.erl index 76161df3ee..2bc64b4de9 100644 --- a/src/mam/mod_mam_mnesia_prefs.erl +++ b/src/mam/mod_mam_mnesia_prefs.erl @@ -13,16 +13,15 @@ %% Exports %% gen_mod handlers +-behaviour(gen_mod). -export([start/2, stop/1, supported_features/0]). %% MAM hook handlers -behaviour(ejabberd_gen_mam_prefs). --export([get_behaviour/5, - get_prefs/4, - set_prefs/7, - remove_archive/4]). - --ignore_xref([remove_archive/4, start/2, stop/1, supported_features/0]). +-export([get_behaviour/3, + get_prefs/3, + set_prefs/3, + remove_archive/3]). -include("mongoose.hrl"). -include("jlib.hrl"). @@ -46,12 +45,12 @@ start(HostType, Opts) -> [{disc_copies, [node()]}, {attributes, record_info(fields, mam_prefs)}]), mnesia:add_table_copy(mam_prefs, node(), disc_copies), - ejabberd_hooks:add(hooks(HostType, Opts)). + gen_hook:add_handlers(hooks(HostType, Opts)). -spec stop(mongooseim:host_type()) -> ok. stop(HostType) -> Opts = gen_mod:get_loaded_module_opts(HostType, ?MODULE), - ejabberd_hooks:delete(hooks(HostType, Opts)). + gen_hook:delete_handlers(hooks(HostType, Opts)). -spec supported_features() -> [atom()]. supported_features() -> @@ -64,51 +63,52 @@ hooks(HostType, Opts) -> lists:flatmap(fun(Type) -> hooks(HostType, Type, Opts) end, [pm, muc]). hooks(HostType, pm, #{pm := true}) -> - [{mam_get_behaviour, HostType, ?MODULE, get_behaviour, 50}, - {mam_get_prefs, HostType, ?MODULE, get_prefs, 50}, - {mam_set_prefs, HostType, ?MODULE, set_prefs, 50}, - {mam_remove_archive, HostType, ?MODULE, remove_archive, 50}]; + [{mam_get_behaviour, HostType, fun ?MODULE:get_behaviour/3, #{}, 50}, + {mam_get_prefs, HostType, fun ?MODULE:get_prefs/3, #{}, 50}, + {mam_set_prefs, HostType, fun ?MODULE:set_prefs/3, #{}, 50}, + {mam_remove_archive, HostType, fun ?MODULE:remove_archive/3, #{}, 50}]; hooks(HostType, muc, #{muc := true}) -> - [{mam_muc_get_behaviour, HostType, ?MODULE, get_behaviour, 50}, - {mam_muc_get_prefs, HostType, ?MODULE, get_prefs, 50}, - {mam_muc_set_prefs, HostType, ?MODULE, set_prefs, 50}, - {mam_muc_remove_archive, HostType, ?MODULE, remove_archive, 50}]; + [{mam_muc_get_behaviour, HostType, fun ?MODULE:get_behaviour/3, #{}, 50}, + {mam_muc_get_prefs, HostType, fun ?MODULE:get_prefs/3, #{}, 50}, + {mam_muc_set_prefs, HostType, fun ?MODULE:set_prefs/3, #{}, 50}, + {mam_muc_remove_archive, HostType, fun ?MODULE:remove_archive/3, #{}, 50}]; hooks(_HostType, _Opt, _Opts) -> []. %% ---------------------------------------------------------------------- %% Internal functions and callbacks --spec get_behaviour(Default :: behaviour(), Host :: jid:server(), - ArcID :: mod_mam:archive_id(), LocJID :: jid:jid(), - RemJID :: jid:jid()) -> any(). -get_behaviour(DefaultBehaviour, _Host, - _ArcID, - LocJID=#jid{}, - RemJID=#jid{}) -> +-spec get_behaviour(Acc, Params, Extra) -> {ok, Acc} when + Acc :: mod_mam:archive_behaviour(), + Params :: map(), + Extra :: map(). +get_behaviour(DefaultBehaviour, + #{owner := LocJID, remote := RemJID}, + _Extra) -> SU = su_key(LocJID), case mnesia:dirty_read(mam_prefs, SU) of - [] -> DefaultBehaviour; - [User] -> get_behaviour(User, LocJID, RemJID) + [] -> {ok, DefaultBehaviour}; + [User] -> {ok, get_behaviour2(User, LocJID, RemJID)} end. --spec get_behaviour(mam_prefs(), LocJID :: jid:jid(), +-spec get_behaviour2(mam_prefs(), LocJID :: jid:jid(), RemJID :: jid:jid()) -> behaviour(). -get_behaviour(#mam_prefs{default_mode = always, never_rules=NeverJIDs}, LocJID, RemJID) -> +get_behaviour2(#mam_prefs{default_mode = always, never_rules = NeverJIDs}, LocJID, RemJID) -> IsNever = match_jid(LocJID, RemJID, NeverJIDs), case IsNever of true -> never; false -> always end; -get_behaviour(#mam_prefs{default_mode = never, always_rules=AlwaysJIDs}, LocJID, RemJID) -> +get_behaviour2(#mam_prefs{default_mode = never, always_rules = AlwaysJIDs}, LocJID, RemJID) -> IsAlways = match_jid(LocJID, RemJID, AlwaysJIDs), case IsAlways of true -> always; false -> never end; -get_behaviour(#mam_prefs{default_mode = roster, - never_rules=NeverJIDs, always_rules=AlwaysJIDs}, LocJID, RemJID) -> +get_behaviour2(#mam_prefs{default_mode = roster, + never_rules = NeverJIDs, + always_rules = AlwaysJIDs}, LocJID, RemJID) -> IsNever = match_jid(LocJID, RemJID, NeverJIDs), case IsNever of true -> never; @@ -120,17 +120,18 @@ get_behaviour(#mam_prefs{default_mode = roster, end end. - --spec set_prefs(Result :: any(), Host :: jid:server(), - ArcID :: mod_mam:archive_id(), ArcJID :: jid:jid(), - DefaultMode :: mod_mam:archive_behaviour(), - AlwaysJIDs :: [jid:literal_jid()], - NeverJIDs :: [jid:literal_jid()]) -> any(). -set_prefs(_Result, _Host, ArcID, ArcJID, DefaultMode, AlwaysJIDs, NeverJIDs) -> +-spec set_prefs(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +set_prefs(_Result, + #{archive_id := ArcID, owner := ArcJID, default_mode := DefaultMode, + always_jids := AlwaysJIDs, never_jids := NeverJIDs}, + _Extra) -> try - set_prefs1(ArcID, ArcJID, DefaultMode, AlwaysJIDs, NeverJIDs) + {ok, set_prefs1(ArcID, ArcJID, DefaultMode, AlwaysJIDs, NeverJIDs)} catch _Type:Error -> - {error, Error} + {ok, {error, Error}} end. set_prefs1(_ArcID, ArcJID, DefaultMode, AlwaysJIDs, NeverJIDs) -> @@ -149,24 +150,30 @@ set_prefs1(_ArcID, ArcJID, DefaultMode, AlwaysJIDs, NeverJIDs) -> ok. --spec get_prefs(mod_mam:preference(), _Host :: jid:server(), - _ArcId :: mod_mam:archive_id(), ArcJID :: jid:jid() - ) -> mod_mam:preference(). -get_prefs({GlobalDefaultMode, _, _}, _Host, _ArcID, ArcJID) -> +-spec get_prefs(Acc, Params, Extra) -> {ok, Acc} when + Acc :: mod_mam:preference(), + Params :: map(), + Extra :: map(). +get_prefs({GlobalDefaultMode, _, _}, #{owner := ArcJID}, _Extra) -> + SU = su_key(ArcJID), case mnesia:dirty_read(mam_prefs, SU) of [] -> - {GlobalDefaultMode, [], []}; + {ok, {GlobalDefaultMode, [], []}}; [#mam_prefs{default_mode=DefaultMode, always_rules=ARules, never_rules=NRules}] -> AlwaysJIDs = jids(ArcJID, ARules), NeverJIDs = jids(ArcJID, NRules), - {DefaultMode, AlwaysJIDs, NeverJIDs} + {ok, {DefaultMode, AlwaysJIDs, NeverJIDs}} end. -remove_archive(Acc, _Host, _ArcID, ArcJID) -> +-spec remove_archive(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +remove_archive(Acc, #{owner := ArcJID}, _Extra) -> remove_archive(ArcJID), - Acc. + {ok, Acc}. remove_archive(ArcJID) -> SU = su_key(ArcJID), diff --git a/src/mam/mod_mam_muc.erl b/src/mam/mod_mam_muc.erl index a65af61257..4d0fbd8ed2 100644 --- a/src/mam/mod_mam_muc.erl +++ b/src/mam/mod_mam_muc.erl @@ -43,10 +43,11 @@ -export([start/2, stop/1, supported_features/0]). %% ejabberd room handlers --export([disco_muc_features/1, +-export([disco_muc_features/3, filter_room_packet/3, - room_process_mam_iq/5, - forget_room/4]). + forget_room/3]). + +-export([room_process_mam_iq/5]). %% gdpr callback -export([get_personal_data/3]). @@ -56,9 +57,8 @@ -export([lookup_messages/2]). -export([archive_id_int/2]). --ignore_xref([archive_id/2, archive_message_for_ct/1, archive_size/2, behaviour_info/1, - delete_archive/2, disco_muc_features/1, filter_room_packet/3, - forget_room/4, get_personal_data/3, start/2, stop/1, supported_features/0]). +-ignore_xref([archive_id/2, archive_message_for_ct/1, archive_size/2, delete_archive/2, + start/2, stop/1, supported_features/0]). -include_lib("mongoose.hrl"). -include_lib("jlib.hrl"). @@ -82,12 +82,14 @@ %% ---------------------------------------------------------------------- %% API --spec get_personal_data(gdpr:personal_data(), mongooseim:host_type(), jid:jid()) -> - gdpr:personal_data(). -get_personal_data(Acc, HostType, ArcJID) -> +-spec get_personal_data(Acc, Params, Extra) -> {ok, Acc} when + Acc :: gdpr:personal_data(), + Params :: map(), + Extra :: map(). +get_personal_data(Acc, #{jid := ArcJID}, #{host_type := HostType}) -> Schema = ["id", "message"], Entries = mongoose_hooks:get_mam_muc_gdpr_data(HostType, ArcJID), - [{mam_muc, Schema, Entries} | Acc]. + {ok, [{mam_muc, Schema, Entries} | Acc]}. -spec delete_archive(jid:server(), jid:user()) -> ok. delete_archive(MucHost, RoomName) when is_binary(MucHost), is_binary(RoomName) -> @@ -119,14 +121,14 @@ archive_id(MucHost, RoomName) when is_binary(MucHost), is_binary(RoomName) -> start(HostType, Opts) -> ?LOG_DEBUG(#{what => mam_muc_starting}), ensure_metrics(HostType), - ejabberd_hooks:add(hooks(HostType)), + gen_hook:add_handlers(hooks(HostType)), add_iq_handlers(HostType, Opts), ok. -spec stop(host_type()) -> any(). stop(HostType) -> ?LOG_DEBUG(#{what => mam_muc_stopping}), - ejabberd_hooks:delete(hooks(HostType)), + gen_hook:delete_handlers(hooks(HostType)), remove_iq_handlers(HostType), ok. @@ -137,16 +139,21 @@ supported_features() -> %% ---------------------------------------------------------------------- %% hooks and handlers for MUC --spec disco_muc_features(mongoose_disco:feature_acc()) -> mongoose_disco:feature_acc(). -disco_muc_features(Acc = #{host_type := HostType, node := <<>>}) -> - mongoose_disco:add_features(mod_mam_utils:features(?MODULE, HostType), Acc); -disco_muc_features(Acc) -> - Acc. +-spec disco_muc_features(Acc, Params, Extra) -> {ok, Acc} when + Acc :: mongoose_disco:feature_acc(), + Params :: map(), + Extra :: map(). +disco_muc_features(Acc = #{host_type := HostType, node := <<>>}, _Params, _Extra) -> + {ok, mongoose_disco:add_features(mod_mam_utils:features(?MODULE, HostType), Acc)}; +disco_muc_features(Acc, _Params, _Extra) -> + {ok, Acc}. %% @doc Handle public MUC-message. --spec filter_room_packet(Packet :: packet(), HostType :: host_type(), - EventData :: mod_muc:room_event_data()) -> packet(). -filter_room_packet(Packet, HostType, EventData = #{}) -> +-spec filter_room_packet(Acc, Params, Extra) -> {ok, Acc} when + Acc :: exml:element(), + Params :: map(), + Extra :: map(). +filter_room_packet(Packet, #{event_data := #{} = EventData}, #{host_type := HostType}) -> ?LOG_DEBUG(#{what => mam_room_packet, text => <<"Incoming room packet">>, packet => Packet, event_data => EventData}), IsArchivable = is_archivable_message(HostType, incoming, Packet), @@ -154,9 +161,10 @@ filter_room_packet(Packet, HostType, EventData = #{}) -> true -> #{from_nick := FromNick, from_jid := FromJID, room_jid := RoomJID, role := Role, affiliation := Affiliation, timestamp := TS} = EventData, - archive_room_packet(HostType, Packet, FromNick, FromJID, - RoomJID, Role, Affiliation, TS); - false -> Packet + {ok, archive_room_packet(HostType, Packet, FromNick, FromJID, + RoomJID, Role, Affiliation, TS)}; + false -> + {ok, Packet} end. %% @doc Archive without validation. @@ -233,10 +241,13 @@ room_process_mam_iq(Acc, From, To, IQ, #{host_type := HostType}) -> {Acc, return_action_not_allowed_error_iq(Reason, IQ)} end. --spec forget_room(map(), host_type(), jid:lserver(), binary()) -> map(). -forget_room(Acc, _HostType, MucServer, RoomName) -> +-spec forget_room(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +forget_room(Acc, #{muc_host := MucServer, room := RoomName}, _Extra) -> delete_archive(MucServer, RoomName), - Acc. + {ok, Acc}. %% ---------------------------------------------------------------------- %% Internal functions @@ -616,12 +627,12 @@ is_archivable_message(HostType, Dir, Packet) -> ArchiveChatMarkers = mod_mam_params:archive_chat_markers(?MODULE, HostType), erlang:apply(M, is_archivable_message, [?MODULE, Dir, Packet, ArchiveChatMarkers]). --spec hooks(host_type()) -> [ejabberd_hooks:hook()]. +-spec hooks(mongooseim:host_type()) -> gen_hook:hook_list(). hooks(HostType) -> - [{disco_muc_features, HostType, ?MODULE, disco_muc_features, 99}, - {filter_room_packet, HostType, ?MODULE, filter_room_packet, 60}, - {forget_room, HostType, ?MODULE, forget_room, 90}, - {get_personal_data, HostType, ?MODULE, get_personal_data, 50} + [{disco_muc_features, HostType, fun ?MODULE:disco_muc_features/3, #{}, 99}, + {filter_room_packet, HostType, fun ?MODULE:filter_room_packet/3, #{}, 60}, + {forget_room, HostType, fun ?MODULE:forget_room/3, #{}, 90}, + {get_personal_data, HostType, fun ?MODULE:get_personal_data/3, #{}, 50} | mongoose_metrics_mam_hooks:get_mam_muc_hooks(HostType)]. add_iq_handlers(HostType, Opts) -> diff --git a/src/mam/mod_mam_muc_cassandra_arch.erl b/src/mam/mod_mam_muc_cassandra_arch.erl index 29da664ff6..14e6b42aad 100644 --- a/src/mam/mod_mam_muc_cassandra_arch.erl +++ b/src/mam/mod_mam_muc_cassandra_arch.erl @@ -7,22 +7,20 @@ -module(mod_mam_muc_cassandra_arch). -behaviour(mongoose_cassandra). -behaviour(ejabberd_gen_mam_archive). +-behaviour(gen_mod). %% gen_mod handlers -export([start/2, stop/1]). %% MAM hook handlers --export([archive_size/4, +-export([archive_size/3, archive_message/3, lookup_messages/3, - remove_archive/4]). + remove_archive/3, + get_mam_muc_gdpr_data/3]). %% mongoose_cassandra callbacks --export([prepared_queries/0, get_mam_muc_gdpr_data/3]). - --ignore_xref([ - behaviour_info/1, remove_archive/4, start/2, stop/1 -]). +-export([prepared_queries/0]). %% ---------------------------------------------------------------------- %% Imports @@ -74,21 +72,22 @@ -spec start(host_type(), gen_mod:module_opts()) -> ok. start(HostType, _Opts) -> - ejabberd_hooks:add(hooks(HostType)). + gen_hook:add_handlers(hooks(HostType)). -spec stop(host_type()) -> ok. stop(HostType) -> - ejabberd_hooks:delete(hooks(HostType)). + gen_hook:delete_handlers(hooks(HostType)). %% ---------------------------------------------------------------------- %% Add hooks for mod_mam_muc +-spec hooks(mongooseim:host_type()) -> gen_hook:hook_list(). hooks(HostType) -> - [{mam_muc_archive_message, HostType, ?MODULE, archive_message, 50}, - {mam_muc_archive_size, HostType, ?MODULE, archive_size, 50}, - {mam_muc_lookup_messages, HostType, ?MODULE, lookup_messages, 50}, - {mam_muc_remove_archive, HostType, ?MODULE, remove_archive, 50}, - {get_mam_muc_gdpr_data, HostType, ?MODULE, get_mam_muc_gdpr_data, 50}]. + [{mam_muc_archive_message, HostType, fun ?MODULE:archive_message/3, #{}, 50}, + {mam_muc_archive_size, HostType, fun ?MODULE:archive_size/3, #{}, 50}, + {mam_muc_lookup_messages, HostType, fun ?MODULE:lookup_messages/3, #{}, 50}, + {mam_muc_remove_archive, HostType, fun ?MODULE:remove_archive/3, #{}, 50}, + {get_mam_muc_gdpr_data, HostType, fun ?MODULE:get_mam_muc_gdpr_data/3, #{}, 50}]. %% ---------------------------------------------------------------------- %% mongoose_cassandra_worker callbacks @@ -114,11 +113,15 @@ prepared_queries() -> %% ---------------------------------------------------------------------- %% Internal functions and callbacks -archive_size(Size, HostType, _RoomID, RoomJID) when is_integer(Size) -> +-spec archive_size(Acc, Params, Extra) -> {ok, Acc} when + Acc :: integer(), + Params :: map(), + Extra :: map(). +archive_size(Size, #{room := RoomJID}, #{host_type := HostType}) when is_integer(Size) -> PoolName = pool_name(HostType), Borders = Start = End = WithNick = undefined, Filter = prepare_filter(RoomJID, Borders, Start, End, WithNick), - calc_count(PoolName, RoomJID, HostType, Filter). + {ok, calc_count(PoolName, RoomJID, HostType, Filter)}. %% ---------------------------------------------------------------------- @@ -129,11 +132,15 @@ insert_query_cql() -> "(id, room_jid, from_jid, nick_name, with_nick, message) " "VALUES (?, ?, ?, ?, ?, ?)". -archive_message(_Result, HostType, Params) -> +-spec archive_message(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ok | {error, term()}, + Params :: map(), + Extra :: map(). +archive_message(_Result, #{params := Params}, #{host_type := HostType}) -> try - archive_message2(Params, HostType) + {ok, archive_message2(Params, HostType)} catch _Type:Reason -> - {error, Reason} + {ok, {error, Reason}} end. archive_message2(#{message_id := MessID, @@ -184,7 +191,11 @@ remove_archive_offsets_query_cql() -> select_for_removal_query_cql() -> "SELECT DISTINCT room_jid, with_nick FROM mam_muc_message WHERE room_jid = ?". -remove_archive(Acc, HostType, _RoomID, RoomJID) -> +-spec remove_archive(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +remove_archive(Acc, #{room := RoomJID}, #{host_type := HostType}) -> BRoomJID = mod_mam_utils:bare_jid(RoomJID), PoolName = pool_name(HostType), Params = #{room_jid => BRoomJID}, @@ -200,31 +211,34 @@ remove_archive(Acc, HostType, _RoomID, RoomJID) -> mongoose_cassandra:cql_foldl(PoolName, RoomJID, ?MODULE, select_for_removal_query, Params, DeleteFun, []), - Acc. + {ok, Acc}. %% ---------------------------------------------------------------------- %% SELECT MESSAGES --spec lookup_messages(Result :: any(), HostType :: host_type(), Params :: map()) -> - {ok, mod_mam:lookup_result()}. -lookup_messages({error, _Reason} = Result, _HostType, _Params) -> - Result; -lookup_messages(_Result, _HostType, #{search_text := <<_/binary>>}) -> - {error, 'not-supported'}; -lookup_messages(_Result, HostType, - #{owner_jid := RoomJID, rsm := RSM, borders := Borders, - start_ts := Start, end_ts := End, with_jid := WithJID, - search_text := undefined, page_size := PageSize, - is_simple := IsSimple}) -> +-spec lookup_messages(Acc, Params, Extra) -> {ok, Acc} when + Acc :: {ok, mod_mam:lookup_result()}, + Params :: map(), + Extra :: map(). +lookup_messages({error, _Reason} = Result, _Params, _Extra) -> + {ok, Result}; +lookup_messages(_Result, #{params := #{search_text := <<_/binary>>}}, _Extra) -> + {ok, {error, 'not-supported'}}; +lookup_messages(_Result, + #{params := #{owner_jid := RoomJID, rsm := RSM, borders := Borders, + start_ts := Start, end_ts := End, with_jid := WithJID, + search_text := undefined, page_size := PageSize, + is_simple := IsSimple}}, + #{host_type := HostType}) -> try WithNick = maybe_jid_to_nick(WithJID), PoolName = pool_name(HostType), - lookup_messages2(PoolName, HostType, + {ok, lookup_messages2(PoolName, HostType, RoomJID, RSM, Borders, Start, End, WithNick, - PageSize, IsSimple) - catch _Type:Reason:S -> - {error, {Reason, {stacktrace, S}}} + PageSize, IsSimple)} + catch _Type:Reason:Stacktrace -> + {ok, {error, {Reason, {stacktrace, Stacktrace}}}} end. maybe_jid_to_nick(#jid{lresource = BNick}) -> BNick; @@ -421,17 +435,18 @@ row_to_uniform_format(#{nick_name := BNick, message := Data, id := MessID}, row_to_message_id(#{id := MsgID}) -> MsgID. --spec get_mam_muc_gdpr_data(ejabberd_gen_mam_archive:mam_muc_gdpr_data(), - host_type(), jid:jid()) -> - ejabberd_gen_mam_archive:mam_muc_gdpr_data(). -get_mam_muc_gdpr_data(Acc, HostType, Jid) -> +-spec get_mam_muc_gdpr_data(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ejabberd_gen_mam_archive:mam_muc_gdpr_data(), + Params :: map(), + Extra :: map(). +get_mam_muc_gdpr_data(Acc, #{jid := Jid}, #{host_type := HostType}) -> BinJid = jid:to_binary(jid:to_lower(Jid)), PoolName = pool_name(HostType), FilterMap = #{from_jid => BinJid}, Rows = fetch_user_messages(PoolName, Jid, FilterMap), Messages = [{Id, exml:to_binary(stored_binary_to_packet(HostType, Data))} || #{message := Data, id:= Id} <- Rows], - Messages ++ Acc. + {ok, Messages ++ Acc}. %% Offset is not supported %% Each record is a tuple of form diff --git a/src/mam/mod_mam_muc_elasticsearch_arch.erl b/src/mam/mod_mam_muc_elasticsearch_arch.erl index af41199f60..a91d5032f2 100644 --- a/src/mam/mod_mam_muc_elasticsearch_arch.erl +++ b/src/mam/mod_mam_muc_elasticsearch_arch.erl @@ -28,14 +28,10 @@ %% ejabberd_gen_mam_archive callbacks -export([archive_message/3]). -export([lookup_messages/3]). --export([remove_archive/4]). --export([archive_size/4]). - -%gdpr +-export([remove_archive/3]). +-export([archive_size/3]). -export([get_mam_muc_gdpr_data/3]). --ignore_xref([remove_archive/4]). - -include("mongoose.hrl"). -include("mongoose_rsm.hrl"). -include("mod_mam.hrl"). @@ -50,21 +46,22 @@ -spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok. start(HostType, _Opts) -> - ejabberd_hooks:add(hooks(HostType)), + gen_hook:add_handlers(hooks(HostType)), ok. -spec stop(mongooseim:host_type()) -> ok. stop(HostType) -> - ejabberd_hooks:delete(hooks(HostType)), + gen_hook:delete_handlers(hooks(HostType)), ok. %%------------------------------------------------------------------- %% ejabberd_gen_mam_archive callbacks %%------------------------------------------------------------------- --spec get_mam_muc_gdpr_data(ejabberd_gen_mam_archive:mam_muc_gdpr_data(), - mongooseim:host_type(), jid:jid()) -> - ejabberd_gen_mam_archive:mam_muc_gdpr_data(). -get_mam_muc_gdpr_data(Acc, _HostType, Source) -> +-spec get_mam_muc_gdpr_data(Acc, Params, Extra) -> {ok | stop, Acc} when + Acc :: ejabberd_gen_mam_archive:mam_muc_gdpr_data(), + Params :: map(), + Extra :: map(). +get_mam_muc_gdpr_data(Acc, #{jid := Source}, _Extra) -> BinSource = mod_mam_utils:bare_jid(Source), Filter = #{term => #{from_jid => BinSource}}, Sorting = #{mam_id => #{order => asc}}, @@ -73,16 +70,21 @@ get_mam_muc_gdpr_data(Acc, _HostType, Source) -> case mongoose_elasticsearch:search(?INDEX_NAME, ?TYPE_NAME, SearchQuery) of {ok, #{<<"hits">> := #{<<"hits">> := Hits}}} -> Messages = lists:map(fun hit_to_gdpr_mam_message/1, Hits), - Messages ++ Acc; + {ok, Messages ++ Acc}; {error, _} -> - Acc + {ok, Acc} end. -archive_message(_Result, Host, #{message_id := MessageId, - local_jid := RoomJid, - remote_jid := FromJID, - source_jid := SourceJid, - packet := Packet} = Params) -> +-spec archive_message(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ok | {error, term()}, + Params :: map(), + Extra :: map(). +archive_message(_Result, #{params := Params}, #{host_type := HostType}) -> + #{message_id := MessageId, + local_jid := RoomJid, + remote_jid := FromJID, + source_jid := SourceJid, + packet := Packet} = Params, Room = mod_mam_utils:bare_jid(RoomJid), SourceBinJid = mod_mam_utils:full_jid(SourceJid), From = mod_mam_utils:bare_jid(FromJID), @@ -90,29 +92,38 @@ archive_message(_Result, Host, #{message_id := MessageId, Doc = make_document(MessageId, Room, SourceBinJid, Packet, From), case mongoose_elasticsearch:insert_document(?INDEX_NAME, ?TYPE_NAME, DocId, Doc) of {ok, _} -> - ok; + {ok, ok}; {error, Reason} = Err -> ?LOG_ERROR(maps:merge(Params, #{what => archive_muc_message_failed, reason => Reason, - server => Host, room => Room, source => SourceBinJid, + server => HostType, room => Room, source => SourceBinJid, message_id => MessageId})), - mongoose_metrics:update(Host, modMamDropped, 1), - Err + mongoose_metrics:update(HostType, modMamDropped, 1), + {ok, Err} end. -lookup_messages(Result, Host, #{rsm := #rsm_in{direction = before, id = ID} = RSM} = Params) +-spec lookup_messages(Acc, Params, Extra) -> {ok, Acc} when + Acc :: {ok, mod_mam:lookup_result()} | {error, term()}, + Params :: map(), + Extra :: map(). +lookup_messages(Result, + #{params := #{rsm := #rsm_in{direction = before, id = ID} = RSM} = Params}, + #{host_type := HostType}) when ID =/= undefined -> - lookup_message_page(Result, Host, RSM, Params); -lookup_messages(Result, Host, #{rsm := #rsm_in{direction = aft, id = ID} = RSM} = Params) + {ok, lookup_message_page(Result, HostType, RSM, Params)}; +lookup_messages(Result, + #{params := #{rsm := #rsm_in{direction = aft, id = ID} = RSM} = Params}, + #{host_type := HostType}) when ID =/= undefined -> - lookup_message_page(Result, Host, RSM, Params); -lookup_messages(Result, Host, Params) -> - do_lookup_messages(Result, Host, Params). + {ok, lookup_message_page(Result, HostType, RSM, Params)}; +lookup_messages(Result, #{params := Params}, #{host_type := HostType}) -> + {ok, do_lookup_messages(Result, HostType, Params)}. lookup_message_page(AccResult, Host, RSM, Params) -> PageSize = maps:get(page_size, Params), case do_lookup_messages(AccResult, Host, Params#{page_size := 1 + PageSize}) of - {error, _} = Err -> Err; + {error, _} = Err -> + Err; {ok, LookupResult} -> mod_mam_utils:check_for_item_not_found(RSM, PageSize, LookupResult) end. @@ -134,41 +145,41 @@ do_lookup_messages(_Result, Host, Params) -> Err end. --spec archive_size(Size :: integer(), - Host :: jid:server(), - _ArchiveId, - RoomJid :: jid:jid()) -> non_neg_integer(). -archive_size(_Size, _Host, _ArchiveId, RoomJid) -> +-spec archive_size(Acc, Params, Extra) -> {ok, Acc} when + Acc :: integer(), + Params :: map(), + Extra :: map(). +archive_size(_Size, #{room := RoomJid}, _Extra) -> SearchQuery = build_search_query(#{owner_jid => RoomJid}), - archive_size(SearchQuery). + {ok, archive_size(SearchQuery)}. --spec remove_archive(Acc :: mongoose_acc:t(), - Host :: jid:server(), - ArchiveId :: mod_mam:archive_id(), - RoomJid :: jid:jid()) -> Acc when Acc :: map(). -remove_archive(Acc, Host, _ArchiveId, RoomJid) -> +-spec remove_archive(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ok, + Params :: map(), + Extra :: map(). +remove_archive(Acc, #{room := RoomJid}, #{host_type := HostType}) -> SearchQuery = build_search_query(#{owner_jid => RoomJid}), case mongoose_elasticsearch:delete_by_query(?INDEX_NAME, ?TYPE_NAME, SearchQuery) of ok -> ok; {error, Reason} -> ?LOG_ERROR(#{what => remove_muc_archive_failed, - server => Host, room_jid => RoomJid, reason => Reason}), + server => HostType, room_jid => RoomJid, reason => Reason}), ok end, - Acc. + {ok, Acc}. %%------------------------------------------------------------------- %% Helpers %%------------------------------------------------------------------- --spec hooks(jid:lserver()) -> [ejabberd_hooks:hook()]. +-spec hooks(mongooseim:host_type()) -> gen_hook:hook_list(). hooks(Host) -> - [{mam_muc_archive_message, Host, ?MODULE, archive_message, 50}, - {mam_muc_lookup_messages, Host, ?MODULE, lookup_messages, 50}, - {mam_muc_archive_size, Host, ?MODULE, archive_size, 50}, - {mam_muc_remove_archive, Host, ?MODULE, remove_archive, 50}, - {get_mam_muc_gdpr_data, Host, ?MODULE, get_mam_muc_gdpr_data, 50}]. + [{mam_muc_archive_message, Host, fun ?MODULE:archive_message/3, #{}, 50}, + {mam_muc_lookup_messages, Host, fun ?MODULE:lookup_messages/3, #{}, 50}, + {mam_muc_archive_size, Host, fun ?MODULE:archive_size/3, #{}, 50}, + {mam_muc_remove_archive, Host, fun ?MODULE:remove_archive/3, #{}, 50}, + {get_mam_muc_gdpr_data, Host, fun ?MODULE:get_mam_muc_gdpr_data/3, #{}, 50}]. -spec make_document_id(binary(), mod_mam:message_id()) -> binary(). make_document_id(Room, MessageId) -> diff --git a/src/mam/mod_mam_muc_rdbms_arch.erl b/src/mam/mod_mam_muc_rdbms_arch.erl index 5106ea3388..4036b4fdcf 100644 --- a/src/mam/mod_mam_muc_rdbms_arch.erl +++ b/src/mam/mod_mam_muc_rdbms_arch.erl @@ -20,20 +20,17 @@ -callback encode(term()) -> binary(). -callback decode(binary()) -> term(). --export([archive_size/4, +-export([archive_size/3, archive_message/3, lookup_messages/3, - remove_archive/4, - remove_domain/3]). - --export([get_mam_muc_gdpr_data/3]). + remove_archive/3, + remove_domain/3, + get_mam_muc_gdpr_data/3]). %% Called from mod_mam_muc_rdbms_async_pool_writer -export([prepare_message/2, retract_message/2, prepare_insert/2]). -export([extend_params_with_sender_id/2]). --ignore_xref([behaviour_info/1, remove_archive/4]). - %% ---------------------------------------------------------------------- %% Imports @@ -65,18 +62,19 @@ stop(HostType) -> supported_features() -> [dynamic_domains]. --spec get_mam_muc_gdpr_data(ejabberd_gen_mam_archive:mam_pm_gdpr_data(), - host_type(), jid:jid()) -> - ejabberd_gen_mam_archive:mam_muc_gdpr_data(). -get_mam_muc_gdpr_data(Acc, HostType, #jid{luser = LUser, lserver = LServer} = _UserJID) -> +-spec get_mam_muc_gdpr_data(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ejabberd_gen_mam_archive:mam_muc_gdpr_data(), + Params :: map(), + Extra :: map(). +get_mam_muc_gdpr_data(Acc, #{jid := #jid{luser = LUser, lserver = LServer}}, #{host_type := HostType}) -> case mod_mam_pm:archive_id(LServer, LUser) of undefined -> - Acc; + {ok, Acc}; SenderID -> %% We don't know the real room JID here, use FakeEnv FakeEnv = env_vars(HostType, jid:make(<<>>, <<>>, <<>>)), {selected, Rows} = extract_gdpr_messages(HostType, SenderID), - [mam_decoder:decode_muc_gdpr_row(Row, FakeEnv) || Row <- Rows] ++ Acc + {ok, [mam_decoder:decode_muc_gdpr_row(Row, FakeEnv) || Row <- Rows] ++ Acc} end. %% ---------------------------------------------------------------------- @@ -84,30 +82,26 @@ get_mam_muc_gdpr_data(Acc, HostType, #jid{luser = LUser, lserver = LServer} = _U -spec start_hooks(host_type()) -> ok. start_hooks(HostType) -> - ejabberd_hooks:add(legacy_hooks(HostType)), gen_hook:add_handlers(hooks(HostType)). -spec stop_hooks(host_type()) -> ok. stop_hooks(HostType) -> - ejabberd_hooks:delete(legacy_hooks(HostType)), gen_hook:delete_handlers(hooks(HostType)). -legacy_hooks(HostType) -> +-spec hooks(mongooseim:host_type()) -> gen_hook:hook_list(). +hooks(HostType) -> case gen_mod:get_module_opt(HostType, ?MODULE, no_writer) of true -> []; false -> - [{mam_muc_archive_message, HostType, ?MODULE, archive_message, 50}] + [{mam_muc_archive_message, HostType, fun ?MODULE:archive_message/3, #{}, 50}] end ++ - [{mam_muc_archive_size, HostType, ?MODULE, archive_size, 50}, - {mam_muc_lookup_messages, HostType, ?MODULE, lookup_messages, 50}, - {mam_muc_remove_archive, HostType, ?MODULE, remove_archive, 50}, - {get_mam_muc_gdpr_data, HostType, ?MODULE, get_mam_muc_gdpr_data, 50}]. - --spec hooks(mongooseim:host_type()) -> gen_hook:hook_list(). -hooks(HostType) -> [ - {remove_domain, HostType, fun ?MODULE:remove_domain/3, #{}, 50} + {remove_domain, HostType, fun ?MODULE:remove_domain/3, #{}, 50}, + {mam_muc_archive_size, HostType, fun ?MODULE:archive_size/3, #{}, 50}, + {mam_muc_lookup_messages, HostType, fun ?MODULE:lookup_messages/3, #{}, 50}, + {mam_muc_remove_archive, HostType, fun ?MODULE:remove_archive/3, #{}, 50}, + {get_mam_muc_gdpr_data, HostType, fun ?MODULE:get_mam_muc_gdpr_data/3, #{}, 50} ]. %% ---------------------------------------------------------------------- @@ -217,33 +211,37 @@ get_retract_id(Packet, #{has_message_retraction := Enabled}) -> %% ---------------------------------------------------------------------- %% Internal functions and callbacks --spec archive_size(Size :: integer(), HostType :: mongooseim:host_type(), - ArcId :: mod_mam:archive_id(), ArcJID :: jid:jid()) -> integer(). -archive_size(Size, HostType, ArcID, ArcJID) when is_integer(Size) -> +-spec archive_size(Acc, Params, Extra) -> {ok, Acc} when + Acc :: integer(), + Params :: map(), + Extra :: map(). +archive_size(Size, #{archive_id := ArcID, room := ArcJID}, #{host_type := HostType}) when is_integer(Size) -> Filter = [{equal, room_id, ArcID}], Env = env_vars(HostType, ArcJID), Result = lookup_query(count, Env, Filter, unordered, all), - mongoose_rdbms:selected_to_integer(Result). + {ok, mongoose_rdbms:selected_to_integer(Result)}. extend_params_with_sender_id(HostType, Params = #{remote_jid := SenderJID}) -> BareSenderJID = jid:to_bare(SenderJID), SenderID = mod_mam_pm:archive_id_int(HostType, BareSenderJID), Params#{sender_id => SenderID}. --spec archive_message(_Result, HostType :: mongooseim:host_type(), - mod_mam:archive_message_params()) -> ok. -archive_message(_Result, HostType, Params0 = #{local_jid := ArcJID}) -> +-spec archive_message(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ok, + Params :: map(), + Extra :: map(). +archive_message(_Result, #{params := #{local_jid := ArcJID} = Params0}, #{host_type := HostType}) -> try Params = extend_params_with_sender_id(HostType, Params0), Env = env_vars(HostType, ArcJID), do_archive_message(HostType, Params, Env), retract_message(HostType, Params, Env), - ok + {ok, ok} catch error:Reason:StackTrace -> - ?LOG_ERROR(#{what => archive_message_failed, - host_type => HostType, mam_params => Params0, - reason => Reason, stacktrace => StackTrace}), - erlang:raise(error, Reason, StackTrace) + ?LOG_ERROR(#{what => archive_message_failed, + host_type => HostType, mam_params => Params0, + reason => Reason, stacktrace => StackTrace}), + erlang:raise(error, Reason, StackTrace) end. do_archive_message(HostType, Params, Env) -> @@ -311,12 +309,13 @@ prepare_insert(Name, NumRows) -> ok. %% Removal logic --spec remove_archive(Acc :: mongoose_acc:t(), HostType :: mongooseim:host_type(), - ArcID :: mod_mam:archive_id(), - ArcJID :: jid:jid()) -> mongoose_acc:t(). -remove_archive(Acc, HostType, ArcID, _ArcJID) -> +-spec remove_archive(Acc, Params, Extra) -> {ok, Acc} when + Acc :: mongoose_acc:t(), + Params :: map(), + Extra :: map(). +remove_archive(Acc, #{archive_id := ArcID}, #{host_type := HostType}) -> mongoose_rdbms:execute_successfully(HostType, mam_muc_archive_remove, [ArcID]), - Acc. + {ok, Acc}. -spec remove_domain(Acc, Params, Extra) -> {ok | stop, Acc} when Acc :: mongoose_domain_api:remove_domain_acc(), @@ -371,15 +370,17 @@ extract_gdpr_messages(HostType, SenderID) -> mongoose_rdbms:execute_successfully(HostType, mam_muc_extract_gdpr_messages, [SenderID]). %% Lookup logic --spec lookup_messages(Result :: any(), HostType :: mongooseim:host_type(), Params :: map()) -> - {ok, mod_mam:lookup_result()}. -lookup_messages({error, _Reason} = Result, _HostType, _Params) -> - Result; -lookup_messages(_Result, HostType, Params = #{owner_jid := ArcJID}) -> +-spec lookup_messages(Acc, Params, Extra) -> {ok, Acc} when + Acc :: {ok, mod_mam:lookup_result()}, + Params :: map(), + Extra :: map(). +lookup_messages({error, _Reason} = Result, _Params, _Extra) -> + {ok, Result}; +lookup_messages(_Result, #{params := #{owner_jid := ArcJID} = Params}, #{host_type := HostType}) -> Env = env_vars(HostType, ArcJID), ExdParams = mam_encoder:extend_lookup_params(Params, Env), Filter = mam_filter:produce_filter(ExdParams, lookup_fields()), - mam_lookup:lookup(Env, Filter, ExdParams). + {ok, mam_lookup:lookup(Env, Filter, ExdParams)}. lookup_query(QueryType, Env, Filters, Order, OffsetLimit) -> mam_lookup_sql:lookup_query(QueryType, Env, Filters, Order, OffsetLimit). diff --git a/src/mam/mod_mam_muc_rdbms_arch_async.erl b/src/mam/mod_mam_muc_rdbms_arch_async.erl index 6199a9e8d4..328d45a0c8 100644 --- a/src/mam/mod_mam_muc_rdbms_arch_async.erl +++ b/src/mam/mod_mam_muc_rdbms_arch_async.erl @@ -9,18 +9,28 @@ -behaviour(gen_mod). -export([start/2, stop/1, supported_features/0]). --export([archive_muc_message/3, mam_muc_archive_sync/2, flush/2]). --ignore_xref([archive_muc_message/3, mam_muc_archive_sync/2, flush/2]). +-export([archive_muc_message/3, mam_muc_archive_sync/3]). +-export([flush/2]). +-ignore_xref([flush/2]). --spec archive_muc_message(_Result, mongooseim:host_type(), mod_mam:archive_message_params()) -> ok. -archive_muc_message(_Result, HostType, Params0 = #{archive_id := RoomID}) -> +-spec archive_muc_message(Acc, Params, Extra) -> {ok, Acc} when + Acc :: {ok, mod_mam:lookup_result()}, + Params :: map(), + Extra :: map(). +archive_muc_message(Result, + #{params := #{archive_id := RoomID} = Params0}, + #{host_type := HostType}) -> Params = mod_mam_muc_rdbms_arch:extend_params_with_sender_id(HostType, Params0), - mongoose_async_pools:put_task(HostType, muc_mam, RoomID, Params). + mongoose_async_pools:put_task(HostType, muc_mam, RoomID, Params), + {ok, Result}. --spec mam_muc_archive_sync(term(), mongooseim:host_type()) -> term(). -mam_muc_archive_sync(Result, HostType) -> +-spec mam_muc_archive_sync(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ok, + Params :: map(), + Extra :: map(). +mam_muc_archive_sync(Result, _Params, #{host_type := HostType}) -> mongoose_async_pools:sync(HostType, muc_mam), - Result. + {ok, Result}. %%% gen_mod callbacks -spec start(mongooseim:host_type(), gen_mod:module_opts()) -> any(). @@ -29,16 +39,21 @@ start(HostType, Opts) -> mod_mam_rdbms_arch_async:prepare_insert_queries(muc, Extra), mongoose_metrics:ensure_metric(HostType, ?PER_MESSAGE_FLUSH_TIME, histogram), mongoose_metrics:ensure_metric(HostType, ?FLUSH_TIME, histogram), - ejabberd_hooks:add(mam_muc_archive_sync, HostType, ?MODULE, mam_muc_archive_sync, 50), - ejabberd_hooks:add(mam_muc_archive_message, HostType, ?MODULE, archive_muc_message, 50), + gen_hook:add_handlers(hooks(HostType)), mongoose_async_pools:start_pool(HostType, muc_mam, PoolOpts). -spec stop(mongooseim:host_type()) -> any(). stop(HostType) -> - ejabberd_hooks:delete(mam_muc_archive_sync, HostType, ?MODULE, mam_muc_archive_sync, 50), - ejabberd_hooks:delete(mam_muc_archive_message, HostType, ?MODULE, archive_muc_message, 50), + gen_hook:delete_handlers(hooks(HostType)), mongoose_async_pools:stop_pool(HostType, muc_mam). +-spec hooks(mongooseim:host_type()) -> gen_hook:hook_list(). +hooks(HostType) -> + [ + {mam_muc_archive_sync, HostType, fun ?MODULE:mam_muc_archive_sync/3, #{}, 50}, + {mam_muc_archive_message, HostType, fun ?MODULE:archive_muc_message/3, #{}, 50} + ]. + -spec supported_features() -> [atom()]. supported_features() -> [dynamic_domains]. diff --git a/src/mam/mod_mam_pm.erl b/src/mam/mod_mam_pm.erl index c558484afd..b9c5ebfccd 100644 --- a/src/mam/mod_mam_pm.erl +++ b/src/mam/mod_mam_pm.erl @@ -112,7 +112,6 @@ archive_id(Server, User) start(HostType, Opts) -> ?LOG_INFO(#{what => mam_starting, host_type => HostType}), ensure_metrics(HostType), - ejabberd_hooks:add(legacy_hooks(HostType)), gen_hook:add_handlers(hooks(HostType)), add_iq_handlers(HostType, Opts), ok. @@ -120,7 +119,6 @@ start(HostType, Opts) -> -spec stop(host_type()) -> any(). stop(HostType) -> ?LOG_INFO(#{what => mam_stopping, host_type => HostType}), - ejabberd_hooks:delete(legacy_hooks(HostType)), gen_hook:delete_handlers(hooks(HostType)), remove_iq_handlers(HostType), ok. @@ -664,10 +662,6 @@ is_archivable_message(HostType, Dir, Packet) -> ArchiveChatMarkers = mod_mam_params:archive_chat_markers(?MODULE, HostType), erlang:apply(M, is_archivable_message, [?MODULE, Dir, Packet, ArchiveChatMarkers]). --spec legacy_hooks(jid:lserver()) -> [ejabberd_hooks:hook()]. -legacy_hooks(HostType) -> - mongoose_metrics_mam_hooks:get_mam_hooks(HostType). - hooks(HostType) -> [ {disco_local_features, HostType, fun ?MODULE:disco_local_features/3, #{}, 99}, @@ -678,7 +672,7 @@ hooks(HostType) -> {amp_determine_strategy, HostType, fun ?MODULE:determine_amp_strategy/3, #{}, 20}, {sm_filter_offline_message, HostType, fun ?MODULE:sm_filter_offline_message/3, #{}, 50}, {get_personal_data, HostType, fun ?MODULE:get_personal_data/3, #{}, 50} - ]. + ] ++ mongoose_metrics_mam_hooks:get_mam_hooks(HostType). add_iq_handlers(HostType, Opts) -> Component = ejabberd_sm, diff --git a/src/mam/mod_mam_rdbms_arch.erl b/src/mam/mod_mam_rdbms_arch.erl index 6e2800b903..f6e9367b77 100644 --- a/src/mam/mod_mam_rdbms_arch.erl +++ b/src/mam/mod_mam_rdbms_arch.erl @@ -20,19 +20,16 @@ -callback encode(term()) -> binary(). -callback decode(binary()) -> term(). --export([archive_size/4, +-export([archive_size/3, archive_message/3, lookup_messages/3, - remove_archive/4, - remove_domain/3]). - --export([get_mam_pm_gdpr_data/3]). + remove_archive/3, + remove_domain/3, + get_mam_pm_gdpr_data/3]). %% Called from mod_mam_rdbms_async_pool_writer -export([prepare_message/2, retract_message/2, prepare_insert/2]). --ignore_xref([behaviour_info/1, remove_archive/4]). - -type host_type() :: mongooseim:host_type(). %% ---------------------------------------------------------------------- @@ -80,18 +77,20 @@ stop(HostType) -> supported_features() -> [dynamic_domains]. --spec get_mam_pm_gdpr_data(ejabberd_gen_mam_archive:mam_pm_gdpr_data(), - host_type(), jid:jid()) -> - ejabberd_gen_mam_archive:mam_pm_gdpr_data(). -get_mam_pm_gdpr_data(Acc, HostType, - #jid{luser = LUser, lserver = LServer} = ArcJID) -> +-spec get_mam_pm_gdpr_data(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ejabberd_gen_mam_archive:mam_pm_gdpr_data(), + Params :: map(), + Extra :: map(). +get_mam_pm_gdpr_data(Acc, + #{jid := #jid{luser = LUser, lserver = LServer} = ArcJID}, + #{host_type := HostType}) -> case mod_mam_pm:archive_id(LServer, LUser) of undefined -> - Acc; + {ok, Acc}; ArcID -> Env = env_vars(HostType, ArcJID), {selected, Rows} = extract_gdpr_messages(Env, ArcID), - [uniform_to_gdpr(row_to_uniform_format(Row, Env)) || Row <- Rows] ++ Acc + {ok, [uniform_to_gdpr(row_to_uniform_format(Row, Env)) || Row <- Rows] ++ Acc} end. -spec uniform_to_gdpr(mod_mam:message_row()) -> tuple(). @@ -103,31 +102,25 @@ uniform_to_gdpr(#{id := MessID, jid := RemoteJID, packet := Packet}) -> -spec start_hooks(host_type()) -> ok. start_hooks(HostType) -> - ejabberd_hooks:add(legacy_hooks(HostType)), gen_hook:add_handlers(hooks(HostType)). -spec stop_hooks(host_type()) -> ok. stop_hooks(HostType) -> - ejabberd_hooks:delete(legacy_hooks(HostType)), gen_hook:delete_handlers(hooks(HostType)). -legacy_hooks(HostType) -> - case gen_mod:get_module_opt(HostType, ?MODULE, no_writer) of +-spec hooks(mongooseim:host_type()) -> gen_hook:hook_list(). +hooks(HostType) -> +case gen_mod:get_module_opt(HostType, ?MODULE, no_writer) of true -> []; false -> - [{mam_archive_message, HostType, ?MODULE, archive_message, 50}] + [{mam_archive_message, HostType, fun ?MODULE:archive_message/3, #{}, 50}] end ++ - [{mam_archive_size, HostType, ?MODULE, archive_size, 50}, - {mam_lookup_messages, HostType, ?MODULE, lookup_messages, 50}, - {mam_remove_archive, HostType, ?MODULE, remove_archive, 50}, - {get_mam_pm_gdpr_data, HostType, ?MODULE, get_mam_pm_gdpr_data, 50}]. - --spec hooks(mongooseim:host_type()) -> gen_hook:hook_list(). -hooks(HostType) -> - [ - {remove_domain, HostType, fun ?MODULE:remove_domain/3, #{}, 50} - ]. + [{mam_archive_size, HostType, fun ?MODULE:archive_size/3, #{}, 50}, + {mam_lookup_messages, HostType, fun ?MODULE:lookup_messages/3, #{}, 50}, + {mam_remove_archive, HostType, fun ?MODULE:remove_archive/3, #{}, 50}, + {get_mam_pm_gdpr_data, HostType, fun ?MODULE:get_mam_pm_gdpr_data/3, #{}, 50}, + {remove_domain, HostType, fun ?MODULE:remove_domain/3, #{}, 50}]. %% ---------------------------------------------------------------------- %% SQL queries @@ -247,27 +240,32 @@ get_retract_id(Packet, #{has_message_retraction := Enabled}) -> %% ---------------------------------------------------------------------- %% Internal functions and callbacks --spec archive_size(Size :: integer(), HostType :: host_type(), - ArcId :: mod_mam:archive_id(), ArcJID :: jid:jid()) -> integer(). -archive_size(Size, HostType, ArcID, ArcJID) when is_integer(Size) -> +-spec archive_size(Acc, Params, Extra) -> {ok, Acc} when + Acc :: integer(), + Params :: map(), + Extra :: map(). +archive_size(Size, #{archive_id := ArcID, owner := ArcJID}, #{host_type := HostType}) when is_integer(Size) -> Filter = [{equal, user_id, ArcID}], Env = env_vars(HostType, ArcJID), Result = lookup_query(count, Env, Filter, unordered, all), - mongoose_rdbms:selected_to_integer(Result). + {ok, mongoose_rdbms:selected_to_integer(Result)}. --spec archive_message(_Result, host_type(), mod_mam:archive_message_params()) -> ok. -archive_message(_Result, HostType, Params = #{local_jid := ArcJID}) -> +-spec archive_message(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ok, + Params :: map(), + Extra :: map(). +archive_message(_Result, #{params := Params = #{local_jid := ArcJID} = Params}, #{host_type := HostType}) -> try assert_archive_id_provided(Params), Env = env_vars(HostType, ArcJID), do_archive_message(HostType, Params, Env), retract_message(HostType, Params, Env), - ok + {ok, ok} catch error:Reason:StackTrace -> - ?LOG_ERROR(#{what => archive_message_failed, - host_type => HostType, mam_params => Params, - reason => Reason, stacktrace => StackTrace}), - erlang:raise(error, Reason, StackTrace) + ?LOG_ERROR(#{what => archive_message_failed, + host_type => HostType, mam_params => Params, + reason => Reason, stacktrace => StackTrace}), + erlang:raise(error, Reason, StackTrace) end. do_archive_message(HostType, Params, Env) -> @@ -340,12 +338,13 @@ prepare_insert(Name, NumRows) -> ok. %% Removal logic --spec remove_archive(Acc :: mongoose_acc:t(), HostType :: host_type(), - ArcID :: mod_mam:archive_id(), - RoomJID :: jid:jid()) -> mongoose_acc:t(). -remove_archive(Acc, HostType, ArcID, _ArcJID) -> +-spec remove_archive(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +remove_archive(Acc, #{archive_id := ArcID}, #{host_type := HostType}) -> mongoose_rdbms:execute_successfully(HostType, mam_archive_remove, [ArcID]), - Acc. + {ok, Acc}. -spec remove_domain(Acc, Params, Extra) -> {ok | stop, Acc} when Acc :: mongoose_domain_api:remove_domain_acc(), @@ -382,15 +381,17 @@ extract_gdpr_messages(Env, ArcID) -> lookup_query(lookup, Env, Filters, asc, all). %% Lookup logic --spec lookup_messages(Result :: any(), HostType :: host_type(), Params :: map()) -> - {ok, mod_mam:lookup_result()}. -lookup_messages({error, _Reason}=Result, _HostType, _Params) -> - Result; -lookup_messages(_Result, HostType, Params = #{owner_jid := ArcJID}) -> +-spec lookup_messages(Acc, Params, Extra) -> {ok, Acc} when + Acc :: {ok, mod_mam:lookup_result()}, + Params :: map(), + Extra :: map(). +lookup_messages({error, _Reason} = Result, _Params, _Extra) -> + {ok, Result}; +lookup_messages(_Result, #{params := #{owner_jid := ArcJID} = Params}, #{host_type := HostType}) -> Env = env_vars(HostType, ArcJID), ExdParams = mam_encoder:extend_lookup_params(Params, Env), Filter = mam_filter:produce_filter(ExdParams, lookup_fields()), - mam_lookup:lookup(Env, Filter, ExdParams). + {ok, mam_lookup:lookup(Env, Filter, ExdParams)}. lookup_query(QueryType, Env, Filters, Order, OffsetLimit) -> mam_lookup_sql:lookup_query(QueryType, Env, Filters, Order, OffsetLimit). diff --git a/src/mam/mod_mam_rdbms_arch_async.erl b/src/mam/mod_mam_rdbms_arch_async.erl index dd765cfcec..528fa29828 100644 --- a/src/mam/mod_mam_rdbms_arch_async.erl +++ b/src/mam/mod_mam_rdbms_arch_async.erl @@ -8,20 +8,27 @@ -define(FLUSH_TIME, [mod_mam_rdbms_async_pool_writer, flush_time]). -behaviour(gen_mod). + -export([start/2, stop/1, supported_features/0]). --export([archive_pm_message/3, mam_archive_sync/2, flush/2]). --ignore_xref([archive_pm_message/3, mam_archive_sync/2]). +-export([archive_pm_message/3, mam_archive_sync/3]). +-export([flush/2]). -export([make_pool_opts/2, prepare_insert_queries/2]). --spec archive_pm_message(_Result, mongooseim:host_type(), mod_mam:archive_message_params()) -> ok. -archive_pm_message(_Result, HostType, Params = #{archive_id := ArcID}) -> - mongoose_async_pools:put_task(HostType, pm_mam, ArcID, Params). - --spec mam_archive_sync(term(), mongooseim:host_type()) -> term(). -mam_archive_sync(Result, HostType) -> +-spec archive_pm_message(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ok, + Params :: map(), + Extra :: map(). +archive_pm_message(_Result, #{params := #{archive_id := ArcID} = Params}, #{host_type := HostType}) -> + {ok, mongoose_async_pools:put_task(HostType, pm_mam, ArcID, Params)}. + +-spec mam_archive_sync(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ok, + Params :: map(), + Extra :: map(). +mam_archive_sync(Result, _Params, #{host_type := HostType}) -> mongoose_async_pools:sync(HostType, pm_mam), - Result. + {ok, Result}. %%% gen_mod callbacks -spec start(mongooseim:host_type(), gen_mod:module_opts()) -> any(). @@ -30,16 +37,21 @@ start(HostType, Opts) -> prepare_insert_queries(pm, Extra), mongoose_metrics:ensure_metric(HostType, ?PER_MESSAGE_FLUSH_TIME, histogram), mongoose_metrics:ensure_metric(HostType, ?FLUSH_TIME, histogram), - ejabberd_hooks:add(mam_archive_sync, HostType, ?MODULE, mam_archive_sync, 50), - ejabberd_hooks:add(mam_archive_message, HostType, ?MODULE, archive_pm_message, 50), + gen_hook:add_handlers(hooks(HostType)), mongoose_async_pools:start_pool(HostType, pm_mam, PoolOpts). -spec stop(mongooseim:host_type()) -> any(). stop(HostType) -> - ejabberd_hooks:delete(mam_archive_message, HostType, ?MODULE, archive_pm_message, 50), - ejabberd_hooks:delete(mam_archive_sync, HostType, ?MODULE, mam_archive_sync, 50), + gen_hook:delete_handlers(hooks(HostType)), mongoose_async_pools:stop_pool(HostType, pm_mam). +-spec hooks(mongooseim:host_type()) -> gen_hook:hook_list(). +hooks(HostType) -> + [ + {mam_archive_sync, HostType, fun ?MODULE:mam_archive_sync/3, #{}, 50}, + {mam_archive_message, HostType, fun ?MODULE:archive_pm_message/3, #{}, 50} + ]. + -spec supported_features() -> [atom()]. supported_features() -> [dynamic_domains]. diff --git a/src/mam/mod_mam_rdbms_prefs.erl b/src/mam/mod_mam_rdbms_prefs.erl index b4cfa01b01..b4791198e0 100644 --- a/src/mam/mod_mam_rdbms_prefs.erl +++ b/src/mam/mod_mam_rdbms_prefs.erl @@ -10,16 +10,15 @@ %% Exports %% gen_mod handlers +-behaviour(gen_mod). -export([start/2, stop/1, supported_features/0]). %% MAM hook handlers -behaviour(ejabberd_gen_mam_prefs). --export([get_behaviour/5, - get_prefs/4, - set_prefs/7, - remove_archive/4]). - --ignore_xref([remove_archive/4, start/2, stop/1, supported_features/0]). +-export([get_behaviour/3, + get_prefs/3, + set_prefs/3, + remove_archive/3]). -import(mongoose_rdbms, [prepare/4]). @@ -34,12 +33,12 @@ -spec start(mongooseim:host_type(), _) -> ok. start(HostType, _Opts) -> prepare_queries(HostType), - ejabberd_hooks:add(hooks(HostType)), + gen_hook:add_handlers(hooks(HostType)), ok. -spec stop(mongooseim:host_type()) -> ok. stop(HostType) -> - ejabberd_hooks:delete(hooks(HostType)), + gen_hook:delete_handlers(hooks(HostType)), ok. -spec supported_features() -> [atom()]. @@ -58,16 +57,16 @@ maybe_muc_hooks(true, HostType) -> muc_hooks(HostType); maybe_muc_hooks(false, _HostType) -> []. pm_hooks(HostType) -> - [{mam_get_behaviour, HostType, ?MODULE, get_behaviour, 50}, - {mam_get_prefs, HostType, ?MODULE, get_prefs, 50}, - {mam_set_prefs, HostType, ?MODULE, set_prefs, 50}, - {mam_remove_archive, HostType, ?MODULE, remove_archive, 50}]. + [{mam_get_behaviour, HostType, fun ?MODULE:get_behaviour/3, #{}, 50}, + {mam_get_prefs, HostType, fun ?MODULE:get_prefs/3, #{}, 50}, + {mam_set_prefs, HostType, fun ?MODULE:set_prefs/3, #{}, 50}, + {mam_remove_archive, HostType, fun ?MODULE:remove_archive/3, #{}, 50}]. muc_hooks(HostType) -> - [{mam_muc_get_behaviour, HostType, ?MODULE, get_behaviour, 50}, - {mam_muc_get_prefs, HostType, ?MODULE, get_prefs, 50}, - {mam_muc_set_prefs, HostType, ?MODULE, set_prefs, 50}, - {mam_muc_remove_archive, HostType, ?MODULE, remove_archive, 50}]. + [{mam_muc_get_behaviour, HostType, fun ?MODULE:get_behaviour/3, #{}, 50}, + {mam_muc_get_prefs, HostType, fun ?MODULE:get_prefs/3, #{}, 50}, + {mam_muc_set_prefs, HostType, fun ?MODULE:set_prefs/3, #{}, 50}, + {mam_muc_remove_archive, HostType, fun ?MODULE:remove_archive/3, #{}, 50}]. %% Prepared queries prepare_queries(HostType) -> @@ -103,20 +102,23 @@ order_by_remote_jid_in_delete(HostType) -> %% ---------------------------------------------------------------------- %% Internal functions and callbacks --spec get_behaviour(Default :: mod_mam:archive_behaviour(), - HostType :: mongooseim:host_type(), ArchiveID :: mod_mam:archive_id(), - LocJID :: jid:jid(), RemJID :: jid:jid()) -> any(). -get_behaviour(DefaultBehaviour, HostType, UserID, _LocJID, RemJID) +-spec get_behaviour(Acc, Params, Extra) -> {ok, Acc} when + Acc :: mod_mam:archive_behaviour(), + Params :: map(), + Extra :: map(). +get_behaviour(DefaultBehaviour, + #{archive_id := UserID, remote := RemJID}, + #{host_type := HostType}) when is_integer(UserID) -> RemLJID = jid:to_lower(RemJID), BRemLBareJID = jid:to_bare_binary(RemLJID), BRemLJID = jid:to_binary(RemLJID), case query_behaviour(HostType, UserID, BRemLJID, BRemLBareJID) of {selected, []} -> - DefaultBehaviour; + {ok, DefaultBehaviour}; {selected, RemoteJid2Behaviour} -> DbBehaviour = choose_behaviour(BRemLJID, BRemLBareJID, RemoteJid2Behaviour), - decode_behaviour(DbBehaviour) + {ok, decode_behaviour(DbBehaviour)} end. -spec choose_behaviour(binary(), binary(), [{binary(), binary()}]) -> binary(). @@ -135,16 +137,18 @@ choose_behaviour(BRemLJID, BRemLBareJID, RemoteJid2Behaviour) -> end end. --spec set_prefs(Result :: any(), HostType :: mongooseim:host_type(), - ArchiveID :: mod_mam:archive_id(), ArchiveJID :: jid:jid(), - DefaultMode :: mod_mam:archive_behaviour(), - AlwaysJIDs :: [jid:literal_jid()], - NeverJIDs :: [jid:literal_jid()]) -> any(). -set_prefs(_Result, HostType, UserID, _ArcJID, DefaultMode, AlwaysJIDs, NeverJIDs) -> +-spec set_prefs(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +set_prefs(_Result, + #{archive_id := UserID, default_mode := DefaultMode, + always_jids := AlwaysJIDs, never_jids := NeverJIDs}, + #{host_type := HostType}) -> try - set_prefs1(HostType, UserID, DefaultMode, AlwaysJIDs, NeverJIDs) + {ok, set_prefs1(HostType, UserID, DefaultMode, AlwaysJIDs, NeverJIDs)} catch _Type:Error -> - {error, Error} + {ok, {error, Error}} end. set_prefs1(HostType, UserID, DefaultMode, AlwaysJIDs, NeverJIDs) -> @@ -159,19 +163,21 @@ set_prefs1(HostType, UserID, DefaultMode, AlwaysJIDs, NeverJIDs) -> end, #{user_id => UserID, retries => 5, delay => 100}), ok. --spec get_prefs(mod_mam:preference(), HostType :: mongooseim:host_type(), - ArchiveID :: mod_mam:archive_id(), ArchiveJID :: jid:jid()) - -> mod_mam:preference(). -get_prefs({GlobalDefaultMode, _, _}, HostType, UserID, _ArcJID) -> +-spec get_prefs(Acc, Params, Extra) -> {ok, Acc} when + Acc :: mod_mam:preference(), + Params :: map(), + Extra :: map(). +get_prefs({GlobalDefaultMode, _, _}, #{archive_id := UserID}, #{host_type := HostType}) -> {selected, Rows} = mongoose_rdbms:execute(HostType, mam_prefs_select, [UserID]), - decode_prefs_rows(Rows, GlobalDefaultMode, [], []). + {ok, decode_prefs_rows(Rows, GlobalDefaultMode, [], [])}. --spec remove_archive(mongoose_acc:t(), mongooseim:host_type(), - mod_mam:archive_id(), jid:jid()) -> - mongoose_acc:t(). -remove_archive(Acc, HostType, UserID, _ArcJID) -> +-spec remove_archive(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +remove_archive(Acc, #{archive_id := UserID}, #{host_type := HostType}) -> remove_archive(HostType, UserID), - Acc. + {ok, Acc}. remove_archive(HostType, UserID) -> {updated, _} = diff --git a/src/mam/mod_mam_rdbms_user.erl b/src/mam/mod_mam_rdbms_user.erl index ba2a6e477f..6acf382b33 100644 --- a/src/mam/mod_mam_rdbms_user.erl +++ b/src/mam/mod_mam_rdbms_user.erl @@ -9,19 +9,18 @@ %%% @end %%%------------------------------------------------------------------- -module(mod_mam_rdbms_user). +-behaviour(gen_mod). %% gen_mod handlers -export([start/2, stop/1, supported_features/0]). %% ejabberd handlers -export([archive_id/3, - remove_archive/4]). + remove_archive/3]). %% For debugging ONLY -export([create_user_archive/3]). - --ignore_xref([archive_id/3, create_user_archive/3, remove_archive/4, start/2, - stop/1, supported_features/0]). +-ignore_xref([create_user_archive/3]). -include("mongoose.hrl"). -include("jlib.hrl"). @@ -31,12 +30,12 @@ -spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok. start(HostType, _Opts) -> prepare_queries(), - ejabberd_hooks:add(hooks(HostType)), + gen_hook:add_handlers(hooks(HostType)), ok. -spec stop(mongooseim:host_type()) -> ok. stop(HostType) -> - ejabberd_hooks:delete(hooks(HostType)), + gen_hook:delete_handlers(hooks(HostType)), ok. -spec supported_features() -> [atom()]. @@ -44,7 +43,7 @@ supported_features() -> [dynamic_domains]. hooks(HostType) -> - [{Hook, HostType, ?MODULE, Fun, N} + [{Hook, HostType, Fun, #{}, N} || {true, Hook, Fun, N} <- hooks2(HostType)]. hooks2(HostType) -> @@ -52,10 +51,10 @@ hooks2(HostType) -> AR = gen_mod:get_module_opt(HostType, ?MODULE, auto_remove, false), PM = gen_mod:get_module_opt(HostType, ?MODULE, pm, false), MUC = gen_mod:get_module_opt(HostType, ?MODULE, muc, false), - [{PM, mam_archive_id, archive_id, 50}, - {PM and AR, mam_remove_archive, remove_archive, 90}, - {MUC, mam_muc_archive_id, archive_id, 50}, - {MUC and AR, mam_muc_remove_archive, remove_archive, 90}]. + [{PM, mam_archive_id, fun ?MODULE:archive_id/3, 50}, + {PM and AR, mam_remove_archive, fun ?MODULE:remove_archive/3, 90}, + {MUC, mam_muc_archive_id, fun ?MODULE:archive_id/3, 50}, + {MUC and AR, mam_muc_remove_archive, fun ?MODULE:remove_archive/3, 90}]. prepare_queries() -> mongoose_rdbms:prepare(mam_user_insert, mam_server_user, [server, user_name], @@ -69,20 +68,26 @@ prepare_queries() -> %%==================================================================== %% API %%==================================================================== --spec archive_id(ArcID :: undefined | mod_mam:archive_id(), - HostType :: mongooseim:host_type(), - ArchiveJID :: jid:jid()) -> mod_mam:archive_id(). -archive_id(undefined, HostType, _ArcJID=#jid{lserver = LServer, luser = LUser}) -> - query_archive_id(HostType, LServer, LUser); -archive_id(ArcID, _Host, _ArcJID) -> - ArcID. - --spec remove_archive(Acc :: map(), HostType :: mongooseim:host_type(), - ArchiveID :: mod_mam:archive_id(), - ArchiveJID :: jid:jid()) -> map(). -remove_archive(Acc, HostType, _ArcID, _ArcJID = #jid{lserver = LServer, luser = LUser}) -> +-spec archive_id(Acc, Params, Extra) -> {ok, Acc} when + Acc :: mod_mam:archive_id() | undefined, + Params :: map(), + Extra :: map(). +archive_id(undefined, + #{owner := #jid{lserver = LServer, luser = LUser}}, + #{host_type := HostType}) -> + {ok, query_archive_id(HostType, LServer, LUser)}; +archive_id(ArcID, _Params, _Extra) -> + {ok, ArcID}. + +-spec remove_archive(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +remove_archive(Acc, + #{owner := #jid{lserver = LServer, luser = LUser}}, + #{host_type := HostType}) -> execute_user_remove(HostType, LServer, LUser), - Acc. + {ok, Acc}. %%==================================================================== %% Internal functions diff --git a/src/mam/mod_mam_riak_timed_arch_yz.erl b/src/mam/mod_mam_riak_timed_arch_yz.erl index d4dae30b25..53f3f1ee19 100644 --- a/src/mam/mod_mam_riak_timed_arch_yz.erl +++ b/src/mam/mod_mam_riak_timed_arch_yz.erl @@ -30,14 +30,14 @@ %% API -export([start/2, stop/1, - archive_size/4, - lookup_messages/2, - remove_archive/4]). + archive_size/3, + lookup_messages/2]). -export([archive_message/3, archive_message_muc/3, lookup_messages/3, - lookup_messages_muc/3]). + lookup_messages_muc/3, + remove_archive/3]). -export([key/3]). @@ -47,9 +47,8 @@ -export([get_mam_muc_gdpr_data/3, get_mam_pm_gdpr_data/3]). --ignore_xref([archive_message_muc/3, behaviour_info/1, bucket/2, create_obj/6, - key/3, list_mam_buckets/1, lookup_messages/2, lookup_messages_muc/3, - read_archive/8, remove_archive/4, remove_bucket/1]). +-ignore_xref([bucket/2, create_obj/6, key/3, list_mam_buckets/1, lookup_messages/2, + read_archive/8, remove_bucket/1]). -type yearweeknum() :: {non_neg_integer(), 1..53}. @@ -62,28 +61,29 @@ %% Use both options `pm, muc' to archive both MUC and private messages -spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok. start(HostType, Opts) -> - ejabberd_hooks:add(hooks(HostType, Opts)). + gen_hook:add_handlers(hooks(HostType, Opts)). -spec stop(mongooseim:host_type()) -> ok. stop(HostType) -> Opts = gen_mod:get_loaded_module_opts(HostType, ?MODULE), - ejabberd_hooks:delete(hooks(HostType, Opts)). + gen_hook:delete_handlers(hooks(HostType, Opts)). +-spec hooks(mongooseim:host_type(), gen_mod:module_opts()) -> gen_hook:hook_list(). hooks(HostType, Opts) -> lists:flatmap(fun(Type) -> hooks(HostType, Type, Opts) end, [pm, muc]). hooks(HostType, pm, #{pm := true}) -> - [{mam_archive_message, HostType, ?MODULE, archive_message, 50}, - {mam_archive_size, HostType, ?MODULE, archive_size, 50}, - {mam_lookup_messages, HostType, ?MODULE, lookup_messages, 50}, - {mam_remove_archive, HostType, ?MODULE, remove_archive, 50}, - {get_mam_pm_gdpr_data, HostType, ?MODULE, get_mam_pm_gdpr_data, 50}]; + [{mam_archive_message, HostType, fun ?MODULE:archive_message/3, #{}, 50}, + {mam_archive_size, HostType, fun ?MODULE:archive_size/3, #{}, 50}, + {mam_lookup_messages, HostType, fun ?MODULE:lookup_messages/3, #{}, 50}, + {mam_remove_archive, HostType, fun ?MODULE:remove_archive/3, #{}, 50}, + {get_mam_pm_gdpr_data, HostType, fun ?MODULE:get_mam_pm_gdpr_data/3, #{}, 50}]; hooks(HostType, muc, #{muc := true}) -> - [{mam_muc_archive_message, HostType, ?MODULE, archive_message_muc, 50}, - {mam_muc_archive_size, HostType, ?MODULE, archive_size, 50}, - {mam_muc_lookup_messages, HostType, ?MODULE, lookup_messages_muc, 50}, - {mam_muc_remove_archive, HostType, ?MODULE, remove_archive, 50}, - {get_mam_muc_gdpr_data, HostType, ?MODULE, get_mam_muc_gdpr_data, 50}]; + [{mam_muc_archive_message, HostType, fun ?MODULE:archive_message_muc/3, #{}, 50}, + {mam_muc_archive_size, HostType, fun ?MODULE:archive_size/3, #{}, 50}, + {mam_muc_lookup_messages, HostType, fun ?MODULE:lookup_messages_muc/3, #{}, 50}, + {mam_muc_remove_archive, HostType, fun ?MODULE:remove_archive/3, #{}, 50}, + {get_mam_muc_gdpr_data, HostType, fun ?MODULE:get_mam_muc_gdpr_data/3, #{}, 50}]; hooks(_HostType, _Opt, _Opts) -> []. @@ -96,40 +96,52 @@ mam_bucket_type(Host) -> %% LocJID - archive owner's JID %% RemJID - interlocutor's JID %% SrcJID - "Real" sender JID -archive_message(_Result, Host, #{message_id := MessId, - local_jid := LocJID, - remote_jid := RemJID, - source_jid := SrcJID, - packet := Packet} = Params) -> +-spec archive_message(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ok, + Params :: map(), + Extra :: map(). +archive_message(_Result, + #{params := #{message_id := MessId, + local_jid := LocJID, + remote_jid := RemJID, + source_jid := SrcJID, + packet := Packet} = Params}, + #{host_type := Host}) -> try - archive_message(Host, MessId, LocJID, RemJID, SrcJID, LocJID, Packet, pm) + {ok, archive_message(Host, MessId, LocJID, RemJID, SrcJID, LocJID, Packet, pm)} catch Class:Reason:StackTrace -> ?LOG_WARNING(maps:merge(Params, #{what => archive_message_failed, text => <<"Could not write message to archive">>, class => Class, reason => Reason, stacktrace => StackTrace})), mongoose_metrics:update(Host, modMamDropped, 1), - {error, Reason} + {ok, {error, Reason}} end. +-spec archive_message_muc(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ok, + Params :: map(), + Extra :: map(). %% LocJID - MUC/MUC Light room's JID %% FromJID - "Real" sender JID %% SrcJID - Full JID of user within room (room@domain/user) -archive_message_muc(_Result, Host, #{message_id := MessId, - local_jid := LocJID, - remote_jid := FromJID, - source_jid := SrcJID, - packet := Packet} = Params) -> +archive_message_muc(_Result, + #{params := #{message_id := MessId, + local_jid := LocJID, + remote_jid := FromJID, + source_jid := SrcJID, + packet := Packet} = Params}, + #{host_type := Host}) -> RemJIDMuc = maybe_muc_jid(SrcJID), try - archive_message(Host, MessId, LocJID, RemJIDMuc, SrcJID, FromJID, Packet, muc) + {ok, archive_message(Host, MessId, LocJID, RemJIDMuc, SrcJID, FromJID, Packet, muc)} catch Class:Reason:StackTrace -> ?LOG_WARNING(maps:merge(Params, #{what => archive_muc_message_failed, text => <<"Could not write MUC message to archive">>, class => Class, reason => Reason, stacktrace => StackTrace})), mongoose_metrics:update(Host, modMamDropped, 1), - {error, Reason} + {ok, {error, Reason}} end. maybe_muc_jid(#jid{lresource = RemRes}) -> @@ -138,22 +150,38 @@ maybe_muc_jid(Other) -> Other. -lookup_messages({error, _Reason} = Result, _Host, _Params) -> - Result; -lookup_messages(_Result, Host, Params) -> +-spec lookup_messages(Acc, Params, Extra) -> {ok, Acc} when + Acc :: {ok, mod_mam:lookup_result()} | {error, term()}, + Params :: map(), + Extra :: map(). +lookup_messages({error, _Reason} = Result, _Params, _Extra) -> + {ok, Result}; +lookup_messages(_Result, #{params := Params}, #{host_type := Host}) -> try - lookup_messages(Host, Params) + {ok, lookup_messages(Host, Params)} catch _Type:Reason:S -> - {error, {Reason, {stacktrace, S}}} + {ok, {error, {Reason, {stacktrace, S}}}} end. - -lookup_messages_muc(Result, Host, #{with_jid := WithJID} = Params) -> +-spec lookup_messages_muc(Acc, Params, Extra) -> {ok, Acc} when + Acc :: {ok, mod_mam:lookup_result()} | {error, term()}, + Params :: map(), + Extra :: map(). +lookup_messages_muc(Result, #{params := #{with_jid := WithJID} = Params} = Params0, Extra) -> WithJIDMuc = maybe_muc_jid(WithJID), - lookup_messages(Result, Host, Params#{with_jid => WithJIDMuc}). + lookup_messages(Result, Params0#{params => Params#{with_jid => WithJIDMuc}}, Extra). + +-spec archive_size(Acc, Params, Extra) -> {ok, Acc} when + Acc :: integer(), + Params :: map(), + Extra :: map(). +archive_size(_Size, #{owner := ArchiveJID}, #{host_type := Host}) -> + archive_size(ArchiveJID, Host); +archive_size(_Size, #{room := ArchiveJID}, #{host_type := Host}) -> + archive_size(ArchiveJID, Host). -archive_size(_Size, Host, _ArchiveID, ArchiveJID) -> +archive_size(ArchiveJID, Host) -> OwnerJID = mod_mam_utils:bare_jid(ArchiveJID), RemoteJID = undefined, {MsgIdStartNoRSM, MsgIdEndNoRSM} = @@ -162,7 +190,7 @@ archive_size(_Size, Host, _ArchiveID, ArchiveJID) -> {TotalCount, _} = read_archive(Host, OwnerJID, RemoteJID, MsgIdStartNoRSM, MsgIdEndNoRSM, undefined, [{rows, 1}], F), - TotalCount. + {ok, TotalCount}. %% use correct bucket for given date @@ -337,19 +365,23 @@ get_message2(Host, MsgId, Bucket, Key) -> _ -> [] end. --spec get_mam_pm_gdpr_data(ejabberd_gen_mam_archive:mam_pm_gdpr_data(), - mongooseim:host_type(), jid:jid()) -> - ejabberd_gen_mam_archive:mam_pm_gdpr_data(). -get_mam_pm_gdpr_data(Acc, _HostType, OwnerJid) -> - Messages = get_mam_gdpr_data(OwnerJid, <<"pm">>), - [{Id, jid:to_binary(Jid), exml:to_binary(Packet)} || #{id := Id, jid := Jid, packet := Packet} <- Messages] ++ Acc. --spec get_mam_muc_gdpr_data(ejabberd_gen_mam_archive:mam_muc_gdpr_data(), - mongooseim:host_type(), jid:jid()) -> - ejabberd_gen_mam_archive:mam_muc_gdpr_data(). -get_mam_muc_gdpr_data(Acc, _HostType, JID) -> +-spec get_mam_pm_gdpr_data(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ejabberd_gen_mam_archive:mam_pm_gdpr_data(), + Params :: map(), + Extra :: map(). +get_mam_pm_gdpr_data(Acc, #{jid := OwnerJid}, _Extra) -> + Messages = get_mam_gdpr_data(OwnerJid, <<"pm">>), + {ok, [{Id, jid:to_binary(Jid), exml:to_binary(Packet)} + || #{id := Id, jid := Jid, packet := Packet} <- Messages] ++ Acc}. + +-spec get_mam_muc_gdpr_data(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ejabberd_gen_mam_archive:mam_muc_gdpr_data(), + Params :: map(), + Extra :: map(). +get_mam_muc_gdpr_data(Acc, #{jid := JID}, _Extra) -> Messages = get_mam_gdpr_data(JID, <<"muc">>), - [{MsgId, exml:to_binary(Packet)} || #{id := MsgId, packet := Packet} <- Messages] ++ Acc. + {ok, [{MsgId, exml:to_binary(Packet)} || #{id := MsgId, packet := Packet} <- Messages] ++ Acc}. get_mam_gdpr_data(#jid{ lserver = LServer } = BareJid, Type) -> BareLJidBin = jid:to_binary(jid:to_lower(BareJid)), @@ -358,9 +390,16 @@ get_mam_gdpr_data(#jid{ lserver = LServer } = BareJid, Type) -> {ok, _Cnt, _, MsgIds} = fold_archive(LServer, fun get_msg_id_key/3, Query, SearchOpts, []), get_messages(LServer, MsgIds). -remove_archive(Acc, Host, _ArchiveID, ArchiveJID) -> +-spec remove_archive(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +remove_archive(Acc, #{owner := ArchiveJID}, #{host_type := Host}) -> + remove_archive(Host, ArchiveJID), + {ok, Acc}; +remove_archive(Acc, #{room := ArchiveJID}, #{host_type := Host}) -> remove_archive(Host, ArchiveJID), - Acc. + {ok, Acc}. remove_archive(Host, ArchiveJID) -> {ok, TotalCount, _, _} = R = remove_chunk(Host, ArchiveJID, 0), diff --git a/src/metrics/mongoose_metrics_mam_hooks.erl b/src/metrics/mongoose_metrics_mam_hooks.erl index 870910db12..8442462da7 100644 --- a/src/metrics/mongoose_metrics_mam_hooks.erl +++ b/src/metrics/mongoose_metrics_mam_hooks.erl @@ -16,80 +16,78 @@ %%------------------- %% Internal exports %%------------------- --export([mam_get_prefs/4, - mam_set_prefs/7, - mam_remove_archive/4, +-export([mam_get_prefs/3, + mam_set_prefs/3, + mam_remove_archive/3, mam_lookup_messages/3, mam_archive_message/3, mam_flush_messages/3, - mam_muc_get_prefs/4, - mam_muc_set_prefs/7, - mam_muc_remove_archive/4, + mam_muc_get_prefs/3, + mam_muc_set_prefs/3, + mam_muc_remove_archive/3, mam_muc_lookup_messages/3, mam_muc_archive_message/3, mam_muc_flush_messages/3]). --ignore_xref([mam_archive_message/3, mam_get_prefs/4, mam_lookup_messages/3, mam_flush_messages/3, - mam_muc_archive_message/3, mam_muc_flush_messages/3, mam_muc_get_prefs/4, - mam_muc_lookup_messages/3, mam_muc_remove_archive/4, mam_muc_set_prefs/7, - mam_remove_archive/4, mam_set_prefs/7]). - --type metrics_notify_return() :: mongoose_metrics_hooks:metrics_notify_return(). - %%------------------- %% Implementation %%------------------- %% @doc Here will be declared which hooks should be registered when mod_mam_pm is enabled. --spec get_mam_hooks(_) -> [ejabberd_hooks:hook(), ...]. +-spec get_mam_hooks(mongooseim:host_type()) -> gen_hook:hook_list(). get_mam_hooks(Host) -> [ - {mam_set_prefs, Host, ?MODULE, mam_set_prefs, 50}, - {mam_get_prefs, Host, ?MODULE, mam_get_prefs, 50}, - {mam_archive_message, Host, ?MODULE, mam_archive_message, 50}, - {mam_remove_archive, Host, ?MODULE, mam_remove_archive, 50}, - {mam_lookup_messages, Host, ?MODULE, mam_lookup_messages, 100}, - {mam_flush_messages, Host, ?MODULE, mam_flush_messages, 50} + {mam_set_prefs, Host, fun ?MODULE:mam_set_prefs/3, #{}, 50}, + {mam_get_prefs, Host, fun ?MODULE:mam_get_prefs/3, #{}, 50}, + {mam_archive_message, Host, fun ?MODULE:mam_archive_message/3, #{}, 50}, + {mam_remove_archive, Host, fun ?MODULE:mam_remove_archive/3, #{}, 50}, + {mam_lookup_messages, Host, fun ?MODULE:mam_lookup_messages/3, #{}, 100}, + {mam_flush_messages, Host, fun ?MODULE:mam_flush_messages/3, #{}, 50} ]. %% @doc Here will be declared which hooks should be registered when mod_mam_muc is enabled. --spec get_mam_muc_hooks(_) -> [ejabberd_hooks:hook(), ...]. +-spec get_mam_muc_hooks(mongooseim:host_type()) -> gen_hook:hook_list(). get_mam_muc_hooks(Host) -> [ - {mam_muc_set_prefs, Host, ?MODULE, mam_muc_set_prefs, 50}, - {mam_muc_get_prefs, Host, ?MODULE, mam_muc_get_prefs, 50}, - {mam_muc_archive_message, Host, ?MODULE, mam_muc_archive_message, 50}, - {mam_muc_remove_archive, Host, ?MODULE, mam_muc_remove_archive, 50}, - {mam_muc_lookup_messages, Host, ?MODULE, mam_muc_lookup_messages, 100}, - {mam_muc_flush_messages, Host, ?MODULE, mam_muc_flush_messages, 50} + {mam_muc_set_prefs, Host, fun ?MODULE:mam_muc_set_prefs/3, #{}, 50}, + {mam_muc_get_prefs, Host, fun ?MODULE:mam_muc_get_prefs/3, #{}, 50}, + {mam_muc_archive_message, Host, fun ?MODULE:mam_muc_archive_message/3, #{}, 50}, + {mam_muc_remove_archive, Host, fun ?MODULE:mam_muc_remove_archive/3, #{}, 50}, + {mam_muc_lookup_messages, Host, fun ?MODULE:mam_muc_lookup_messages/3, #{}, 100}, + {mam_muc_flush_messages, Host, fun ?MODULE:mam_muc_flush_messages/3, #{}, 50} ]. --spec mam_get_prefs(Result :: any(), - Host :: jid:server(), - _ArcID :: mod_mam:archive_id(), - _ArcJID :: jid:jid()) -> any(). -mam_get_prefs(Result, Host, _ArcID, _ArcJID) -> +-spec mam_get_prefs(Acc, Params, Extra) -> {ok, Acc} when + Acc :: mod_mam:preference() | {error, Reason :: term()}, + Params :: map(), + Extra :: map(). +mam_get_prefs(Result, _Params, #{host_type := Host}) -> mongoose_metrics:update(Host, modMamPrefsGets, 1), - Result. + {ok, Result}. --spec mam_set_prefs(Result :: any(), Host :: jid:server(), - _ArcID :: mod_mam:archive_id(), _ArcJID :: jid:jid(), - _DefaultMode :: any(), _AlwaysJIDs :: [jid:literal_jid()], - _NeverJIDs :: [jid:literal_jid()]) -> any(). -mam_set_prefs(Result, Host, _ArcID, _ArcJID, _DefaultMode, _AlwaysJIDs, _NeverJIDs) -> +-spec mam_set_prefs(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +mam_set_prefs(Result, _Params, #{host_type := Host}) -> mongoose_metrics:update(Host, modMamPrefsSets, 1), - Result. + {ok, Result}. --spec mam_remove_archive(Acc :: map(), - Host :: jid:server(), - _ArcID :: mod_mam:archive_id(), - _ArcJID :: jid:jid()) -> metrics_notify_return(). -mam_remove_archive(Acc, Host, _ArcID, _ArcJID) -> +-spec mam_remove_archive(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +mam_remove_archive(Acc, _Params, #{host_type := Host}) -> mongoose_metrics:update(Host, modMamArchiveRemoved, 1), - Acc. - -mam_lookup_messages(Result = {ok, {_TotalCount, _Offset, MessageRows}}, - Host, #{is_simple := IsSimple}) -> + {ok, Acc}. + +-spec mam_lookup_messages(Acc, Params, Extra) -> {ok, Acc} when + Acc :: {ok, mod_mam:lookup_result()} | {error, term()}, + Params :: map(), + Extra :: map(). +mam_lookup_messages({ok, {_TotalCount, _Offset, MessageRows}} = Result, + #{params := #{is_simple := IsSimple}}, + #{host_type := Host}) -> mongoose_metrics:update(Host, modMamForwarded, length(MessageRows)), mongoose_metrics:update(Host, modMamLookups, 1), case IsSimple of @@ -98,53 +96,78 @@ mam_lookup_messages(Result = {ok, {_TotalCount, _Offset, MessageRows}}, _ -> ok end, - Result; -mam_lookup_messages(Result = {error, _}, _Host, _Params) -> - Result. - --spec mam_archive_message(Result :: any(), Host :: jid:server(), - _Params :: mod_mam:archive_message_params()) -> any(). -mam_archive_message(Result, Host, _Params) -> + {ok, Result}; +mam_lookup_messages(Result = {error, _}, _Params, _Extra) -> + {ok, Result}. + +-spec mam_archive_message(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ok | {error, timeout}, + Params :: map(), + Extra :: map(). +mam_archive_message(Result, _Params, #{host_type := Host}) -> mongoose_metrics:update(Host, modMamArchived, 1), - Result. + {ok, Result}. -mam_flush_messages(Acc, Host, MessageCount) -> +-spec mam_flush_messages(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ok, + Params :: map(), + Extra :: map(). +mam_flush_messages(Acc, #{count := MessageCount}, #{host_type := Host}) -> mongoose_metrics:update(Host, modMamFlushed, MessageCount), - Acc. + {ok, Acc}. %% ---------------------------------------------------------------------------- %% mod_mam_muc -mam_muc_get_prefs(Result, Host, _ArcID, _ArcJID) -> +-spec mam_muc_get_prefs(Acc, Params, Extra) -> {ok, Acc} when + Acc :: mod_mam:preference() | {error, Reason :: term()}, + Params :: map(), + Extra :: map(). +mam_muc_get_prefs(Result, _Params, #{host_type := Host}) -> mongoose_metrics:update(Host, modMucMamPrefsGets, 1), - Result. + {ok, Result}. -mam_muc_set_prefs(Result, Host, _ArcID, _ArcJID, _DefaultMode, _AlwaysJIDs, _NeverJIDs) -> +-spec mam_muc_set_prefs(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +mam_muc_set_prefs(Result, _Params, #{host_type := Host}) -> mongoose_metrics:update(Host, modMucMamPrefsSets, 1), - Result. + {ok, Result}. -mam_muc_remove_archive(Acc, Host, _ArcID, _ArcJID) -> +-spec mam_muc_remove_archive(Acc, Params, Extra) -> {ok, Acc} when + Acc :: term(), + Params :: map(), + Extra :: map(). +mam_muc_remove_archive(Acc, _Params, #{host_type := Host}) -> mongoose_metrics:update(Host, modMucMamArchiveRemoved, 1), - Acc. - -mam_muc_lookup_messages(Result = {ok, {_TotalCount, _Offset, MessageRows}}, - Host, _Params) -> + {ok, Acc}. + +-spec mam_muc_lookup_messages(Acc, Params, Extra) -> {ok, Acc} when + Acc :: {ok, mod_mam:lookup_result()} | {error, term()}, + Params :: map(), + Extra :: map(). +mam_muc_lookup_messages({ok, {_TotalCount, _Offset, MessageRows}} = Result, + _Params, + #{host_type := Host}) -> mongoose_metrics:update(Host, modMucMamForwarded, length(MessageRows)), mongoose_metrics:update(Host, modMucMamLookups, 1), - Result; -mam_muc_lookup_messages(Result = {error, _}, - _Host, _Params) -> - Result. - --spec mam_muc_archive_message(Result :: any(), Host :: jid:server(), - _Params :: mod_mam:archive_message_params()) -> any(). -mam_muc_archive_message(Result, Host, _Params) -> + {ok, Result}; +mam_muc_lookup_messages(Result = {error, _}, _Params, _Extra) -> + {ok, Result}. + +-spec mam_muc_archive_message(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ok | {error, timeout}, + Params :: map(), + Extra :: map(). +mam_muc_archive_message(Result, _Params, #{host_type := Host}) -> mongoose_metrics:update(Host, modMucMamArchived, 1), - Result. + {ok, Result}. -%% #rh -mam_muc_flush_messages(Acc, Host, MessageCount) -> +-spec mam_muc_flush_messages(Acc, Params, Extra) -> {ok, Acc} when + Acc :: ok, + Params :: map(), + Extra :: map(). +mam_muc_flush_messages(Acc, #{count := MessageCount}, #{host_type := Host}) -> mongoose_metrics:update(Host, modMucMamFlushed, MessageCount), - Acc. - -%%% vim: set sts=4 ts=4 sw=4 et filetype=erlang foldmarker=%%%',%%%. foldmethod=marker: + {ok, Acc}. diff --git a/src/mod_dynamic_domains_test.erl b/src/mod_dynamic_domains_test.erl index 0baaccfdf3..a518a1579a 100644 --- a/src/mod_dynamic_domains_test.erl +++ b/src/mod_dynamic_domains_test.erl @@ -4,6 +4,7 @@ -include("jlib.hrl"). -behaviour(mongoose_packet_handler). +-behaviour(gen_mod). %% API -export([start/2, stop/1, @@ -11,8 +12,6 @@ supported_features/0]). -export([process_packet/5, process_iq/5]). --ignore_xref([config_spec/0, process_packet/5, start/2, stop/1, supported_features/0]). - -spec config_spec() -> mongoose_config_spec:config_section(). config_spec() -> #section{items = #{<<"host1">> => diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index e905fc8b1b..9dbc91f6e0 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -1061,110 +1061,140 @@ room_new_affiliations(Acc, Room, NewAffs, Version) -> %%% %%% If a MAM backend doesn't support or doesn't require archive IDs, %%% `undefined' may be returned. --spec mam_archive_id(HostType, OwnerJID) -> Result when +-spec mam_archive_id(HostType, Owner) -> Result when HostType :: mongooseim:host_type(), - OwnerJID :: jid:jid(), + Owner :: jid:jid(), Result :: undefined | mod_mam:archive_id(). -mam_archive_id(HostType, OwnerJID) -> - run_hook_for_host_type(mam_archive_id, HostType, undefined, [HostType, OwnerJID]). +mam_archive_id(HostType, Owner) -> + Params = #{owner => Owner}, + Args = [HostType, Owner], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), + run_hook_for_host_type(mam_archive_id, HostType, undefined, ParamsWithLegacyArgs). %%% @doc The `mam_archive_size' hook is called to determine the size %%% of the archive for a given JID --spec mam_archive_size(HostType, ArchiveID, OwnerJID) -> Result when +-spec mam_archive_size(HostType, ArchiveID, Owner) -> Result when HostType :: mongooseim:host_type(), ArchiveID :: undefined | mod_mam:archive_id(), - OwnerJID :: jid:jid(), + Owner :: jid:jid(), Result :: integer(). -mam_archive_size(HostType, ArchiveID, OwnerJID) -> +mam_archive_size(HostType, ArchiveID, Owner) -> + Params = #{archive_id => ArchiveID, owner => Owner}, + Args = [HostType, ArchiveID, Owner], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), run_hook_for_host_type(mam_archive_size, HostType, 0, - [HostType, ArchiveID, OwnerJID]). + ParamsWithLegacyArgs). %%% @doc The `mam_get_behaviour' hooks is called to determine if a message %%% should be archived or not based on a given pair of JIDs. --spec mam_get_behaviour(HookServer, ArchiveID, - OwnerJID, RemoteJID) -> Result when - HookServer :: jid:lserver(), +-spec mam_get_behaviour(HostType, ArchiveID, + Owner, Remote) -> Result when + HostType :: jid:lserver(), ArchiveID :: undefined | mod_mam:archive_id(), - OwnerJID :: jid:jid(), - RemoteJID :: jid:jid(), + Owner :: jid:jid(), + Remote :: jid:jid(), Result :: mod_mam:archive_behaviour(). -mam_get_behaviour(HookServer, ArchiveID, OwnerJID, RemoteJID) -> - run_hook_for_host_type(mam_get_behaviour, HookServer, always, - [HookServer, ArchiveID, OwnerJID, RemoteJID]). +mam_get_behaviour(HostType, ArchiveID, Owner, Remote) -> + Params = #{archive_id => ArchiveID, owner => Owner, remote => Remote}, + Args = [HostType, ArchiveID, Owner, Remote], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), + run_hook_for_host_type(mam_get_behaviour, HostType, always, + ParamsWithLegacyArgs). %%% @doc The `mam_set_prefs' hook is called to set a user's archive preferences. %%% %%% It's possible to set which JIDs are always or never allowed in the archive --spec mam_set_prefs(HookServer, ArchiveId, OwnerJID, +-spec mam_set_prefs(HostType, ArchiveId, Owner, DefaultMode, AlwaysJIDs, NeverJIDs) -> Result when - HookServer :: jid:lserver(), + HostType :: jid:lserver(), ArchiveId :: undefined | mod_mam:archive_id(), - OwnerJID :: jid:jid(), + Owner :: jid:jid(), DefaultMode :: mod_mam:archive_behaviour(), AlwaysJIDs :: [jid:literal_jid()], NeverJIDs :: [jid:literel_jid()], Result :: any(). -mam_set_prefs(HookServer, ArchiveID, OwnerJID, DefaultMode, AlwaysJIDs, NeverJIDs) -> - run_hook_for_host_type(mam_set_prefs, HookServer, {error, not_implemented}, - [HookServer, ArchiveID, OwnerJID, - DefaultMode, AlwaysJIDs, NeverJIDs]). +mam_set_prefs(HostType, ArchiveID, Owner, DefaultMode, AlwaysJIDs, NeverJIDs) -> + Params = #{archive_id => ArchiveID, owner => Owner, + default_mode => DefaultMode, always_jids => AlwaysJIDs, never_jids => NeverJIDs}, + Args = [HostType, ArchiveID, Owner, + DefaultMode, AlwaysJIDs, NeverJIDs], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), + run_hook_for_host_type(mam_set_prefs, HostType, {error, not_implemented}, + ParamsWithLegacyArgs). %%% @doc The `mam_get_prefs' hook is called to read %%% the archive settings for a given user. --spec mam_get_prefs(HookServer, DefaultMode, ArchiveID, OwnerJID) -> Result when - HookServer :: jid:lserver(), +-spec mam_get_prefs(HostType, DefaultMode, ArchiveID, Owner) -> Result when + HostType :: jid:lserver(), DefaultMode :: mod_mam:archive_behaviour(), ArchiveID :: undefined | mod_mam:archive_id(), - OwnerJID :: jid:jid(), + Owner :: jid:jid(), Result :: mod_mam:preference() | {error, Reason :: term()}. -mam_get_prefs(HookServer, DefaultMode, ArchiveID, OwnerJID) -> +mam_get_prefs(HostType, DefaultMode, ArchiveID, Owner) -> + Params = #{archive_id => ArchiveID, owner => Owner}, + Args = [HostType, ArchiveID, Owner], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), InitialAccValue = {DefaultMode, [], []}, %% mod_mam:preference() type - run_hook_for_host_type(mam_get_prefs, HookServer, InitialAccValue, - [HookServer, ArchiveID, OwnerJID]). + run_hook_for_host_type(mam_get_prefs, HostType, InitialAccValue, + ParamsWithLegacyArgs). %%% @doc The `mam_remove_archive' hook is called in order to %%% remove the entire archive for a particular user. --spec mam_remove_archive(HookServer, ArchiveID, OwnerJID) -> any() when - HookServer :: jid:lserver(), +-spec mam_remove_archive(HostType, ArchiveID, Owner) -> any() when + HostType :: jid:lserver(), ArchiveID :: undefined | mod_mam:archive_id(), - OwnerJID :: jid:jid(). -mam_remove_archive(HookServer, ArchiveID, OwnerJID) -> - run_hook_for_host_type(mam_remove_archive, HookServer, ok, - [HookServer, ArchiveID, OwnerJID]). + Owner :: jid:jid(). +mam_remove_archive(HostType, ArchiveID, Owner) -> + Params = #{archive_id => ArchiveID, owner => Owner}, + Args = [HostType, ArchiveID, Owner], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), + run_hook_for_host_type(mam_remove_archive, HostType, ok, + ParamsWithLegacyArgs). %%% @doc The `mam_lookup_messages' hook is to retrieve %%% archived messages for given search parameters. --spec mam_lookup_messages(HookServer, Params) -> Result when - HookServer :: jid:lserver(), +-spec mam_lookup_messages(HostType, Params) -> Result when + HostType :: jid:lserver(), Params :: map(), Result :: {ok, mod_mam:lookup_result()}. -mam_lookup_messages(HookServer, Params) -> +mam_lookup_messages(HostType, Params0) -> + Params = #{params => Params0}, + Args = [HostType, Params0], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), InitialLookupValue = {0, 0, []}, %% mod_mam:lookup_result() type - run_hook_for_host_type(mam_lookup_messages, HookServer, {ok, InitialLookupValue}, - [HookServer, Params]). + run_hook_for_host_type(mam_lookup_messages, HostType, {ok, InitialLookupValue}, + ParamsWithLegacyArgs). %%% @doc The `mam_archive_message' hook is called in order %%% to store the message in the archive. --spec mam_archive_message(HookServer, Params) -> +-spec mam_archive_message(HostType, Params) -> Result when - HookServer :: jid:lserver(), + HostType :: jid:lserver(), Params :: mod_mam:archive_message_params(), Result :: ok | {error, timeout}. -mam_archive_message(HookServer, Params) -> - run_hook_for_host_type(mam_archive_message, HookServer, ok, [HookServer, Params]). +mam_archive_message(HostType, Params0) -> + Params = #{params => Params0}, + Args = [HostType, Params0], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), + run_hook_for_host_type(mam_archive_message, HostType, ok, ParamsWithLegacyArgs). %%% @doc The `mam_flush_messages' hook is run after the async bulk write %%% happens for messages despite the result of the write. --spec mam_flush_messages(HookServer :: jid:lserver(), +-spec mam_flush_messages(HostType :: jid:lserver(), MessageCount :: integer()) -> ok. -mam_flush_messages(HookServer, MessageCount) -> - run_hook_for_host_type(mam_flush_messages, HookServer, ok, - [HookServer, MessageCount]). +mam_flush_messages(HostType, MessageCount) -> + Params = #{count => MessageCount}, + Args = [HostType, MessageCount], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), + run_hook_for_host_type(mam_flush_messages, HostType, ok, + ParamsWithLegacyArgs). %% @doc Waits until all pending messages are written -spec mam_archive_sync(HostType :: mongooseim:host_type()) -> ok. mam_archive_sync(HostType) -> - run_hook_for_host_type(mam_archive_sync, HostType, ok, [HostType]). + Args = [HostType], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(#{}, Args), + run_hook_for_host_type(mam_archive_sync, HostType, ok, ParamsWithLegacyArgs). %% @doc Notifies of a message retraction -spec mam_retraction(mongooseim:host_type(), @@ -1187,79 +1217,97 @@ mam_retraction(HostType, RetractionInfo, Env) -> %%% %%% If a MAM backend doesn't support or doesn't require archive IDs, %%% `undefined' may be returned. --spec mam_muc_archive_id(HookServer, OwnerJID) -> Result when - HookServer :: jid:lserver(), - OwnerJID :: jid:jid(), +-spec mam_muc_archive_id(HostType, Owner) -> Result when + HostType :: jid:lserver(), + Owner :: jid:jid(), Result :: undefined | mod_mam:archive_id(). -mam_muc_archive_id(HookServer, OwnerJID) -> - run_hook_for_host_type(mam_muc_archive_id, HookServer, undefined, - [HookServer, OwnerJID]). +mam_muc_archive_id(HostType, Owner) -> + Params = #{owner => Owner}, + Args = [HostType, Owner], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), + run_hook_for_host_type(mam_muc_archive_id, HostType, undefined, + ParamsWithLegacyArgs). %%% @doc The `mam_muc_archive_size' hook is called to determine %%% the archive size for a given room. --spec mam_muc_archive_size(HostType, ArchiveID, RoomJID) -> Result when +-spec mam_muc_archive_size(HostType, ArchiveID, Room) -> Result when HostType :: mongooseim:host_type(), ArchiveID :: undefined | mod_mam:archive_id(), - RoomJID :: jid:jid(), + Room :: jid:jid(), Result :: integer(). -mam_muc_archive_size(HostType, ArchiveID, RoomJID) -> - run_hook_for_host_type(mam_muc_archive_size, HostType, 0, - [HostType, ArchiveID, RoomJID]). +mam_muc_archive_size(HostType, ArchiveID, Room) -> + Params = #{archive_id => ArchiveID, room => Room}, + Args = [HostType, ArchiveID, Room], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), + run_hook_for_host_type(mam_muc_archive_size, HostType, 0, ParamsWithLegacyArgs). %%% @doc The `mam_muc_get_behaviour' hooks is called to determine if a message should %%% be archived or not based on the given room and user JIDs. -spec mam_muc_get_behaviour(HostType, ArchiveID, - RoomJID, RemoteJID) -> Result when + Room, Remote) -> Result when HostType :: mongooseim:host_type(), ArchiveID :: undefined | mod_mam:archive_id(), - RoomJID :: jid:jid(), - RemoteJID :: jid:jid(), + Room :: jid:jid(), + Remote :: jid:jid(), Result :: mod_mam:archive_behaviour(). -mam_muc_get_behaviour(HostType, ArchiveID, RoomJID, RemoteJID) -> +mam_muc_get_behaviour(HostType, ArchiveID, Room, Remote) -> + Params = #{archive_id => ArchiveID, room => Room, remote => Remote}, + Args = [HostType, ArchiveID, Room, Remote], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), DefaultBehaviour = always, %% mod_mam:archive_behaviour() type run_hook_for_host_type(mam_muc_get_behaviour, HostType, DefaultBehaviour, - [HostType, ArchiveID, RoomJID, RemoteJID]). + ParamsWithLegacyArgs). %%% @doc The `mam_muc_set_prefs' hook is called to set a room's archive preferences. %%% %%% It's possible to set which JIDs are always or never allowed in the archive --spec mam_muc_set_prefs(HostType, ArchiveId, RoomJID, +-spec mam_muc_set_prefs(HostType, ArchiveId, Room, DefaultMode, AlwaysJIDs, NeverJIDs) -> Result when HostType :: mongooseim:host_type(), ArchiveId :: undefined | mod_mam:archive_id(), - RoomJID :: jid:jid(), + Room :: jid:jid(), DefaultMode :: mod_mam:archive_behaviour(), AlwaysJIDs :: [jid:literal_jid()], NeverJIDs :: [jid:literel_jid()], Result :: any(). -mam_muc_set_prefs(HostType, ArchiveID, RoomJID, DefaultMode, AlwaysJIDs, NeverJIDs) -> +mam_muc_set_prefs(HostType, ArchiveID, Room, DefaultMode, AlwaysJIDs, NeverJIDs) -> + Params = #{archive_id => ArchiveID, room => Room, default_mode => DefaultMode, + always_jids => AlwaysJIDs, never_jids => NeverJIDs}, + Args = [HostType, ArchiveID, Room, DefaultMode, + AlwaysJIDs, NeverJIDs], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), InitialAcc = {error, not_implemented}, run_hook_for_host_type(mam_muc_set_prefs, HostType, InitialAcc, - [HostType, ArchiveID, RoomJID, DefaultMode, - AlwaysJIDs, NeverJIDs]). + ParamsWithLegacyArgs). %%% @doc The `mam_muc_get_prefs' hook is called to read %%% the archive settings for a given room. --spec mam_muc_get_prefs(HostType, DefaultMode, ArchiveID, RoomJID) -> Result when +-spec mam_muc_get_prefs(HostType, DefaultMode, ArchiveID, Room) -> Result when HostType :: mongooseim:host_type(), DefaultMode :: mod_mam:archive_behaviour(), ArchiveID :: undefined | mod_mam:archive_id(), - RoomJID :: jid:jid(), + Room :: jid:jid(), Result :: mod_mam:preference() | {error, Reason :: term()}. -mam_muc_get_prefs(HostType, DefaultMode, ArchiveID, RoomJID) -> +mam_muc_get_prefs(HostType, DefaultMode, ArchiveID, Room) -> + Params = #{archive_id => ArchiveID, room => Room}, + Args = [HostType, ArchiveID, Room], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), InitialAcc = {DefaultMode, [], []}, %% mod_mam:preference() type run_hook_for_host_type(mam_muc_get_prefs, HostType, InitialAcc, - [HostType, ArchiveID, RoomJID]). + ParamsWithLegacyArgs). %%% @doc The `mam_muc_remove_archive' hook is called in order to remove the entire %%% archive for a particular user. --spec mam_muc_remove_archive(HostType, ArchiveID, RoomJID) -> any() when +-spec mam_muc_remove_archive(HostType, ArchiveID, Room) -> any() when HostType :: mongooseim:host_type(), ArchiveID :: undefined | mod_mam:archive_id(), - RoomJID :: jid:jid(). -mam_muc_remove_archive(HostType, ArchiveID, RoomJID) -> + Room :: jid:jid(). +mam_muc_remove_archive(HostType, ArchiveID, Room) -> + Params = #{archive_id => ArchiveID, room => Room}, + Args = [HostType, ArchiveID, Room], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), run_hook_for_host_type(mam_muc_remove_archive, HostType, ok, - [HostType, ArchiveID, RoomJID]). + ParamsWithLegacyArgs). %%% @doc The `mam_muc_lookup_messages' hook is to retrieve archived %%% MUC messages for any given search parameters. @@ -1267,10 +1315,13 @@ mam_muc_remove_archive(HostType, ArchiveID, RoomJID) -> HostType :: mongooseim:host_type(), Params :: map(), Result :: {ok, mod_mam:lookup_result()}. -mam_muc_lookup_messages(HostType, Params) -> +mam_muc_lookup_messages(HostType, Params0) -> + Params = #{params => Params0}, + Args = [HostType, Params], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), InitialLookupValue = {0, 0, []}, %% mod_mam:lookup_result() type run_hook_for_host_type(mam_muc_lookup_messages, HostType, {ok, InitialLookupValue}, - [HostType, Params]). + ParamsWithLegacyArgs). %%% @doc The `mam_muc_archive_message' hook is called in order %%% to store the MUC message in the archive. @@ -1278,21 +1329,29 @@ mam_muc_lookup_messages(HostType, Params) -> HostType :: mongooseim:host_type(), Params :: mod_mam:archive_message_params(), Result :: ok | {error, timeout}. -mam_muc_archive_message(HostType, Params) -> - run_hook_for_host_type(mam_muc_archive_message, HostType, ok, [HostType, Params]). +mam_muc_archive_message(HostType, Params0) -> + Params = #{params => Params0}, + Args = [HostType, Params], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), + run_hook_for_host_type(mam_muc_archive_message, HostType, ok, ParamsWithLegacyArgs). %%% @doc The `mam_muc_flush_messages' hook is run after the async bulk write %%% happens for MUC messages despite the result of the write. --spec mam_muc_flush_messages(HookServer :: jid:lserver(), +-spec mam_muc_flush_messages(HostType :: jid:lserver(), MessageCount :: integer()) -> ok. -mam_muc_flush_messages(HookServer, MessageCount) -> - run_hook_for_host_type(mam_muc_flush_messages, HookServer, ok, - [HookServer, MessageCount]). +mam_muc_flush_messages(HostType, MessageCount) -> + Params = #{count => MessageCount}, + Args = [HostType, MessageCount], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), + run_hook_for_host_type(mam_muc_flush_messages, HostType, ok, + ParamsWithLegacyArgs). %% @doc Waits until all pending messages are written -spec mam_muc_archive_sync(HostType :: mongooseim:host_type()) -> ok. mam_muc_archive_sync(HostType) -> - run_hook_for_host_type(mam_muc_archive_sync, HostType, ok, [HostType]). + Args = [HostType], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(#{}, Args), + run_hook_for_host_type(mam_muc_archive_sync, HostType, ok, ParamsWithLegacyArgs). %% @doc Notifies of a muc message retraction -spec mam_muc_retraction(mongooseim:host_type(), @@ -1311,7 +1370,10 @@ mam_muc_retraction(HostType, RetractionInfo, Env) -> JID :: jid:jid(), Result :: ejabberd_gen_mam_archive:mam_pm_gdpr_data(). get_mam_pm_gdpr_data(HostType, JID) -> - run_hook_for_host_type(get_mam_pm_gdpr_data, HostType, [], [HostType, JID]). + Params = #{jid => JID}, + Args = [HostType, JID], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), + run_hook_for_host_type(get_mam_pm_gdpr_data, HostType, [], ParamsWithLegacyArgs). %%% @doc `get_mam_muc_gdpr_data' hook is called to provide %%% a user's archive for GDPR purposes. @@ -1320,7 +1382,10 @@ get_mam_pm_gdpr_data(HostType, JID) -> JID :: jid:jid(), Result :: ejabberd_gen_mam_archive:mam_muc_gdpr_data(). get_mam_muc_gdpr_data(HostType, JID) -> - run_hook_for_host_type(get_mam_muc_gdpr_data, HostType, [], [HostType, JID]). + Params = #{jid => JID}, + Args = [HostType, JID], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), + run_hook_for_host_type(get_mam_muc_gdpr_data, HostType, [], ParamsWithLegacyArgs). %%% @doc `get_personal_data' hook is called to retrieve %%% a user's personal data for GDPR purposes. @@ -1502,7 +1567,10 @@ amp_verify_support(HostType, Rules) -> EventData :: mod_muc:room_event_data(), Result :: exml:element(). filter_room_packet(HostType, Packet, EventData) -> - run_hook_for_host_type(filter_room_packet, HostType, Packet, [HostType, EventData]). + Params = #{packet => Packet, event_data => EventData}, + Args = [HostType, EventData], + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args), + run_hook_for_host_type(filter_room_packet, HostType, Packet, ParamsWithLegacyArgs). %%% @doc The `forget_room' hook is called when a room is removed from the database. -spec forget_room(HostType, MucHost, Room) -> Result when