@@ -11,9 +11,37 @@ import (
1111
1212 "code.gitea.io/gitea/models"
1313 "code.gitea.io/gitea/modules/context"
14+ "code.gitea.io/gitea/modules/log"
1415 "code.gitea.io/gitea/routers/api/v1/utils"
1516)
1617
18+ func statusStringToNotificationStatus (status string ) models.NotificationStatus {
19+ switch strings .ToLower (strings .TrimSpace (status )) {
20+ case "unread" :
21+ return models .NotificationStatusUnread
22+ case "read" :
23+ return models .NotificationStatusRead
24+ case "pinned" :
25+ return models .NotificationStatusPinned
26+ default :
27+ return 0
28+ }
29+ }
30+
31+ func statusStringsToNotificationStatuses (statuses []string , defaultStatuses []string ) []models.NotificationStatus {
32+ if len (statuses ) == 0 {
33+ statuses = defaultStatuses
34+ }
35+ results := make ([]models.NotificationStatus , 0 , len (statuses ))
36+ for _ , status := range statuses {
37+ notificationStatus := statusStringToNotificationStatus (status )
38+ if notificationStatus > 0 {
39+ results = append (results , notificationStatus )
40+ }
41+ }
42+ return results
43+ }
44+
1745// ListRepoNotifications list users's notification threads on a specific repo
1846func ListRepoNotifications (ctx * context.APIContext ) {
1947 // swagger:operation GET /repos/{owner}/{repo}/notifications notification notifyGetRepoList
@@ -39,6 +67,14 @@ func ListRepoNotifications(ctx *context.APIContext) {
3967 // description: If true, show notifications marked as read. Default value is false
4068 // type: string
4169 // required: false
70+ // - name: status-types
71+ // in: query
72+ // description: "Show notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread & pinned"
73+ // type: array
74+ // collectionFormat: multi
75+ // items:
76+ // type: string
77+ // required: false
4278 // - name: since
4379 // in: query
4480 // description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
@@ -75,9 +111,10 @@ func ListRepoNotifications(ctx *context.APIContext) {
75111 UpdatedBeforeUnix : before ,
76112 UpdatedAfterUnix : since ,
77113 }
78- qAll := strings .Trim (ctx .Query ("all" ), " " )
79- if qAll != "true" {
80- opts .Status = models .NotificationStatusUnread
114+
115+ if ! ctx .QueryBool ("all" ) {
116+ statuses := ctx .QueryStrings ("status-types" )
117+ opts .Status = statusStringsToNotificationStatuses (statuses , []string {"unread" , "pinned" })
81118 }
82119 nl , err := models .GetNotifications (opts )
83120 if err != nil {
@@ -97,7 +134,7 @@ func ListRepoNotifications(ctx *context.APIContext) {
97134func ReadRepoNotifications (ctx * context.APIContext ) {
98135 // swagger:operation PUT /repos/{owner}/{repo}/notifications notification notifyReadRepoList
99136 // ---
100- // summary: Mark notification threads as read on a specific repo
137+ // summary: Mark notification threads as read, pinned or unread on a specific repo
101138 // consumes:
102139 // - application/json
103140 // produces:
@@ -113,6 +150,24 @@ func ReadRepoNotifications(ctx *context.APIContext) {
113150 // description: name of the repo
114151 // type: string
115152 // required: true
153+ // - name: all
154+ // in: query
155+ // description: If true, mark all notifications on this repo. Default value is false
156+ // type: string
157+ // required: false
158+ // - name: status-types
159+ // in: query
160+ // description: "Mark notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread."
161+ // type: array
162+ // collectionFormat: multi
163+ // items:
164+ // type: string
165+ // required: false
166+ // - name: to-status
167+ // in: query
168+ // description: Status to mark notifications as. Defaults to read.
169+ // type: string
170+ // required: false
116171 // - name: last_read_at
117172 // in: query
118173 // description: Describes the last point that notifications were checked. Anything updated since this time will not be updated.
@@ -135,20 +190,31 @@ func ReadRepoNotifications(ctx *context.APIContext) {
135190 lastRead = tmpLastRead .Unix ()
136191 }
137192 }
193+
138194 opts := models.FindNotificationOptions {
139195 UserID : ctx .User .ID ,
140196 RepoID : ctx .Repo .Repository .ID ,
141197 UpdatedBeforeUnix : lastRead ,
142- Status : models .NotificationStatusUnread ,
198+ }
199+
200+ if ! ctx .QueryBool ("all" ) {
201+ statuses := ctx .QueryStrings ("status-types" )
202+ opts .Status = statusStringsToNotificationStatuses (statuses , []string {"unread" })
203+ log .Error ("%v" , opts .Status )
143204 }
144205 nl , err := models .GetNotifications (opts )
145206 if err != nil {
146207 ctx .InternalServerError (err )
147208 return
148209 }
149210
211+ targetStatus := statusStringToNotificationStatus (ctx .Query ("to-status" ))
212+ if targetStatus == 0 {
213+ targetStatus = models .NotificationStatusRead
214+ }
215+
150216 for _ , n := range nl {
151- err := models .SetNotificationStatus (n .ID , ctx .User , models . NotificationStatusRead )
217+ err := models .SetNotificationStatus (n .ID , ctx .User , targetStatus )
152218 if err != nil {
153219 ctx .InternalServerError (err )
154220 return
0 commit comments