diff --git a/channel.go b/channel.go index 7da8e3c..a61a358 100644 --- a/channel.go +++ b/channel.go @@ -3,6 +3,7 @@ package mock import ( "errors" "math/rand" + "strconv" "sync/atomic" "time" @@ -12,6 +13,7 @@ import ( type Channel struct { session *Service + id uint32 name string done chan struct{} send chan Message // ideally this should be another type @@ -23,6 +25,10 @@ var ( _ cchat.ServerMessage = (*Channel)(nil) ) +func (ch *Channel) ID() string { + return strconv.Itoa(int(ch.id)) +} + func (ch *Channel) Name() (string, error) { return ch.name, nil } @@ -72,7 +78,11 @@ func (ch *Channel) SendMessage(msg cchat.SendableMessage) error { func generateChannels(s *Service, amount int) []cchat.Server { var channels = make([]cchat.Server, amount) for i := range channels { - channels[i] = &Channel{session: s, name: "#" + randomdata.Noun()} + channels[i] = &Channel{ + session: s, + id: atomic.AddUint32(&s.lastid, 1), + name: "#" + randomdata.Noun(), + } } return channels } diff --git a/go.mod b/go.mod index a6809ca..f22271a 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module github.com/diamondburned/cchat-mock go 1.14 require ( - github.com/Pallinder/go-randomdata v1.2.0 // indirect - github.com/diamondburned/cchat v0.0.2 // indirect + github.com/Pallinder/go-randomdata v1.2.0 + github.com/diamondburned/cchat v0.0.4 ) diff --git a/go.sum b/go.sum index ff76081..a49ba50 100644 --- a/go.sum +++ b/go.sum @@ -4,3 +4,7 @@ github.com/diamondburned/cchat v0.0.1 h1:ABoYPQ+uhsUopdDDw62jFsEjvsz6dlOttL/xIeU github.com/diamondburned/cchat v0.0.1/go.mod h1:2MdhWABRer4WhwcuLR0b2VY5S22Y1zDTpFqriAFrC08= github.com/diamondburned/cchat v0.0.2 h1:WfynDllfkUeTWKGwNWz5AWEXL88rl+6FO/uSfMdNiss= github.com/diamondburned/cchat v0.0.2/go.mod h1:2MdhWABRer4WhwcuLR0b2VY5S22Y1zDTpFqriAFrC08= +github.com/diamondburned/cchat v0.0.3 h1:XpuRcs+RhdFOGfsD3uCporEhVo8HQ5s95Qlj+4U2Xno= +github.com/diamondburned/cchat v0.0.3/go.mod h1:2MdhWABRer4WhwcuLR0b2VY5S22Y1zDTpFqriAFrC08= +github.com/diamondburned/cchat v0.0.4 h1:RSuAX3T4PsPOPBDNZMTt6Mr+g1UZQ3A7vSBf9QPS7uQ= +github.com/diamondburned/cchat v0.0.4/go.mod h1:2MdhWABRer4WhwcuLR0b2VY5S22Y1zDTpFqriAFrC08= diff --git a/server.go b/server.go index 7350792..e7acbd6 100644 --- a/server.go +++ b/server.go @@ -2,6 +2,8 @@ package mock import ( "math/rand" + "strconv" + "sync/atomic" "github.com/Pallinder/go-randomdata" "github.com/diamondburned/cchat" @@ -9,6 +11,7 @@ import ( type Server struct { session *Service + id uint32 name string children []cchat.Server } @@ -18,6 +21,10 @@ var ( _ cchat.ServerList = (*Server)(nil) ) +func (sv *Server) ID() string { + return strconv.Itoa(int(sv.id)) +} + func (sv *Server) Name() (string, error) { return sv.name, nil } @@ -36,6 +43,7 @@ func generateServers(s *Service, amount int) []cchat.Server { for i := range channels { channels[i] = &Server{ session: s, + id: atomic.AddUint32(&s.lastid, 1), name: randomdata.Noun(), children: generateChannels(s, rand.Intn(12)), } diff --git a/service.go b/service.go index 81d4c96..0f809fc 100644 --- a/service.go +++ b/service.go @@ -11,6 +11,7 @@ import ( type Service struct { username string servers []cchat.Server + lastid uint32 } var ( @@ -36,8 +37,8 @@ func (s *Service) Authenticate(form []string) error { return nil } -func (s *Service) Name() (string, error) { - return "Mock backend", nil +func (s *Service) Name() string { + return "Mock backend" } func (s *Service) Servers(container cchat.ServersContainer) error {