Pagination de l'API des derniers messages

Bonjour à tous !

Je récupère les derniers messages via l’API en utilisant ce point de terminaison : https://community.learnaboutgmp.com/posts.json

Comment puis-je obtenir plus de résultats pour les derniers messages ? Est-ce possible ?

J’ai essayé https://community.learnaboutgmp.com/posts.json?page=2 mais cela ne fonctionne pas.

Merci d’avance !

When I figured that out before I followed the instructions in the “reverse engineering” topic. I thought that I’d done it in GitHub - pfaffman/discourse-downloader: Download a Discourse topic for offline analysis, but I don’t see it there.

You might have to hit /latest.json?page=2 instead of /posts.json and filter out the posts from the result.

https://meta.discourse.org/latest.json?page=2

EDIT: Ohh you want latest posts, not latest topics. Considering /posts by itself doesn’t have a UI, it probably is missing pagination, I’m sure a PR could be created for this. But to get what you want right now you could still parse the /latest.json endpoint and parse out the topic_id and the post number and fetch the post with /t/<topic_id>/<post_number>, but yea I can see why pagination on the /posts endpoint would be helpful.

Okay, now that I’ve looked at the controller for /posts:

def latest
  params.permit(:before)
  ...

there is a “before” parameter, not a “page” parameter, but using it like a “page” is a bit different because it filters out private posts and such. But I think roughly you can use it like this:

  • hit /posts.json and get the first post id (currently it is 22000)
  • subtract 50 (that endpoint only shows at most 50 at a time)
  • hit /posts.json?before=21950
  • repeat

Part of the issue which doesn’t make total sense to me is that we are limiting the sql query to 50 results, but then we do more filtering on it after the initial sql query which is why you will not always get 50 results.

Thanks guys! I’ll play with it.

as the API designer: it works this way to make things easier on the Discourse server; if the server had to do a bunch of work to always fill up 50 posts anything using the API might start being a perf problem.

Basically it forces the consumers to be “good neighbors”, in a weird sense of the term.

Question sur l’utilisation de l’API (que j’utilise via le gem discourse_api, génial !) – elle concerne la pagination (je voudrais aussi utiliser le point de terminaison des derniers messages), mais pour mon exemple actuel ci-dessous, je vais fournir quelque chose relatif aux utilisateurs, puisque j’ai commencé à travailler là-dessus en premier.

Je souhaite obtenir toute l’activité d’un utilisateur (les sujets et les réponses conviennent), alors j’essaie ceci :

vsoch = client.user_topics_and_replies("vsoch")

Et je reçois systématiquement une liste de 30 éléments, même si l’utilisateur en a visiblement plus. J’ai essayé de modifier le code source pour ajouter une variable de page :

# frozen_string_literal: true
module DiscourseApi
  module API
    module UserActions
      def user_replies(username, page=1)
        params = { "username": username, "filter": '5', page: page }
        response = get("/user_actions.json", params)
        response.body["user_actions"]
      end

      def user_topics_and_replies(username, page=1)
        params = { "username": username, "filter": "4,5", page: page }
        response = get("/user_actions.json", params)
        response.body["user_actions"]
      end
    end
  end
end

mais cela ne semble rien changer – c’est toujours la même liste de 30 éléments, quelle que soit la valeur du numéro de page. Est-il possible d’obtenir plus de 30 éléments ? Est-ce que j’essaie la mauvaise approche ? Toute aide serait grandement appréciée – si je parviens à comprendre comment faire, je serai ravi de soumettre une PR pour que d’autres puissent en profiter aussi :slight_smile: