diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2019-09-25 01:39:50 +0800 |
---|---|---|
committer | techknowlogick <techknowlogick@gitea.io> | 2019-09-24 13:39:50 -0400 |
commit | 061388379aae8fa5edc2b41a53512f17adc7a672 (patch) | |
tree | 8f866f552d99374a5c9bb54da0b4f9aa18b59254 /services/comments/comments.go | |
parent | 3dd1cee331b8d0beb84e2f92481183520939213a (diff) | |
download | gitea-061388379aae8fa5edc2b41a53512f17adc7a672.tar.gz gitea-061388379aae8fa5edc2b41a53512f17adc7a672.zip |
Move create issue comment to comments package (#8212)
* move create issue comment to comments package
* extract actions on update/delete comment from models to comment service
* fix lint
* fix lint
Diffstat (limited to 'services/comments/comments.go')
-rw-r--r-- | services/comments/comments.go | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/services/comments/comments.go b/services/comments/comments.go index bd261ff0a5..e8448e9065 100644 --- a/services/comments/comments.go +++ b/services/comments/comments.go @@ -11,10 +11,41 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/services/gitdiff" ) +// CreateIssueComment creates a plain issue comment. +func CreateIssueComment(doer *models.User, repo *models.Repository, issue *models.Issue, content string, attachments []string) (*models.Comment, error) { + comment, err := models.CreateComment(&models.CreateCommentOptions{ + Type: models.CommentTypeComment, + Doer: doer, + Repo: repo, + Issue: issue, + Content: content, + Attachments: attachments, + }) + if err != nil { + return nil, err + } + + mode, _ := models.AccessLevel(doer, repo) + if err = models.PrepareWebhooks(repo, models.HookEventIssueComment, &api.IssueCommentPayload{ + Action: api.HookIssueCommentCreated, + Issue: issue.APIFormat(), + Comment: comment.APIFormat(), + Repository: repo.APIFormat(mode), + Sender: doer.APIFormat(), + }); err != nil { + log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err) + } else { + go models.HookQueue.Add(repo.ID) + } + return comment, nil +} + // CreateCodeComment creates a plain code comment at the specified line / path func CreateCodeComment(doer *models.User, repo *models.Repository, issue *models.Issue, content, treePath string, line, reviewID int64) (*models.Comment, error) { var commitID, patch string @@ -67,3 +98,75 @@ func CreateCodeComment(doer *models.User, repo *models.Repository, issue *models Patch: patch, }) } + +// UpdateComment updates information of comment. +func UpdateComment(c *models.Comment, doer *models.User, oldContent string) error { + if err := models.UpdateComment(c, doer); err != nil { + return err + } + + if err := c.LoadPoster(); err != nil { + return err + } + if err := c.LoadIssue(); err != nil { + return err + } + + if err := c.Issue.LoadAttributes(); err != nil { + return err + } + + mode, _ := models.AccessLevel(doer, c.Issue.Repo) + if err := models.PrepareWebhooks(c.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{ + Action: api.HookIssueCommentEdited, + Issue: c.Issue.APIFormat(), + Comment: c.APIFormat(), + Changes: &api.ChangesPayload{ + Body: &api.ChangesFromPayload{ + From: oldContent, + }, + }, + Repository: c.Issue.Repo.APIFormat(mode), + Sender: doer.APIFormat(), + }); err != nil { + log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err) + } else { + go models.HookQueue.Add(c.Issue.Repo.ID) + } + + return nil +} + +// DeleteComment deletes the comment +func DeleteComment(comment *models.Comment, doer *models.User) error { + if err := models.DeleteComment(comment, doer); err != nil { + return err + } + + if err := comment.LoadPoster(); err != nil { + return err + } + if err := comment.LoadIssue(); err != nil { + return err + } + + if err := comment.Issue.LoadAttributes(); err != nil { + return err + } + + mode, _ := models.AccessLevel(doer, comment.Issue.Repo) + + if err := models.PrepareWebhooks(comment.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{ + Action: api.HookIssueCommentDeleted, + Issue: comment.Issue.APIFormat(), + Comment: comment.APIFormat(), + Repository: comment.Issue.Repo.APIFormat(mode), + Sender: doer.APIFormat(), + }); err != nil { + log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err) + } else { + go models.HookQueue.Add(comment.Issue.Repo.ID) + } + + return nil +} |