code.gitea.io/gitea@v1.22.3/routers/api/v1/notify/notifications.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package notify
     5  
     6  import (
     7  	"net/http"
     8  	"strings"
     9  
    10  	activities_model "code.gitea.io/gitea/models/activities"
    11  	"code.gitea.io/gitea/models/db"
    12  	api "code.gitea.io/gitea/modules/structs"
    13  	"code.gitea.io/gitea/routers/api/v1/utils"
    14  	"code.gitea.io/gitea/services/context"
    15  )
    16  
    17  // NewAvailable check if unread notifications exist
    18  func NewAvailable(ctx *context.APIContext) {
    19  	// swagger:operation GET /notifications/new notification notifyNewAvailable
    20  	// ---
    21  	// summary: Check if unread notifications exist
    22  	// responses:
    23  	//   "200":
    24  	//     "$ref": "#/responses/NotificationCount"
    25  
    26  	total, err := db.Count[activities_model.Notification](ctx, activities_model.FindNotificationOptions{
    27  		UserID: ctx.Doer.ID,
    28  		Status: []activities_model.NotificationStatus{activities_model.NotificationStatusUnread},
    29  	})
    30  	if err != nil {
    31  		ctx.Error(http.StatusUnprocessableEntity, "db.Count[activities_model.Notification]", err)
    32  		return
    33  	}
    34  
    35  	ctx.JSON(http.StatusOK, api.NotificationCount{New: total})
    36  }
    37  
    38  func getFindNotificationOptions(ctx *context.APIContext) *activities_model.FindNotificationOptions {
    39  	before, since, err := context.GetQueryBeforeSince(ctx.Base)
    40  	if err != nil {
    41  		ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
    42  		return nil
    43  	}
    44  	opts := &activities_model.FindNotificationOptions{
    45  		ListOptions:       utils.GetListOptions(ctx),
    46  		UserID:            ctx.Doer.ID,
    47  		UpdatedBeforeUnix: before,
    48  		UpdatedAfterUnix:  since,
    49  	}
    50  	if !ctx.FormBool("all") {
    51  		statuses := ctx.FormStrings("status-types")
    52  		opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread", "pinned"})
    53  	}
    54  
    55  	subjectTypes := ctx.FormStrings("subject-type")
    56  	if len(subjectTypes) != 0 {
    57  		opts.Source = subjectToSource(subjectTypes)
    58  	}
    59  
    60  	return opts
    61  }
    62  
    63  func subjectToSource(value []string) (result []activities_model.NotificationSource) {
    64  	for _, v := range value {
    65  		switch strings.ToLower(v) {
    66  		case "issue":
    67  			result = append(result, activities_model.NotificationSourceIssue)
    68  		case "pull":
    69  			result = append(result, activities_model.NotificationSourcePullRequest)
    70  		case "commit":
    71  			result = append(result, activities_model.NotificationSourceCommit)
    72  		case "repository":
    73  			result = append(result, activities_model.NotificationSourceRepository)
    74  		}
    75  	}
    76  	return result
    77  }