2024-09-12 05:53:40 +02:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
2024-12-08 13:44:17 +01:00
|
|
|
"context"
|
2024-12-04 15:57:50 +01:00
|
|
|
"slices"
|
2024-12-08 13:44:17 +01:00
|
|
|
"strconv"
|
2024-09-12 05:53:40 +02:00
|
|
|
|
2024-12-11 07:33:24 +01:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2024-09-12 05:53:40 +02:00
|
|
|
"code.gitea.io/gitea/models/user"
|
2024-12-11 07:33:24 +01:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2024-09-12 05:53:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func MakeSelfOnTop(doer *user.User, users []*user.User) []*user.User {
|
|
|
|
if doer != nil {
|
2024-12-04 15:57:50 +01:00
|
|
|
idx := slices.IndexFunc(users, func(u *user.User) bool {
|
|
|
|
return u.ID == doer.ID
|
2024-09-12 05:53:40 +02:00
|
|
|
})
|
2024-12-04 15:57:50 +01:00
|
|
|
if idx > 0 {
|
|
|
|
newUsers := make([]*user.User, len(users))
|
|
|
|
newUsers[0] = users[idx]
|
|
|
|
copy(newUsers[1:], users[:idx])
|
|
|
|
copy(newUsers[idx+1:], users[idx+1:])
|
|
|
|
return newUsers
|
|
|
|
}
|
2024-09-12 05:53:40 +02:00
|
|
|
}
|
|
|
|
return users
|
|
|
|
}
|
2024-12-08 13:44:17 +01:00
|
|
|
|
|
|
|
// GetFilterUserIDByName tries to get the user ID from the given username.
|
|
|
|
// Before, the "issue filter" passes user ID to query the list, but in many cases, it's impossible to pre-fetch the full user list.
|
|
|
|
// So it's better to make it work like GitHub: users could input username directly.
|
|
|
|
// Since it only converts the username to ID directly and is only used internally (to search issues), so no permission check is needed.
|
2024-12-11 07:33:24 +01:00
|
|
|
// Return values:
|
|
|
|
// * nil: no filter
|
|
|
|
// * some(id): match the id, the id could be -1 to match the issues without assignee
|
|
|
|
// * some(NonExistingID): match no issue (due to the user doesn't exist)
|
|
|
|
func GetFilterUserIDByName(ctx context.Context, name string) optional.Option[int64] {
|
2024-12-08 13:44:17 +01:00
|
|
|
if name == "" {
|
2024-12-11 07:33:24 +01:00
|
|
|
return optional.None[int64]()
|
2024-12-08 13:44:17 +01:00
|
|
|
}
|
|
|
|
u, err := user.GetUserByName(ctx, name)
|
|
|
|
if err != nil {
|
|
|
|
if id, err := strconv.ParseInt(name, 10, 64); err == nil {
|
2024-12-11 07:33:24 +01:00
|
|
|
return optional.Some(id)
|
2024-12-08 13:44:17 +01:00
|
|
|
}
|
2024-12-11 07:33:24 +01:00
|
|
|
return optional.Some(db.NonExistingID)
|
2024-12-08 13:44:17 +01:00
|
|
|
}
|
2024-12-11 07:33:24 +01:00
|
|
|
return optional.Some(u.ID)
|
2024-12-08 13:44:17 +01:00
|
|
|
}
|