code.gitea.io/gitea@v1.21.7/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/modules/context"
    12  	api "code.gitea.io/gitea/modules/structs"
    13  	"code.gitea.io/gitea/routers/api/v1/utils"
    14  )
    15  
    16  // NewAvailable check if unread notifications exist
    17  func NewAvailable(ctx *context.APIContext) {
    18  	// swagger:operation GET /notifications/new notification notifyNewAvailable
    19  	// ---
    20  	// summary: Check if unread notifications exist
    21  	// responses:
    22  	//   "200":
    23  	//     "$ref": "#/responses/NotificationCount"
    24  	ctx.JSON(http.StatusOK, api.NotificationCount{New: activities_model.CountUnread(ctx, ctx.Doer.ID)})
    25  }
    26  
    27  func getFindNotificationOptions(ctx *context.APIContext) *activities_model.FindNotificationOptions {
    28  	before, since, err := context.GetQueryBeforeSince(ctx.Base)
    29  	if err != nil {
    30  		ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
    31  		return nil
    32  	}
    33  	opts := &activities_model.FindNotificationOptions{
    34  		ListOptions:       utils.GetListOptions(ctx),
    35  		UserID:            ctx.Doer.ID,
    36  		UpdatedBeforeUnix: before,
    37  		UpdatedAfterUnix:  since,
    38  	}
    39  	if !ctx.FormBool("all") {
    40  		statuses := ctx.FormStrings("status-types")
    41  		opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread", "pinned"})
    42  	}
    43  
    44  	subjectTypes := ctx.FormStrings("subject-type")
    45  	if len(subjectTypes) != 0 {
    46  		opts.Source = subjectToSource(subjectTypes)
    47  	}
    48  
    49  	return opts
    50  }
    51  
    52  func subjectToSource(value []string) (result []activities_model.NotificationSource) {
    53  	for _, v := range value {
    54  		switch strings.ToLower(v) {
    55  		case "issue":
    56  			result = append(result, activities_model.NotificationSourceIssue)
    57  		case "pull":
    58  			result = append(result, activities_model.NotificationSourcePullRequest)
    59  		case "commit":
    60  			result = append(result, activities_model.NotificationSourceCommit)
    61  		case "repository":
    62  			result = append(result, activities_model.NotificationSourceRepository)
    63  		}
    64  	}
    65  	return result
    66  }