@@ -136,49 +136,41 @@ func GetNotifications(ctx context.Context, options *FindNotificationOptions) (nl
136136}
137137
138138// 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 {})
141141}
142142
143143// 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
152147
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 ,
161165 RepoID : repo .ID ,
162166 Status : NotificationStatusUnread ,
163167 UpdatedBy : doer .ID ,
164168 Source : NotificationSourceRepository ,
165- })
169+ }}
166170 }
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- }
180171
181- return committer .Commit ()
172+ return db .Insert (ctx , notify )
173+ })
182174}
183175
184176// CreateOrUpdateIssueNotifications creates an issue notification
@@ -379,11 +371,7 @@ func CountUnread(ctx context.Context, userID int64) int64 {
379371}
380372
381373// 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 ) {
387375 if err = n .loadRepo (ctx ); err != nil {
388376 return
389377 }
@@ -481,10 +469,10 @@ func (n *Notification) APIURL() string {
481469type NotificationList []* Notification
482470
483471// 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 {
485473 var err error
486474 for i := 0 ; i < len (nl ); i ++ {
487- err = nl [i ].LoadAttributes ()
475+ err = nl [i ].LoadAttributes (ctx )
488476 if err != nil && ! issues_model .IsErrCommentNotExist (err ) {
489477 return err
490478 }
@@ -504,7 +492,7 @@ func (nl NotificationList) getPendingRepoIDs() []int64 {
504492}
505493
506494// 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 ) {
508496 if len (nl ) == 0 {
509497 return repo_model.RepositoryList {}, []int {}, nil
510498 }
@@ -517,7 +505,7 @@ func (nl NotificationList) LoadRepos() (repo_model.RepositoryList, []int, error)
517505 if left < limit {
518506 limit = left
519507 }
520- rows , err := db .GetEngine (db . DefaultContext ).
508+ rows , err := db .GetEngine (ctx ).
521509 In ("id" , repoIDs [:limit ]).
522510 Rows (new (repo_model.Repository ))
523511 if err != nil {
@@ -578,7 +566,7 @@ func (nl NotificationList) getPendingIssueIDs() []int64 {
578566}
579567
580568// LoadIssues loads issues from database
581- func (nl NotificationList ) LoadIssues () ([]int , error ) {
569+ func (nl NotificationList ) LoadIssues (ctx context. Context ) ([]int , error ) {
582570 if len (nl ) == 0 {
583571 return []int {}, nil
584572 }
@@ -591,7 +579,7 @@ func (nl NotificationList) LoadIssues() ([]int, error) {
591579 if left < limit {
592580 limit = left
593581 }
594- rows , err := db .GetEngine (db . DefaultContext ).
582+ rows , err := db .GetEngine (ctx ).
595583 In ("id" , issueIDs [:limit ]).
596584 Rows (new (issues_model.Issue ))
597585 if err != nil {
@@ -662,7 +650,7 @@ func (nl NotificationList) getPendingCommentIDs() []int64 {
662650}
663651
664652// LoadComments loads comments from database
665- func (nl NotificationList ) LoadComments () ([]int , error ) {
653+ func (nl NotificationList ) LoadComments (ctx context. Context ) ([]int , error ) {
666654 if len (nl ) == 0 {
667655 return []int {}, nil
668656 }
@@ -675,7 +663,7 @@ func (nl NotificationList) LoadComments() ([]int, error) {
675663 if left < limit {
676664 limit = left
677665 }
678- rows , err := db .GetEngine (db . DefaultContext ).
666+ rows , err := db .GetEngine (ctx ).
679667 In ("id" , commentIDs [:limit ]).
680668 Rows (new (issues_model.Comment ))
681669 if err != nil {
@@ -775,8 +763,8 @@ func SetRepoReadBy(ctx context.Context, userID, repoID int64) error {
775763}
776764
777765// 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 )
780768 if err != nil {
781769 return notification , err
782770 }
@@ -787,16 +775,12 @@ func SetNotificationStatus(notificationID int64, user *user_model.User, status N
787775
788776 notification .Status = status
789777
790- _ , err = db .GetEngine (db . DefaultContext ).ID (notificationID ).Update (notification )
778+ _ , err = db .GetEngine (ctx ).ID (notificationID ).Update (notification )
791779 return notification , err
792780}
793781
794782// 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 ) {
800784 notification := new (Notification )
801785 ok , err := db .GetEngine (ctx ).
802786 Where ("id = ?" , notificationID ).
@@ -813,9 +797,9 @@ func getNotificationByID(ctx context.Context, notificationID int64) (*Notificati
813797}
814798
815799// 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 {
817801 n := & Notification {Status : desiredStatus , UpdatedBy : user .ID }
818- _ , err := db .GetEngine (db . DefaultContext ).
802+ _ , err := db .GetEngine (ctx ).
819803 Where ("user_id = ? AND status = ?" , user .ID , currentStatus ).
820804 Cols ("status" , "updated_by" , "updated_unix" ).
821805 Update (n )
0 commit comments