Skip to content

Commit

Permalink
[C-5189] Fix react and reply comment count (#10075)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacsolo authored Oct 16, 2024
1 parent 2f3b7ab commit f423dd0
Showing 1 changed file with 72 additions and 44 deletions.
116 changes: 72 additions & 44 deletions packages/discovery-provider/src/queries/get_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,6 @@ def get_track_comments(args, track_id, current_user_id=None):
offset, limit = format_offset(args), format_limit(
args, default_limit=COMMENT_THREADS_LIMIT
)
# default to top sort
sort_method = args.get("sort_method", "top")
if sort_method == "top":
sort_method_order_by = desc(func.count(CommentReaction.comment_id))
elif sort_method == "newest":
sort_method_order_by = desc(Comment.created_at)
elif sort_method == "timestamp":
sort_method_order_by = asc(Comment.track_timestamp_s).nullslast()

track_comments = []
db = get_db_read_replica()
Expand All @@ -134,15 +126,37 @@ def get_track_comments(args, track_id, current_user_id=None):
ReplyCountAlias = aliased(CommentThread)

with db.scoped_session() as session:
react_count_subquery = (
session.query(
CommentReaction.comment_id,
func.count(CommentReaction.comment_id).label("react_count"),
)
.filter(CommentReaction.is_delete == False)
.group_by(CommentReaction.comment_id)
.subquery()
)

# default to top sort
sort_method = args.get("sort_method", "top")
if sort_method == "top":
sort_method_order_by = desc(
func.coalesce(react_count_subquery.c.react_count, 0)
) # Ordering by reaction count in descending order
elif sort_method == "newest":
sort_method_order_by = desc(Comment.created_at)
elif sort_method == "timestamp":
sort_method_order_by = asc(Comment.track_timestamp_s).nullslast()

track = session.query(Track).filter(Track.track_id == track_id).first()
artist_id = track.owner_id
pinned_comment_id = track.pinned_comment_id

track_comments = (
session.query(
Comment,
func.count(CommentReaction.comment_id).label("react_count"),
func.count(ReplyCountAlias.comment_id).label("reply_count"),
func.coalesce(react_count_subquery.c.react_count, 0).label(
"react_count"
),
CommentNotificationSetting.is_muted,
)
.outerjoin(
Expand All @@ -158,7 +172,8 @@ def get_track_comments(args, track_id, current_user_id=None):
AggregateUser.user_id == CommentReport.user_id,
)
.outerjoin(
CommentReaction, Comment.comment_id == CommentReaction.comment_id
react_count_subquery,
Comment.comment_id == react_count_subquery.c.comment_id,
)
.outerjoin(
MutedUser,
Expand All @@ -176,7 +191,11 @@ def get_track_comments(args, track_id, current_user_id=None):
(Comment.comment_id == CommentNotificationSetting.entity_id)
& (CommentNotificationSetting.entity_type == "Comment"),
)
.group_by(Comment.comment_id, CommentNotificationSetting.is_muted)
.group_by(
Comment.comment_id,
react_count_subquery.c.react_count,
CommentNotificationSetting.is_muted,
)
.filter(
Comment.entity_id == track_id,
Comment.entity_type == "Track",
Expand Down Expand Up @@ -215,40 +234,49 @@ def get_track_comments(args, track_id, current_user_id=None):
)
.offset(offset)
.limit(limit)
.all()
)

return [
{
"id": encode_int_id(track_comment.comment_id),
"user_id": (
encode_int_id(track_comment.user_id)
if not track_comment.is_delete
else None
),
"message": (
track_comment.text if not track_comment.is_delete else "[Removed]"
),
"is_edited": track_comment.is_edited,
"track_timestamp_s": track_comment.track_timestamp_s,
"react_count": react_count,
"reply_count": reply_count,
"is_current_user_reacted": get_is_reacted(
session, current_user_id, track_comment.comment_id
),
"is_artist_reacted": get_is_reacted(
session, artist_id, track_comment.comment_id
),
"replies": get_replies(
session, track_comment.comment_id, current_user_id, artist_id
),
"created_at": str(track_comment.created_at),
"updated_at": str(track_comment.updated_at),
"is_tombstone": track_comment.is_delete and reply_count > 0,
"is_muted": is_muted if is_muted is not None else False,
}
for [track_comment, react_count, reply_count, is_muted] in track_comments
]
track_comment_res = []
for [track_comment, react_count, is_muted] in track_comments.all():
replies = get_replies(
session,
track_comment.comment_id,
current_user_id,
artist_id,
limit=None,
)
reply_count = len(replies)
track_comment_res.append(
{
"id": encode_int_id(track_comment.comment_id),
"user_id": (
encode_int_id(track_comment.user_id)
if not track_comment.is_delete
else None
),
"message": (
track_comment.text
if not track_comment.is_delete
else "[Removed]"
),
"is_edited": track_comment.is_edited,
"track_timestamp_s": track_comment.track_timestamp_s,
"react_count": react_count,
"reply_count": reply_count,
"is_current_user_reacted": get_is_reacted(
session, current_user_id, track_comment.comment_id
),
"is_artist_reacted": get_is_reacted(
session, artist_id, track_comment.comment_id
),
"replies": replies[:3],
"created_at": str(track_comment.created_at),
"updated_at": str(track_comment.updated_at),
"is_tombstone": track_comment.is_delete and reply_count > 0,
"is_muted": is_muted if is_muted is not None else False,
}
)
return track_comment_res


def get_muted_users(current_user_id):
Expand Down

0 comments on commit f423dd0

Please sign in to comment.