MCPcopy Create free account
hub / github.com/GoMudEngine/GoMud / SearchUsersByRole

Function SearchUsersByRole

internal/users/search.go:73–117  ·  view source on GitHub ↗

SearchUsersByRole returns all users whose Role matches the given role string (case-insensitive). Results are capped at 500.

(role string)

Source from the content-addressed store, hash-verified

71// SearchUsersByRole returns all users whose Role matches the given role string
72// (case-insensitive). Results are capped at 500.
73func SearchUsersByRole(role string) []UserSearchResult {
74 if role == "" {
75 return nil
76 }
77 needle := strings.ToLower(role)
78
79 type candidate struct {
80 userId int
81 username string
82 }
83
84 var matches []candidate
85 GetUserIndex().ForEachRecord(func(rec IndexUserRecord) bool {
86 if len(matches) >= 500 {
87 return false
88 }
89 matches = append(matches, candidate{
90 userId: int(rec.UserID),
91 username: string(bytes.TrimRight(rec.Username[:], "\x00")),
92 })
93 return true
94 })
95
96 results := make([]UserSearchResult, 0, len(matches))
97 for _, c := range matches {
98 var userRole, userEmail string
99 if u := GetByUserId(c.userId); u != nil {
100 userRole = u.Role
101 userEmail = u.EmailAddress
102 } else if loaded, err := LoadUser(c.username, true); err == nil {
103 userRole = loaded.Role
104 userEmail = loaded.EmailAddress
105 }
106 if strings.ToLower(userRole) != needle {
107 continue
108 }
109 results = append(results, UserSearchResult{
110 UserId: c.userId,
111 Username: c.username,
112 Role: userRole,
113 Email: userEmail,
114 })
115 }
116 return results
117}
118
119func searchByUsername(searchName string) []UserSearchResult {
120 needle := strings.ToLower(searchName)

Callers 1

apiV1SearchUsersFunction · 0.92

Calls 4

GetUserIndexFunction · 0.85
GetByUserIdFunction · 0.85
LoadUserFunction · 0.85
ForEachRecordMethod · 0.80

Tested by

no test coverage detected