@@ -136,49 +136,41 @@ func GetNotifications(ctx context.Context, options *FindNotificationOptions) (nl
136
136
}
137
137
138
138
// CountNotifications count all notifications that fit to the given options and ignore pagination.
139
- func CountNotifications (opts * FindNotificationOptions ) (int64 , error ) {
140
- return db .GetEngine (db . DefaultContext ).Where (opts .ToCond ()).Count (& Notification {})
139
+ func CountNotifications (ctx context. Context , opts * FindNotificationOptions ) (int64 , error ) {
140
+ return db .GetEngine (ctx ).Where (opts .ToCond ()).Count (& Notification {})
141
141
}
142
142
143
143
// CreateRepoTransferNotification creates notification for the user a repository was transferred to
144
- func CreateRepoTransferNotification (doer , newOwner * user_model.User , repo * repo_model.Repository ) error {
145
- ctx , committer , err := db .TxContext (db .DefaultContext )
146
- if err != nil {
147
- return err
148
- }
149
- defer committer .Close ()
150
-
151
- var notify []* Notification
144
+ func CreateRepoTransferNotification (ctx context.Context , doer , newOwner * user_model.User , repo * repo_model.Repository ) error {
145
+ return db .AutoTx (ctx , func (ctx context.Context ) error {
146
+ var notify []* Notification
152
147
153
- if newOwner .IsOrganization () {
154
- users , err := organization .GetUsersWhoCanCreateOrgRepo (ctx , newOwner .ID )
155
- if err != nil || len (users ) == 0 {
156
- return err
157
- }
158
- for i := range users {
159
- notify = append (notify , & Notification {
160
- UserID : users [i ].ID ,
148
+ if newOwner .IsOrganization () {
149
+ users , err := organization .GetUsersWhoCanCreateOrgRepo (ctx , newOwner .ID )
150
+ if err != nil || len (users ) == 0 {
151
+ return err
152
+ }
153
+ for i := range users {
154
+ notify = append (notify , & Notification {
155
+ UserID : users [i ].ID ,
156
+ RepoID : repo .ID ,
157
+ Status : NotificationStatusUnread ,
158
+ UpdatedBy : doer .ID ,
159
+ Source : NotificationSourceRepository ,
160
+ })
161
+ }
162
+ } else {
163
+ notify = []* Notification {{
164
+ UserID : newOwner .ID ,
161
165
RepoID : repo .ID ,
162
166
Status : NotificationStatusUnread ,
163
167
UpdatedBy : doer .ID ,
164
168
Source : NotificationSourceRepository ,
165
- })
169
+ }}
166
170
}
167
- } else {
168
- notify = []* Notification {{
169
- UserID : newOwner .ID ,
170
- RepoID : repo .ID ,
171
- Status : NotificationStatusUnread ,
172
- UpdatedBy : doer .ID ,
173
- Source : NotificationSourceRepository ,
174
- }}
175
- }
176
-
177
- if err := db .Insert (ctx , notify ); err != nil {
178
- return err
179
- }
180
171
181
- return committer .Commit ()
172
+ return db .Insert (ctx , notify )
173
+ })
182
174
}
183
175
184
176
// CreateOrUpdateIssueNotifications creates an issue notification
@@ -379,11 +371,7 @@ func CountUnread(ctx context.Context, userID int64) int64 {
379
371
}
380
372
381
373
// LoadAttributes load Repo Issue User and Comment if not loaded
382
- func (n * Notification ) LoadAttributes () (err error ) {
383
- return n .loadAttributes (db .DefaultContext )
384
- }
385
-
386
- func (n * Notification ) loadAttributes (ctx context.Context ) (err error ) {
374
+ func (n * Notification ) LoadAttributes (ctx context.Context ) (err error ) {
387
375
if err = n .loadRepo (ctx ); err != nil {
388
376
return
389
377
}
@@ -481,10 +469,10 @@ func (n *Notification) APIURL() string {
481
469
type NotificationList []* Notification
482
470
483
471
// LoadAttributes load Repo Issue User and Comment if not loaded
484
- func (nl NotificationList ) LoadAttributes () error {
472
+ func (nl NotificationList ) LoadAttributes (ctx context. Context ) error {
485
473
var err error
486
474
for i := 0 ; i < len (nl ); i ++ {
487
- err = nl [i ].LoadAttributes ()
475
+ err = nl [i ].LoadAttributes (ctx )
488
476
if err != nil && ! issues_model .IsErrCommentNotExist (err ) {
489
477
return err
490
478
}
@@ -504,7 +492,7 @@ func (nl NotificationList) getPendingRepoIDs() []int64 {
504
492
}
505
493
506
494
// LoadRepos loads repositories from database
507
- func (nl NotificationList ) LoadRepos () (repo_model.RepositoryList , []int , error ) {
495
+ func (nl NotificationList ) LoadRepos (ctx context. Context ) (repo_model.RepositoryList , []int , error ) {
508
496
if len (nl ) == 0 {
509
497
return repo_model.RepositoryList {}, []int {}, nil
510
498
}
@@ -517,7 +505,7 @@ func (nl NotificationList) LoadRepos() (repo_model.RepositoryList, []int, error)
517
505
if left < limit {
518
506
limit = left
519
507
}
520
- rows , err := db .GetEngine (db . DefaultContext ).
508
+ rows , err := db .GetEngine (ctx ).
521
509
In ("id" , repoIDs [:limit ]).
522
510
Rows (new (repo_model.Repository ))
523
511
if err != nil {
@@ -578,7 +566,7 @@ func (nl NotificationList) getPendingIssueIDs() []int64 {
578
566
}
579
567
580
568
// LoadIssues loads issues from database
581
- func (nl NotificationList ) LoadIssues () ([]int , error ) {
569
+ func (nl NotificationList ) LoadIssues (ctx context. Context ) ([]int , error ) {
582
570
if len (nl ) == 0 {
583
571
return []int {}, nil
584
572
}
@@ -591,7 +579,7 @@ func (nl NotificationList) LoadIssues() ([]int, error) {
591
579
if left < limit {
592
580
limit = left
593
581
}
594
- rows , err := db .GetEngine (db . DefaultContext ).
582
+ rows , err := db .GetEngine (ctx ).
595
583
In ("id" , issueIDs [:limit ]).
596
584
Rows (new (issues_model.Issue ))
597
585
if err != nil {
@@ -662,7 +650,7 @@ func (nl NotificationList) getPendingCommentIDs() []int64 {
662
650
}
663
651
664
652
// LoadComments loads comments from database
665
- func (nl NotificationList ) LoadComments () ([]int , error ) {
653
+ func (nl NotificationList ) LoadComments (ctx context. Context ) ([]int , error ) {
666
654
if len (nl ) == 0 {
667
655
return []int {}, nil
668
656
}
@@ -675,7 +663,7 @@ func (nl NotificationList) LoadComments() ([]int, error) {
675
663
if left < limit {
676
664
limit = left
677
665
}
678
- rows , err := db .GetEngine (db . DefaultContext ).
666
+ rows , err := db .GetEngine (ctx ).
679
667
In ("id" , commentIDs [:limit ]).
680
668
Rows (new (issues_model.Comment ))
681
669
if err != nil {
@@ -775,8 +763,8 @@ func SetRepoReadBy(ctx context.Context, userID, repoID int64) error {
775
763
}
776
764
777
765
// SetNotificationStatus change the notification status
778
- func SetNotificationStatus (notificationID int64 , user * user_model.User , status NotificationStatus ) (* Notification , error ) {
779
- notification , err := getNotificationByID ( db . DefaultContext , notificationID )
766
+ func SetNotificationStatus (ctx context. Context , notificationID int64 , user * user_model.User , status NotificationStatus ) (* Notification , error ) {
767
+ notification , err := GetNotificationByID ( ctx , notificationID )
780
768
if err != nil {
781
769
return notification , err
782
770
}
@@ -787,16 +775,12 @@ func SetNotificationStatus(notificationID int64, user *user_model.User, status N
787
775
788
776
notification .Status = status
789
777
790
- _ , err = db .GetEngine (db . DefaultContext ).ID (notificationID ).Update (notification )
778
+ _ , err = db .GetEngine (ctx ).ID (notificationID ).Update (notification )
791
779
return notification , err
792
780
}
793
781
794
782
// GetNotificationByID return notification by ID
795
- func GetNotificationByID (notificationID int64 ) (* Notification , error ) {
796
- return getNotificationByID (db .DefaultContext , notificationID )
797
- }
798
-
799
- func getNotificationByID (ctx context.Context , notificationID int64 ) (* Notification , error ) {
783
+ func GetNotificationByID (ctx context.Context , notificationID int64 ) (* Notification , error ) {
800
784
notification := new (Notification )
801
785
ok , err := db .GetEngine (ctx ).
802
786
Where ("id = ?" , notificationID ).
@@ -813,9 +797,9 @@ func getNotificationByID(ctx context.Context, notificationID int64) (*Notificati
813
797
}
814
798
815
799
// UpdateNotificationStatuses updates the statuses of all of a user's notifications that are of the currentStatus type to the desiredStatus
816
- func UpdateNotificationStatuses (user * user_model.User , currentStatus , desiredStatus NotificationStatus ) error {
800
+ func UpdateNotificationStatuses (ctx context. Context , user * user_model.User , currentStatus , desiredStatus NotificationStatus ) error {
817
801
n := & Notification {Status : desiredStatus , UpdatedBy : user .ID }
818
- _ , err := db .GetEngine (db . DefaultContext ).
802
+ _ , err := db .GetEngine (ctx ).
819
803
Where ("user_id = ? AND status = ?" , user .ID , currentStatus ).
820
804
Cols ("status" , "updated_by" , "updated_unix" ).
821
805
Update (n )
0 commit comments