You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

comments.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package issue
  4. import (
  5. "context"
  6. "fmt"
  7. "code.gitea.io/gitea/models/db"
  8. issues_model "code.gitea.io/gitea/models/issues"
  9. repo_model "code.gitea.io/gitea/models/repo"
  10. user_model "code.gitea.io/gitea/models/user"
  11. "code.gitea.io/gitea/modules/notification"
  12. "code.gitea.io/gitea/modules/timeutil"
  13. )
  14. // CreateComment creates comment of issue or commit.
  15. func CreateComment(opts *issues_model.CreateCommentOptions) (comment *issues_model.Comment, err error) {
  16. ctx, committer, err := db.TxContext(db.DefaultContext)
  17. if err != nil {
  18. return nil, err
  19. }
  20. defer committer.Close()
  21. comment, err = issues_model.CreateComment(ctx, opts)
  22. if err != nil {
  23. return nil, err
  24. }
  25. if err = committer.Commit(); err != nil {
  26. return nil, err
  27. }
  28. return comment, nil
  29. }
  30. // CreateRefComment creates a commit reference comment to issue.
  31. func CreateRefComment(doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, content, commitSHA string) error {
  32. if len(commitSHA) == 0 {
  33. return fmt.Errorf("cannot create reference with empty commit SHA")
  34. }
  35. // Check if same reference from same commit has already existed.
  36. has, err := db.GetEngine(db.DefaultContext).Get(&issues_model.Comment{
  37. Type: issues_model.CommentTypeCommitRef,
  38. IssueID: issue.ID,
  39. CommitSHA: commitSHA,
  40. })
  41. if err != nil {
  42. return fmt.Errorf("check reference comment: %w", err)
  43. } else if has {
  44. return nil
  45. }
  46. _, err = CreateComment(&issues_model.CreateCommentOptions{
  47. Type: issues_model.CommentTypeCommitRef,
  48. Doer: doer,
  49. Repo: repo,
  50. Issue: issue,
  51. CommitSHA: commitSHA,
  52. Content: content,
  53. })
  54. return err
  55. }
  56. // CreateIssueComment creates a plain issue comment.
  57. func CreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, content string, attachments []string) (*issues_model.Comment, error) {
  58. comment, err := CreateComment(&issues_model.CreateCommentOptions{
  59. Type: issues_model.CommentTypeComment,
  60. Doer: doer,
  61. Repo: repo,
  62. Issue: issue,
  63. Content: content,
  64. Attachments: attachments,
  65. })
  66. if err != nil {
  67. return nil, err
  68. }
  69. mentions, err := issues_model.FindAndUpdateIssueMentions(ctx, issue, doer, comment.Content)
  70. if err != nil {
  71. return nil, err
  72. }
  73. notification.NotifyCreateIssueComment(ctx, doer, repo, issue, comment, mentions)
  74. return comment, nil
  75. }
  76. // UpdateComment updates information of comment.
  77. func UpdateComment(ctx context.Context, c *issues_model.Comment, doer *user_model.User, oldContent string) error {
  78. needsContentHistory := c.Content != oldContent && c.Type.HasContentSupport()
  79. if needsContentHistory {
  80. hasContentHistory, err := issues_model.HasIssueContentHistory(ctx, c.IssueID, c.ID)
  81. if err != nil {
  82. return err
  83. }
  84. if !hasContentHistory {
  85. if err = issues_model.SaveIssueContentHistory(ctx, c.PosterID, c.IssueID, c.ID,
  86. c.CreatedUnix, oldContent, true); err != nil {
  87. return err
  88. }
  89. }
  90. }
  91. if err := issues_model.UpdateComment(c, doer); err != nil {
  92. return err
  93. }
  94. if needsContentHistory {
  95. err := issues_model.SaveIssueContentHistory(ctx, doer.ID, c.IssueID, c.ID, timeutil.TimeStampNow(), c.Content, false)
  96. if err != nil {
  97. return err
  98. }
  99. }
  100. notification.NotifyUpdateComment(ctx, doer, c, oldContent)
  101. return nil
  102. }
  103. // DeleteComment deletes the comment
  104. func DeleteComment(ctx context.Context, doer *user_model.User, comment *issues_model.Comment) error {
  105. err := db.WithTx(ctx, func(ctx context.Context) error {
  106. return issues_model.DeleteComment(ctx, comment)
  107. })
  108. if err != nil {
  109. return err
  110. }
  111. notification.NotifyDeleteComment(ctx, doer, comment)
  112. return nil
  113. }