Skip to content

Commit 9a63ecb

Browse files
committed
fix some bugs
1 parent 4258f9d commit 9a63ecb

File tree

12 files changed

+116
-31
lines changed

12 files changed

+116
-31
lines changed

models/issue.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,21 @@ func (issue *Issue) LoadPullRequest() error {
179179
}
180180

181181
func (issue *Issue) loadComments(e Engine) (err error) {
182+
return issue.loadCommentsByType(e, CommentTypeUnknown)
183+
}
184+
185+
// LoadDiscussComments loads discuss comments
186+
func (issue *Issue) LoadDiscussComments() error {
187+
return issue.loadCommentsByType(x, CommentTypeComment)
188+
}
189+
190+
func (issue *Issue) loadCommentsByType(e Engine, tp CommentType) (err error) {
182191
if issue.Comments != nil {
183192
return nil
184193
}
185194
issue.Comments, err = findComments(e, FindCommentsOptions{
186195
IssueID: issue.ID,
187-
Type: CommentTypeUnknown,
196+
Type: tp,
188197
})
189198
return err
190199
}
@@ -1212,6 +1221,12 @@ func getIssuesByIDs(e Engine, issueIDs []int64) ([]*Issue, error) {
12121221
return issues, e.In("id", issueIDs).Find(&issues)
12131222
}
12141223

1224+
func getIssueIDsByRepoID(e Engine, repoID int64) ([]int64, error) {
1225+
var ids = make([]int64, 0, 10)
1226+
err := e.Table("issue").Where("repo_id = ?", repoID).Find(&ids)
1227+
return ids, err
1228+
}
1229+
12151230
// GetIssuesByIDs return issues with the given IDs.
12161231
func GetIssuesByIDs(issueIDs []int64) ([]*Issue, error) {
12171232
return getIssuesByIDs(x, issueIDs)

models/issue_comment.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,13 +1032,6 @@ func UpdateComment(doer *User, c *Comment, oldContent string) error {
10321032
return err
10331033
}
10341034

1035-
if c.Type == CommentTypeComment {
1036-
if err := c.Issue.loadComments(x); err != nil {
1037-
return err
1038-
}
1039-
UpdateIssueIndexer(c.Issue)
1040-
}
1041-
10421035
if err := c.Issue.LoadAttributes(); err != nil {
10431036
return err
10441037
}
@@ -1098,13 +1091,6 @@ func DeleteComment(doer *User, comment *Comment) error {
10981091
return err
10991092
}
11001093

1101-
if comment.Type == CommentTypeComment {
1102-
if err := comment.Issue.loadComments(x); err != nil {
1103-
return err
1104-
}
1105-
UpdateIssueIndexer(comment.Issue)
1106-
}
1107-
11081094
if err := comment.Issue.LoadAttributes(); err != nil {
11091095
return err
11101096
}

models/issue_indexer.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,19 @@ func UpdateIssueIndexer(issue *Issue) {
115115

116116
// DeleteRepoIssueIndexer deletes repo's all issues indexes
117117
func DeleteRepoIssueIndexer(repo *Repository) {
118+
var ids []int64
119+
ids, err := getIssueIDsByRepoID(x, repo.ID)
120+
if err != nil {
121+
log.Error(4, "getIssueIDsByRepoID failed: %v", err)
122+
return
123+
}
124+
125+
if len(ids) <= 0 {
126+
return
127+
}
128+
118129
issueIndexerUpdateQueue.Push(&issues.IndexerData{
119-
RepoID: repo.ID,
130+
IDs: ids,
120131
IsDelete: true,
121132
})
122133
}

modules/indexer/issues/bleve.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,17 @@ func (b *BleveIndexer) Index(issues []*IndexerData) error {
205205
return batch.Flush()
206206
}
207207

208+
// Delete deletes indexes by ids
209+
func (b *BleveIndexer) Delete(ids ...int64) error {
210+
batch := rupture.NewFlushingBatch(b.indexer, maxBatchSize)
211+
for _, id := range ids {
212+
if err := batch.Delete(indexerID(id)); err != nil {
213+
return err
214+
}
215+
}
216+
return batch.Flush()
217+
}
218+
208219
// Search searches for issues by given conditions.
209220
// Returns the matching issue IDs
210221
func (b *BleveIndexer) Search(keyword string, repoID int64, limit, start int) (*SearchResult, error) {

modules/indexer/issues/indexer.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ type IndexerData struct {
1111
Title string
1212
Content string
1313
Comments []string
14-
IsDelete bool `json:"-"`
14+
IsDelete bool
15+
IDs []int64
1516
}
1617

1718
// Match represents on search result
@@ -30,5 +31,6 @@ type SearchResult struct {
3031
type Indexer interface {
3132
Init() (bool, error)
3233
Index(issue []*IndexerData) error
34+
Delete(ids ...int64) error
3335
Search(kw string, repoID int64, limit, start int) (*SearchResult, error)
3436
}

modules/indexer/issues/queue_ledis_local.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ func (l *LedisLocalQueue) Run() error {
8383

8484
log.Trace("LedisLocalQueue: task found: %#v", data)
8585

86+
if data.IsDelete {
87+
if data.ID > 0 {
88+
if err = l.indexer.Delete(data.ID); err != nil {
89+
log.Error(4, "indexer.Delete: %v", err)
90+
}
91+
} else if len(data.IDs) > 0 {
92+
if err = l.indexer.Delete(data.IDs...); err != nil {
93+
log.Error(4, "indexer.Delete: %v", err)
94+
}
95+
}
96+
time.Sleep(time.Millisecond * 10)
97+
continue
98+
}
99+
86100
datas = append(datas, &data)
87101
time.Sleep(time.Millisecond * 10)
88102
}

modules/notification/base/notifier.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ type Notifier interface {
3434

3535
NotifyCreateIssueComment(*models.User, *models.Repository,
3636
*models.Issue, *models.Comment)
37-
NotifyUpdateComment(*models.User, *models.Comment, int64, string)
38-
NotifyDeleteComment(*models.User, *models.Comment, int64)
37+
NotifyUpdateComment(*models.User, *models.Comment, string)
38+
NotifyDeleteComment(*models.User, *models.Comment)
3939

4040
NotifyNewRelease(rel *models.Release)
4141
NotifyUpdateRelease(doer *models.User, rel *models.Release)

modules/notification/base/null.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ func (*NullNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *models
4747
}
4848

4949
// NotifyUpdateComment places a place holder function
50-
func (*NullNotifier) NotifyUpdateComment(doer *models.User, c *models.Comment, repoID int64, oldContent string) {
50+
func (*NullNotifier) NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
5151
}
5252

5353
// NotifyDeleteComment places a place holder function
54-
func (*NullNotifier) NotifyDeleteComment(doer *models.User, c *models.Comment, repoID int64) {
54+
func (*NullNotifier) NotifyDeleteComment(doer *models.User, c *models.Comment) {
5555
}
5656

5757
// NotifyDeleteRepository places a place holder function

modules/notification/indexer/indexer.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package indexer
66

77
import (
88
"code.gitea.io/gitea/models"
9+
"code.gitea.io/gitea/modules/log"
910
"code.gitea.io/gitea/modules/notification/base"
1011
)
1112

@@ -25,6 +26,15 @@ func NewNotifier() base.Notifier {
2526
func (r *indexerNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
2627
issue *models.Issue, comment *models.Comment) {
2728
if comment.Type == models.CommentTypeComment {
29+
if issue.Comments == nil {
30+
if err := issue.LoadDiscussComments(); err != nil {
31+
log.Error(4, "LoadComments failed: %v", err)
32+
return
33+
}
34+
} else {
35+
issue.Comments = append(issue.Comments, comment)
36+
}
37+
2838
models.UpdateIssueIndexer(issue)
2939
}
3040
}
@@ -37,14 +47,50 @@ func (r *indexerNotifier) NotifyNewPullRequest(pr *models.PullRequest) {
3747
models.UpdateIssueIndexer(pr.Issue)
3848
}
3949

40-
func (r *indexerNotifier) NotifyUpdateComment(doer *models.User, c *models.Comment, repoID int64, oldContent string) {
50+
func (r *indexerNotifier) NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
4151
if c.Type == models.CommentTypeComment {
52+
var found bool
53+
if c.Issue.Comments != nil {
54+
for i := 0; i < len(c.Issue.Comments); i++ {
55+
if c.Issue.Comments[i].ID == c.ID {
56+
c.Issue.Comments[i] = c
57+
found = true
58+
break
59+
}
60+
}
61+
}
62+
63+
if !found {
64+
if err := c.Issue.LoadDiscussComments(); err != nil {
65+
log.Error(4, "LoadComments failed: %v", err)
66+
return
67+
}
68+
}
69+
4270
models.UpdateIssueIndexer(c.Issue)
4371
}
4472
}
4573

46-
func (r *indexerNotifier) NotifyDeleteComment(doer *models.User, comment *models.Comment, repoID int64) {
74+
func (r *indexerNotifier) NotifyDeleteComment(doer *models.User, comment *models.Comment) {
4775
if comment.Type == models.CommentTypeComment {
76+
var found bool
77+
if comment.Issue.Comments != nil {
78+
for i := 0; i < len(comment.Issue.Comments); i++ {
79+
if comment.Issue.Comments[i].ID == comment.ID {
80+
comment.Issue.Comments = append(comment.Issue.Comments[:i], comment.Issue.Comments[i+1:]...)
81+
found = true
82+
break
83+
}
84+
}
85+
}
86+
87+
if !found {
88+
if err := comment.Issue.LoadDiscussComments(); err != nil {
89+
log.Error(4, "LoadComments failed: %v", err)
90+
return
91+
}
92+
}
93+
// reload comments to delete the old comment
4894
models.UpdateIssueIndexer(comment.Issue)
4995
}
5096
}

modules/notification/notification.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ func NotifyPullRequestReview(pr *models.PullRequest, review *models.Review, comm
7373
}
7474

7575
// NotifyUpdateComment notifies update comment to notifiers
76-
func NotifyUpdateComment(doer *models.User, c *models.Comment, repoID int64, oldContent string) {
76+
func NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
7777
for _, notifier := range notifiers {
78-
notifier.NotifyUpdateComment(doer, c, repoID, oldContent)
78+
notifier.NotifyUpdateComment(doer, c, oldContent)
7979
}
8080
}
8181

8282
// NotifyDeleteComment notifies delete comment to notifiers
83-
func NotifyDeleteComment(doer *models.User, c *models.Comment, repoID int64) {
83+
func NotifyDeleteComment(doer *models.User, c *models.Comment) {
8484
for _, notifier := range notifiers {
85-
notifier.NotifyDeleteComment(doer, c, repoID)
85+
notifier.NotifyDeleteComment(doer, c)
8686
}
8787
}
8888

routers/api/v1/repo/issue_comment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
284284
return
285285
}
286286

287-
notification.NotifyUpdateComment(ctx.User, comment, ctx.Repo.Repository.ID, oldContent)
287+
notification.NotifyUpdateComment(ctx.User, comment, oldContent)
288288

289289
ctx.JSON(200, comment.APIFormat())
290290
}
@@ -375,7 +375,7 @@ func deleteIssueComment(ctx *context.APIContext) {
375375
return
376376
}
377377

378-
notification.NotifyDeleteComment(ctx.User, comment, ctx.Repo.Repository.ID)
378+
notification.NotifyDeleteComment(ctx.User, comment)
379379

380380
ctx.Status(204)
381381
}

routers/repo/issue.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ func UpdateCommentContent(ctx *context.Context) {
12591259
return
12601260
}
12611261

1262-
notification.NotifyUpdateComment(ctx.User, comment, comment.Issue.RepoID, oldContent)
1262+
notification.NotifyUpdateComment(ctx.User, comment, oldContent)
12631263

12641264
ctx.JSON(200, map[string]interface{}{
12651265
"content": string(markdown.Render([]byte(comment.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())),
@@ -1292,7 +1292,7 @@ func DeleteComment(ctx *context.Context) {
12921292
return
12931293
}
12941294

1295-
notification.NotifyDeleteComment(ctx.User, comment, comment.Issue.RepoID)
1295+
notification.NotifyDeleteComment(ctx.User, comment)
12961296

12971297
ctx.Status(200)
12981298
}

0 commit comments

Comments
 (0)