Skip to content

Commit db2dca8

Browse files
committed
introduce GET /notifications/new
1 parent ce274d6 commit db2dca8

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

models/notification.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"path"
1010

11+
"code.gitea.io/gitea/modules/log"
1112
"code.gitea.io/gitea/modules/setting"
1213
api "code.gitea.io/gitea/modules/structs"
1314
"code.gitea.io/gitea/modules/timeutil"
@@ -294,6 +295,20 @@ func notificationsForUser(e Engine, user *User, statuses []NotificationStatus, p
294295
return
295296
}
296297

298+
// UnreadAvailable check if unread notifications exist
299+
func UnreadAvailable(user *User) bool {
300+
return unreadAvailable(x, user.ID)
301+
}
302+
303+
func unreadAvailable(e Engine, userID int64) bool {
304+
exist, err := e.Where("user_id = ?", userID).And("status = ?", NotificationStatusUnread).Get(new(Notification))
305+
if err != nil {
306+
log.Error("newsAvailable", err)
307+
return false
308+
}
309+
return exist
310+
}
311+
297312
// APIFormat converts a Notification to api.NotificationThread
298313
func (n *Notification) APIFormat() *api.NotificationThread {
299314
result := &api.NotificationThread{

routers/api/v1/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ func RegisterRoutes(m *macaron.Macaron) {
518518
m.Combo("").
519519
Get(notify.ListNotifications).
520520
Put(notify.ReadNotifications)
521+
m.Get("/new", notify.NewAvailable)
521522
m.Combo("/threads/:id").
522523
Get(notify.GetThread).
523524
Patch(notify.ReadThread)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package notify
6+
7+
import (
8+
"net/http"
9+
10+
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/modules/context"
12+
)
13+
14+
// NewAvailable check if unread notifications exist
15+
func NewAvailable(ctx *context.APIContext) {
16+
// swagger:operation GET /notifications/new notification notifyNewAvailable
17+
// ---
18+
// summary: Check if unread notifications exist
19+
// responses:
20+
// "204":
21+
// description: No unread notification
22+
// "302":
23+
// description: Unread notification found
24+
25+
if models.UnreadAvailable(ctx.User) {
26+
ctx.Status(http.StatusFound)
27+
} else {
28+
ctx.Status(http.StatusNoContent)
29+
}
30+
}

templates/swagger/v1_json.tmpl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,23 @@
494494
}
495495
}
496496
},
497+
"/notifications/new": {
498+
"get": {
499+
"tags": [
500+
"notification"
501+
],
502+
"summary": "Check if unread notifications exist",
503+
"operationId": "notifyNewAvailable",
504+
"responses": {
505+
"204": {
506+
"description": "No unread notification"
507+
},
508+
"302": {
509+
"description": "Unread notification found"
510+
}
511+
}
512+
}
513+
},
497514
"/notifications/threads/{id}": {
498515
"get": {
499516
"consumes": [

0 commit comments

Comments
 (0)