Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding comment trees. #2362

Merged
merged 18 commits into from
Jul 30, 2022
Merged

Adding comment trees. #2362

merged 18 commits into from
Jul 30, 2022

Conversation

dessalines
Copy link
Member

  • Extracted comment replies into its own table.
  • Added ltree column to comment
  • Added parent_id param to GetComments to fetch a tree branch
  • No paging / limiting yet

@dessalines dessalines marked this pull request as ready for review July 27, 2022 15:55
@dessalines dessalines requested a review from Nutomic as a code owner July 27, 2022 15:55
@dessalines
Copy link
Member Author

dessalines commented Jul 27, 2022

Okay I got this working finally! Demo in the lemmy-ui PR.

Might be easy also to see the API changes here.

This PR:

  • Adds an Ltree path column to the comment table.
    • Queries can now be limited by a given max_depth.
    • A postgres upgrade is unfortunately necessary. I wrote a script for prod docker deploys to run, but I don't know a clean way to upgrade production docker postgres DBs.
  • Comments now come back through GetComments ( and not GetPost ), which has an optional post_id and parent_id, making front-end fetches possible.
  • Moved CommentReply 's to their own table, and they function exactly like PersonMention.
  • Added a specific CommentSortType, distinct from the Post SortType.

Some limitations:

  • It can only limit posts by max_depth, meaning if any level has thousands of comments, we still have issues. Here are some articles talking about how difficult tree paging is. I have an open question about this on Stackoverflow, but it looks like this can't be done through the database, and would require code manipulation of the fetched results.

@@ -139,14 +86,71 @@ where ca.comment_id = c.id",
.get_result::<Self>(conn)
}

pub fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Comment, Error> {
pub fn create(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should still be called upsert as thats what it does. Maybe consider having separate public methods for create/insert, and a private method which does the ltree related work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'd mean that every Comment::create needs to be changed to comment::upsert, since they can and should do the same thing.

.order_by(comment_aggregates::score.desc()),
CommentSortType::New => query.then_order_by(comment::published.desc()),
CommentSortType::Old => query.then_order_by(comment::published.asc()),
CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason to remove these sort options for comments? Should probably mention this in the changelog too once released.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep we can do that in the changelog. Post SortType has things like Active and MostComments which don't make sense for comment sorting, so I made another type.

| SortType::TopWeek
| SortType::TopYear
| SortType::TopMonth => CommentSortType::Top,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use SortType::*

Copy link
Member Author

@dessalines dessalines Jul 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? The SortType is already imported at the top. I also prefer explicit match case handling above the default _, because if we add sorts later, we'll be forced to see them in the compiler.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With that use statement, you can just write TopWeek instead of SortType::TopWeek etc, making the code shorter and easier to read.

Copy link
Member

@Nutomic Nutomic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to merge when you are ready.

@dessalines dessalines enabled auto-merge (squash) July 30, 2022 03:32
@dessalines dessalines merged commit 9c3efe3 into main Jul 30, 2022
@dessalines dessalines changed the title First pass at adding comment trees. Adding comment trees. Aug 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants