Skip to content

Commit d9ef4e4

Browse files
committed
Update comment count correctly when adding/removing comments
1 parent ac4152d commit d9ef4e4

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

models/conversations/comment.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ func CreateComment(ctx context.Context, opts *CreateCommentOptions) (_ *Conversa
242242
PosterID: opts.Doer.ID,
243243
Poster: opts.Doer,
244244
Content: opts.Content,
245+
Conversation: opts.Conversation,
245246
ConversationID: opts.ConversationID,
246247
}
247248
if _, err = e.Insert(comment); err != nil {
@@ -252,6 +253,10 @@ func CreateComment(ctx context.Context, opts *CreateCommentOptions) (_ *Conversa
252253
return nil, err
253254
}
254255

256+
if err = updateCommentInfos(ctx, opts, comment); err != nil {
257+
return nil, err
258+
}
259+
255260
if err = committer.Commit(); err != nil {
256261
return nil, err
257262
}
@@ -377,6 +382,12 @@ func DeleteComment(ctx context.Context, comment *ConversationComment) error {
377382
return err
378383
}
379384

385+
if comment.Type == CommentTypeComment {
386+
if _, err := e.ID(comment.ConversationID).Decr("num_comments").Update(new(Conversation)); err != nil {
387+
return err
388+
}
389+
}
390+
380391
if _, err := e.Table("action").
381392
Where("comment_id = ?", comment.ID).
382393
Update(map[string]any{
@@ -564,3 +575,15 @@ func InsertConversationComments(ctx context.Context, comments []*ConversationCom
564575
}
565576
return committer.Commit()
566577
}
578+
579+
func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment *ConversationComment) (err error) {
580+
// Check comment type.
581+
switch opts.Type {
582+
case CommentTypeComment:
583+
if _, err = db.Exec(ctx, "UPDATE `conversation` SET num_comments=num_comments+1 WHERE id=?", opts.Conversation.ID); err != nil {
584+
return err
585+
}
586+
}
587+
// update the conversation's updated_unix column
588+
return UpdateConversationCols(ctx, opts.Conversation, "updated_unix")
589+
}

0 commit comments

Comments
 (0)