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 (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
activities_model "code.gitea.io/gitea/models/activities"
|
2020-01-09 17:26:32 +05:30
|
|
|
"code.gitea.io/gitea/modules/context"
|
2020-12-02 14:54:35 +05:30
|
|
|
"code.gitea.io/gitea/modules/convert"
|
2021-09-18 05:10:50 +05:30
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2020-01-09 17:26:32 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// ListNotifications list users's notification threads
|
|
|
|
func ListNotifications(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /notifications notification notifyGetList
|
|
|
|
// ---
|
|
|
|
// summary: List users's notification threads
|
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: all
|
|
|
|
// in: query
|
|
|
|
// description: If true, show notifications marked as read. Default value is false
|
2021-06-17 19:32:34 +05:30
|
|
|
// type: boolean
|
2020-07-12 03:16:01 +05:30
|
|
|
// - name: status-types
|
|
|
|
// in: query
|
|
|
|
// description: "Show notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread & pinned."
|
|
|
|
// type: array
|
|
|
|
// collectionFormat: multi
|
|
|
|
// items:
|
|
|
|
// type: string
|
2021-06-16 22:34:37 +05:30
|
|
|
// - name: subject-type
|
|
|
|
// in: query
|
|
|
|
// description: "filter notifications by subject type"
|
|
|
|
// type: array
|
|
|
|
// collectionFormat: multi
|
|
|
|
// items:
|
|
|
|
// type: string
|
|
|
|
// enum: [issue,pull,commit,repository]
|
2020-01-09 17:26:32 +05:30
|
|
|
// - name: since
|
|
|
|
// in: query
|
|
|
|
// description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
|
|
|
|
// type: string
|
|
|
|
// format: date-time
|
|
|
|
// - name: before
|
|
|
|
// in: query
|
|
|
|
// description: Only show notifications updated before the given time. This is a timestamp in RFC 3339 format
|
|
|
|
// type: string
|
|
|
|
// format: date-time
|
2020-01-25 00:30:29 +05:30
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-09 10:27:38 +05:30
|
|
|
// description: page size of results
|
2020-01-25 00:30:29 +05:30
|
|
|
// type: integer
|
2020-01-09 17:26:32 +05:30
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/NotificationThreadList"
|
2021-06-16 22:34:37 +05:30
|
|
|
opts := getFindNotificationOptions(ctx)
|
|
|
|
if ctx.Written() {
|
2020-01-09 17:26:32 +05:30
|
|
|
return
|
|
|
|
}
|
2021-06-16 22:34:37 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
totalCount, err := activities_model.CountNotifications(ctx, opts)
|
2021-08-12 18:13:08 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
nl, err := activities_model.GetNotifications(ctx, opts)
|
2020-01-09 17:26:32 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 13:42:33 +05:30
|
|
|
err = nl.LoadAttributes(ctx)
|
2020-01-09 17:26:32 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-12 18:13:08 +05:30
|
|
|
ctx.SetTotalCountHeader(totalCount)
|
2020-12-02 14:54:35 +05:30
|
|
|
ctx.JSON(http.StatusOK, convert.ToNotifications(nl))
|
2020-01-09 17:26:32 +05:30
|
|
|
}
|
|
|
|
|
2020-07-12 03:16:01 +05:30
|
|
|
// ReadNotifications mark notification threads as read, unread, or pinned
|
2020-01-09 17:26:32 +05:30
|
|
|
func ReadNotifications(ctx *context.APIContext) {
|
|
|
|
// swagger:operation PUT /notifications notification notifyReadList
|
|
|
|
// ---
|
2020-07-12 03:16:01 +05:30
|
|
|
// summary: Mark notification threads as read, pinned or unread
|
2020-01-09 17:26:32 +05:30
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: last_read_at
|
|
|
|
// in: query
|
|
|
|
// description: Describes the last point that notifications were checked. Anything updated since this time will not be updated.
|
|
|
|
// type: string
|
|
|
|
// format: date-time
|
|
|
|
// required: false
|
2020-07-12 03:16:01 +05:30
|
|
|
// - name: all
|
|
|
|
// in: query
|
|
|
|
// description: If true, mark all notifications on this repo. Default value is false
|
|
|
|
// type: string
|
|
|
|
// required: false
|
|
|
|
// - name: status-types
|
|
|
|
// in: query
|
|
|
|
// description: "Mark notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread."
|
|
|
|
// type: array
|
|
|
|
// collectionFormat: multi
|
|
|
|
// items:
|
|
|
|
// type: string
|
|
|
|
// required: false
|
|
|
|
// - name: to-status
|
|
|
|
// in: query
|
|
|
|
// description: Status to mark notifications as, Defaults to read.
|
|
|
|
// type: string
|
|
|
|
// required: false
|
2020-01-09 17:26:32 +05:30
|
|
|
// responses:
|
|
|
|
// "205":
|
2021-09-18 05:10:50 +05:30
|
|
|
// "$ref": "#/responses/NotificationThreadList"
|
2020-01-09 17:26:32 +05:30
|
|
|
|
|
|
|
lastRead := int64(0)
|
2021-08-11 20:38:52 +05:30
|
|
|
qLastRead := ctx.FormTrim("last_read_at")
|
2020-01-09 17:26:32 +05:30
|
|
|
if len(qLastRead) > 0 {
|
|
|
|
tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
|
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !tmpLastRead.IsZero() {
|
|
|
|
lastRead = tmpLastRead.Unix()
|
|
|
|
}
|
|
|
|
}
|
2022-08-25 08:01:57 +05:30
|
|
|
opts := &activities_model.FindNotificationOptions{
|
2022-03-22 12:33:22 +05:30
|
|
|
UserID: ctx.Doer.ID,
|
2020-01-09 17:26:32 +05:30
|
|
|
UpdatedBeforeUnix: lastRead,
|
2020-07-12 03:16:01 +05:30
|
|
|
}
|
2021-07-29 07:12:15 +05:30
|
|
|
if !ctx.FormBool("all") {
|
|
|
|
statuses := ctx.FormStrings("status-types")
|
2020-07-12 03:16:01 +05:30
|
|
|
opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread"})
|
2020-01-09 17:26:32 +05:30
|
|
|
}
|
2022-08-25 08:01:57 +05:30
|
|
|
nl, err := activities_model.GetNotifications(ctx, opts)
|
2020-01-09 17:26:32 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-18 05:10:50 +05:30
|
|
|
changed := make([]*structs.NotificationThread, 0, len(nl))
|
|
|
|
|
2020-01-09 17:26:32 +05:30
|
|
|
for _, n := range nl {
|
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
|
|
|
_ = notif.LoadAttributes(ctx)
|
2021-09-18 05:10:50 +05:30
|
|
|
changed = append(changed, convert.ToNotificationThread(notif))
|
2020-01-09 17:26:32 +05:30
|
|
|
}
|
|
|
|
|
2021-09-18 05:10:50 +05:30
|
|
|
ctx.JSON(http.StatusResetContent, changed)
|
2020-01-09 17:26:32 +05:30
|
|
|
}
|