Skip to content

Commit

Permalink
Add send_message/4 callback for MAM
Browse files Browse the repository at this point in the history
  • Loading branch information
arcusfelis committed Apr 26, 2021
1 parent 60b43a4 commit d302457
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 31 deletions.
8 changes: 8 additions & 0 deletions doc/modules/mod_mam.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ Do not add a `<stanza-id/>` element from MAM v0.6.

Name of a module implementing [`is_archivable_message/3` callback](#is_archivable_message) that determines if the message should be archived.

### `modules.mod_mam_meta.send_message`
* **Syntax:** non-empty string
* **Default:** `"mod_mam_utils"`
* **Example:** `send_message = "mod_mam_utils"`

Name of a module implementing `send_message/4` callback that routes a message during lookup operation.
Consult with `mod_mam_utils:send_message/4` code for more information.

### `modules.mod_mam_meta.archive_chat_markers`
* **Syntax:** boolean
* **Default:** `false`
Expand Down
15 changes: 9 additions & 6 deletions src/mam/mod_mam.erl
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@

%% ejabberd
-import(mod_mam_utils,
[send_message/3,
is_jid_in_user_roster/2]).
[is_jid_in_user_roster/2]).


-include("mongoose.hrl").
Expand Down Expand Up @@ -450,12 +449,16 @@ forward_messages(From, ArcJID, MamNs, QueryID, MessageRows, SetClientNs) ->
[_|_] -> {message_row_to_ext_id(hd(MessageRows)),
message_row_to_ext_id(lists:last(MessageRows))}
end,

[send_message(ArcJID, From,
message_row_to_xml(MamNs, M, QueryID, SetClientNs))
|| M <- MessageRows],
Host = ArcJID#jid.lserver,
SendModule = mod_mam_params:send_message_mod(?MODULE, Host),
[send_message(SendModule, Row, ArcJID, From,
message_row_to_xml(MamNs, Row, QueryID, SetClientNs))
|| Row <- MessageRows],
{FirstMessID, LastMessID}.

send_message(SendModule, Row, ArcJID, From, Packet) ->
SendModule:send_message(Row, ArcJID, From, Packet).

-spec handle_get_message_form(jid:jid(), jid:jid(), jlib:iq()) ->
jlib:iq().
handle_get_message_form(_From=#jid{lserver = Host}, _ArcJID=#jid{}, IQ=#iq{}) ->
Expand Down
3 changes: 3 additions & 0 deletions src/mam/mod_mam_meta.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ config_items() ->
<<"no_stanzaid_element">> => #option{type = boolean},
<<"is_archivable_message">> => #option{type = atom,
validate = module},
<<"send_message">> => #option{type = atom,
validate = module},
<<"archive_chat_markers">> => #option{type = boolean},
<<"message_retraction">> => #option{type = boolean},

Expand Down Expand Up @@ -202,6 +204,7 @@ valid_core_mod_opts(mod_mam_muc) ->

common_opts() ->
[is_archivable_message,
send_message,
archive_chat_markers,
extra_lookup_params,
full_text_search,
Expand Down
15 changes: 8 additions & 7 deletions src/mam/mod_mam_muc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@
[mess_id_to_external_binary/1,
is_complete_result_page/4]).

%% ejabberd
-import(mod_mam_utils,
[send_message/3]).


-include_lib("mongoose.hrl").
-include_lib("jlib.hrl").
-include_lib("exml/include/exml.hrl").
Expand Down Expand Up @@ -422,11 +417,17 @@ forward_messages(From, ArcJID, MamNs, QueryID, MessageRows, SetClientNs) ->
message_row_to_ext_id(lists:last(MessageRows)),
is_user_identity_hidden(From, ArcJID)}
end,
[send_message(ArcJID, From, message_row_to_xml(MamNs, From, HideUser, SetClientNs, Row,
QueryID))
{ok, Host} = mongoose_subhosts:get_host(ArcJID#jid.lserver),
SendModule = mod_mam_params:send_message_mod(?MODULE, Host),
[send_message(SendModule, Row, ArcJID, From,
message_row_to_xml(MamNs, From, HideUser, SetClientNs, Row,
QueryID))
|| Row <- MessageRows],
{FirstMessID, LastMessID}.

send_message(SendModule, Row, ArcJID, From, Packet) ->
SendModule:send_message(Row, ArcJID, From, Packet).

-spec handle_get_message_form(jid:jid(), jid:jid(), jlib:iq()) ->
jlib:iq().
handle_get_message_form(_From = #jid{lserver = Host}, _ArcJID = #jid{}, IQ = #iq{}) ->
Expand Down
8 changes: 6 additions & 2 deletions src/mam/mod_mam_params.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
-type mam_module() :: mod_mam | mod_mam_muc.

-export([extra_params_module/2, max_result_limit/2, default_result_limit/2,
has_full_text_search/2, is_archivable_message_fun/2, archive_chat_markers/2,
add_stanzaid_element/2]).
has_full_text_search/2, is_archivable_message_fun/2, send_message_mod/2,
archive_chat_markers/2, add_stanzaid_element/2]).

%%--------------------------------------------------------------------
%% API
Expand Down Expand Up @@ -58,6 +58,10 @@ is_archivable_message_fun(Module, Host) ->
end,
{IsArchivableModule, IsArchivableFunction}.

-spec send_message_mod(mam_module(), Host :: jid:lserver()) -> module().
send_message_mod(Module, Host) ->
param(Module, Host, send_message, mod_mam_utils).

-spec archive_chat_markers(mam_module(), Host :: jid:lserver()) -> boolean().
archive_chat_markers(Module, Host) ->
param(Module, Host, archive_chat_markers, false).
Expand Down
19 changes: 3 additions & 16 deletions src/mam/mod_mam_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
check_for_item_not_found/3]).

%% Ejabberd
-export([send_message/3,
-export([send_message/4,
maybe_set_client_xmlns/2,
is_jid_in_user_roster/2]).

Expand Down Expand Up @@ -1061,23 +1061,10 @@ wait_shaper(Host, Action, From) ->
%% -----------------------------------------------------------------------
%% Ejabberd

-spec send_message(jid:jid(), jid:jid(), exml:element()
) -> mongoose_acc:t().

-ifdef(MAM_COMPACT_FORWARDED).

send_message(_From, To, Mess) ->
From = jid:from_binary(exml_query:attr(Mess, <<"from">>)),
ejabberd_sm:route(From, To, Mess).

-else.

send_message(From, To, Mess) ->
-spec send_message(mod_mam:message_row(), jid:jid(), jid:jid(), exml:element()) -> mongoose_acc:t().
send_message(_Row, From, To, Mess) ->
ejabberd_sm:route(From, To, Mess).

-endif.


-spec is_jid_in_user_roster(jid:jid(), jid:jid()) -> boolean().
is_jid_in_user_roster(#jid{lserver = LServer} = ToJID,
#jid{} = RemJID) ->
Expand Down

0 comments on commit d302457

Please sign in to comment.