RedditSharp-DEPRECATED- icon indicating copy to clipboard operation
RedditSharp-DEPRECATED- copied to clipboard

Use "More" to "Continue this thread"

Open Slord6 opened this issue 5 years ago • 6 comments

Hi, I'm struggling to iterate down a comment tree. Based on what I've seen of the RedditSharp code and the Reddit API, I think I should get back a More object on comments that on Reddit show with a Continue this thread link. I'm not seeing this and I'm not sure if it's not implemented or I'm doing something wrong...

The post I'm looking at specifically in trying to solve this is this one, specifically this comment thread. And my current implementation looks something like this, where I've retrieved the above linked post, and I'm traversing down the above comment thread.

Post post = GetPost(xyz);
List<Comment> authorComments = GetAllComments(post); // Gets top level comments by the post author
while (authorComments.Count > 0)
            {
                // Grab highest voted comment
                Comment topComment = authorComments.OrderByDescending(comment => comment.Upvotes).First();
               // Add contents to thread text
                threadContents += Environment.NewLine + Environment.NewLine + topComment.Body;
                
                if (topComment.More != null)
                {
                    Console.WriteLine("MORE!"); // This is never hit
                    // Go fetch the 'more' child comments (here lies the problem) + assign to authorComments
                } else {
                    // Otherwise, get the child comments by the same author
                    authorComments = topComment.Comments.Where(authorCommentsFilter).ToList();
                }
            }

Slord6 avatar Nov 27 '19 12:11 Slord6

Whats the implementation of GetAllComments?

If you call EnumerateCommentTreeAsync on the Post this will give you an enumerator that does all the "more" parsing for you if that solves your problem.

CrustyJew avatar Dec 10 '19 05:12 CrustyJew

Hi, I'm on mobile so apologies in advance for dodgy formatting.

GetAllComments look like:

private static List<Comment> GetAllComments(Post post, CommentSort sorting)
{
     Task<List<Thing>> getCommentsTask = post.GetCommentsWithMoresAsync(0, sorting);
     getCommentsTask.Wait();
     List<Comment> comments = ResolveToComments(getCommentsTask.Result);
     getCommentsTask.Dispose();

    return comments;
}

private static List<Comment> ResolveToComments(List<Thing> things)
{
        List<Comment> comments = new List<Comment>();
        foreach (Thing item in things)
        {
            if (item is Comment)
            {
                comments.Add((Comment)item);
            }
            else if(item is More)
            {
                More more = (More)item;
                Task<List<Thing>> getThingsTask = more.GetThingsAsync();
                getThingsTask.Wait();
                List<Thing> moreItems = getThingsTask.Result;
                getThingsTask.Dispose();
                comments.AddRange(ResolveToComments(moreItems));
            }
            else
            {
                Console.WriteLine("Could not resolve thing to comment - " + item.ToString() + " - " + item.GetType().ToString());
            }
        }
        return comments;
}

Call signature may not match as I simplified the original code I posted a bit for clarity. Off the top of my head the else if(item is More) block above does resolve the Mores it sees, which are top-level expansions for more comments on the Post, but I don't see More objects in specific comment threads. I believed I tried that, with similar results but will try and have another look this week.

Slord6 avatar Dec 10 '19 10:12 Slord6

Ok, think I've tracked down the issue.

The initial issue I raised I think was a problem in my code, not the lib. However, there is a closely related issue that has been mentioned in a couple of places elsewhere. The second thread there link through to the praw solution.

But to summarise - on some deeply nested comments, like those I linked in the original issue, the "Continue this thread" object looks like this:

{{
  "count": 0,
  "name": "t1__",
  "id": "_",
  "parent_id": "t1_cydnx3d",	
  "depth": 10,
  "children": []
}}

Which obviously isn't particularly useful. The solution to get the perma-link of the parent to get an updated context returns a 403 when calling like so:

string urlFormat = "https://www.reddit.com{0}";
Uri permaLink = new Uri(String.Format(urlFormat, ((Comment)parent).Permalink));
Task<Comment> getPermaTask = Reddit.GetCommentAsync(permaLink);
getPermaTask.Wait();
parent = getPermaTask.Result;

So the called endpoint ends up being https://www.reddit.com/r/HFY/comments/3yi82b/oc_prey/cydnx3d/.json which resolves fine in the browser, nut not through RedditSharp?

I also tried adding depth support to a couple of endpoints in the library but that seems to have no impact either. Not sure what else to try - any thoughts?

Slord6 avatar Dec 18 '19 19:12 Slord6

~~Checked into this a little. It looks like more objects isn't including the children. I believe that when this was implemented that array was populated and the morechildren endpoint would return the comments. If there aren't ids in the children array it won't work anymore and EnumerateCommentTreeAsync probably won't either~~

Actually was thinking of the EnumerateComments method in Post on master. The test for EnumerateCommentTreeAsync still passes.

czf avatar Feb 07 '20 06:02 czf

@czf your changes didn't fix it, but a fresh look at your version and looking at Praw and SnooWrap meant I figured it out - thanks!

@CrustyJew I forked cfz's fork, so up to you if you merge both PRs or not, don't suppose it makes a difference? 🤷‍♂

Slord6 avatar Feb 17 '20 18:02 Slord6

@czf thanks for the help on this one.

CrustyJew avatar Feb 17 '20 22:02 CrustyJew