2020-01-09 14:56:32 +03:00
|
|
|
// 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"
|
2022-05-08 16:46:34 +03:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 12:37:59 +03:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2020-01-09 14:56:32 +03:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2020-12-02 12:24:35 +03:00
|
|
|
"code.gitea.io/gitea/modules/convert"
|
2020-01-09 14:56:32 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2022-06-13 12:37:59 +03:00
|
|
|
if err := n.LoadAttributes(); err != nil && !issues_model.IsErrCommentNotExist(err) {
|
2020-01-09 14:56:32 +03:00
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-02 12:24:35 +03:00
|
|
|
ctx.JSON(http.StatusOK, convert.ToNotificationThread(n))
|
2020-01-09 14:56:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2020-07-12 00:46:01 +03:00
|
|
|
// - name: to-status
|
|
|
|
// in: query
|
|
|
|
// description: Status to mark notifications as
|
|
|
|
// type: string
|
|
|
|
// default: read
|
|
|
|
// required: false
|
2020-01-09 14:56:32 +03:00
|
|
|
// responses:
|
|
|
|
// "205":
|
2021-09-18 02:40:50 +03:00
|
|
|
// "$ref": "#/responses/NotificationThread"
|
2020-01-09 14:56:32 +03:00
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
|
|
n := getThread(ctx)
|
|
|
|
if n == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-11 03:31:13 +03:00
|
|
|
targetStatus := statusStringToNotificationStatus(ctx.FormString("to-status"))
|
2020-07-12 00:46:01 +03:00
|
|
|
if targetStatus == 0 {
|
|
|
|
targetStatus = models.NotificationStatusRead
|
|
|
|
}
|
|
|
|
|
2022-03-22 10:03:22 +03:00
|
|
|
notif, err := models.SetNotificationStatus(n.ID, ctx.Doer, targetStatus)
|
2020-01-09 14:56:32 +03:00
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
2022-06-13 12:37:59 +03:00
|
|
|
if err = notif.LoadAttributes(); err != nil && !issues_model.IsErrCommentNotExist(err) {
|
2021-09-18 02:40:50 +03:00
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusResetContent, convert.ToNotificationThread(notif))
|
2020-01-09 14:56:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func getThread(ctx *context.APIContext) *models.Notification {
|
|
|
|
n, err := models.GetNotificationByID(ctx.ParamsInt64(":id"))
|
|
|
|
if err != nil {
|
2022-05-08 16:46:34 +03:00
|
|
|
if db.IsErrNotExist(err) {
|
2020-01-09 14:56:32 +03:00
|
|
|
ctx.Error(http.StatusNotFound, "GetNotificationByID", err)
|
|
|
|
} else {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-22 10:03:22 +03:00
|
|
|
if n.UserID != ctx.Doer.ID && !ctx.Doer.IsAdmin {
|
2020-01-09 14:56:32 +03:00
|
|
|
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
|
|
|
|
}
|