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 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package comments
  5. import (
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/models/db"
  8. "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. // CreateIssueComment creates a plain issue comment.
  15. func CreateIssueComment(doer *user_model.User, repo *repo_model.Repository, issue *models.Issue, content string, attachments []string) (*models.Comment, error) {
  16. comment, err := models.CreateComment(&models.CreateCommentOptions{
  17. Type: models.CommentTypeComment,
  18. Doer: doer,
  19. Repo: repo,
  20. Issue: issue,
  21. Content: content,
  22. Attachments: attachments,
  23. })
  24. if err != nil {
  25. return nil, err
  26. }
  27. mentions, err := issue.FindAndUpdateIssueMentions(db.DefaultContext, doer, comment.Content)
  28. if err != nil {
  29. return nil, err
  30. }
  31. notification.NotifyCreateIssueComment(doer, repo, issue, comment, mentions)
  32. return comment, nil
  33. }
  34. // UpdateComment updates information of comment.
  35. func UpdateComment(c *models.Comment, doer *user_model.User, oldContent string) error {
  36. var needsContentHistory = c.Content != oldContent &&
  37. (c.Type == models.CommentTypeComment || c.Type == models.CommentTypeReview || c.Type == models.CommentTypeCode)
  38. if needsContentHistory {
  39. hasContentHistory, err := issues.HasIssueContentHistory(db.DefaultContext, c.IssueID, c.ID)
  40. if err != nil {
  41. return err
  42. }
  43. if !hasContentHistory {
  44. if err = issues.SaveIssueContentHistory(db.GetEngine(db.DefaultContext), c.PosterID, c.IssueID, c.ID,
  45. c.CreatedUnix, oldContent, true); err != nil {
  46. return err
  47. }
  48. }
  49. }
  50. if err := models.UpdateComment(c, doer); err != nil {
  51. return err
  52. }
  53. if needsContentHistory {
  54. err := issues.SaveIssueContentHistory(db.GetEngine(db.DefaultContext), doer.ID, c.IssueID, c.ID, timeutil.TimeStampNow(), c.Content, false)
  55. if err != nil {
  56. return err
  57. }
  58. }
  59. notification.NotifyUpdateComment(doer, c, oldContent)
  60. return nil
  61. }
  62. // DeleteComment deletes the comment
  63. func DeleteComment(doer *user_model.User, comment *models.Comment) error {
  64. if err := models.DeleteComment(comment); err != nil {
  65. return err
  66. }
  67. notification.NotifyDeleteComment(doer, comment)
  68. return nil
  69. }