Make GetPossibleUserByID can handle deleted user (#37430)

Make sure deleted user won't cause 500 error, simplify the caller's code
This commit is contained in:
wxiaoguang
2026-04-27 00:57:53 +08:00
committed by GitHub
parent 2f42c8cf72
commit 068b59aa97
11 changed files with 36 additions and 74 deletions
+13 -7
View File
@@ -7,6 +7,7 @@ package user
import (
"context"
"encoding/hex"
"errors"
"fmt"
"html/template"
"mime"
@@ -1013,17 +1014,22 @@ func GetUserByIDs(ctx context.Context, ids []int64) ([]*User, error) {
return users, err
}
// GetPossibleUserByID returns the user if id > 0 or returns system user if id < 0
func GetPossibleUserByID(ctx context.Context, id int64) (*User, error) {
// GetPossibleUserByID returns the possible user and its ID. If the user doesn't exist, it returns Ghost user
func GetPossibleUserByID(ctx context.Context, id int64) (_ int64, u *User, err error) {
if id < 0 {
if newFunc, ok := globalVars().systemUserNewFuncs[id]; ok {
return newFunc(), nil
u = newFunc()
}
return nil, ErrUserNotExist{UID: id}
} else if id == 0 {
return nil, ErrUserNotExist{}
}
return GetUserByID(ctx, id)
if u == nil {
u, err = GetUserByID(ctx, id)
if errors.Is(err, util.ErrNotExist) {
u = NewGhostUser()
} else if err != nil {
return 0, nil, err
}
}
return u.ID, u, nil
}
// GetPossibleUserByIDs returns the users if id > 0 or returns system users if id < 0