@@ -7,6 +7,7 @@ package models
7
7
import (
8
8
"fmt"
9
9
10
+ "code.gitea.io/gitea/modules/log"
10
11
"code.gitea.io/gitea/modules/timeutil"
11
12
)
12
13
@@ -281,9 +282,9 @@ func (nl NotificationList) getPendingRepoIDs() []int64 {
281
282
}
282
283
283
284
// LoadRepos loads repositories from database
284
- func (nl NotificationList ) LoadRepos () (RepositoryList , error ) {
285
+ func (nl NotificationList ) LoadRepos () (RepositoryList , [] int , error ) {
285
286
if len (nl ) == 0 {
286
- return RepositoryList {}, nil
287
+ return RepositoryList {}, [] int {}, nil
287
288
}
288
289
289
290
var repoIDs = nl .getPendingRepoIDs ()
@@ -298,15 +299,15 @@ func (nl NotificationList) LoadRepos() (RepositoryList, error) {
298
299
In ("id" , repoIDs [:limit ]).
299
300
Rows (new (Repository ))
300
301
if err != nil {
301
- return nil , err
302
+ return nil , nil , err
302
303
}
303
304
304
305
for rows .Next () {
305
306
var repo Repository
306
307
err = rows .Scan (& repo )
307
308
if err != nil {
308
309
rows .Close ()
309
- return nil , err
310
+ return nil , nil , err
310
311
}
311
312
312
313
repos [repo .ID ] = & repo
@@ -317,14 +318,21 @@ func (nl NotificationList) LoadRepos() (RepositoryList, error) {
317
318
repoIDs = repoIDs [limit :]
318
319
}
319
320
321
+ failed := []int {}
322
+
320
323
var reposList = make (RepositoryList , 0 , len (repoIDs ))
321
- for _ , notification := range nl {
324
+ for i , notification := range nl {
322
325
if notification .Repository == nil {
323
326
notification .Repository = repos [notification .RepoID ]
324
327
}
328
+ if notification .Repository == nil {
329
+ log .Error ("Notification[%d]: RepoID: %d not found" , notification .ID , notification .RepoID )
330
+ failed = append (failed , i )
331
+ continue
332
+ }
325
333
var found bool
326
334
for _ , r := range reposList {
327
- if r .ID == notification .Repository . ID {
335
+ if r .ID == notification .RepoID {
328
336
found = true
329
337
break
330
338
}
@@ -333,7 +341,7 @@ func (nl NotificationList) LoadRepos() (RepositoryList, error) {
333
341
reposList = append (reposList , notification .Repository )
334
342
}
335
343
}
336
- return reposList , nil
344
+ return reposList , failed , nil
337
345
}
338
346
339
347
func (nl NotificationList ) getPendingIssueIDs () []int64 {
@@ -350,9 +358,9 @@ func (nl NotificationList) getPendingIssueIDs() []int64 {
350
358
}
351
359
352
360
// LoadIssues loads issues from database
353
- func (nl NotificationList ) LoadIssues () error {
361
+ func (nl NotificationList ) LoadIssues () ([] int , error ) {
354
362
if len (nl ) == 0 {
355
- return nil
363
+ return [] int {}, nil
356
364
}
357
365
358
366
var issueIDs = nl .getPendingIssueIDs ()
@@ -367,15 +375,15 @@ func (nl NotificationList) LoadIssues() error {
367
375
In ("id" , issueIDs [:limit ]).
368
376
Rows (new (Issue ))
369
377
if err != nil {
370
- return err
378
+ return nil , err
371
379
}
372
380
373
381
for rows .Next () {
374
382
var issue Issue
375
383
err = rows .Scan (& issue )
376
384
if err != nil {
377
385
rows .Close ()
378
- return err
386
+ return nil , err
379
387
}
380
388
381
389
issues [issue .ID ] = & issue
@@ -386,13 +394,38 @@ func (nl NotificationList) LoadIssues() error {
386
394
issueIDs = issueIDs [limit :]
387
395
}
388
396
389
- for _ , notification := range nl {
397
+ failures := []int {}
398
+
399
+ for i , notification := range nl {
390
400
if notification .Issue == nil {
391
401
notification .Issue = issues [notification .IssueID ]
402
+ if notification .Issue == nil {
403
+ log .Error ("Notification[%d]: IssueID: %d Not Found" , notification .ID , notification .IssueID )
404
+ failures = append (failures , i )
405
+ continue
406
+ }
392
407
notification .Issue .Repo = notification .Repository
393
408
}
394
409
}
395
- return nil
410
+ return failures , nil
411
+ }
412
+
413
+ // Without returns the notification list without the failures
414
+ func (nl NotificationList ) Without (failures []int ) NotificationList {
415
+ if len (failures ) == 0 {
416
+ return nl
417
+ }
418
+ remaining := make ([]* Notification , 0 , len (nl ))
419
+ last := - 1
420
+ var i int
421
+ for _ , i = range failures {
422
+ remaining = append (remaining , nl [last + 1 :i ]... )
423
+ last = i
424
+ }
425
+ if len (nl ) > i {
426
+ remaining = append (remaining , nl [i + 1 :]... )
427
+ }
428
+ return remaining
396
429
}
397
430
398
431
func (nl NotificationList ) getPendingCommentIDs () []int64 {
@@ -409,9 +442,9 @@ func (nl NotificationList) getPendingCommentIDs() []int64 {
409
442
}
410
443
411
444
// LoadComments loads comments from database
412
- func (nl NotificationList ) LoadComments () error {
445
+ func (nl NotificationList ) LoadComments () ([] int , error ) {
413
446
if len (nl ) == 0 {
414
- return nil
447
+ return [] int {}, nil
415
448
}
416
449
417
450
var commentIDs = nl .getPendingCommentIDs ()
@@ -426,15 +459,15 @@ func (nl NotificationList) LoadComments() error {
426
459
In ("id" , commentIDs [:limit ]).
427
460
Rows (new (Comment ))
428
461
if err != nil {
429
- return err
462
+ return nil , err
430
463
}
431
464
432
465
for rows .Next () {
433
466
var comment Comment
434
467
err = rows .Scan (& comment )
435
468
if err != nil {
436
469
rows .Close ()
437
- return err
470
+ return nil , err
438
471
}
439
472
440
473
comments [comment .ID ] = & comment
@@ -445,13 +478,19 @@ func (nl NotificationList) LoadComments() error {
445
478
commentIDs = commentIDs [limit :]
446
479
}
447
480
448
- for _ , notification := range nl {
481
+ failures := []int {}
482
+ for i , notification := range nl {
449
483
if notification .CommentID > 0 && notification .Comment == nil && comments [notification .CommentID ] != nil {
450
484
notification .Comment = comments [notification .CommentID ]
485
+ if notification .Comment == nil {
486
+ log .Error ("Notification[%d]: CommentID[%d] failed to load" , notification .ID , notification .CommentID )
487
+ failures = append (failures , i )
488
+ continue
489
+ }
451
490
notification .Comment .Issue = notification .Issue
452
491
}
453
492
}
454
- return nil
493
+ return failures , nil
455
494
}
456
495
457
496
// GetNotificationCount returns the notification count for user
0 commit comments