BP-REST icon indicating copy to clipboard operation
BP-REST copied to clipboard

Private messages pagination

Open durdenx opened this issue 3 years ago • 10 comments

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.

durdenx avatar Jun 24 '21 09:06 durdenx

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 avatar Jun 24 '21 11:06 renatonascalves

@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

durdenx avatar Jun 24 '21 23:06 durdenx

That's correct! And I see your point. I'll update that.

renatonascalves avatar Jun 25 '21 02:06 renatonascalves

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.

renatonascalves avatar Jul 10 '21 00:07 renatonascalves

Support added! :)

renatonascalves avatar Dec 04 '21 22:12 renatonascalves

@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 avatar Apr 04 '24 14:04 berzki

@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 avatar Apr 04 '24 16:04 renatonascalves

@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 avatar Apr 04 '24 20:04 berzki

@berzki That looks like a bug then. What's your BuddyPress version?

renatonascalves avatar Apr 05 '24 02:04 renatonascalves

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 avatar Apr 05 '24 03:04 berzki

@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 avatar Apr 21 '24 20:04 renatonascalves

@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 avatar Apr 22 '24 03:04 berzki

@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 avatar Apr 22 '24 03:04 renatonascalves

@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.

berzki avatar Apr 22 '24 04:04 berzki

By the above, I mean /messages. The list of all messages.

berzki avatar Apr 22 '24 05:04 berzki

@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

renatonascalves avatar Apr 22 '24 11:04 renatonascalves

Thanks for the insight. Would be very handy to add separately if it's something the team would be interested in? Understandable if not!

berzki avatar Apr 23 '24 01:04 berzki

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.

renatonascalves avatar Apr 25 '24 01:04 renatonascalves