Skip to content

Commit

Permalink
Add Users.Find helper
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 29, 2024
1 parent 7f49107 commit 8209f78
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
17 changes: 14 additions & 3 deletions pachca.go
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,17 @@ func (u *User) IsRegular() bool {
return u != nil && u.Role == ROLE_REGULAR
}

// Find tries to find user with given mail or nickname
func (u Users) Find(nicknameOrEmail string) *User {
for _, uu := range u {
if uu.Nickname == nicknameOrEmail || uu.Email == nicknameOrEmail {
return uu
}
}

return nil
}

// Active returns slice with active users
func (u Users) Active() Users {
var result Users
Expand Down Expand Up @@ -1771,7 +1782,7 @@ func (u Users) Admins() Users {
var result Users

for _, uu := range u {
if uu.Role == ROLE_ADMIN {
if uu.IsAdmin() {
result = append(result, uu)
}
}
Expand All @@ -1784,7 +1795,7 @@ func (u Users) Regular() Users {
var result Users

for _, uu := range u {
if uu.Role == ROLE_REGULAR {
if uu.IsRegular() {
result = append(result, uu)
}
}
Expand All @@ -1797,7 +1808,7 @@ func (u Users) Guests() Users {
var result Users

for _, uu := range u {
if uu.Role == ROLE_MULTI_GUEST {
if uu.IsGuest() {
result = append(result, uu)
}
}
Expand Down
6 changes: 5 additions & 1 deletion pachca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (s *PachcaSuite) TestUsersHelpers(c *C) {
{ID: 3, IsSuspended: false, InviteStatus: INVITE_CONFIRMED, IsBot: false, Role: ROLE_ADMIN},
{ID: 4, IsSuspended: false, InviteStatus: INVITE_CONFIRMED, IsBot: false, Role: ROLE_MULTI_GUEST},
{ID: 5, IsSuspended: false, InviteStatus: INVITE_CONFIRMED, IsBot: true, Role: ROLE_REGULAR},
{ID: 6, IsSuspended: true, InviteStatus: INVITE_CONFIRMED, IsBot: false, Role: ROLE_REGULAR},
{ID: 6, IsSuspended: true, InviteStatus: INVITE_CONFIRMED, IsBot: false, Role: ROLE_REGULAR, Nickname: "j.doe", Email: "[email protected]"},
}

c.Assert(uu.Active(), HasLen, 4)
Expand All @@ -389,6 +389,10 @@ func (s *PachcaSuite) TestUsersHelpers(c *C) {
c.Assert(uu.Regular()[0].IsRegular(), Equals, true)
c.Assert(uu.Guests()[0].ID, Equals, ID(4))
c.Assert(uu.Guests()[0].IsGuest(), Equals, true)

c.Assert(uu.Find("test"), IsNil)
c.Assert(uu.Find("j.doe"), NotNil)
c.Assert(uu.Find("[email protected]"), NotNil)
}

func (s *PachcaSuite) TestChatsHelpers(c *C) {
Expand Down

0 comments on commit 8209f78

Please sign in to comment.