mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-20 08:11:02 +00:00
14a9687444
(extend #9200) * add query param for GET functions (created Bevore & after) * add test * generalize func GetQueryBeforeSince Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
// 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
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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
|
|
}
|