From a2ee2000bbe7a7b44f475caf09feecb2cd424c9b Mon Sep 17 00:00:00 2001 From: Nick Presta Date: Sat, 3 Jan 2015 01:28:48 -0500 Subject: [PATCH] Adding impl for quiet mode with tests. --- chat/channel.go | 6 +++ chat/channel_test.go | 108 +++++++++++++++++++++++++++++++++++++++++++ chat/command.go | 18 ++++++++ chat/user.go | 7 +++ 4 files changed, 139 insertions(+) diff --git a/chat/channel.go b/chat/channel.go index 94916de..4dadbae 100644 --- a/chat/channel.go +++ b/chat/channel.go @@ -85,6 +85,12 @@ func (ch *Channel) HandleMsg(m Message) { // Skip return } + if _, ok := m.(*AnnounceMsg); ok { + if user.Config.Quiet { + // Skip + return + } + } err := user.Send(m) if err != nil { ch.Leave(user) diff --git a/chat/channel_test.go b/chat/channel_test.go index 10e990d..9ba73d8 100644 --- a/chat/channel_test.go +++ b/chat/channel_test.go @@ -57,6 +57,113 @@ func TestChannelJoin(t *testing.T) { } } +func TestChannelDoesntBroadcastAnnounceMessagesWhenQuiet(t *testing.T) { + u := NewUser("foo") + u.Config = UserConfig{ + Quiet: true, + } + + ch := NewChannel() + defer ch.Close() + + err := ch.Join(u) + if err != nil { + t.Fatal(err) + } + + // Drain the initial Join message + <-ch.broadcast + + go func() { + for msg := range u.msg { + if _, ok := msg.(*AnnounceMsg); ok { + t.Errorf("Got unexpected `%T`", msg) + } + } + }() + + // Call with an AnnounceMsg and all the other types + // and assert we received only non-announce messages + ch.HandleMsg(NewAnnounceMsg("Ignored")) + // Assert we still get all other types of messages + ch.HandleMsg(NewEmoteMsg("hello", u)) + ch.HandleMsg(NewSystemMsg("hello", u)) + ch.HandleMsg(NewPrivateMsg("hello", u, u)) + ch.HandleMsg(NewPublicMsg("hello", u)) +} + +func TestChannelQuietToggleBroadcasts(t *testing.T) { + u := NewUser("foo") + u.Config = UserConfig{ + Quiet: true, + } + + ch := NewChannel() + defer ch.Close() + + err := ch.Join(u) + if err != nil { + t.Fatal(err) + } + + // Drain the initial Join message + <-ch.broadcast + + u.ToggleQuietMode() + + expectedMsg := NewAnnounceMsg("Ignored") + ch.HandleMsg(expectedMsg) + msg := <-u.msg + if _, ok := msg.(*AnnounceMsg); !ok { + t.Errorf("Got: `%T`; Expected: `%T`", msg, expectedMsg) + } + + u.ToggleQuietMode() + + ch.HandleMsg(NewAnnounceMsg("Ignored")) + ch.HandleMsg(NewSystemMsg("hello", u)) + msg = <-u.msg + if _, ok := msg.(*AnnounceMsg); ok { + t.Errorf("Got unexpected `%T`", msg) + } +} + +func TestQuietToggleDisplayState(t *testing.T) { + var expected, actual []byte + + s := &MockScreen{} + u := NewUser("foo") + + ch := NewChannel() + go ch.Serve() + defer ch.Close() + + err := ch.Join(u) + if err != nil { + t.Fatal(err) + } + + // Drain the initial Join message + <-ch.broadcast + + ch.Send(ParseInput("/quiet", u)) + u.ConsumeOne(s) + expected = []byte("-> Quiet mode is toggled ON" + Newline) + s.Read(&actual) + if !reflect.DeepEqual(actual, expected) { + t.Errorf("Got: `%s`; Expected: `%s`", actual, expected) + } + + ch.Send(ParseInput("/quiet", u)) + u.ConsumeOne(s) + expected = []byte("-> Quiet mode is toggled OFF" + Newline) + + s.Read(&actual) + if !reflect.DeepEqual(actual, expected) { + t.Errorf("Got: `%s`; Expected: `%s`", actual, expected) + } +} + func TestChannelNames(t *testing.T) { var expected, actual []byte @@ -72,6 +179,7 @@ func TestChannelNames(t *testing.T) { t.Fatal(err) } + // Drain the initial Join message <-ch.broadcast ch.Send(ParseInput("/names", u)) diff --git a/chat/command.go b/chat/command.go index 66e9446..1328c49 100644 --- a/chat/command.go +++ b/chat/command.go @@ -196,6 +196,24 @@ func InitCommands(c *Commands) { }, }) + c.Add(Command{ + Prefix: "/quiet", + Help: "Silence announcement-type messages (join, part, rename, etc).", + Handler: func(channel *Channel, msg CommandMsg) error { + u := msg.From() + u.ToggleQuietMode() + + var body string + if u.Config.Quiet { + body = "Quiet mode is toggled ON" + } else { + body = "Quiet mode is toggled OFF" + } + channel.Send(NewSystemMsg(body, u)) + return nil + }, + }) + c.Add(Command{ Op: true, Prefix: "/op", diff --git a/chat/user.go b/chat/user.go index 542f0bc..203a48a 100644 --- a/chat/user.go +++ b/chat/user.go @@ -59,6 +59,11 @@ func (u *User) SetName(name string) { u.SetColorIdx(rand.Int()) } +// ToggleQuietMode will toggle whether or not quiet mode is enabled +func (u *User) ToggleQuietMode() { + u.Config.Quiet = !u.Config.Quiet +} + // SetColorIdx will set the colorIdx to a specific value, primarily used for // testing. func (u *User) SetColorIdx(idx int) { @@ -122,6 +127,7 @@ func (u *User) Send(m Message) error { type UserConfig struct { Highlight bool Bell bool + Quiet bool Theme *Theme } @@ -132,6 +138,7 @@ func init() { DefaultUserConfig = &UserConfig{ Highlight: true, Bell: false, + Quiet: false, } // TODO: Seed random?