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
Next Next commit
introduce GET /notifications/new
  • Loading branch information
6543 committed Jan 14, 2020
commit db2dca8b03740ea88995bfdbb8c05a81ad056a71
15 changes: 15 additions & 0 deletions models/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"path"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
Expand Down Expand Up @@ -294,6 +295,20 @@ 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)
}

func unreadAvailable(e Engine, userID int64) bool {
exist, err := e.Where("user_id = ?", userID).And("status = ?", NotificationStatusUnread).Get(new(Notification))
if err != nil {
log.Error("newsAvailable", err)
return false
}
return exist
}

// APIFormat converts a Notification to api.NotificationThread
func (n *Notification) APIFormat() *api.NotificationThread {
result := &api.NotificationThread{
Expand Down
1 change: 1 addition & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Combo("").
Get(notify.ListNotifications).
Put(notify.ReadNotifications)
m.Get("/new", notify.NewAvailable)
m.Combo("/threads/:id").
Get(notify.GetThread).
Patch(notify.ReadThread)
Expand Down
30 changes: 30 additions & 0 deletions routers/api/v1/notify/notifications.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package notify

import (
"net/http"

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

// NewAvailable check if unread notifications exist
func NewAvailable(ctx *context.APIContext) {
// swagger:operation GET /notifications/new notification notifyNewAvailable
// ---
// summary: Check if unread notifications exist
// responses:
// "204":
// description: No unread notification
// "302":
6543 marked this conversation as resolved.
Show resolved Hide resolved
// description: Unread notification found

if models.UnreadAvailable(ctx.User) {
ctx.Status(http.StatusFound)
} else {
ctx.Status(http.StatusNoContent)
}
}
17 changes: 17 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,23 @@
}
}
},
"/notifications/new": {
"get": {
"tags": [
"notification"
],
"summary": "Check if unread notifications exist",
"operationId": "notifyNewAvailable",
"responses": {
"204": {
"description": "No unread notification"
},
"302": {
6543 marked this conversation as resolved.
Show resolved Hide resolved
"description": "Unread notification found"
}
}
}
},
"/notifications/threads/{id}": {
"get": {
"consumes": [
Expand Down