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

Adds comments #42

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Adds comments #42

wants to merge 2 commits into from

Conversation

mattrltrent
Copy link
Member

@mattrltrent mattrltrent commented Mar 29, 2023

New features

  • Adds GET /comments/ for fetching comments from either a post or another comment. Allows you to sort by: newest, top, worst, most controversial, overall best, and most replies.
  • Adds POST /comments/ for creating a comment as either a direct reply to the post, or a reply to another comment.
  • Adds DELETE /comments/{comment_id}/ for deleting a comment. A deleted comment will be rendered with text "[deleted]" to indicate it's been deleted, but also not totally removed from the database so the threaded tree structure is not destroyed.
  • Adds PUT /comments/{comment_id}/vote for voting on a comment.

Old changes/updates

  • Added a new impl to the MaskedObjectId struct to help with threaded comment sorting.
  • Refactored the get_trending_score_time method into its own file, as I'm now using it for comments as well as posts.
  • Split the generic "votes" collection into "comment_votes" and "post_votes" collections. This was because I added votes for comments and thought comment votes and post votes should be stored separately.

Docs

  • Adds openapi.yaml docs for routes: GET /comments/, POST /comments/, DELETE /comments/{comment_id}/, and PUT /comments/{comment_id}/vote.

Notes

  • I'm a little unsure if the number of indices I've added for the comments collection is too many, and will cause a noticeable delay in writes.

Closes #15, #16, #17, and #19.

@mattrltrent mattrltrent added the feature Something to be added or improved upon. label Mar 29, 2023
@mattrltrent mattrltrent requested a review from minitech March 29, 2023 06:19
@mattrltrent mattrltrent self-assigned this Mar 29, 2023
@hn275 hn275 mentioned this pull request Apr 7, 2023
pub id: ObjectId,
pub owner: ObjectId,
pub parent_post: ObjectId,
pub parent_comments: Vec<ObjectId>,
Copy link
Contributor

Choose a reason for hiding this comment

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

What is parent_comments?

Copy link
Contributor

@minitech minitech left a comment

Choose a reason for hiding this comment

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

How does the randomness work? Is that documented somewhere?

@@ -69,6 +73,51 @@ async fn initialize_database(db: &Database) -> mongodb::error::Result<()> {
.build(),
None,
),
// Is this too many indices? Probably...
Copy link
Contributor

Choose a reason for hiding this comment

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

Yep

Suggested change
// Is this too many indices? Probably...

@@ -69,6 +73,51 @@ async fn initialize_database(db: &Database) -> mongodb::error::Result<()> {
.build(),
None,
),
// Is this too many indices? Probably...
comments.create_index(
IndexModel::builder().keys(doc! {"parent_post": -1}).build(),
Copy link
Contributor

Choose a reason for hiding this comment

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

The direction of single-key indexes doesn’t matter, so use 1 for consistency.

Suggested change
IndexModel::builder().keys(doc! {"parent_post": -1}).build(),
IndexModel::builder().keys(doc! {"parent_post": 1}).build(),

Comment on lines +103 to +129
comments.create_index(
IndexModel::builder()
.keys(doc! {"absolute_score": -1})
.build(),
None,
),
Copy link
Contributor

Choose a reason for hiding this comment

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

Only need an index in one direction.

Suggested change
comments.create_index(
IndexModel::builder()
.keys(doc! {"absolute_score": -1})
.build(),
None,
),

),
comments.create_index(
IndexModel::builder()
.keys(doc! {"sequential_id": -1})
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.keys(doc! {"sequential_id": -1})
.keys(doc! {"sequential_id": 1})

}

// If the comment is trying to be nested too deep, send a 400 Bad Request.
if request.parent_comments.len() > conf::COMMENT_MAX_DEPTH {
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn’t be trusting the client to send the correct path. Can we store just the parent id instead of the whole list of ancestor ids? I’m not clear on what that’s used for, other than the depth check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Something to be added or improved upon.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Loading the infinitely-scrollable comment section.
3 participants