Skip to content

Commit

Permalink
Refactored hook handlers in mongoose_metrics_mam_hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pawlooss1 committed Nov 2, 2022
1 parent 0231bf3 commit ea74886
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 106 deletions.
17 changes: 11 additions & 6 deletions src/mam/mod_mam_muc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,16 @@ 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)),
ejabberd_hooks:add(legacy_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)),
ejabberd_hooks:delete(legacy_hooks(HostType)),
gen_hook:delete_handlers(hooks(HostType)),
remove_iq_handlers(HostType),
ok.

Expand Down Expand Up @@ -616,13 +618,16 @@ 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()].
hooks(HostType) ->
-spec legacy_hooks(host_type()) -> [ejabberd_hooks:hook()].
legacy_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}
| mongoose_metrics_mam_hooks:get_mam_muc_hooks(HostType)].
{get_personal_data, HostType, ?MODULE, get_personal_data, 50}].

-spec hooks(mongooseim:host_type()) -> gen_hook:hook_list().
hooks(HostType) ->
mongoose_metrics_mam_hooks:get_mam_muc_hooks(HostType).

add_iq_handlers(HostType, Opts) ->
IQDisc = gen_mod:get_opt(iqdisc, Opts, parallel),
Expand Down
8 changes: 2 additions & 6 deletions src/mam/mod_mam_pm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,13 @@ 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.

-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.
Expand Down Expand Up @@ -664,10 +662,7 @@ 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).

-spec hooks(mongooseim:host_type()) -> gen_hook:hook_list().
hooks(HostType) ->
[
{disco_local_features, HostType, fun ?MODULE:disco_local_features/3, #{}, 99},
Expand All @@ -678,6 +673,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) ->
Expand Down
173 changes: 98 additions & 75 deletions src/metrics/mongoose_metrics_mam_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,80 +16,77 @@
%%-------------------
%% 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(_) -> 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(_) -> 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 :: any(),
Params :: map(),
Extra :: #{host_type := mongooseim:host_type()}.
mam_get_prefs(Acc, _, #{host_type := Host}) ->
mongoose_metrics:update(Host, modMamPrefsGets, 1),
Result.
{ok, Acc}.

-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 :: any(),
Params :: map(),
Extra :: #{host_type := mongooseim:host_type()}.
mam_set_prefs(Acc, _, #{host_type := Host}) ->
mongoose_metrics:update(Host, modMamPrefsSets, 1),
Result.
{ok, Acc}.

-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 :: any(),
Params :: map(),
Extra :: #{host_type := mongooseim:host_type()}.
mam_remove_archive(Acc, _, #{host_type := Host}) ->
mongoose_metrics:update(Host, modMamArchiveRemoved, 1),
Acc.
{ok, Acc}.

-spec mam_lookup_messages(Result, Params, Extra) -> {ok, Result} when
Result :: {ok | error, mod_mam:lookup_result()},
Params :: map(),
Extra :: #{host_type := mongooseim:host_type()}.
mam_lookup_messages(Result = {ok, {_TotalCount, _Offset, MessageRows}},
Host, #{is_simple := IsSimple}) ->
#{is_simple := IsSimple}, #{host_type := Host}) ->
mongoose_metrics:update(Host, modMamForwarded, length(MessageRows)),
mongoose_metrics:update(Host, modMamLookups, 1),
case IsSimple of
Expand All @@ -98,53 +95,79 @@ 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, _}, _, _) ->
{ok, Result}.

-spec mam_archive_message(Acc, Params, Extra) -> {ok, Acc} when
Acc :: any(),
Params :: mod_mam:archive_message_params(),
Extra :: #{host_type := mongooseim:host_type()}.
mam_archive_message(Acc, _, #{host_type := Host}) ->
mongoose_metrics:update(Host, modMamArchived, 1),
Result.
{ok, Acc}.

mam_flush_messages(Acc, Host, MessageCount) ->
-spec mam_flush_messages(Acc, Params, Extra) -> {ok, Acc} when
Acc :: any(),
Params :: #{message_count := integer()},
Extra :: #{host_type := mongooseim:host_type()}.
mam_flush_messages(Acc, #{message_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 :: any(),
Params :: map(),
Extra :: #{host_type := mongooseim:host_type()}.
mam_muc_get_prefs(Acc, _, #{host_type := Host}) ->
mongoose_metrics:update(Host, modMucMamPrefsGets, 1),
Result.
{ok, Acc}.

mam_muc_set_prefs(Result, Host, _ArcID, _ArcJID, _DefaultMode, _AlwaysJIDs, _NeverJIDs) ->
-spec mam_muc_set_prefs(Acc, Params, Extra) -> {ok, Acc} when
Acc :: any(),
Params :: map(),
Extra :: #{host_type := mongooseim:host_type()}.
mam_muc_set_prefs(Acc, _, #{host_type := Host}) ->
mongoose_metrics:update(Host, modMucMamPrefsSets, 1),
Result.
{ok, Acc}.

mam_muc_remove_archive(Acc, Host, _ArcID, _ArcJID) ->
-spec mam_muc_remove_archive(Acc, Params, Extra) -> {ok, Acc} when
Acc :: any(),
Params :: map(),
Extra :: #{host_type := mongooseim:host_type()}.
mam_muc_remove_archive(Acc, _, #{host_type := Host}) ->
mongoose_metrics:update(Host, modMucMamArchiveRemoved, 1),
Acc.
{ok, Acc}.

-spec mam_muc_lookup_messages(Result, Params, Extra) -> {ok, Result} when
Result :: {ok | error, mod_mam:lookup_result()},
Params :: map(),
Extra :: #{host_type := mongooseim:host_type()}.
mam_muc_lookup_messages(Result = {ok, {_TotalCount, _Offset, MessageRows}},
Host, _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, _}, _, _) ->
{ok, Result}.

-spec mam_muc_archive_message(Acc, Params, Extra) -> {ok, Acc} when
Acc :: any(),
Params :: mod_mam:archive_message_params(),
Extra :: #{host_type := mongooseim:host_type()}.
mam_muc_archive_message(Acc, _, #{host_type := Host}) ->
mongoose_metrics:update(Host, modMucMamArchived, 1),
Result.
{ok, Acc}.

%% #rh
mam_muc_flush_messages(Acc, Host, MessageCount) ->
-spec mam_muc_flush_messages(Acc, Params, Extra) -> {ok, Acc} when
Acc :: any(),
Params :: #{message_count := integer()},
Extra :: #{host_type := mongooseim:host_type()}.
mam_muc_flush_messages(Acc, #{message_count := MessageCount}, #{host_type := Host}) ->
mongoose_metrics:update(Host, modMucMamFlushed, MessageCount),
Acc.
{ok, Acc}.

%%% vim: set sts=4 ts=4 sw=4 et filetype=erlang foldmarker=%%%',%%%. foldmethod=marker:
Loading

0 comments on commit ea74886

Please sign in to comment.