From 8209f7872ab95cb56c8f0ea77444f9f80d6fef26 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 29 Oct 2024 12:39:06 +0300 Subject: [PATCH] Add Users.Find helper --- pachca.go | 17 ++++++++++++++--- pachca_test.go | 6 +++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pachca.go b/pachca.go index 1da0692..1496c6f 100644 --- a/pachca.go +++ b/pachca.go @@ -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 @@ -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) } } @@ -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) } } @@ -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) } } diff --git a/pachca_test.go b/pachca_test.go index ddeb30a..3eb1937 100644 --- a/pachca_test.go +++ b/pachca_test.go @@ -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: "test@example.com"}, } c.Assert(uu.Active(), HasLen, 4) @@ -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("test@example.com"), NotNil) } func (s *PachcaSuite) TestChatsHelpers(c *C) {