|
9 | 9 | "code.gitea.io/gitea/models/db" |
10 | 10 | repo_model "code.gitea.io/gitea/models/repo" |
11 | 11 | user_model "code.gitea.io/gitea/models/user" |
| 12 | + "code.gitea.io/gitea/modules/container" |
12 | 13 | "code.gitea.io/gitea/modules/log" |
13 | 14 | "code.gitea.io/gitea/modules/optional" |
14 | 15 | "code.gitea.io/gitea/modules/structs" |
@@ -525,7 +526,7 @@ func (c *Comment) UpdateAttachments(ctx context.Context, uuids []string) error { |
525 | 526 | return committer.Commit() |
526 | 527 | } |
527 | 528 |
|
528 | | -// HashTag returns unique hash tag for issue. |
| 529 | +// HashTag returns unique hash tag for conversation. |
529 | 530 | func (comment *Comment) HashTag() string { |
530 | 531 | return fmt.Sprintf("comment-%d", comment.ID) |
531 | 532 | } |
@@ -584,3 +585,43 @@ func (c *Comment) ConversationURL(ctx context.Context) string { |
584 | 585 | } |
585 | 586 | return c.Conversation.HTMLURL() |
586 | 587 | } |
| 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