2017-07-12 09:23:41 +08:00
|
|
|
// Copyright 2017 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 utils
|
|
|
|
|
2020-01-09 05:14:00 +08:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-01-25 03:00:29 +08:00
|
|
|
"code.gitea.io/gitea/models"
|
2020-01-09 05:14:00 +08:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2020-01-25 03:00:29 +08:00
|
|
|
"code.gitea.io/gitea/modules/convert"
|
2020-01-09 05:14:00 +08:00
|
|
|
)
|
2017-07-12 09:23:41 +08:00
|
|
|
|
|
|
|
// UserID user ID of authenticated user, or 0 if not authenticated
|
|
|
|
func UserID(ctx *context.APIContext) int64 {
|
|
|
|
if ctx.User == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return ctx.User.ID
|
|
|
|
}
|
2020-01-09 05:14:00 +08:00
|
|
|
|
|
|
|
// GetQueryBeforeSince return parsed time (unix format) from URL query's before and since
|
|
|
|
func GetQueryBeforeSince(ctx *context.APIContext) (before, since int64, err error) {
|
|
|
|
qCreatedBefore := strings.Trim(ctx.Query("before"), " ")
|
|
|
|
if qCreatedBefore != "" {
|
|
|
|
createdBefore, err := time.Parse(time.RFC3339, qCreatedBefore)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
if !createdBefore.IsZero() {
|
|
|
|
before = createdBefore.Unix()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qCreatedAfter := strings.Trim(ctx.Query("since"), " ")
|
|
|
|
if qCreatedAfter != "" {
|
|
|
|
createdAfter, err := time.Parse(time.RFC3339, qCreatedAfter)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
if !createdAfter.IsZero() {
|
|
|
|
since = createdAfter.Unix()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return before, since, nil
|
|
|
|
}
|
2020-01-25 03:00:29 +08:00
|
|
|
|
|
|
|
// GetListOptions returns list options using the page and limit parameters
|
|
|
|
func GetListOptions(ctx *context.APIContext) models.ListOptions {
|
|
|
|
return models.ListOptions{
|
|
|
|
Page: ctx.QueryInt("page"),
|
|
|
|
PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
|
|
|
|
}
|
|
|
|
}
|