From 67b316a954b161cac27e16b6455837881919dd94 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 16 Dec 2019 05:57:34 +0800 Subject: Refactor comment (#9330) * Refactor comment * fix test * improve code --- modules/notification/action/action.go | 54 +++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'modules/notification/action') diff --git a/modules/notification/action/action.go b/modules/notification/action/action.go index 9caeb5aac0..7441de638a 100644 --- a/modules/notification/action/action.go +++ b/modules/notification/action/action.go @@ -53,6 +53,60 @@ func (a *actionNotifier) NotifyNewIssue(issue *models.Issue) { } } +// NotifyIssueChangeStatus notifies close or reopen issue to notifiers +func (a *actionNotifier) NotifyIssueChangeStatus(doer *models.User, issue *models.Issue, actionComment *models.Comment, closeOrReopen bool) { + // Compose comment action, could be plain comment, close or reopen issue/pull request. + // This object will be used to notify watchers in the end of function. + act := &models.Action{ + ActUserID: doer.ID, + ActUser: doer, + Content: fmt.Sprintf("%d|%s", issue.Index, ""), + RepoID: issue.Repo.ID, + Repo: issue.Repo, + Comment: actionComment, + CommentID: actionComment.ID, + IsPrivate: issue.Repo.IsPrivate, + } + // Check comment type. + if closeOrReopen { + act.OpType = models.ActionCloseIssue + if issue.IsPull { + act.OpType = models.ActionClosePullRequest + } + } else { + act.OpType = models.ActionReopenIssue + if issue.IsPull { + act.OpType = models.ActionReopenPullRequest + } + } + + // Notify watchers for whatever action comes in, ignore if no action type. + if err := models.NotifyWatchers(act); err != nil { + log.Error("NotifyWatchers: %v", err) + } +} + +// NotifyCreateIssueComment notifies comment on an issue to notifiers +func (a *actionNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository, + issue *models.Issue, comment *models.Comment) { + act := &models.Action{ + OpType: models.ActionCommentIssue, + ActUserID: doer.ID, + ActUser: doer, + Content: fmt.Sprintf("%d|%s", issue.Index, comment.Content), + RepoID: issue.Repo.ID, + Repo: issue.Repo, + Comment: comment, + CommentID: comment.ID, + IsPrivate: issue.Repo.IsPrivate, + } + + // Notify watchers for whatever action comes in, ignore if no action type. + if err := models.NotifyWatchers(act); err != nil { + log.Error("NotifyWatchers: %v", err) + } +} + func (a *actionNotifier) NotifyNewPullRequest(pull *models.PullRequest) { if err := pull.LoadIssue(); err != nil { log.Error("pull.LoadIssue: %v", err) -- cgit v1.2.3