Skip to content

Commit

Permalink
More helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 29, 2024
1 parent b3d2bd6 commit a2fff04
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
17 changes: 14 additions & 3 deletions pachca.go
Original file line number Diff line number Diff line change
Expand Up @@ -1762,7 +1762,7 @@ func (u Users) InChat(chat *Chat) Users {
return result
}

// Find tries to find user with given mail or nickname
// Find returns user with given nickname or email
func (u Users) Find(nicknameOrEmail string) *User {
for _, uu := range u {
if uu.Nickname == nicknameOrEmail || uu.Email == nicknameOrEmail {
Expand Down Expand Up @@ -1864,8 +1864,19 @@ func (u Users) Guests() Users {
return result
}

// Get returns chat with given name
func (c Chats) Get(name string) *Chat {
// Get returns chat with given ID
func (c Chats) Get(id uint64) *Chat {
for _, cc := range c {
if cc.ID == id {
return cc
}
}

return nil
}

// Find returns chat with given name
func (c Chats) Find(name string) *Chat {
for _, cc := range c {
if cc.Name == name {
return cc
Expand Down
7 changes: 5 additions & 2 deletions pachca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,11 @@ func (s *PachcaSuite) TestChatsHelpers(c *C) {
{ID: 5, Name: "", IsPublic: false, IsChannel: false},
}

c.Assert(cc.Get("test"), IsNil)
c.Assert(cc.Get("test1"), NotNil)
c.Assert(cc.Get(1), NotNil)
c.Assert(cc.Get(100), IsNil)

c.Assert(cc.Find("test"), IsNil)
c.Assert(cc.Find("test1"), NotNil)

c.Assert(cc.Public()[0].ID, Equals, uint64(3))
c.Assert(cc.Channels()[0].ID, Equals, uint64(4))
Expand Down

0 comments on commit a2fff04

Please sign in to comment.