Skip to content

Commit

Permalink
Use map for mod_mam:message_row()
Browse files Browse the repository at this point in the history
  • Loading branch information
arcusfelis committed Apr 26, 2021
1 parent 1436629 commit 60b43a4
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/mam/mam_decoder.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ decode_row({ExtMessID, ExtSrcJID, ExtData}, Env) ->
MessID = mongoose_rdbms:result_to_integer(ExtMessID),
SrcJID = decode_jid(ExtSrcJID, Env),
Packet = decode_packet(ExtData, Env),
{MessID, SrcJID, Packet}.
#{id => MessID, jid => SrcJID, packet => Packet}.

-spec decode_muc_row(db_muc_row(), env_vars()) -> mod_mam:message_row().
decode_muc_row({ExtMessID, Nick, ExtData}, Env = #{archive_jid := RoomJID}) ->
MessID = mongoose_rdbms:result_to_integer(ExtMessID),
SrcJID = jid:replace_resource(RoomJID, Nick),
Packet = decode_packet(ExtData, Env),
{MessID, SrcJID, Packet}.
#{id => MessID, jid => SrcJID, packet => Packet}.

-spec decode_muc_gdpr_row(db_muc_gdpr_row(), env_vars()) -> decoded_muc_gdpr_row().
decode_muc_gdpr_row({ExtMessID, ExtData}, Env) ->
Expand Down
3 changes: 2 additions & 1 deletion src/mam/mam_lookup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ decode_rows(MessageRows, Env) ->
[decode_row(Row, Env) || Row <- MessageRows].

%% First element of the tuple is decoded message ID
decoded_row_to_message_id(DecodedRow) -> element(1, DecodedRow).
-spec decoded_row_to_message_id(mod_mam:message_row()) -> mod_mam:message_id().
decoded_row_to_message_id(#{id := MessId}) -> MessId.

-spec extract_messages(Env :: env_vars(),
Filter :: filter(), Offset :: non_neg_integer(), Max :: pos_integer(),
Expand Down
13 changes: 5 additions & 8 deletions src/mam/mod_mam.erl
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@

-type borders() :: #mam_borders{}.

-type message_row() :: {message_id(), jid:jid(), exml:element()}.
-type message_row() :: #{id => message_id(), jid => jid:jid(), packet => exml:element()}.
-type lookup_result() :: {TotalCount :: non_neg_integer() | undefined,
Offset :: non_neg_integer() | undefined,
MessageRows :: [message_row()]}.
Expand Down Expand Up @@ -609,20 +609,17 @@ archive_message(Host, Params) ->
%% ----------------------------------------------------------------------
%% Helpers

-type messid_jid_packet() :: {MessId :: integer(),
SrcJID :: jid:jid(),
Packet :: exml:element()}.
-spec message_row_to_xml(binary(), messid_jid_packet(), QueryId :: binary(), boolean()) ->
-spec message_row_to_xml(binary(), message_row(), QueryId :: binary(), boolean()) ->
exml:element().
message_row_to_xml(MamNs, {MessID, SrcJID, Packet}, QueryID, SetClientNs) ->
message_row_to_xml(MamNs, #{id := MessID, jid := SrcJID, packet := Packet}, QueryID, SetClientNs) ->
{Microseconds, _NodeMessID} = decode_compact_uuid(MessID),
TS = calendar:system_time_to_rfc3339(erlang:convert_time_unit(Microseconds, microsecond, second), [{offset, "Z"}]),
BExtMessID = mess_id_to_external_binary(MessID),
Packet1 = mod_mam_utils:maybe_set_client_xmlns(SetClientNs, Packet),
wrap_message(MamNs, Packet1, QueryID, BExtMessID, TS, SrcJID).

-spec message_row_to_ext_id(messid_jid_packet()) -> binary().
message_row_to_ext_id({MessID, _, _}) ->
-spec message_row_to_ext_id(message_row()) -> binary().
message_row_to_ext_id(#{id := MessID}) ->
mess_id_to_external_binary(MessID).

handle_error_iq(Host, Acc, _To, _Action, {error, _Reason, IQ}) ->
Expand Down
2 changes: 1 addition & 1 deletion src/mam/mod_mam_cassandra_arch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ rows_to_uniform_format(MessageRows) ->
row_to_uniform_format(#{from_jid := FromJID, message := Msg, id := MsgID}) ->
SrcJID = jid:from_binary(FromJID),
Packet = stored_binary_to_packet(Msg),
{MsgID, SrcJID, Packet}.
#{id => MsgID, jid => SrcJID, packet => Packet}.

row_to_message_id(#{id := MsgID}) ->
MsgID.
Expand Down
5 changes: 2 additions & 3 deletions src/mam/mod_mam_elasticsearch_arch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,13 @@ search_result_to_mam_lookup_result(Result, Params) ->
{CorrectedTotalCount, Offset, Messages}
end.

-spec hit_to_mam_message(map()) -> {mod_mam:message_id(), jid:jid(), exml:element()}.
-spec hit_to_mam_message(map()) -> mod_mam:message_row().
hit_to_mam_message(#{<<"_source">> := JSON}) ->
MessageId = maps:get(<<"mam_id">>, JSON),
Packet = maps:get(<<"message">>, JSON),
SourceBinJid = maps:get(<<"source_jid">>, JSON),

{ok, Stanza} = exml:parse(Packet),
{MessageId, jid:from_binary(SourceBinJid), Stanza}.
#{id => MessageId, jid => jid:from_binary(SourceBinJid), packet => Stanza}.

hit_to_gdpr_mam_message(#{<<"_source">> := JSON}) ->
MessageId = maps:get(<<"mam_id">>, JSON),
Expand Down
8 changes: 4 additions & 4 deletions src/mam/mod_mam_muc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
-type row_batch() :: {TotalCount :: non_neg_integer(),
Offset :: non_neg_integer(),
MessageRows :: [row()]}.
-type row() :: {mod_mam:message_id(), jid:jid(), exml:element()}.
-type row() :: mod_mam:message_row().

-export_type([row/0, row_batch/0]).

Expand Down Expand Up @@ -505,8 +505,8 @@ archive_message(Host, Params) ->

-spec message_row_to_xml(binary(), jid:jid(), boolean(), boolean(), row(), binary() | undefined) ->
exml:element().
message_row_to_xml(MamNs, ReceiverJID, HideUser, SetClientNs, {MessID, SrcJID, Packet}, QueryID) ->

message_row_to_xml(MamNs, ReceiverJID, HideUser, SetClientNs,
#{id := MessID, jid := SrcJID, packet := Packet}, QueryID) ->
{Microseconds, _NodeMessID} = decode_compact_uuid(MessID),
TS = calendar:system_time_to_rfc3339(erlang:convert_time_unit(Microseconds, microsecond, second), [{offset, "Z"}]),
BExtMessID = mess_id_to_external_binary(MessID),
Expand Down Expand Up @@ -536,7 +536,7 @@ replace_from_to_attributes(SrcJID, Packet = #xmlel{attrs = Attrs}) ->
Packet#xmlel{attrs = NewAttrs}.

-spec message_row_to_ext_id(row()) -> binary().
message_row_to_ext_id({MessID, _, _}) ->
message_row_to_ext_id(#{id := MessID}) ->
mess_id_to_external_binary(MessID).

-spec handle_error_iq(mongoose_acc:t(), jid:lserver(), jid:jid(), atom(),
Expand Down
2 changes: 1 addition & 1 deletion src/mam/mod_mam_muc_cassandra_arch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ rows_to_uniform_format(MessageRows, RoomJID) ->
row_to_uniform_format(#{nick_name := BNick, message := Data, id := MessID}, RoomJID) ->
SrcJID = jid:replace_resource(RoomJID, BNick),
Packet = stored_binary_to_packet(Data),
{MessID, SrcJID, Packet}.
#{id => MessID, jid => SrcJID, packet => Packet}.

row_to_message_id(#{id := MsgID}) ->
MsgID.
Expand Down
5 changes: 2 additions & 3 deletions src/mam/mod_mam_muc_elasticsearch_arch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,13 @@ search_result_to_mam_lookup_result(Result, Params) ->
{CorrectedTotalCount, Offset, Messages}
end.

-spec hit_to_mam_message(map()) -> {mod_mam:message_id(), jid:jid(), exml:element()}.
-spec hit_to_mam_message(map()) -> mod_mam:message_row().
hit_to_mam_message(#{<<"_source">> := JSON}) ->
MessageId = maps:get(<<"mam_id">>, JSON),
Packet = maps:get(<<"message">>, JSON),
SourceJid = maps:get(<<"source_jid">>, JSON),

{ok, Stanza} = exml:parse(Packet),
{MessageId, jid:from_binary(SourceJid), Stanza}.
#{id => MessageId, jid => jid:from_binary(SourceJid), packet => Stanza}.

hit_to_gdpr_mam_message(#{<<"_source">> := JSON}) ->
MessageId = maps:get(<<"mam_id">>, JSON),
Expand Down
3 changes: 2 additions & 1 deletion src/mam/mod_mam_rdbms_arch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ get_mam_pm_gdpr_data(Acc, #jid{luser = User, lserver = Host} = ArcJID) ->
[uniform_to_gdpr(row_to_uniform_format(Row, Env)) || Row <- Rows] ++ Acc
end.

uniform_to_gdpr({MessID, RemoteJID, Packet}) ->
-spec uniform_to_gdpr(mod_mam:message_row()) -> tuple().
uniform_to_gdpr(#{id := MessID, jid := RemoteJID, packet := Packet}) ->
{integer_to_binary(MessID), jid:to_binary(RemoteJID), exml:to_binary(Packet)}.

%% ----------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions src/mam/mod_mam_riak_timed_arch_yz.erl
Original file line number Diff line number Diff line change
Expand Up @@ -370,21 +370,21 @@ get_message2(Host, MsgId, Bucket, Key) ->
SourceJID = riakc_map:fetch({<<"source_jid">>, register}, RiakMap),
PacketBin = riakc_map:fetch({<<"packet">>, register}, RiakMap),
Packet = stored_binary_to_packet(Host, PacketBin),
{MsgId, jid:from_binary(SourceJID), Packet};
#{id => MsgId, jid => jid:from_binary(SourceJID), packet => Packet};
_ ->
[]
end.
-spec get_mam_pm_gdpr_data(ejabberd_gen_mam_archive:mam_pm_gdpr_data(), jid:jid()) ->
ejabberd_gen_mam_archive:mam_pm_gdpr_data().
get_mam_pm_gdpr_data(Acc, OwnerJid) ->
Messages = get_mam_gdpr_data(OwnerJid, <<"pm">>),
[{Id, jid:to_binary(Jid), exml:to_binary(Packet)} || {Id, Jid, Packet} <- Messages] ++ Acc.
[{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(), jid:jid()) ->
ejabberd_gen_mam_archive:mam_muc_gdpr_data().
get_mam_muc_gdpr_data(Acc, JID) ->
Messages = get_mam_gdpr_data(JID, <<"muc">>),
[{MsgId, exml:to_binary(Packet)} || {MsgId, _, Packet} <- Messages] ++ Acc.
[{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)),
Expand Down
4 changes: 2 additions & 2 deletions src/mam/mod_mam_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ is_policy_violation(TotalCount, Offset, MaxResultLimit, LimitPassed) ->
check_for_item_not_found(#rsm_in{direction = before, id = ID},
PageSize, {TotalCount, Offset, MessageRows}) ->
case maybe_last(MessageRows) of
{ok, {ID, _, _}} = _IntervalEndpoint ->
{ok, #{id := ID}} = _IntervalEndpoint ->
Page = lists:sublist(MessageRows, PageSize),
{ok, {TotalCount, Offset, Page}};
undefined ->
Expand All @@ -1138,7 +1138,7 @@ check_for_item_not_found(#rsm_in{direction = before, id = ID},
check_for_item_not_found(#rsm_in{direction = aft, id = ID},
_PageSize, {TotalCount, Offset, MessageRows0}) ->
case MessageRows0 of
[{ID, _, _} = _IntervalEndpoint | MessageRows] ->
[#{id := ID} = _IntervalEndpoint | MessageRows] ->
{ok, {TotalCount, Offset, MessageRows}};
_ ->
{error, item_not_found}
Expand Down
5 changes: 3 additions & 2 deletions src/mod_commands.erl
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ get_recent_messages(Caller, With, 0, Limit) ->
get_recent_messages(Caller, With, Future, Limit);
get_recent_messages(Caller, With, Before, Limit) ->
Res = lookup_recent_messages(Caller, With, Before, Limit),
lists:map(fun record_to_map/1, Res).
lists:map(fun row_to_map/1, Res).

change_user_password(Host, User, Password) ->
JID = jid:make(User, Host, <<>>),
Expand All @@ -427,7 +427,8 @@ change_user_password(Host, User, Password) ->
{error, bad_request, "invalid jid"}
end.

record_to_map({Id, From, Msg}) ->
-spec row_to_map(mod_mam:message_row()) -> map().
row_to_map(#{id := Id, jid := From, packet := Msg}) ->
Jbin = jid:to_binary(From),
{Msec, _} = mod_mam_utils:decode_compact_uuid(Id),
MsgId = case xml:get_tag_attr(<<"id">>, Msg) of
Expand Down
2 changes: 1 addition & 1 deletion src/mongoose_client_api/mongoose_client_api_messages.erl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ maybe_to_json_with_jid(WithJID, #jid{lserver = Server} = JID, Req, State) ->
max_result_limit => 50,
is_simple => true}),
{ok, {_, _, Msgs}} = R,
Resp = [make_json_msg(Msg, MAMId) || {MAMId, _, Msg} <- Msgs],
Resp = [make_json_msg(Msg, MAMId) || #{id := MAMId, packet := Msg} <- Msgs],
{jiffy:encode(Resp), Req, State}.

send_message(Req, #{user := RawUser, jid := FromJID} = State) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ encode(Packet, Timestamp) ->
Msg = make_json_item(Packet, FromJID, Timestamp),
Msg#{room => FromJID#jid.luser}.

make_json_item({MAMID, JID, Msg}) ->
-spec make_json_item(mod_mam:message_row()) -> term().
make_json_item(#{id := MAMID, jid := JID, packet := Msg}) ->
{Microsec, _} = mod_mam_utils:decode_compact_uuid(MAMID),
make_json_item(Msg, JID, Microsec div 1000).

Expand Down

0 comments on commit 60b43a4

Please sign in to comment.