mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
75 lines
2.3 KiB
Go
75 lines
2.3 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package user
|
|
|
|
import (
|
|
"context"
|
|
"slices"
|
|
"strconv"
|
|
|
|
"github.com/hanzoai/git/models/user"
|
|
)
|
|
|
|
type SearchUserInfo struct {
|
|
UserID int64 `json:"user_id"`
|
|
UserName string `json:"username"`
|
|
AvatarLink string `json:"avatar_link"`
|
|
FullName string `json:"full_name"`
|
|
}
|
|
|
|
type SearchUserResponse struct {
|
|
Results []*SearchUserInfo `json:"results"`
|
|
}
|
|
|
|
func MakeSelfOnTop(doer *user.User, users []*user.User) []*user.User {
|
|
if doer == nil {
|
|
return users
|
|
}
|
|
idx := slices.IndexFunc(users, func(u *user.User) bool {
|
|
return u.ID == doer.ID
|
|
})
|
|
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
|
|
}
|
|
return users
|
|
}
|
|
|
|
func ToSearchUserResponse(ctx context.Context, self *user.User, users []*user.User) (ret *SearchUserResponse) {
|
|
infos := MakeSelfOnTop(self, users)
|
|
resp := &SearchUserResponse{Results: make([]*SearchUserInfo, len(infos))}
|
|
for i, u := range infos {
|
|
resp.Results[i] = &SearchUserInfo{UserID: u.ID, UserName: u.Name, AvatarLink: u.AvatarLink(ctx)}
|
|
resp.Results[i].FullName = u.FullName
|
|
}
|
|
return resp
|
|
}
|
|
|
|
// 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.
|
|
// Return values:
|
|
// * "": no filter
|
|
// * "{the-id}": match the id
|
|
// * "(none)": match no issue (due to the user doesn't exist)
|
|
func GetFilterUserIDByName(ctx context.Context, name string) string {
|
|
if name == "" {
|
|
return ""
|
|
}
|
|
u, err := user.GetUserByName(ctx, name)
|
|
if err != nil {
|
|
if id, err := strconv.ParseInt(name, 10, 64); err == nil {
|
|
return strconv.FormatInt(id, 10)
|
|
}
|
|
// The "(none)" is for internal usage only: when doer tries to search non-existing user, use "(none)" to return empty result.
|
|
return "(none)"
|
|
}
|
|
return strconv.FormatInt(u.ID, 10)
|
|
}
|