Skip to content

Commit bea92f6

Browse files
committed
Add insertconversationcomments
1 parent 692f0aa commit bea92f6

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

models/conversations/comment.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"code.gitea.io/gitea/models/db"
1010
repo_model "code.gitea.io/gitea/models/repo"
1111
user_model "code.gitea.io/gitea/models/user"
12+
"code.gitea.io/gitea/modules/container"
1213
"code.gitea.io/gitea/modules/log"
1314
"code.gitea.io/gitea/modules/optional"
1415
"code.gitea.io/gitea/modules/structs"
@@ -525,7 +526,7 @@ func (c *Comment) UpdateAttachments(ctx context.Context, uuids []string) error {
525526
return committer.Commit()
526527
}
527528

528-
// HashTag returns unique hash tag for issue.
529+
// HashTag returns unique hash tag for conversation.
529530
func (comment *Comment) HashTag() string {
530531
return fmt.Sprintf("comment-%d", comment.ID)
531532
}
@@ -584,3 +585,43 @@ func (c *Comment) ConversationURL(ctx context.Context) string {
584585
}
585586
return c.Conversation.HTMLURL()
586587
}
588+
589+
// InsertConversationComments inserts many comments of conversations.
590+
func InsertConversationComments(ctx context.Context, comments []*Comment) error {
591+
if len(comments) == 0 {
592+
return nil
593+
}
594+
595+
conversationIDs := container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
596+
return comment.ConversationID, true
597+
})
598+
599+
ctx, committer, err := db.TxContext(ctx)
600+
if err != nil {
601+
return err
602+
}
603+
defer committer.Close()
604+
for _, comment := range comments {
605+
if _, err := db.GetEngine(ctx).NoAutoTime().Insert(comment); err != nil {
606+
return err
607+
}
608+
609+
for _, reaction := range comment.Reactions {
610+
reaction.ConversationID = comment.ConversationID
611+
reaction.CommentID = comment.ID
612+
}
613+
if len(comment.Reactions) > 0 {
614+
if err := db.Insert(ctx, comment.Reactions); err != nil {
615+
return err
616+
}
617+
}
618+
}
619+
620+
for _, conversationID := range conversationIDs {
621+
if _, err := db.Exec(ctx, "UPDATE conversation set num_comments = (SELECT count(*) FROM comment WHERE conversation_id = ? AND `type`=?) WHERE id = ?",
622+
conversationID, CommentTypeComment, conversationID); err != nil {
623+
return err
624+
}
625+
}
626+
return committer.Commit()
627+
}

0 commit comments

Comments
 (0)