BP-REST
BP-REST copied to clipboard
Private messages pagination
I don't see how to paginate large conversations in a Thread. All messages are included in the thread, and i don't think it will scale, especially in a single thread conversation app.
You can actually paginate a thread: https://developer.buddypress.org/bp-rest-api/reference/private-messaging/#list-messages-threads
Going to close this issue as invalid but feel free to ask any questions.:)
@renatonascalves Thank you for your answer.
I was talking about the messages
field in a specific thread. It’s an array containing all the messages of the thread.
messagesarray | List of message objects for the Thread. Read only Context: view, edit |
---|
That's correct! And I see your point. I'll update that.
It appears that paginating messages from a thread is not part of BuddyPress core. I created this ticket https://buddypress.trac.wordpress.org/ticket/8508 to add this enhancement.
After that's done, I'll apply it here.
Support added! :)
@renatonascalves Any tips on how to use this using Postman? I am trying to retrieve a thread, for example, /wp-json/buddypress/v1/messages/123 and trying to page it as it is a large thread. Are you able to give an example as to how paging works within a thread? I was not able to find any examples in the documentation. Cheers! :)
@berzki Have you looked at the documentation? You can use the messages_page
and messages_per_page
to paginate messages of a large thread.
https://developer.buddypress.org/bp-rest-api/reference/private-messaging/#list-messages-threads
@renatonascalves Yes, but I am getting the whole thread back when doing the following. I am trying to get approx. 50 messages within a thread that has a few thousand entries.
GET /wp-json/buddypress/v1/messages/123
{
"messages_page": "1",
"messages_per_page": "50"
}
@berzki That looks like a bug then. What's your BuddyPress version?
I am using version 12.3.0 of BuddyPress. I can paginate the list of message threads (the one in your link) with no problems, but not when retrieving a specific message thread.
Thanks for your support @renatonascalves
@berzki Confirmed. Apparently, I forgot to add support for pagination when getting a single thread, and its messages.
Could you test this pr please?
@renatonascalves Working fine, cheers. Is it possible to add an order? Like asc and desc. To get the total pages, how do I go about that? X-WP-Total* headers are not being returned in the single thread. I would like to load older messages when the user scrolls up in the chat. What would you recommend here?
@berzki Ops! Good catch. I totally forgot about the pagination headers. Added. Also, it's possible to sort the messages.
Could you test the pull request again?
@renatonascalves Tested updated PR and I can see the headers now. Fantastic! Looking closer at the order, you're right. I can sort the single thread. Is it possible to sort the message list in asc and desc? Asking because it doesn't work for me in case it is possible.
By the above, I mean /messages. The list of all messages.
@berzki I think it does work as designed, maybe not as intended?
All messages are sorted before they are split for pagination.
https://github.com/buddypress/buddypress/blob/5a8f3eace1ab3db572cff6a7ed96e6f0807da239/src/bp-messages/classes/class-bp-messages-thread.php#L413-L422
If you have 1, 2, 3, 4, 5. And if you sort by DESC, with 1 item per page, you will get:
5
4
3
2
1
The sorting is not performed in the final slice of the messages, but in the total amount of messages. We could change this behavior but it might be too disrusptive for folks relying on this behavior.
cc: @imath
Thanks for the insight. Would be very handy to add separately if it's something the team would be interested in? Understandable if not!
I have a few updates to the Messages component regarding pagination. It's possible we might be able to add this later, but not now.