Skip to content

Commit 82e08a3

Browse files
authored
Refactor notification for indexer (#5111)
* notification for indexer * use NullNotifier as parent struct
1 parent 477a80f commit 82e08a3

File tree

8 files changed

+75
-15
lines changed

8 files changed

+75
-15
lines changed

models/issue.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,8 +1112,6 @@ func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, assigneeIDs []in
11121112
return fmt.Errorf("Commit: %v", err)
11131113
}
11141114

1115-
UpdateIssueIndexer(issue.ID)
1116-
11171115
if err = NotifyWatchers(&Action{
11181116
ActUserID: issue.Poster.ID,
11191117
ActUser: issue.Poster,
@@ -1652,7 +1650,6 @@ func updateIssue(e Engine, issue *Issue) error {
16521650
if err != nil {
16531651
return err
16541652
}
1655-
UpdateIssueIndexer(issue.ID)
16561653
return nil
16571654
}
16581655

models/issue_comment.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -818,9 +818,6 @@ func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
818818
return nil, err
819819
}
820820

821-
if opts.Type == CommentTypeComment {
822-
UpdateIssueIndexer(opts.Issue.ID)
823-
}
824821
return comment, nil
825822
}
826823

@@ -1022,8 +1019,6 @@ func GetCommentsByRepoIDSince(repoID, since int64) ([]*Comment, error) {
10221019
func UpdateComment(doer *User, c *Comment, oldContent string) error {
10231020
if _, err := x.ID(c.ID).AllCols().Update(c); err != nil {
10241021
return err
1025-
} else if c.Type == CommentTypeComment {
1026-
UpdateIssueIndexer(c.IssueID)
10271022
}
10281023

10291024
if err := c.LoadPoster(); err != nil {
@@ -1082,8 +1077,6 @@ func DeleteComment(doer *User, comment *Comment) error {
10821077

10831078
if err := sess.Commit(); err != nil {
10841079
return err
1085-
} else if comment.Type == CommentTypeComment {
1086-
UpdateIssueIndexer(comment.IssueID)
10871080
}
10881081

10891082
if err := comment.LoadPoster(); err != nil {

models/issue_indexer.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ func updateNeededCols(cols []string) bool {
117117
// UpdateIssueIndexerCols update an issue in the issue indexer, given changes
118118
// to the specified columns
119119
func UpdateIssueIndexerCols(issueID int64, cols ...string) {
120-
if updateNeededCols(cols) {
121-
UpdateIssueIndexer(issueID)
122-
}
120+
updateNeededCols(cols)
123121
}
124122

125123
// UpdateIssueIndexer add/update an issue to the issue indexer

models/pull.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,8 +833,6 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
833833
return fmt.Errorf("Commit: %v", err)
834834
}
835835

836-
UpdateIssueIndexer(pull.ID)
837-
838836
if err = NotifyWatchers(&Action{
839837
ActUserID: pull.Poster.ID,
840838
ActUser: pull.Poster,
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
5+
package indexer
6+
7+
import (
8+
"code.gitea.io/gitea/models"
9+
"code.gitea.io/gitea/modules/notification/base"
10+
)
11+
12+
type indexerNotifier struct {
13+
base.NullNotifier
14+
}
15+
16+
var (
17+
_ base.Notifier = &indexerNotifier{}
18+
)
19+
20+
// NewNotifier create a new indexerNotifier notifier
21+
func NewNotifier() base.Notifier {
22+
return &indexerNotifier{}
23+
}
24+
25+
func (r *indexerNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
26+
issue *models.Issue, comment *models.Comment) {
27+
if comment.Type == models.CommentTypeComment {
28+
models.UpdateIssueIndexer(issue.ID)
29+
}
30+
}
31+
32+
func (r *indexerNotifier) NotifyNewIssue(issue *models.Issue) {
33+
models.UpdateIssueIndexer(issue.ID)
34+
}
35+
36+
func (r *indexerNotifier) NotifyNewPullRequest(pr *models.PullRequest) {
37+
models.UpdateIssueIndexer(pr.Issue.ID)
38+
}
39+
40+
func (r *indexerNotifier) NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
41+
if c.Type == models.CommentTypeComment {
42+
models.UpdateIssueIndexer(c.IssueID)
43+
}
44+
}
45+
46+
func (r *indexerNotifier) NotifyDeleteComment(doer *models.User, comment *models.Comment) {
47+
if comment.Type == models.CommentTypeComment {
48+
models.UpdateIssueIndexer(comment.IssueID)
49+
}
50+
}
51+
52+
func (r *indexerNotifier) NotifyDeleteRepository(doer *models.User, repo *models.Repository) {
53+
models.DeleteRepoFromIndexer(repo)
54+
}
55+
56+
func (r *indexerNotifier) NotifyIssueChangeContent(doer *models.User, issue *models.Issue, oldContent string) {
57+
models.UpdateIssueIndexer(issue.ID)
58+
}
59+
60+
func (r *indexerNotifier) NotifyIssueChangeTitle(doer *models.User, issue *models.Issue, oldTitle string) {
61+
models.UpdateIssueIndexer(issue.ID)
62+
}

modules/notification/notification.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"code.gitea.io/git"
99
"code.gitea.io/gitea/models"
1010
"code.gitea.io/gitea/modules/notification/base"
11+
"code.gitea.io/gitea/modules/notification/indexer"
1112
"code.gitea.io/gitea/modules/notification/mail"
1213
"code.gitea.io/gitea/modules/notification/ui"
1314
)
@@ -25,6 +26,7 @@ func RegisterNotifier(notifier base.Notifier) {
2526
func init() {
2627
RegisterNotifier(ui.NewNotifier())
2728
RegisterNotifier(mail.NewNotifier())
29+
RegisterNotifier(indexer.NewNotifier())
2830
}
2931

3032
// NotifyCreateIssueComment notifies issue comment related message to notifiers

routers/api/v1/repo/issue_comment.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
283283
ctx.Error(500, "UpdateComment", err)
284284
return
285285
}
286+
287+
notification.NotifyUpdateComment(ctx.User, comment, oldContent)
288+
286289
ctx.JSON(200, comment.APIFormat())
287290
}
288291

@@ -371,5 +374,8 @@ func deleteIssueComment(ctx *context.APIContext) {
371374
ctx.Error(500, "DeleteCommentByID", err)
372375
return
373376
}
377+
378+
notification.NotifyDeleteComment(ctx.User, comment)
379+
374380
ctx.Status(204)
375381
}

routers/repo/issue.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,8 @@ func UpdateCommentContent(ctx *context.Context) {
12321232
return
12331233
}
12341234

1235+
notification.NotifyUpdateComment(ctx.User, comment, oldContent)
1236+
12351237
ctx.JSON(200, map[string]interface{}{
12361238
"content": string(markdown.Render([]byte(comment.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())),
12371239
})
@@ -1263,6 +1265,8 @@ func DeleteComment(ctx *context.Context) {
12631265
return
12641266
}
12651267

1268+
notification.NotifyDeleteComment(ctx.User, comment)
1269+
12661270
ctx.Status(200)
12671271
}
12681272

0 commit comments

Comments
 (0)