Repository: go-gitea/gitea
File: models/user/user.go (Go)

Code before:
	_, ok := err.(ErrDeleteLastAdminUser)
	return ok
}

func (err ErrDeleteLastAdminUser) Error() string {
	return fmt.Sprintf("can not delete the last admin user [uid: %d]", err.UID)
}

// IsLastAdminUser check whether user is the last admin
func IsLastAdminUser(ctx context.Context, user *User) bool {
	if user.IsAdmin && CountUsers(ctx, &CountUserFilter{IsAdmin: optional.Some(true)}) <= 1 {
		return true
	}
	return false
}

// CountUserFilter represent optional filters for CountUsers
type CountUserFilter struct {
	LastLoginSince *int64
	IsAdmin        optional.Option[bool]
	IsActive       optional.Option[bool]
}

// HasUsers returns true if any user exists in the database.
// It performs a much more efficient check than counting all users.
func HasUsers(ctx context.Context) (bool, error) {
	sess := db.GetEngine(ctx)
	cond := builder.NewCond()
	cond = cond.And(builder.Eq{"type": UserTypeIndividual})

	exists, err := sess.Where(cond).Limit(1).Exist(new(User))
	if err != nil {
		return false, fmt.Errorf("error checking user existence: %w", err)
	}

	return exists, nil
}

// CountUsers returns number of users.
func CountUsers(ctx context.Context, opts *CountUserFilter) int64 {
	return countUsers(ctx, opts)
}

func countUsers(ctx context.Context, opts *CountUserFilter) int64 {
	sess := db.GetEngine(ctx)
	cond := builder.NewCond()
	cond = cond.And(builder.Eq{"type": UserTypeIndividual})

	if opts != nil {
		if opts.LastLoginSince != nil {
			cond = cond.And(builder.Gte{"last_login_unix": *opts.LastLoginSince})

Review comment (suggestion):
I think it'll be better to rename this to something like `HasIndividualUsers()` to reflect that we only check for the existence of regular users

Code after:
	_, ok := err.(ErrDeleteLastAdminUser)
	return ok
}

func (err ErrDeleteLastAdminUser) Error() string {
	return fmt.Sprintf("can not delete the last admin user [uid: %d]", err.UID)
}

// IsLastAdminUser check whether user is the last admin
func IsLastAdminUser(ctx context.Context, user *User) bool {
	if user.IsAdmin && CountUsers(ctx, &CountUserFilter{IsAdmin: optional.Some(true)}) <= 1 {
		return true
	}
	return false
}

// CountUserFilter represent optional filters for CountUsers
type CountUserFilter struct {
	LastLoginSince *int64
	IsAdmin        optional.Option[bool]
	IsActive       optional.Option[bool]
}

// HasUsers checks whether there are any users in the database, or only one user exists.
func HasUsers(ctx context.Context) (ret struct {
	HasAnyUser, HasOnlyOneUser bool
}, err error,
) {
	res, err := db.GetEngine(ctx).Table(&User{}).Cols("id").Limit(2).Query()
	if err != nil {
		return ret, fmt.Errorf("error checking user existence: %w", err)
	}
	ret.HasAnyUser = len(res) != 0
	ret.HasOnlyOneUser = len(res) == 1
	return ret, nil
}

// CountUsers returns number of users.
func CountUsers(ctx context.Context, opts *CountUserFilter) int64 {
	return countUsers(ctx, opts)
}

func countUsers(ctx context.Context, opts *CountUserFilter) int64 {
	sess := db.GetEngine(ctx)
	cond := builder.NewCond()
	cond = cond.And(builder.Eq{"type": UserTypeIndividual})

	if opts != nil {
		if opts.LastLoginSince != nil {
			cond = cond.And(builder.Gte{"last_login_unix": *opts.LastLoginSince})
		}