2020-01-14 21:07:19 +05:30
|
|
|
// 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 (
|
|
|
|
"net/http"
|
2021-06-16 22:34:37 +05:30
|
|
|
"strings"
|
2020-01-14 21:07:19 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-06-16 22:34:37 +05:30
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2020-01-14 21:07:19 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// 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 05:19:39 +05:30
|
|
|
// "$ref": "#/responses/NotificationCount"
|
2022-05-20 19:38:52 +05:30
|
|
|
ctx.JSON(http.StatusOK, api.NotificationCount{New: models.CountUnread(ctx, ctx.Doer.ID)})
|
2020-01-14 21:07:19 +05:30
|
|
|
}
|
2021-06-16 22:34:37 +05:30
|
|
|
|
|
|
|
func getFindNotificationOptions(ctx *context.APIContext) *models.FindNotificationOptions {
|
2022-04-08 00:29:56 +05:30
|
|
|
before, since, err := context.GetQueryBeforeSince(ctx.Context)
|
2021-06-16 22:34:37 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
opts := &models.FindNotificationOptions{
|
|
|
|
ListOptions: utils.GetListOptions(ctx),
|
2022-03-22 12:33:22 +05:30
|
|
|
UserID: ctx.Doer.ID,
|
2021-06-16 22:34:37 +05:30
|
|
|
UpdatedBeforeUnix: before,
|
|
|
|
UpdatedAfterUnix: since,
|
|
|
|
}
|
2021-07-29 07:12:15 +05:30
|
|
|
if !ctx.FormBool("all") {
|
|
|
|
statuses := ctx.FormStrings("status-types")
|
2021-06-16 22:34:37 +05:30
|
|
|
opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread", "pinned"})
|
|
|
|
}
|
|
|
|
|
2021-07-29 07:12:15 +05:30
|
|
|
subjectTypes := ctx.FormStrings("subject-type")
|
2021-06-16 22:34:37 +05:30
|
|
|
if len(subjectTypes) != 0 {
|
|
|
|
opts.Source = subjectToSource(subjectTypes)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func subjectToSource(value []string) (result []models.NotificationSource) {
|
|
|
|
for _, v := range value {
|
|
|
|
switch strings.ToLower(v) {
|
|
|
|
case "issue":
|
|
|
|
result = append(result, models.NotificationSourceIssue)
|
|
|
|
case "pull":
|
|
|
|
result = append(result, models.NotificationSourcePullRequest)
|
|
|
|
case "commit":
|
|
|
|
result = append(result, models.NotificationSourceCommit)
|
|
|
|
case "repository":
|
|
|
|
result = append(result, models.NotificationSourceRepository)
|
|
|
|
}
|
|
|
|
}
|
2022-06-20 15:32:49 +05:30
|
|
|
return result
|
2021-06-16 22:34:37 +05:30
|
|
|
}
|