Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inbox/extend messages #3910

Merged
merged 2 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion big_tests/tests/inbox_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,7 @@ rpc_stop_hook_handler(HookExtra, HostType) ->
gen_hook:delete_handler(does_user_exist, HostType, fun ?MODULE:hook_handler_fn/3, HookExtra, 1).

hook_handler_fn(Acc,
#{args := [_HostType, User, _Stored]} = _Params,
#{jid := User} = _Params,
#{test_case_pid := Pid} = _Extra) ->
Pid ! {input, User#jid.luser},
{ok, Acc}.
Expand Down
7 changes: 5 additions & 2 deletions include/mod_inbox.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
timestamp := integer(),
muted_until := integer(),
unread_count := integer(),
box := binary()}.
box := binary(),
extra := [exml:element()]}.

-type entry_properties() :: #{muted_until := integer(),
unread_count := integer(),
box := binary()} | inbox_res().
box := binary(),
extra := [exml:element()]}
| inbox_res().
15 changes: 7 additions & 8 deletions src/inbox/mod_inbox.erl
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,10 @@ process_iq(Acc, From, _To, #iq{type = set, sub_el = QueryEl} = IQ, _Extra) ->
{Acc, IQ#iq{type = error, sub_el = [mongoose_xmpp_errors:Error(<<"en">>, Msg)]}};
Params ->
List0 = mod_inbox_backend:get_inbox(HostType, LUser, LServer, Params),
List = with_rsm(List0, Params),
forward_messages(Acc, List, IQ, From),
Res = IQ#iq{type = result, sub_el = [build_result_iq(List)]},
List1 = with_rsm(List0, Params),
List2 = mongoose_hooks:extend_inbox_result(Acc, List1, IQ),
forward_messages(Acc, List2, IQ, From),
Res = IQ#iq{type = result, sub_el = [build_result_iq(List2)]},
{Acc, Res}
end.

Expand Down Expand Up @@ -400,16 +401,14 @@ build_inbox_message(Acc, InboxRes, IQ) ->
children = [build_result_el(Acc, InboxRes, IQ)]}.

-spec build_result_el(mongoose_acc:t(), inbox_res(), jlib:iq()) -> exml:element().
build_result_el(Acc, InboxRes = #{unread_count := Count}, IQ = #iq{id = IqId, sub_el = QueryEl}) ->
build_result_el(Acc, InboxRes = #{unread_count := Count}, #iq{id = IqId, sub_el = QueryEl}) ->
AccTS = mongoose_acc:timestamp(Acc),
Forwarded = mod_inbox_utils:build_forward_el(InboxRes),
Properties = mod_inbox_entries:extensions_result(InboxRes, AccTS),
Extensions = mongoose_hooks:extend_inbox_message(Acc, InboxRes, IQ),
Children = mod_inbox_utils:build_inbox_result_elements(InboxRes, AccTS),
#xmlel{name = <<"result">>,
attrs = [{<<"xmlns">>, ?NS_ESL_INBOX},
{<<"unread">>, integer_to_binary(Count)},
{<<"queryid">>, exml_query:attr(QueryEl, <<"queryid">>, IqId)}],
children = [Forwarded | Properties] ++ Extensions}.
children = Children}.

-spec build_result_iq([inbox_res()]) -> exml:element().
build_result_iq(List) ->
Expand Down
50 changes: 12 additions & 38 deletions src/inbox/mod_inbox_entries.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
% Inbox extensions
-export([process_iq_conversation/5]).
-export([should_be_stored_in_inbox/1]).
-export([extensions_result/2]).

-type entry_query() :: #{box => binary(),
unread_count => 0 | 1,
Expand Down Expand Up @@ -79,11 +78,11 @@ get_properties_for_jid(Acc, IQ, From, EntryJID, QueryType) ->
case fetch_right_query(HostType, InboxEntryKey, QueryType) of
[] -> return_error(Acc, IQ, item_not_found, <<"Entry not found">>);
Result ->
Properties = build_result(Acc, Result, IQ, QueryType),
Children = build_query_children(Acc, Result, IQ, QueryType),
X = [#xmlel{name = <<"query">>,
attrs = [{<<"xmlns">>, ?NS_ESL_INBOX_CONVERSATION},
{<<"jid">>, BinEntryJID}],
children = Properties}],
children = Children}],
{Acc, IQ#iq{type = result, sub_el = X}}
end.

Expand Down Expand Up @@ -122,9 +121,9 @@ process_requests(Acc, IQ, From, EntryJID, Params) ->
-spec forward_result(mongoose_acc:t(), jlib:iq(), jid:jid(), mod_inbox:entry_key(), entry_properties()) ->
{mongoose_acc:t(), jlib:iq()}.
forward_result(Acc, IQ = #iq{id = IqId}, From, {_, _, ToBareJidBin}, Result) ->
Properties = build_result(Acc, Result, IQ, only_properties),
Children = iq_x_wrapper(IQ, ToBareJidBin, Properties),
Msg = #xmlel{name = <<"message">>, attrs = [{<<"id">>, IqId}], children = Children},
Children0 = build_query_children(Acc, Result, IQ, only_properties),
Children1 = iq_x_wrapper(IQ, ToBareJidBin, Children0),
Msg = #xmlel{name = <<"message">>, attrs = [{<<"id">>, IqId}], children = Children1},
Acc1 = ejabberd_router:route(From, jid:to_bare(From), Acc, Msg),
Res = IQ#iq{type = result, sub_el = []},
{Acc1, Res}.
Expand Down Expand Up @@ -157,19 +156,15 @@ process_reset_stanza(Acc, From, IQ, _ResetStanza, InterlocutorJID) ->
%% Helpers
%%--------------------------------------------------------------------

-spec build_result(mongoose_acc:t(), inbox_res() | entry_properties(), jlib:iq(), get_entry_type()) ->
-spec build_query_children(mongoose_acc:t(), inbox_res() | entry_properties(), jlib:iq(), get_entry_type()) ->
[exml:element()].
build_result(Acc, Entry = #{unread_count := UnreadCount}, IQ, QueryType) ->
build_query_children(Acc, Entry0, IQ, full_entry) ->
CurrentTS = mongoose_acc:timestamp(Acc),
maybe_full_entry(Acc, Entry, IQ, QueryType) ++
[ kv_to_el(<<"read">>, mod_inbox_utils:bool_to_binary(0 =:= UnreadCount))
| extensions_result(Entry, CurrentTS)].

maybe_full_entry(Acc, Entry, IQ, full_entry) ->
Extensions = mongoose_hooks:extend_inbox_message(Acc, Entry, IQ),
[ mod_inbox_utils:build_forward_el(Entry) | Extensions];
maybe_full_entry(_, _, _, _) ->
[].
[Entry1] = mongoose_hooks:extend_inbox_result(Acc, [Entry0], IQ),
mod_inbox_utils:build_inbox_result_elements(Entry1, CurrentTS);
build_query_children(Acc, Entry, _IQ, only_properties) ->
CurrentTS = mongoose_acc:timestamp(Acc),
mod_inbox_utils:build_entry_result_elements(Entry, CurrentTS).

-spec return_error(mongoose_acc:t(), jlib:iq(), bad_request | item_not_found, binary()) ->
{mongoose_acc:t(), jlib:iq()}.
Expand Down Expand Up @@ -223,24 +218,3 @@ is_inbox_update(Msg) ->
undefined -> false;
_ -> true
end.

-spec extensions_result(entry_properties(), integer()) -> [exml:element()].
extensions_result(#{box := Box, muted_until := MutedUntil}, AccTS) ->
[ kv_to_el(<<"box">>, Box),
kv_to_el(<<"archive">>, is_archive(Box)),
kv_to_el(<<"mute">>, maybe_muted_until(MutedUntil, AccTS))].

-spec kv_to_el(binary(), binary()) -> exml:element().
kv_to_el(Key, Value) ->
#xmlel{name = Key, children = [#xmlcdata{content = Value}]}.

is_archive(<<"archive">>) -> <<"true">>;
is_archive(_) -> <<"false">>.

-spec maybe_muted_until(integer(), integer()) -> binary().
maybe_muted_until(0, _) -> <<"0">>;
maybe_muted_until(MutedUntil, CurrentTS) ->
case CurrentTS =< MutedUntil of
true -> list_to_binary(calendar:system_time_to_rfc3339(MutedUntil, [{offset, "Z"}, {unit, microsecond}]));
false -> <<"0">>
end.
6 changes: 4 additions & 2 deletions src/inbox/mod_inbox_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -507,15 +507,17 @@ decode_row(HostType, {RemBareJID, MsgId, Box, Content, Timestamp, MutedUntil, Co
msg => Parsed,
timestamp => NumericTimestamp,
muted_until => NumericMutedUntil,
unread_count => BCount}.
unread_count => BCount,
extra => []}.

-spec decode_properties({_, _, _}) -> entry_properties().
decode_properties({Box, BMutedUntil, BCount}) ->
Count = mongoose_rdbms:result_to_integer(BCount),
MutedUntil = mongoose_rdbms:result_to_integer(BMutedUntil),
#{box => Box,
unread_count => Count,
muted_until => MutedUntil}.
muted_until => MutedUntil,
extra => []}.

-spec check_result_is_expected(_, list()) -> mod_inbox:write_res().
check_result_is_expected({updated, Val}, ValList) when is_list(ValList) ->
Expand Down
43 changes: 37 additions & 6 deletions src/inbox/mod_inbox_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
binary_to_bool/1,
bool_to_binary/1,
build_inbox_entry_key/2,
build_forward_el/1,
build_inbox_result_elements/2,
build_entry_result_elements/2,
all_valid_boxes_for_query/1,
list_single_form_field/3,
calculate_ts_from/2
Expand Down Expand Up @@ -236,11 +237,41 @@ build_inbox_entry_key(FromJid, ToJid) ->
ToBareJid = jid:nameprep(jid:to_bare_binary(ToJid)),
{LUser, LServer, ToBareJid}.

-spec build_forward_el(inbox_res()) -> exml:element().
build_forward_el(#{msg := Content, timestamp := Timestamp}) ->
Delay = build_delay_el(Timestamp),
#xmlel{name = <<"forwarded">>, attrs = [{<<"xmlns">>, ?NS_FORWARD}],
children = [Delay, Content]}.
-spec build_inbox_result_elements(inbox_res(), integer()) -> [exml:element()].
build_inbox_result_elements(#{msg := Content, timestamp := Timestamp, unread_count := UnreadCount,
box := Box, muted_until := MutedUntil,
extra := Extra}, AccTS) ->
[ #xmlel{name = <<"forwarded">>, attrs = [{<<"xmlns">>, ?NS_FORWARD}],
children = [build_delay_el(Timestamp), Content]},
kv_to_el(<<"read">>, mod_inbox_utils:bool_to_binary(0 =:= UnreadCount)),
kv_to_el(<<"box">>, Box),
kv_to_el(<<"archive">>, is_archive(Box)),
kv_to_el(<<"mute">>, maybe_muted_until(MutedUntil, AccTS))
| Extra ].

-spec build_entry_result_elements(entry_properties(), integer()) -> [exml:element()].
build_entry_result_elements(#{box := Box, muted_until := MutedUntil,
unread_count := UnreadCount, extra := Extra}, AccTS) ->
[ kv_to_el(<<"read">>, mod_inbox_utils:bool_to_binary(0 =:= UnreadCount)),
kv_to_el(<<"box">>, Box), kv_to_el(<<"archive">>, is_archive(Box)),
kv_to_el(<<"mute">>, maybe_muted_until(MutedUntil, AccTS))
| Extra ].

-spec kv_to_el(binary(), binary()) -> exml:element().
kv_to_el(Key, Value) ->
#xmlel{name = Key, children = [#xmlcdata{content = Value}]}.

-spec is_archive(binary()) -> binary().
is_archive(<<"archive">>) -> <<"true">>;
is_archive(_) -> <<"false">>.

-spec maybe_muted_until(integer(), integer()) -> binary().
maybe_muted_until(0, _) -> <<"0">>;
maybe_muted_until(MutedUntil, CurrentTS) ->
case CurrentTS =< MutedUntil of
true -> list_to_binary(calendar:system_time_to_rfc3339(MutedUntil, [{offset, "Z"}, {unit, microsecond}]));
false -> <<"0">>
end.

-spec build_delay_el(Timestamp :: integer()) -> exml:element().
build_delay_el(Timestamp) ->
Expand Down
13 changes: 6 additions & 7 deletions src/mongoose_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
%%% make sure they pass the expected arguments.
-module(mongoose_hooks).

-include("jlib.hrl").
-include("mod_privacy.hrl").
-include("mongoose.hrl").

Expand All @@ -18,7 +17,7 @@
filter_local_packet/1,
filter_packet/1,
inbox_unread_count/3,
extend_inbox_message/3,
extend_inbox_result/3,
get_key/2,
packet_to_component/3,
presence_probe_hook/5,
Expand Down Expand Up @@ -280,12 +279,12 @@ inbox_unread_count(LServer, Acc, User) ->
Params = #{user => User},
run_hook_for_host_type(inbox_unread_count, LServer, Acc, Params).

-spec extend_inbox_message(mongoose_acc:t(), mod_inbox:inbox_res(), jlib:iq()) ->
[exml:element()].
extend_inbox_message(MongooseAcc, InboxRes, IQ) ->
-spec extend_inbox_result(mongoose_acc:t(), [mod_inbox:inbox_res()], jlib:iq()) ->
[mod_inbox:inbox_res()].
extend_inbox_result(MongooseAcc, InboxResults, IQ) ->
HostType = mongoose_acc:host_type(MongooseAcc),
HookParams = #{mongoose_acc => MongooseAcc, inbox_res => InboxRes, iq => IQ},
run_hook_for_host_type(extend_inbox_message, HostType, [], HookParams).
HookParams = #{mongoose_acc => MongooseAcc, iq => IQ},
run_hook_for_host_type(extend_inbox_result, HostType, InboxResults, HookParams).

%%% @doc The `get_key' hook is called to extract a key from `mod_keystore'.
-spec get_key(HostType, KeyName) -> Result when
Expand Down