Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] add endpoint to check notifications [Extend #9488] #9595

Merged
merged 6 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
return number of notifications if unreaded exist
  • Loading branch information
6543 committed Jan 14, 2020
commit 42ef28e97b5b798eb46a28bdc154a64beda7c027
14 changes: 7 additions & 7 deletions models/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,16 @@ func notificationsForUser(e Engine, user *User, statuses []NotificationStatus, p
return
}

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

func unreadAvailable(e Engine, userID int64) bool {
exist, err := e.Where("user_id = ?", userID).And("status = ?", NotificationStatusUnread).Get(new(Notification))
func countUnread(e Engine, userID int64) int64 {
exist, err := e.Where("user_id = ?", userID).And("status = ?", NotificationStatusUnread).Count(new(Notification))
if err != nil {
log.Error("newsAvailable", err)
return false
log.Error("countUnread", err)
return 0
}
return exist
}
Expand Down
5 changes: 5 additions & 0 deletions modules/structs/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ type NotificationSubject struct {
LatestCommentURL string `json:"latest_comment_url"`
Type string `json:"type" binding:"In(Issue,Pull,Commit)"`
}

// NotificationCount number of unread notifications
type NotificationCount struct {
New int64 `json:"new"`
}
9 changes: 6 additions & 3 deletions routers/api/v1/notify/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
api "code.gitea.io/gitea/modules/structs"
)

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

if models.UnreadAvailable(ctx.User) {
ctx.Status(http.StatusFound)
count := models.CountUnread(ctx.User)

if count > 0 {
ctx.JSON(http.StatusFound, api.NotificationCount{New: count})
} else {
ctx.Status(http.StatusNoContent)
}
Expand Down
7 changes: 7 additions & 0 deletions routers/api/v1/swagger/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ type swaggerNotificationThreadList struct {
// in:body
Body []api.NotificationThread `json:"body"`
}

// Number of unread notifications
// swagger:response NotificationCount
type swaggerNotificationCount struct {
// in:body
Body api.NotificationCount `json:"body"`
}
20 changes: 19 additions & 1 deletion templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@
"description": "No unread notification"
},
"302": {
"description": "Unread notification found"
"$ref": "#/responses/NotificationCount"
}
}
}
Expand Down Expand Up @@ -10928,6 +10928,18 @@
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"NotificationCount": {
"description": "NotificationCount number of unread notifications",
"type": "object",
"properties": {
"new": {
"type": "integer",
"format": "int64",
"x-go-name": "New"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"NotificationSubject": {
"description": "NotificationSubject contains the notification subject (Issue/Pull/Commit)",
"type": "object",
Expand Down Expand Up @@ -12414,6 +12426,12 @@
}
}
},
"NotificationCount": {
"description": "Number of unread notifications",
"schema": {
"$ref": "#/definitions/NotificationCount"
}
},
"NotificationThread": {
"description": "NotificationThread",
"schema": {
Expand Down