Skip to content

Commit

Permalink
More User helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 29, 2024
1 parent fbc63c2 commit 02efafa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
32 changes: 30 additions & 2 deletions pachca.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ const (

const (
ROLE_ADMIN UserRole = "admin"
ROLE_USER UserRole = "user"
ROLE_REGULAR UserRole = "user"
ROLE_MULTI_GUEST UserRole = "multi_guest"
ROLE_GUEST UserRole = "guest"
)

const (
Expand Down Expand Up @@ -1701,6 +1702,33 @@ func (u *User) IsInvited() bool {
return !u.IsSuspended && u.InviteStatus == INVITE_SENT
}

// IsGuest returns true if user is guest or multi-guest
func (u *User) IsGuest() bool {
if u == nil {
return false
}

return u.Role == ROLE_MULTI_GUEST || u.Role == ROLE_GUEST
}

// IsAdmin returns true if user is admin
func (u *User) IsAdmin() bool {
if u == nil {
return false
}

return u.Role == ROLE_ADMIN
}

// IsRegular returns true if user just regular user
func (u *User) IsRegular() bool {
if u == nil {
return false
}

return u.Role == ROLE_REGULAR
}

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

for _, uu := range u {
if uu.Role == ROLE_USER {
if uu.Role == ROLE_REGULAR {
result = append(result, uu)
}
}
Expand Down
14 changes: 10 additions & 4 deletions pachca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ func (s *PachcaSuite) TestUsersHelpers(c *C) {
c.Assert(u.FullName(), Equals, "")
c.Assert(u.IsActive(), Equals, false)
c.Assert(u.IsInvited(), Equals, false)
c.Assert(u.IsGuest(), Equals, false)
c.Assert(u.IsAdmin(), Equals, false)
c.Assert(u.IsRegular(), Equals, false)

u = &User{ID: 1234, FirstName: "John", LastName: "Doe", Nickname: "j.doe"}
c.Assert(u.FullName(), Equals, "John Doe")
Expand All @@ -369,12 +372,12 @@ func (s *PachcaSuite) TestUsersHelpers(c *C) {
c.Assert(u.IsActive(), Equals, true)

uu := Users{
{ID: 1, IsSuspended: false, InviteStatus: INVITE_SENT, IsBot: false, Role: ROLE_USER},
{ID: 2, IsSuspended: false, InviteStatus: INVITE_CONFIRMED, IsBot: false, Role: ROLE_USER},
{ID: 1, IsSuspended: false, InviteStatus: INVITE_SENT, IsBot: false, Role: ROLE_REGULAR},
{ID: 2, IsSuspended: false, InviteStatus: INVITE_CONFIRMED, IsBot: false, Role: ROLE_REGULAR},
{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_USER},
{ID: 6, IsSuspended: true, InviteStatus: INVITE_CONFIRMED, IsBot: false, Role: ROLE_USER},
{ID: 5, IsSuspended: false, InviteStatus: INVITE_CONFIRMED, IsBot: true, Role: ROLE_REGULAR},
{ID: 6, IsSuspended: true, InviteStatus: INVITE_CONFIRMED, IsBot: false, Role: ROLE_REGULAR},
}

c.Assert(uu.Active(), HasLen, 4)
Expand All @@ -384,9 +387,12 @@ func (s *PachcaSuite) TestUsersHelpers(c *C) {
c.Assert(uu.Invited()[0].ID, Equals, ID(1))
c.Assert(uu.Bots()[0].ID, Equals, ID(5))
c.Assert(uu.Admins()[0].ID, Equals, ID(3))
c.Assert(uu.Admins()[0].IsAdmin(), Equals, true)
c.Assert(uu.Regular(), HasLen, 4)
c.Assert(uu.Regular()[0].ID, Equals, ID(1))
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)
}

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

0 comments on commit 02efafa

Please sign in to comment.