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

add API routes for comment likes #8439

Merged
merged 2 commits into from
Jun 4, 2024

Conversation

jhass
Copy link
Member

@jhass jhass commented Nov 24, 2023

No description provided.

Copy link
Member

@tclaus tclaus left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@SuperTux88 SuperTux88 left a comment

Choose a reason for hiding this comment

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

Looks mostly good, only one little comment for when checking if a comment exists and belongs to the correct post.

Comment on lines 123 to 143
def comment_and_post_validate(post_guid, comment_guid)
if !comment_exists(comment_guid) || !comment_is_for_post(post_guid, comment_guid)
render_error 404, "Comment not found for the given post"
false
else
true
end
end

def comment_exists(comment_guid)
comment = comment_service.find!(comment_guid)
comment ? true : false
rescue ActiveRecord::RecordNotFound
false
end

def comment_is_for_post(post_guid, comment_guid)
comments = comment_service.find_for_post(post_guid)
comment = comments.find {|comment| comment[:guid] == comment_guid }
comment ? true : false
end
Copy link
Member

Choose a reason for hiding this comment

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

The comment_exists is kinda redundant, if the comment doesn't exist, it also doesn't exist "for the post", so only checking comment_is_for_post is enough.

Also in comment_is_for_post you can use .exists? directly on an SQL level, instead of looping through all comments with find.

Like this:

Suggested change
def comment_and_post_validate(post_guid, comment_guid)
if !comment_exists(comment_guid) || !comment_is_for_post(post_guid, comment_guid)
render_error 404, "Comment not found for the given post"
false
else
true
end
end
def comment_exists(comment_guid)
comment = comment_service.find!(comment_guid)
comment ? true : false
rescue ActiveRecord::RecordNotFound
false
end
def comment_is_for_post(post_guid, comment_guid)
comments = comment_service.find_for_post(post_guid)
comment = comments.find {|comment| comment[:guid] == comment_guid }
comment ? true : false
end
def comment_and_post_validate(post_guid, comment_guid)
if comment_is_for_post(post_guid, comment_guid)
true
else
render_error 404, "Comment not found for the given post"
false
end
end
def comment_is_for_post(post_guid, comment_guid)
comments = comment_service.find_for_post(post_guid)
comments.exists?(guid: comment_guid)
end

This way the whole validation is just a single sql-query, instead of 2 queries and looping through all comments in ruby.

It's enough to check if the comment exists on the specified post, if it
doesn't exist at all, that check will also fail.

Also do that check directly on SQL level and just check if the comment
exist instead of looping through all comments.
@SuperTux88 SuperTux88 requested a review from denschub June 4, 2024 22:16
Copy link
Member

@denschub denschub left a comment

Choose a reason for hiding this comment

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

🚢

@SuperTux88 SuperTux88 added this to the 1.0.0 milestone Jun 4, 2024
@SuperTux88 SuperTux88 merged commit 7ec2a68 into diaspora:develop Jun 4, 2024
13 checks passed
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.

None yet

4 participants