diff options
author | Badlop <badlop@process-one.net> | 2021-06-08 16:47:26 +0200 |
---|---|---|
committer | Badlop <badlop@process-one.net> | 2021-06-08 19:00:44 +0200 |
commit | bf8b4acf0150f12cfc3b9b92c2ae38aeadce6275 (patch) | |
tree | 95b505f4e57bfe441ade5b241003ff8ffee215e9 | |
parent | Fix index attribute when getting last page from PubSub with RSM (#3618) (diff) |
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
-rw-r--r-- | src/node_flat.erl | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/src/node_flat.erl b/src/node_flat.erl index cee5bf346..5c34f5fdb 100644 --- a/src/node_flat.erl +++ b/src/node_flat.erl @@ -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). |