i have requests to fetch comment and post data, but i’m struggling to work out how to build a tree from the responses? do i fetch comments with arg parent_id to get a comment’s children, and do so recursively for those comments returned that themselves have comments?

  • nmtake@lemm.ee
    link
    fedilink
    arrow-up
    2
    ·
    1 year ago

    I’d call GET /api/v3/comment/list?post_id=<post_id>&type_=All to get comments, then sort the comments with path attribute.

    • blawsybogsyOP
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      thanks for the clear info. i think i’d missed the All type_. now off to find the path attribute.

      • RoundSparrow
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        i think i’d missed the All type_.

        I ran into the same problem with the API. If the community isn’t local the API call won’t return any comments (even if local users created those comments). https://lemmy.ml/post/1536549

        Anyway, good you found out type_ param.

      • blawsybogsyOP
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        so “path” is a decimal separated list of comment ids? docs say nothing, but it looks like that.

          • shootwhatsmyname@lemm.ee
            link
            fedilink
            arrow-up
            1
            ·
            1 year ago

            chiming in here because I’m trying to figure this out too. If there’s say, thousands of comments, and I don’t want to get all of them at once, how would I sort them based off the path? I don’t want to miss any replies to comments that are loaded, and it doesn’t appear that comments returned are sorted or grouped together in any way by default

            • Cr4yfish@lemmy.world
              link
              fedilink
              arrow-up
              2
              ·
              11 months ago

              Pretty late to the party here, but I have an answer for you. There another parameter called “depth”, you have to set that to “1” on the initial call. Then you only get “root” comments. Then, for each root comment, you call the API again with the parent id of said comment with the “depth” parameter set to depth+1 until there are no more comments.

              So basically, you have to use recursion. If you want I can give you a code example from my App.

            • blawsybogsyOP
              link
              fedilink
              arrow-up
              2
              ·
              1 year ago

              interesting question. do you mean because if you use limit, and as results are returned unsorted, you might end up with children comments without their parents? have you actually had a go using limit to fetch from your 1000s of comments?

              • shootwhatsmyname@lemm.ee
                link
                fedilink
                arrow-up
                2
                ·
                1 year ago

                Yep exactly, I tested it and I do end up with orphaned comments no matter what I set as “limit” or “sort” (as long as limit is less than the total amount of comments). It’s my guess that fetching a large list of comments would be slower than fetching a few, otherwise I’d just get all the comments in one go.

                • RoundSparrow
                  link
                  fedilink
                  arrow-up
                  1
                  ·
                  1 year ago

                  I assume you are sorting by “New”?

                  Can you share a posting / curl example of this problem of orphan comments that aren’t root?

          • blawsybogsyOP
            link
            fedilink
            arrow-up
            1
            ·
            1 year ago

            thanks a lot for sharing the link. they should put it in the docs for path!