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/full pagination xep #3844

Merged
merged 1 commit into from
Nov 9, 2022
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
17 changes: 11 additions & 6 deletions big_tests/tests/inbox_extensions_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ groups() ->
{group, pagination}
]},
{pagination, [parallel], [
bad_id_throws_error,
pagination_error_conditions,
pagination_overrides_form,
can_paginate_forwards,
can_paginate_backwards,
Expand Down Expand Up @@ -673,15 +673,16 @@ properties_many_can_be_set(Config, QueryId) ->
end}], #{box => archive})
end).

bad_id_throws_error(Config) ->
pagination_error_conditions(Config) ->
escalus:fresh_story(Config, [{alice, 1}], fun(Alice) ->
% We set start and end to return Convs with Mike, but using RSM we override that
TS = erlang:system_time(millisecond),
AliceJid = escalus_utils:get_short_jid(Alice),
IdNotDividing = <<(integer_to_binary(TS))/binary, (base64:encode(AliceJid))/binary>>,
verify_returns_error(Alice, #{box => inbox, 'after' => IdNotDividing}),
verify_returns_error(Alice, #{box => inbox, 'after' => IdNotDividing}, <<"bad-request">>),
BadInt = <<(integer_to_binary(-10))/binary, "/", (base64:encode(AliceJid))/binary>>,
verify_returns_error(Alice, #{box => inbox, 'after' => BadInt})
verify_returns_error(Alice, #{box => inbox, 'after' => BadInt}, <<"bad-request">>),
verify_returns_error(Alice, #{box => inbox, index => 10}, <<"feature-not-implemented">>)
end).

pagination_overrides_form(Config) ->
Expand Down Expand Up @@ -971,6 +972,10 @@ muted_status(MutedOrUnmuted, Outer) ->
?assert(Now + Diff < GivenMutedUntil)
end.

verify_returns_error(User, Params) ->
verify_returns_error(User, Params, Error) ->
Stanza = inbox_helper:make_inbox_stanza(Params),
assert_invalid_request(User, Stanza, <<"bad-request">>).
escalus:send(User, Stanza),
[ResIQ] = escalus:wait_for_stanzas(User, 1),
escalus:assert(is_iq_error, [Stanza], ResIQ),
Type = exml_query:path(ResIQ, [{element, <<"error">>}, {element, Error}]),
?assertNotEqual(undefined, Type).
4 changes: 4 additions & 0 deletions big_tests/tests/inbox_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,13 @@ rsm(Params) ->
Max = maps:get(limit, Params, undefined),
Before = maps:get(before, Params, undefined),
After = maps:get('after', Params, undefined),
Index = maps:get(index, Params, undefined),
Elems = [#xmlel{name = <<"max">>,
children = [#xmlcdata{content = to_bin(Max)}]}
|| _ <- [Max], undefined =/= Max ] ++
[#xmlel{name = <<"index">>,
children = [#xmlcdata{content = to_bin(Index)}]}
|| _ <- [Index], undefined =/= Index ] ++
[#xmlel{name = <<"before">>,
children = [#xmlcdata{content = to_bin(Before)}]}
|| _ <- [Before], undefined =/= Before ] ++
Expand Down
2 changes: 1 addition & 1 deletion doc/open-extensions/inbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ It can happen that the amount of inbox entries is too big for a given user, even
```
where `Max` is a non-negative integer.

Inbox also has partial support for pagination as described in [XEP-0059](https://xmpp.org/extensions/xep-0059.html). If specifying `before` or `after`, the `start` and `end` form fields will be overridden.
Inbox also has partial support for pagination as described in [XEP-0059](https://xmpp.org/extensions/xep-0059.html). If specifying `before` or `after`, the `start` and `end` form fields will be overridden. However, inbox pagination does not support total count nor indexes as described in [XEP-0059: #2.6 Retrieving a Page Out of Order](https://xmpp.org/extensions/xep-0059.html#jump).

## Properties of an entry
Given an entry, certain properties are defined for such an entry:
Expand Down
16 changes: 9 additions & 7 deletions src/inbox/mod_inbox.erl
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ process_iq(Acc, From, _To, #iq{type = set, sub_el = QueryEl} = IQ, _Extra) ->
LUser = From#jid.luser,
LServer = From#jid.lserver,
case query_to_params(HostType, QueryEl) of
{error, bad_request, Msg} ->
{Acc, IQ#iq{type = error, sub_el = [mongoose_xmpp_errors:bad_request(<<"en">>, Msg)]}};
{error, Error, Msg} ->
{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),
Expand Down Expand Up @@ -463,18 +463,20 @@ text_single_form_field(Var, DefaultValue) ->
%%%%%%%%%%%%%%%%%%%
%% iq-set
-spec query_to_params(mongooseim:host_type(), QueryEl :: exml:element()) ->
get_inbox_params() | {error, bad_request, binary()}.
get_inbox_params() | {error, atom(), binary()}.
query_to_params(HostType, QueryEl) ->
Form = form_to_params(HostType, exml_query:subelement_with_ns(QueryEl, ?NS_XDATA)),
Rsm = jlib:rsm_decode(QueryEl),
build_params(Form, Rsm).

-spec build_params(get_inbox_params() | {error, bad_request, binary()}, none | jlib:rsm_in()) ->
get_inbox_params() | {error, bad_request, binary()}.
build_params({error, bad_request, Msg}, _) ->
{error, bad_request, Msg};
-spec build_params(get_inbox_params() | {error, atom(), binary()}, none | jlib:rsm_in()) ->
get_inbox_params() | {error, atom(), binary()}.
build_params({error, Error, Msg}, _) ->
{error, Error, Msg};
build_params(_, #rsm_in{max = Max, index = Index}) when Max =:= error; Index =:= error ->
{error, bad_request, <<"bad-request">>};
build_params(_, #rsm_in{index = Index}) when Index =/= undefined ->
{error, feature_not_implemented, <<"Inbox does not expose a total count and indexes">>};
build_params(Params, none) ->
Params;
build_params(Params, #rsm_in{max = Max, id = undefined}) when Max =/= undefined ->
Expand Down