Skip to content

Commit 42ef28e

Browse files
committed
return number of notifications if unreaded exist
1 parent 049d907 commit 42ef28e

File tree

5 files changed

+44
-11
lines changed

5 files changed

+44
-11
lines changed

models/notification.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,16 @@ func notificationsForUser(e Engine, user *User, statuses []NotificationStatus, p
295295
return
296296
}
297297

298-
// UnreadAvailable check if unread notifications exist
299-
func UnreadAvailable(user *User) bool {
300-
return unreadAvailable(x, user.ID)
298+
// CountUnread count unread notifications for a user
299+
func CountUnread(user *User) int64 {
300+
return countUnread(x, user.ID)
301301
}
302302

303-
func unreadAvailable(e Engine, userID int64) bool {
304-
exist, err := e.Where("user_id = ?", userID).And("status = ?", NotificationStatusUnread).Get(new(Notification))
303+
func countUnread(e Engine, userID int64) int64 {
304+
exist, err := e.Where("user_id = ?", userID).And("status = ?", NotificationStatusUnread).Count(new(Notification))
305305
if err != nil {
306-
log.Error("newsAvailable", err)
307-
return false
306+
log.Error("countUnread", err)
307+
return 0
308308
}
309309
return exist
310310
}

modules/structs/notifications.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ type NotificationSubject struct {
2626
LatestCommentURL string `json:"latest_comment_url"`
2727
Type string `json:"type" binding:"In(Issue,Pull,Commit)"`
2828
}
29+
30+
// NotificationCount number of unread notifications
31+
type NotificationCount struct {
32+
New int64 `json:"new"`
33+
}

routers/api/v1/notify/notifications.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"code.gitea.io/gitea/models"
1111
"code.gitea.io/gitea/modules/context"
12+
api "code.gitea.io/gitea/modules/structs"
1213
)
1314

1415
// NewAvailable check if unread notifications exist
@@ -20,10 +21,12 @@ func NewAvailable(ctx *context.APIContext) {
2021
// "204":
2122
// description: No unread notification
2223
// "302":
23-
// description: Unread notification found
24+
// "$ref": "#/responses/NotificationCount"
2425

25-
if models.UnreadAvailable(ctx.User) {
26-
ctx.Status(http.StatusFound)
26+
count := models.CountUnread(ctx.User)
27+
28+
if count > 0 {
29+
ctx.JSON(http.StatusFound, api.NotificationCount{New: count})
2730
} else {
2831
ctx.Status(http.StatusNoContent)
2932
}

routers/api/v1/swagger/notify.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ type swaggerNotificationThreadList struct {
2121
// in:body
2222
Body []api.NotificationThread `json:"body"`
2323
}
24+
25+
// Number of unread notifications
26+
// swagger:response NotificationCount
27+
type swaggerNotificationCount struct {
28+
// in:body
29+
Body api.NotificationCount `json:"body"`
30+
}

templates/swagger/v1_json.tmpl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@
506506
"description": "No unread notification"
507507
},
508508
"302": {
509-
"description": "Unread notification found"
509+
"$ref": "#/responses/NotificationCount"
510510
}
511511
}
512512
}
@@ -10928,6 +10928,18 @@
1092810928
},
1092910929
"x-go-package": "code.gitea.io/gitea/modules/structs"
1093010930
},
10931+
"NotificationCount": {
10932+
"description": "NotificationCount number of unread notifications",
10933+
"type": "object",
10934+
"properties": {
10935+
"new": {
10936+
"type": "integer",
10937+
"format": "int64",
10938+
"x-go-name": "New"
10939+
}
10940+
},
10941+
"x-go-package": "code.gitea.io/gitea/modules/structs"
10942+
},
1093110943
"NotificationSubject": {
1093210944
"description": "NotificationSubject contains the notification subject (Issue/Pull/Commit)",
1093310945
"type": "object",
@@ -12414,6 +12426,12 @@
1241412426
}
1241512427
}
1241612428
},
12429+
"NotificationCount": {
12430+
"description": "Number of unread notifications",
12431+
"schema": {
12432+
"$ref": "#/definitions/NotificationCount"
12433+
}
12434+
},
1241712435
"NotificationThread": {
1241812436
"description": "NotificationThread",
1241912437
"schema": {

0 commit comments

Comments
 (0)