2020-01-14 16:37:19 +01:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-14 16:37:19 +01:00
|
|
|
|
|
|
|
package notify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2021-06-16 19:04:37 +02:00
|
|
|
"strings"
|
2020-01-14 16:37:19 +01:00
|
|
|
|
2022-08-25 04:31:57 +02:00
|
|
|
activities_model "code.gitea.io/gitea/models/activities"
|
2020-01-14 16:37:19 +01:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-06-16 19:04:37 +02:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2020-01-14 16:37:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewAvailable check if unread notifications exist
|
|
|
|
func NewAvailable(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /notifications/new notification notifyNewAvailable
|
|
|
|
// ---
|
|
|
|
// summary: Check if unread notifications exist
|
|
|
|
// responses:
|
|
|
|
// "200":
|
2020-04-11 01:49:39 +02:00
|
|
|
// "$ref": "#/responses/NotificationCount"
|
2022-08-25 04:31:57 +02:00
|
|
|
ctx.JSON(http.StatusOK, api.NotificationCount{New: activities_model.CountUnread(ctx, ctx.Doer.ID)})
|
2020-01-14 16:37:19 +01:00
|
|
|
}
|
2021-06-16 19:04:37 +02:00
|
|
|
|
2022-08-25 04:31:57 +02:00
|
|
|
func getFindNotificationOptions(ctx *context.APIContext) *activities_model.FindNotificationOptions {
|
2023-05-21 03:50:53 +02:00
|
|
|
before, since, err := context.GetQueryBeforeSince(ctx.Base)
|
2021-06-16 19:04:37 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-25 04:31:57 +02:00
|
|
|
opts := &activities_model.FindNotificationOptions{
|
2021-06-16 19:04:37 +02:00
|
|
|
ListOptions: utils.GetListOptions(ctx),
|
2022-03-22 08:03:22 +01:00
|
|
|
UserID: ctx.Doer.ID,
|
2021-06-16 19:04:37 +02:00
|
|
|
UpdatedBeforeUnix: before,
|
|
|
|
UpdatedAfterUnix: since,
|
|
|
|
}
|
2021-07-29 03:42:15 +02:00
|
|
|
if !ctx.FormBool("all") {
|
|
|
|
statuses := ctx.FormStrings("status-types")
|
2021-06-16 19:04:37 +02:00
|
|
|
opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread", "pinned"})
|
|
|
|
}
|
|
|
|
|
2021-07-29 03:42:15 +02:00
|
|
|
subjectTypes := ctx.FormStrings("subject-type")
|
2021-06-16 19:04:37 +02:00
|
|
|
if len(subjectTypes) != 0 {
|
|
|
|
opts.Source = subjectToSource(subjectTypes)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2022-08-25 04:31:57 +02:00
|
|
|
func subjectToSource(value []string) (result []activities_model.NotificationSource) {
|
2021-06-16 19:04:37 +02:00
|
|
|
for _, v := range value {
|
|
|
|
switch strings.ToLower(v) {
|
|
|
|
case "issue":
|
2022-08-25 04:31:57 +02:00
|
|
|
result = append(result, activities_model.NotificationSourceIssue)
|
2021-06-16 19:04:37 +02:00
|
|
|
case "pull":
|
2022-08-25 04:31:57 +02:00
|
|
|
result = append(result, activities_model.NotificationSourcePullRequest)
|
2021-06-16 19:04:37 +02:00
|
|
|
case "commit":
|
2022-08-25 04:31:57 +02:00
|
|
|
result = append(result, activities_model.NotificationSourceCommit)
|
2021-06-16 19:04:37 +02:00
|
|
|
case "repository":
|
2022-08-25 04:31:57 +02:00
|
|
|
result = append(result, activities_model.NotificationSourceRepository)
|
2021-06-16 19:04:37 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-20 12:02:49 +02:00
|
|
|
return result
|
2021-06-16 19:04:37 +02:00
|
|
|
}
|