There is a NEW endpoint /api/alpha/post/replies. In the past comments/list was used to get lists of comments for a variety of situations, making it hard to debug, have many many parameters and it’s response overly verbose. That endpoint will remain there, unchanged, for compatibility reasons. You can continue using it if this new endpoint is not to your liking.
The new endpoint post/replies
is only for getting replies to a post and as such the response is not a flat list of comments, it is a nested hierarchy thing which mirrors the way the replies are displayed in the UI. It’s parameters are the same as comments/list, as much as possible, including max_depth, limit, post_id, parent_id.
Let’s take this post as an example: https://piefed.social/post/1076697?sort=hot#replies. I chose that one because the conversation has a nice hierarchy to it and yet there are only 25 replies so it’s something we can get our arms around.
The comment structure is the same as before except I added a ‘replies’ attribute on the end which is a list of comments, which in turn have ‘replies’ on them. Only the top-level replies have ‘post’ and ‘community’ as this data would be the same for all child replies and is redundant.
To get all the replies to a post:
https://piefed.social/api/alpha/post/replies?post_id=1076697&sort=Hot
To paginate the replies, while getting full unbroken reply trees, specify a page size using ‘limit’
https://piefed.social/api/alpha/post/replies?post_id=1076697&sort=Hot&limit=3
The above limit is intentionally small so you can see that the result includes the entire first branch. IRL you will want 20 or 50. NB the result includes a ‘next_page’ value:
This is different to the pagination on the rest of the API - it is the ID of the first comment on the next page, rather than a page number. You use it the same tho - you can get the next page of results by doing
https://piefed.social/api/alpha/post/replies?post_id=1076697&sort=Hot&limit=3&page=7228615
The problem with page numbers is that people can create replies in the time between when you got the first page and when you got the second (or later) pages, so replies can end up being in two pages of results. By basing the pagination on the ID of the reply we avoid this.
You can omit post_id and instead provide parent_id to get a sub-branch deep in the discussion. The max_depth filter will be applied relative from parent_id, not the root.
So I had a look at the API doc at https://freamon.github.io/piefed-api/ which is linked at the Piefed Codeberg page, but I could not find the new post/replies endpoint there. Will the doc be updated after some time? I’m heavily relying in the swagger.yaml file to have type safety.
Sorry, things are a bit screwed up at the moment - what you have there is an old manually-updated swagger. It is more complete but riddled with slight errors and is becoming increasingly out of date.
We are changing the API over to automatically generate swagger docs, making them accurate and always up to date. But we’re only part-way through the process. https://piefed.wjs018.xyz/api/alpha/swagger is what we have so far. This is a high priority for us and it will be finished pretty quickly.
Great news !
Thanks, that sounds great! I’ll bookmark the new page.
Thanks, this is exactly what I needed! I’m currently using the comments/list endpoint and transform the flat list into nested comments myself, but I sometimes get errors where there list contains comments that could not be added to the tree since their parents aren’t present.
Hello,
Out of curiosity, what app / frontend are you working on?
I’m using React Native, though only targeting Android at the moment. I don’t have released anything yet and honestly need to come up with a good name as well, but I’m going to post some more info and maybe screenshots to the introductions thread soon.
Sounds cool, thanks!