@@ -11,7 +11,6 @@ import (
1111
1212 "code.gitea.io/gitea/models/db"
1313 issues_model "code.gitea.io/gitea/models/issues"
14- "code.gitea.io/gitea/models/organization"
1514 repo_model "code.gitea.io/gitea/models/repo"
1615 user_model "code.gitea.io/gitea/models/user"
1716 "code.gitea.io/gitea/modules/setting"
@@ -46,6 +45,8 @@ const (
4645 NotificationSourceCommit
4746 // NotificationSourceRepository is a notification for a repository
4847 NotificationSourceRepository
48+ // NotificationSourceRelease is a notification for a release
49+ NotificationSourceRelease
4950)
5051
5152// Notification represents a notification
@@ -60,13 +61,15 @@ type Notification struct {
6061 IssueID int64 `xorm:"NOT NULL"`
6162 CommitID string
6263 CommentID int64
64+ ReleaseID int64
6365
6466 UpdatedBy int64 `xorm:"NOT NULL"`
6567
6668 Issue * issues_model.Issue `xorm:"-"`
6769 Repository * repo_model.Repository `xorm:"-"`
6870 Comment * issues_model.Comment `xorm:"-"`
6971 User * user_model.User `xorm:"-"`
72+ Release * repo_model.Release `xorm:"-"`
7073
7174 CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
7275 UpdatedUnix timeutil.TimeStamp `xorm:"updated NOT NULL"`
@@ -104,6 +107,10 @@ func (n *Notification) TableIndices() []*schemas.Index {
104107 commitIDIndex .AddColumn ("commit_id" )
105108 indices = append (indices , commitIDIndex )
106109
110+ releaseIDIndex := schemas .NewIndex ("idx_notification_release_id" , schemas .IndexType )
111+ releaseIDIndex .AddColumn ("release_id" )
112+ indices = append (indices , releaseIDIndex )
113+
107114 updatedByIndex := schemas .NewIndex ("idx_notification_updated_by" , schemas .IndexType )
108115 updatedByIndex .AddColumn ("updated_by" )
109116 indices = append (indices , updatedByIndex )
@@ -116,36 +123,53 @@ func init() {
116123}
117124
118125// CreateRepoTransferNotification creates notification for the user a repository was transferred to
119- func CreateRepoTransferNotification (ctx context.Context , doer , newOwner * user_model.User , repo * repo_model.Repository ) error {
120- return db .WithTx (ctx , func (ctx context.Context ) error {
121- var notify []* Notification
122-
123- if newOwner .IsOrganization () {
124- users , err := organization .GetUsersWhoCanCreateOrgRepo (ctx , newOwner .ID )
125- if err != nil || len (users ) == 0 {
126- return err
127- }
128- for i := range users {
129- notify = append (notify , & Notification {
130- UserID : i ,
131- RepoID : repo .ID ,
132- Status : NotificationStatusUnread ,
133- UpdatedBy : doer .ID ,
134- Source : NotificationSourceRepository ,
135- })
136- }
137- } else {
138- notify = []* Notification {{
139- UserID : newOwner .ID ,
140- RepoID : repo .ID ,
141- Status : NotificationStatusUnread ,
142- UpdatedBy : doer .ID ,
143- Source : NotificationSourceRepository ,
144- }}
145- }
126+ func CreateRepoTransferNotification (ctx context.Context , doerID , repoID , receiverID int64 ) error {
127+ notify := & Notification {
128+ UserID : receiverID ,
129+ RepoID : repoID ,
130+ Status : NotificationStatusUnread ,
131+ UpdatedBy : doerID ,
132+ Source : NotificationSourceRepository ,
133+ }
134+ return db .Insert (ctx , notify )
135+ }
146136
147- return db .Insert (ctx , notify )
148- })
137+ func CreateCommitNotifications (ctx context.Context , doerID , repoID int64 , commitID string , receiverID int64 ) error {
138+ notification := & Notification {
139+ Source : NotificationSourceCommit ,
140+ UserID : receiverID ,
141+ RepoID : repoID ,
142+ CommitID : commitID ,
143+ Status : NotificationStatusUnread ,
144+ UpdatedBy : doerID ,
145+ }
146+
147+ return db .Insert (ctx , notification )
148+ }
149+
150+ func CreateOrUpdateReleaseNotifications (ctx context.Context , doerID , releaseID , receiverID int64 ) error {
151+ notification := new (Notification )
152+ if _ , err := db .GetEngine (ctx ).
153+ Where ("user_id = ?" , receiverID ).
154+ And ("release_id = ?" , releaseID ).
155+ Get (notification ); err != nil {
156+ return err
157+ }
158+ if notification .ID > 0 {
159+ notification .Status = NotificationStatusUnread
160+ notification .UpdatedBy = doerID
161+ _ , err := db .GetEngine (ctx ).ID (notification .ID ).Cols ("status" , "updated_by" ).Update (notification )
162+ return err
163+ }
164+
165+ notification = & Notification {
166+ Source : NotificationSourceRelease ,
167+ UserID : receiverID ,
168+ Status : NotificationStatusUnread ,
169+ ReleaseID : releaseID ,
170+ UpdatedBy : doerID ,
171+ }
172+ return db .Insert (ctx , notification )
149173}
150174
151175func createIssueNotification (ctx context.Context , userID int64 , issue * issues_model.Issue , commentID , updatedByID int64 ) error {
@@ -213,6 +237,9 @@ func (n *Notification) LoadAttributes(ctx context.Context) (err error) {
213237 if err = n .loadComment (ctx ); err != nil {
214238 return err
215239 }
240+ if err = n .loadRelease (ctx ); err != nil {
241+ return err
242+ }
216243 return err
217244}
218245
@@ -253,6 +280,16 @@ func (n *Notification) loadComment(ctx context.Context) (err error) {
253280 return nil
254281}
255282
283+ func (n * Notification ) loadRelease (ctx context.Context ) (err error ) {
284+ if n .Release == nil && n .ReleaseID != 0 {
285+ n .Release , err = repo_model .GetReleaseByID (ctx , n .ReleaseID )
286+ if err != nil {
287+ return fmt .Errorf ("GetReleaseByID [%d]: %w" , n .ReleaseID , err )
288+ }
289+ }
290+ return nil
291+ }
292+
256293func (n * Notification ) loadUser (ctx context.Context ) (err error ) {
257294 if n .User == nil {
258295 n .User , err = user_model .GetUserByID (ctx , n .UserID )
@@ -285,6 +322,8 @@ func (n *Notification) HTMLURL(ctx context.Context) string {
285322 return n .Repository .HTMLURL () + "/commit/" + url .PathEscape (n .CommitID )
286323 case NotificationSourceRepository :
287324 return n .Repository .HTMLURL ()
325+ case NotificationSourceRelease :
326+ return n .Release .HTMLURL ()
288327 }
289328 return ""
290329}
@@ -301,6 +340,8 @@ func (n *Notification) Link(ctx context.Context) string {
301340 return n .Repository .Link () + "/commit/" + url .PathEscape (n .CommitID )
302341 case NotificationSourceRepository :
303342 return n .Repository .Link ()
343+ case NotificationSourceRelease :
344+ return n .Release .Link ()
304345 }
305346 return ""
306347}
@@ -373,6 +414,17 @@ func SetRepoReadBy(ctx context.Context, userID, repoID int64) error {
373414 return err
374415}
375416
417+ // SetReleaseReadBy sets issue to be read by given user.
418+ func SetReleaseReadBy (ctx context.Context , releaseID , userID int64 ) error {
419+ _ , err := db .GetEngine (ctx ).Where (builder.Eq {
420+ "user_id" : userID ,
421+ "status" : NotificationStatusUnread ,
422+ "source" : NotificationSourceRelease ,
423+ "release_id" : releaseID ,
424+ }).Cols ("status" ).Update (& Notification {Status : NotificationStatusRead })
425+ return err
426+ }
427+
376428// SetNotificationStatus change the notification status
377429func SetNotificationStatus (ctx context.Context , notificationID int64 , user * user_model.User , status NotificationStatus ) (* Notification , error ) {
378430 notification , err := GetNotificationByID (ctx , notificationID )
0 commit comments