From 82e08a3364195b515a005180c2bdc08e78aac208 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 17 Jan 2019 22:23:22 +0800 Subject: [PATCH] Refactor notification for indexer (#5111) * notification for indexer * use NullNotifier as parent struct --- models/issue.go | 3 -- models/issue_comment.go | 7 --- models/issue_indexer.go | 4 +- models/pull.go | 2 - modules/notification/indexer/indexer.go | 62 +++++++++++++++++++++++++ modules/notification/notification.go | 2 + routers/api/v1/repo/issue_comment.go | 6 +++ routers/repo/issue.go | 4 ++ 8 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 modules/notification/indexer/indexer.go diff --git a/models/issue.go b/models/issue.go index 4f03ed926e..baca512ab0 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1112,8 +1112,6 @@ func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, assigneeIDs []in return fmt.Errorf("Commit: %v", err) } - UpdateIssueIndexer(issue.ID) - if err = NotifyWatchers(&Action{ ActUserID: issue.Poster.ID, ActUser: issue.Poster, @@ -1652,7 +1650,6 @@ func updateIssue(e Engine, issue *Issue) error { if err != nil { return err } - UpdateIssueIndexer(issue.ID) return nil } diff --git a/models/issue_comment.go b/models/issue_comment.go index 0e40e442b6..6c87650a3c 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -818,9 +818,6 @@ func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) { return nil, err } - if opts.Type == CommentTypeComment { - UpdateIssueIndexer(opts.Issue.ID) - } return comment, nil } @@ -1022,8 +1019,6 @@ func GetCommentsByRepoIDSince(repoID, since int64) ([]*Comment, error) { func UpdateComment(doer *User, c *Comment, oldContent string) error { if _, err := x.ID(c.ID).AllCols().Update(c); err != nil { return err - } else if c.Type == CommentTypeComment { - UpdateIssueIndexer(c.IssueID) } if err := c.LoadPoster(); err != nil { @@ -1082,8 +1077,6 @@ func DeleteComment(doer *User, comment *Comment) error { if err := sess.Commit(); err != nil { return err - } else if comment.Type == CommentTypeComment { - UpdateIssueIndexer(comment.IssueID) } if err := comment.LoadPoster(); err != nil { diff --git a/models/issue_indexer.go b/models/issue_indexer.go index b94ba5f2df..48c0b9f246 100644 --- a/models/issue_indexer.go +++ b/models/issue_indexer.go @@ -117,9 +117,7 @@ func updateNeededCols(cols []string) bool { // UpdateIssueIndexerCols update an issue in the issue indexer, given changes // to the specified columns func UpdateIssueIndexerCols(issueID int64, cols ...string) { - if updateNeededCols(cols) { - UpdateIssueIndexer(issueID) - } + updateNeededCols(cols) } // UpdateIssueIndexer add/update an issue to the issue indexer diff --git a/models/pull.go b/models/pull.go index e1a8b14e45..8b4961d8b6 100644 --- a/models/pull.go +++ b/models/pull.go @@ -833,8 +833,6 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str return fmt.Errorf("Commit: %v", err) } - UpdateIssueIndexer(pull.ID) - if err = NotifyWatchers(&Action{ ActUserID: pull.Poster.ID, ActUser: pull.Poster, diff --git a/modules/notification/indexer/indexer.go b/modules/notification/indexer/indexer.go new file mode 100644 index 0000000000..3fd3352188 --- /dev/null +++ b/modules/notification/indexer/indexer.go @@ -0,0 +1,62 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package indexer + +import ( + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/notification/base" +) + +type indexerNotifier struct { + base.NullNotifier +} + +var ( + _ base.Notifier = &indexerNotifier{} +) + +// NewNotifier create a new indexerNotifier notifier +func NewNotifier() base.Notifier { + return &indexerNotifier{} +} + +func (r *indexerNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository, + issue *models.Issue, comment *models.Comment) { + if comment.Type == models.CommentTypeComment { + models.UpdateIssueIndexer(issue.ID) + } +} + +func (r *indexerNotifier) NotifyNewIssue(issue *models.Issue) { + models.UpdateIssueIndexer(issue.ID) +} + +func (r *indexerNotifier) NotifyNewPullRequest(pr *models.PullRequest) { + models.UpdateIssueIndexer(pr.Issue.ID) +} + +func (r *indexerNotifier) NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) { + if c.Type == models.CommentTypeComment { + models.UpdateIssueIndexer(c.IssueID) + } +} + +func (r *indexerNotifier) NotifyDeleteComment(doer *models.User, comment *models.Comment) { + if comment.Type == models.CommentTypeComment { + models.UpdateIssueIndexer(comment.IssueID) + } +} + +func (r *indexerNotifier) NotifyDeleteRepository(doer *models.User, repo *models.Repository) { + models.DeleteRepoFromIndexer(repo) +} + +func (r *indexerNotifier) NotifyIssueChangeContent(doer *models.User, issue *models.Issue, oldContent string) { + models.UpdateIssueIndexer(issue.ID) +} + +func (r *indexerNotifier) NotifyIssueChangeTitle(doer *models.User, issue *models.Issue, oldTitle string) { + models.UpdateIssueIndexer(issue.ID) +} diff --git a/modules/notification/notification.go b/modules/notification/notification.go index 6f482e8819..e38c36f7dd 100644 --- a/modules/notification/notification.go +++ b/modules/notification/notification.go @@ -8,6 +8,7 @@ import ( "code.gitea.io/git" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/notification/base" + "code.gitea.io/gitea/modules/notification/indexer" "code.gitea.io/gitea/modules/notification/mail" "code.gitea.io/gitea/modules/notification/ui" ) @@ -25,6 +26,7 @@ func RegisterNotifier(notifier base.Notifier) { func init() { RegisterNotifier(ui.NewNotifier()) RegisterNotifier(mail.NewNotifier()) + RegisterNotifier(indexer.NewNotifier()) } // NotifyCreateIssueComment notifies issue comment related message to notifiers diff --git a/routers/api/v1/repo/issue_comment.go b/routers/api/v1/repo/issue_comment.go index a3fc6f41f3..720513f007 100644 --- a/routers/api/v1/repo/issue_comment.go +++ b/routers/api/v1/repo/issue_comment.go @@ -283,6 +283,9 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) ctx.Error(500, "UpdateComment", err) return } + + notification.NotifyUpdateComment(ctx.User, comment, oldContent) + ctx.JSON(200, comment.APIFormat()) } @@ -371,5 +374,8 @@ func deleteIssueComment(ctx *context.APIContext) { ctx.Error(500, "DeleteCommentByID", err) return } + + notification.NotifyDeleteComment(ctx.User, comment) + ctx.Status(204) } diff --git a/routers/repo/issue.go b/routers/repo/issue.go index f025cc3887..6c6c2612bf 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -1232,6 +1232,8 @@ func UpdateCommentContent(ctx *context.Context) { return } + notification.NotifyUpdateComment(ctx.User, comment, oldContent) + ctx.JSON(200, map[string]interface{}{ "content": string(markdown.Render([]byte(comment.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())), }) @@ -1263,6 +1265,8 @@ func DeleteComment(ctx *context.Context) { return } + notification.NotifyDeleteComment(ctx.User, comment) + ctx.Status(200) }