25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-20 16:15:59 +01:00

Return proper index when using after of before in PubSub with RSM (#3618)

This fixes Index attribute in examples from:
https://xmpp.org/extensions/xep-0059.html#forwards
https://xmpp.org/extensions/xep-0059.html#backwards
This commit is contained in:
Badlop 2021-06-08 16:47:26 +02:00
parent 41fd2afeb3
commit bf8b4acf01

View File

@ -761,18 +761,14 @@ get_items(Nidx, _From, #rsm_set{max = Max, index = IncIndex,
{Count, lists:sublist(SubList, Limit)};
{_, Stamp, undefined} ->
BeforeNow = encode_stamp(Stamp),
SubList = lists:dropwhile(
fun(#pubsub_item{creation = {Now, _}}) ->
Now >= BeforeNow
end, lists:reverse(RItems)),
{0, lists:sublist(SubList, Limit)};
{NewIndex, SubList} = extract_sublist(before_now, BeforeNow,
0, lists:reverse(RItems)),
{Count-NewIndex-1, lists:sublist(SubList, Limit)};
{_, undefined, Stamp} ->
AfterNow = encode_stamp(Stamp),
SubList = lists:dropwhile(
fun(#pubsub_item{creation = {Now, _}}) ->
Now =< AfterNow
end, RItems),
{0, lists:sublist(SubList, Limit)}
{NewIndex, SubList} = extract_sublist(after_now, AfterNow,
0, RItems),
{NewIndex, lists:sublist(SubList, Limit)}
end,
Rsm = rsm_page(Count, IncIndex, Offset, ItemsPage),
{result, {ItemsPage, Rsm}}
@ -814,6 +810,13 @@ get_items(Nidx, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId, RSM
get_items(Nidx, JID, RSM)
end.
extract_sublist(A, Now, Index, [#pubsub_item{creation = {Creation, _}} | RItems])
when ((A == before_now) and (Creation >= Now))
or ((A == after_now) and (Creation =< Now)) ->
extract_sublist(A, Now, Index+1, RItems);
extract_sublist(_, _, Index, RItems) ->
{Index, RItems}.
get_only_item(Nidx, From) ->
get_last_items(Nidx, From, 1).