Skip to content

Commit 0ffad31

Browse files
noerwzeripath
andauthored
Notifications API: respond with updated notifications (#17064)
* notifications api: return updated notifications in response * make generate-swagger * openapi fix Co-authored-by: zeripath <art27@cantab.net>
1 parent ba2e600 commit 0ffad31

File tree

7 files changed

+37
-23
lines changed

7 files changed

+37
-23
lines changed

models/notification.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -772,20 +772,20 @@ func setRepoNotificationStatusReadIfUnread(e Engine, userID, repoID int64) error
772772
}
773773

774774
// SetNotificationStatus change the notification status
775-
func SetNotificationStatus(notificationID int64, user *User, status NotificationStatus) error {
775+
func SetNotificationStatus(notificationID int64, user *User, status NotificationStatus) (*Notification, error) {
776776
notification, err := getNotificationByID(x, notificationID)
777777
if err != nil {
778-
return err
778+
return notification, err
779779
}
780780

781781
if notification.UserID != user.ID {
782-
return fmt.Errorf("Can't change notification of another user: %d, %d", notification.UserID, user.ID)
782+
return nil, fmt.Errorf("Can't change notification of another user: %d, %d", notification.UserID, user.ID)
783783
}
784784

785785
notification.Status = status
786786

787787
_, err = x.ID(notificationID).Update(notification)
788-
return err
788+
return notification, err
789789
}
790790

791791
// GetNotificationByID return notification by ID

models/notification_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,15 @@ func TestSetNotificationStatus(t *testing.T) {
7676
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
7777
notf := AssertExistsAndLoadBean(t,
7878
&Notification{UserID: user.ID, Status: NotificationStatusRead}).(*Notification)
79-
assert.NoError(t, SetNotificationStatus(notf.ID, user, NotificationStatusPinned))
79+
_, err := SetNotificationStatus(notf.ID, user, NotificationStatusPinned)
80+
assert.NoError(t, err)
8081
AssertExistsAndLoadBean(t,
8182
&Notification{ID: notf.ID, Status: NotificationStatusPinned})
8283

83-
assert.Error(t, SetNotificationStatus(1, user, NotificationStatusRead))
84-
assert.Error(t, SetNotificationStatus(NonexistentID, user, NotificationStatusRead))
84+
_, err = SetNotificationStatus(1, user, NotificationStatusRead)
85+
assert.Error(t, err)
86+
_, err = SetNotificationStatus(NonexistentID, user, NotificationStatusRead)
87+
assert.Error(t, err)
8588
}
8689

8790
func TestUpdateNotificationStatuses(t *testing.T) {

routers/api/v1/notify/repo.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/modules/context"
1414
"code.gitea.io/gitea/modules/convert"
1515
"code.gitea.io/gitea/modules/log"
16+
"code.gitea.io/gitea/modules/structs"
1617
)
1718

1819
func statusStringToNotificationStatus(status string) models.NotificationStatus {
@@ -176,7 +177,7 @@ func ReadRepoNotifications(ctx *context.APIContext) {
176177
// required: false
177178
// responses:
178179
// "205":
179-
// "$ref": "#/responses/empty"
180+
// "$ref": "#/responses/NotificationThreadList"
180181

181182
lastRead := int64(0)
182183
qLastRead := ctx.FormTrim("last_read_at")
@@ -213,14 +214,16 @@ func ReadRepoNotifications(ctx *context.APIContext) {
213214
targetStatus = models.NotificationStatusRead
214215
}
215216

217+
changed := make([]*structs.NotificationThread, len(nl))
218+
216219
for _, n := range nl {
217-
err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
220+
notif, err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
218221
if err != nil {
219222
ctx.InternalServerError(err)
220223
return
221224
}
222-
ctx.Status(http.StatusResetContent)
225+
_ = notif.LoadAttributes()
226+
changed = append(changed, convert.ToNotificationThread(notif))
223227
}
224-
225-
ctx.Status(http.StatusResetContent)
228+
ctx.JSON(http.StatusResetContent, changed)
226229
}

routers/api/v1/notify/threads.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func ReadThread(ctx *context.APIContext) {
7171
// required: false
7272
// responses:
7373
// "205":
74-
// "$ref": "#/responses/empty"
74+
// "$ref": "#/responses/NotificationThread"
7575
// "403":
7676
// "$ref": "#/responses/forbidden"
7777
// "404":
@@ -87,12 +87,16 @@ func ReadThread(ctx *context.APIContext) {
8787
targetStatus = models.NotificationStatusRead
8888
}
8989

90-
err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
90+
notif, err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
9191
if err != nil {
9292
ctx.InternalServerError(err)
9393
return
9494
}
95-
ctx.Status(http.StatusResetContent)
95+
if err = notif.LoadAttributes(); err != nil {
96+
ctx.InternalServerError(err)
97+
return
98+
}
99+
ctx.JSON(http.StatusResetContent, convert.ToNotificationThread(notif))
96100
}
97101

98102
func getThread(ctx *context.APIContext) *models.Notification {

routers/api/v1/notify/user.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"code.gitea.io/gitea/models"
1212
"code.gitea.io/gitea/modules/context"
1313
"code.gitea.io/gitea/modules/convert"
14+
"code.gitea.io/gitea/modules/structs"
1415
)
1516

1617
// ListNotifications list users's notification threads
@@ -125,7 +126,7 @@ func ReadNotifications(ctx *context.APIContext) {
125126
// required: false
126127
// responses:
127128
// "205":
128-
// "$ref": "#/responses/empty"
129+
// "$ref": "#/responses/NotificationThreadList"
129130

130131
lastRead := int64(0)
131132
qLastRead := ctx.FormTrim("last_read_at")
@@ -158,14 +159,17 @@ func ReadNotifications(ctx *context.APIContext) {
158159
targetStatus = models.NotificationStatusRead
159160
}
160161

162+
changed := make([]*structs.NotificationThread, 0, len(nl))
163+
161164
for _, n := range nl {
162-
err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
165+
notif, err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
163166
if err != nil {
164167
ctx.InternalServerError(err)
165168
return
166169
}
167-
ctx.Status(http.StatusResetContent)
170+
_ = notif.LoadAttributes()
171+
changed = append(changed, convert.ToNotificationThread(notif))
168172
}
169173

170-
ctx.Status(http.StatusResetContent)
174+
ctx.JSON(http.StatusResetContent, changed)
171175
}

routers/web/user/notification.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func NotificationStatusPost(c *context.Context) {
160160
return
161161
}
162162

163-
if err := models.SetNotificationStatus(notificationID, c.User, status); err != nil {
163+
if _, err := models.SetNotificationStatus(notificationID, c.User, status); err != nil {
164164
c.ServerError("SetNotificationStatus", err)
165165
return
166166
}

templates/swagger/v1_json.tmpl

+3-3
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@
739739
],
740740
"responses": {
741741
"205": {
742-
"$ref": "#/responses/empty"
742+
"$ref": "#/responses/NotificationThreadList"
743743
}
744744
}
745745
}
@@ -822,7 +822,7 @@
822822
],
823823
"responses": {
824824
"205": {
825-
"$ref": "#/responses/empty"
825+
"$ref": "#/responses/NotificationThread"
826826
},
827827
"403": {
828828
"$ref": "#/responses/forbidden"
@@ -7058,7 +7058,7 @@
70587058
],
70597059
"responses": {
70607060
"205": {
7061-
"$ref": "#/responses/empty"
7061+
"$ref": "#/responses/NotificationThreadList"
70627062
}
70637063
}
70647064
}

0 commit comments

Comments
 (0)