mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
119 lines
3.0 KiB
Go
119 lines
3.0 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package notify
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
activities_model "github.com/hanzoai/git/models/activities"
|
|
"github.com/hanzoai/git/models/db"
|
|
issues_model "github.com/hanzoai/git/models/issues"
|
|
"github.com/hanzoai/git/services/context"
|
|
"github.com/hanzoai/git/services/convert"
|
|
)
|
|
|
|
// 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(ctx); err != nil && !issues_model.IsErrCommentNotExist(err) {
|
|
ctx.APIErrorInternal(err)
|
|
return
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, convert.ToNotificationThread(ctx, n))
|
|
}
|
|
|
|
// 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
|
|
// - name: to-status
|
|
// in: query
|
|
// description: Status to mark notifications as
|
|
// type: string
|
|
// default: read
|
|
// required: false
|
|
// responses:
|
|
// "205":
|
|
// "$ref": "#/responses/NotificationThread"
|
|
// "403":
|
|
// "$ref": "#/responses/forbidden"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
n := getThread(ctx)
|
|
if n == nil {
|
|
return
|
|
}
|
|
|
|
targetStatus := statusStringToNotificationStatus(ctx.FormString("to-status"))
|
|
if targetStatus == 0 {
|
|
targetStatus = activities_model.NotificationStatusRead
|
|
}
|
|
|
|
notif, err := activities_model.SetNotificationStatus(ctx, n.ID, ctx.Doer, targetStatus)
|
|
if err != nil {
|
|
ctx.APIErrorInternal(err)
|
|
return
|
|
}
|
|
if err = notif.LoadAttributes(ctx); err != nil && !issues_model.IsErrCommentNotExist(err) {
|
|
ctx.APIErrorInternal(err)
|
|
return
|
|
}
|
|
ctx.JSON(http.StatusResetContent, convert.ToNotificationThread(ctx, notif))
|
|
}
|
|
|
|
func getThread(ctx *context.APIContext) *activities_model.Notification {
|
|
n, err := activities_model.GetNotificationByID(ctx, ctx.PathParamInt64("id"))
|
|
if err != nil {
|
|
if db.IsErrNotExist(err) {
|
|
ctx.APIError(http.StatusNotFound, err.Error())
|
|
} else {
|
|
ctx.APIErrorInternal(err)
|
|
}
|
|
return nil
|
|
}
|
|
if n.UserID != ctx.Doer.ID && !ctx.Doer.IsAdmin {
|
|
ctx.APIError(http.StatusForbidden, fmt.Sprintf("only user itself and admin are allowed to read/change this thread %d", n.ID))
|
|
return nil
|
|
}
|
|
return n
|
|
}
|