mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-05-28 03:00:03 +02:00
[API] Add notification endpoint (#9488)
* [API] Add notification endpoints * add func GetNotifications(opts FindNotificationOptions) * add func (n *Notification) APIFormat() * add func (nl NotificationList) APIFormat() * add func (n *Notification) APIURL() * add func (nl NotificationList) APIFormat() * add LoadAttributes functions (loadRepo, loadIssue, loadComment, loadUser) * add func (c *Comment) APIURL() * add func (issue *Issue) GetLastComment() * add endpoint GET /notifications * add endpoint PUT /notifications * add endpoint GET /repos/{owner}/{repo}/notifications * add endpoint PUT /repos/{owner}/{repo}/notifications * add endpoint GET /notifications/threads/{id} * add endpoint PATCH /notifications/threads/{id} * Add TEST * code format * code format
This commit is contained in:
parent
ee9ce0cfa9
commit
6baa5d7588
15 changed files with 1124 additions and 28 deletions
101
routers/api/v1/notify/threads.go
Normal file
101
routers/api/v1/notify/threads.go
Normal file
|
@ -0,0 +1,101 @@
|
|||
// 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 (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
)
|
||||
|
||||
// GetThread get notification by ID
|
||||
func GetThread(ctx *context.APIContext) {
|
||||
// swagger:operation GET /notifications/threads/{id} notification notifyGetThread
|
||||
// ---
|
||||
// summary: Get notification thread by ID
|
||||
// consumes:
|
||||
// - application/json
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: id
|
||||
// in: path
|
||||
// description: id of notification thread
|
||||
// type: string
|
||||
// required: true
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/NotificationThread"
|
||||
// "403":
|
||||
// "$ref": "#/responses/forbidden"
|
||||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
|
||||
n := getThread(ctx)
|
||||
if n == nil {
|
||||
return
|
||||
}
|
||||
if err := n.LoadAttributes(); err != nil {
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, n.APIFormat())
|
||||
}
|
||||
|
||||
// ReadThread mark notification as read by ID
|
||||
func ReadThread(ctx *context.APIContext) {
|
||||
// swagger:operation PATCH /notifications/threads/{id} notification notifyReadThread
|
||||
// ---
|
||||
// summary: Mark notification thread as read by ID
|
||||
// consumes:
|
||||
// - application/json
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: id
|
||||
// in: path
|
||||
// description: id of notification thread
|
||||
// type: string
|
||||
// required: true
|
||||
// responses:
|
||||
// "205":
|
||||
// "$ref": "#/responses/empty"
|
||||
// "403":
|
||||
// "$ref": "#/responses/forbidden"
|
||||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
|
||||
n := getThread(ctx)
|
||||
if n == nil {
|
||||
return
|
||||
}
|
||||
|
||||
err := models.SetNotificationStatus(n.ID, ctx.User, models.NotificationStatusRead)
|
||||
if err != nil {
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
ctx.Status(http.StatusResetContent)
|
||||
}
|
||||
|
||||
func getThread(ctx *context.APIContext) *models.Notification {
|
||||
n, err := models.GetNotificationByID(ctx.ParamsInt64(":id"))
|
||||
if err != nil {
|
||||
if models.IsErrNotExist(err) {
|
||||
ctx.Error(http.StatusNotFound, "GetNotificationByID", err)
|
||||
} else {
|
||||
ctx.InternalServerError(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if n.UserID != ctx.User.ID && !ctx.User.IsAdmin {
|
||||
ctx.Error(http.StatusForbidden, "GetNotificationByID", fmt.Errorf("only user itself and admin are allowed to read/change this thread %d", n.ID))
|
||||
return nil
|
||||
}
|
||||
return n
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue