2020-01-09 17:26:32 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-09 17:26:32 +05:30
|
|
|
|
|
|
|
package notify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
activities_model "code.gitea.io/gitea/models/activities"
|
2022-05-08 19:16:34 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 15:07:59 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2020-01-09 17:26:32 +05:30
|
|
|
"code.gitea.io/gitea/modules/context"
|
2022-12-29 08:27:15 +05:30
|
|
|
"code.gitea.io/gitea/services/convert"
|
2020-01-09 17:26:32 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// 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-11-19 13:42:33 +05:30
|
|
|
if err := n.LoadAttributes(ctx); err != nil && !issues_model.IsErrCommentNotExist(err) {
|
2020-01-09 17:26:32 +05:30
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-02 14:54:35 +05:30
|
|
|
ctx.JSON(http.StatusOK, convert.ToNotificationThread(n))
|
2020-01-09 17:26:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// 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 03:16:01 +05:30
|
|
|
// - name: to-status
|
|
|
|
// in: query
|
|
|
|
// description: Status to mark notifications as
|
|
|
|
// type: string
|
|
|
|
// default: read
|
|
|
|
// required: false
|
2020-01-09 17:26:32 +05:30
|
|
|
// responses:
|
|
|
|
// "205":
|
2021-09-18 05:10:50 +05:30
|
|
|
// "$ref": "#/responses/NotificationThread"
|
2020-01-09 17:26:32 +05:30
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
|
|
n := getThread(ctx)
|
|
|
|
if n == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:01:13 +05:30
|
|
|
targetStatus := statusStringToNotificationStatus(ctx.FormString("to-status"))
|
2020-07-12 03:16:01 +05:30
|
|
|
if targetStatus == 0 {
|
2022-08-25 08:01:57 +05:30
|
|
|
targetStatus = activities_model.NotificationStatusRead
|
2020-07-12 03:16:01 +05:30
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
notif, err := activities_model.SetNotificationStatus(ctx, n.ID, ctx.Doer, targetStatus)
|
2020-01-09 17:26:32 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 13:42:33 +05:30
|
|
|
if err = notif.LoadAttributes(ctx); err != nil && !issues_model.IsErrCommentNotExist(err) {
|
2021-09-18 05:10:50 +05:30
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusResetContent, convert.ToNotificationThread(notif))
|
2020-01-09 17:26:32 +05:30
|
|
|
}
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
func getThread(ctx *context.APIContext) *activities_model.Notification {
|
2022-11-19 13:42:33 +05:30
|
|
|
n, err := activities_model.GetNotificationByID(ctx, ctx.ParamsInt64(":id"))
|
2020-01-09 17:26:32 +05:30
|
|
|
if err != nil {
|
2022-05-08 19:16:34 +05:30
|
|
|
if db.IsErrNotExist(err) {
|
2020-01-09 17:26:32 +05:30
|
|
|
ctx.Error(http.StatusNotFound, "GetNotificationByID", err)
|
|
|
|
} else {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-22 12:33:22 +05:30
|
|
|
if n.UserID != ctx.Doer.ID && !ctx.Doer.IsAdmin {
|
2020-01-09 17:26:32 +05:30
|
|
|
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
|
|
|
|
}
|