Skip to content

Commit

Permalink
Pass caller_jid into lookup_messages hook as a part of params
Browse files Browse the repository at this point in the history
  • Loading branch information
arcusfelis committed Apr 26, 2021
1 parent e08c405 commit ba664d7
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ hooks(Host) ->
[{mam_lookup_messages, Host, ?MODULE, lookup_messages, 60},
{mam_muc_lookup_messages, Host, ?MODULE, lookup_messages, 60}].

%% caller_jid could be used for privacy checking or per-user customization
lookup_messages({error, _Reason} = Result, _Host, _Params) ->
Result;
lookup_messages({ok, {TotalCount, Offset, MessageRows}},
Host, Params = #{owner_jid := ArcJID}) ->
Host, Params = #{owner_jid := ArcJID, caller_jid := _CallerJID}) ->
MessageRows2 = [extend_message(Host, ArcJID, Row) || Row <- MessageRows],
{ok, {TotalCount, Offset, MessageRows2}}.

Expand Down
10 changes: 6 additions & 4 deletions src/mam/mam_iq.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
-export([form_to_with_jid/1]).
-export([form_to_lookup_params/4]).

-export([lookup_params_with_archive_details/3]).
-export([lookup_params_with_archive_details/4]).

-import(mod_mam_utils,
[maybe_microseconds/1,
Expand All @@ -34,6 +34,7 @@
-type lookup_params() :: #{
archive_id => mod_mam:archive_id(),
owner_jid => jid:jid(),
caller_jid => jid:jid(),
rsm => jlib:rsm_in() | undefined,
max_result_limit => non_neg_integer(),
%% Contains page size value provided by client or enforced by server.
Expand Down Expand Up @@ -215,11 +216,12 @@ common_lookup_params(QueryEl, MaxResultLimit, DefaultResultLimit) ->
limit_passed => Limit =/= <<>>,
ordering_direction => ordering_direction(RSM)}.

-spec lookup_params_with_archive_details(lookup_params(), term(), jid:jid()) ->
-spec lookup_params_with_archive_details(lookup_params(), term(), jid:jid(), jid:jid()) ->
lookup_params().
lookup_params_with_archive_details(Params, ArcID, ArcJID) ->
lookup_params_with_archive_details(Params, ArcID, ArcJID, CallerJID) ->
Params#{archive_id => ArcID,
owner_jid => ArcJID}.
owner_jid => ArcJID,
caller_jid => CallerJID}.

ordering_direction(#rsm_in{direction = before}) -> backward;
ordering_direction(_) -> forward.
Expand Down
2 changes: 1 addition & 1 deletion src/mam/mod_mam.erl
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ handle_set_message_form(#jid{} = From, #jid{} = ArcJID,
Params0 = mam_iq:form_to_lookup_params(IQ, mod_mam_params:max_result_limit(?MODULE, Host),
mod_mam_params:default_result_limit(?MODULE, Host),
mod_mam_params:extra_params_module(?MODULE, Host)),
Params = mam_iq:lookup_params_with_archive_details(Params0, ArcID, ArcJID),
Params = mam_iq:lookup_params_with_archive_details(Params0, ArcID, ArcJID, From),
case lookup_messages(Host, Params) of
{error, Reason} ->
report_issue(Reason, mam_lookup_failed, ArcJID, IQ),
Expand Down
2 changes: 1 addition & 1 deletion src/mam/mod_mam_muc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ handle_set_message_form(#jid{} = From, #jid{} = ArcJID, IQ) ->
Params0 = mam_iq:form_to_lookup_params(IQ, mod_mam_params:max_result_limit(?MODULE, Host),
mod_mam_params:default_result_limit(?MODULE, Host),
mod_mam_params:extra_params_module(?MODULE, Host)),
Params = mam_iq:lookup_params_with_archive_details(Params0, ArcID, ArcJID),
Params = mam_iq:lookup_params_with_archive_details(Params0, ArcID, ArcJID, From),
Result = lookup_messages(Host, Params),
handle_lookup_result(Result, From, IQ, Params).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ to_json(Req, #{jid := UserJID, room := Room} = State) ->
R = mod_mam_muc:lookup_messages(Server,
#{archive_id => ArchiveID,
owner_jid => RoomJID,
caller_jid => UserJID,
rsm => RSM,
borders => undefined,
start_ts => undefined,
Expand Down

0 comments on commit ba664d7

Please sign in to comment.