diff --git a/chat/command.go b/chat/command.go index a36e9d1..df403dc 100644 --- a/chat/command.go +++ b/chat/command.go @@ -184,10 +184,11 @@ func InitCommands(c *Commands) { Handler: func(room *Room, msg message.CommandMsg) error { user := msg.From() args := msg.Args() + cfg := user.Config() if len(args) == 0 { theme := "plain" - if user.Config.Theme != nil { - theme = user.Config.Theme.ID() + if cfg.Theme != nil { + theme = cfg.Theme.ID() } body := fmt.Sprintf("Current theme: %s", theme) room.Send(message.NewSystemMsg(body, user)) @@ -197,7 +198,8 @@ func InitCommands(c *Commands) { id := args[0] for _, t := range message.Themes { if t.ID() == id { - user.Config.Theme = &t + cfg.Theme = &t + user.SetConfig(cfg) body := fmt.Sprintf("Set theme: %s", id) room.Send(message.NewSystemMsg(body, user)) return nil @@ -212,10 +214,12 @@ func InitCommands(c *Commands) { Help: "Silence room announcements.", Handler: func(room *Room, msg message.CommandMsg) error { u := msg.From() - u.ToggleQuietMode() + cfg := u.Config() + cfg.Quiet = !cfg.Quiet + u.SetConfig(cfg) var body string - if u.Config.Quiet { + if cfg.Quiet { body = "Quiet mode is toggled ON" } else { body = "Quiet mode is toggled OFF" diff --git a/chat/message/user.go b/chat/message/user.go index 189ec81..0cd700c 100644 --- a/chat/message/user.go +++ b/chat/message/user.go @@ -31,14 +31,14 @@ type User struct { closeOnce sync.Once mu sync.Mutex - Config UserConfig + config UserConfig replyTo *User // Set when user gets a /msg, for replying. } func NewUser(identity Identifier) *User { u := User{ Identifier: identity, - Config: DefaultUserConfig, + config: DefaultUserConfig, joined: time.Now(), msg: make(chan Message, messageBuffer), done: make(chan struct{}), @@ -56,6 +56,18 @@ func NewUserScreen(identity Identifier, screen io.WriteCloser) *User { return u } +func (u *User) Config() UserConfig { + u.mu.Lock() + defer u.mu.Unlock() + return u.config +} + +func (u *User) SetConfig(cfg UserConfig) { + u.mu.Lock() + u.config = cfg + u.mu.Unlock() +} + // Rename the user with a new Identifier. func (u *User) SetID(id string) { u.Identifier.SetID(id) @@ -76,24 +88,12 @@ func (u *User) SetReplyTo(user *User) { u.replyTo = user } -// ToggleQuietMode will toggle whether or not quiet mode is enabled -func (u *User) ToggleQuietMode() { - u.mu.Lock() - defer u.mu.Unlock() - 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) { u.colorIdx = idx } -// Block until user is closed -func (u *User) Wait() { - <-u.done -} - // Disconnect user, stop accepting messages func (u *User) Close() { u.closeOnce.Do(func() { @@ -144,15 +144,13 @@ func (u *User) SetHighlight(s string) error { return err } u.mu.Lock() - u.Config.Highlight = re + u.config.Highlight = re u.mu.Unlock() return nil } func (u *User) render(m Message) string { - u.mu.Lock() - cfg := u.Config - u.mu.Unlock() + cfg := u.Config() switch m := m.(type) { case PublicMsg: return m.RenderFor(cfg) + Newline diff --git a/chat/room.go b/chat/room.go index 8ebb11d..7ce6de1 100644 --- a/chat/room.go +++ b/chat/room.go @@ -110,7 +110,7 @@ func (r *Room) HandleMsg(m message.Message) { return } if _, ok := m.(*message.AnnounceMsg); ok { - if user.Config.Quiet { + if user.Config().Quiet { // Skip announcements return } diff --git a/chat/room_test.go b/chat/room_test.go index 71224fd..e30c170 100644 --- a/chat/room_test.go +++ b/chat/room_test.go @@ -213,9 +213,9 @@ func TestRoomJoin(t *testing.T) { func TestRoomDoesntBroadcastAnnounceMessagesWhenQuiet(t *testing.T) { u := message.NewUser(message.SimpleID("foo")) - u.Config = message.UserConfig{ + u.SetConfig(message.UserConfig{ Quiet: true, - } + }) ch := NewRoom() defer ch.Close() @@ -252,9 +252,9 @@ func TestRoomDoesntBroadcastAnnounceMessagesWhenQuiet(t *testing.T) { func TestRoomQuietToggleBroadcasts(t *testing.T) { u := message.NewUser(message.SimpleID("foo")) - u.Config = message.UserConfig{ + u.SetConfig(message.UserConfig{ Quiet: true, - } + }) ch := NewRoom() defer ch.Close() @@ -267,7 +267,9 @@ func TestRoomQuietToggleBroadcasts(t *testing.T) { // Drain the initial Join message <-ch.broadcast - u.ToggleQuietMode() + u.SetConfig(message.UserConfig{ + Quiet: false, + }) expectedMsg := message.NewAnnounceMsg("Ignored") ch.HandleMsg(expectedMsg) @@ -276,7 +278,9 @@ func TestRoomQuietToggleBroadcasts(t *testing.T) { t.Errorf("Got: `%T`; Expected: `%T`", msg, expectedMsg) } - u.ToggleQuietMode() + u.SetConfig(message.UserConfig{ + Quiet: true, + }) ch.HandleMsg(message.NewAnnounceMsg("Ignored")) ch.HandleMsg(message.NewSystemMsg("hello", u)) diff --git a/host.go b/host.go index 738a565..39c38cf 100644 --- a/host.go +++ b/host.go @@ -21,8 +21,9 @@ const maxInputLength int = 1024 // GetPrompt will render the terminal prompt string based on the user. func GetPrompt(user *message.User) string { name := user.Name() - if user.Config.Theme != nil { - name = user.Config.Theme.ColorName(user) + cfg := user.Config() + if cfg.Theme != nil { + name = cfg.Theme.ColorName(user) } return fmt.Sprintf("[%s] ", name) } @@ -91,7 +92,9 @@ func (h *Host) isOp(conn sshd.Connection) bool { func (h *Host) Connect(term *sshd.Terminal) { id := NewIdentity(term.Conn) user := message.NewUserScreen(id, term) - user.Config.Theme = &h.theme + cfg := user.Config() + cfg.Theme = &h.theme + user.SetConfig(cfg) go user.Consume() // Close term once user is closed. diff --git a/host_test.go b/host_test.go index c959ceb..7c2f976 100644 --- a/host_test.go +++ b/host_test.go @@ -35,7 +35,9 @@ func TestHostGetPrompt(t *testing.T) { t.Errorf("Got: %q; Expected: %q", actual, expected) } - u.Config.Theme = &message.Themes[0] + u.SetConfig(message.UserConfig{ + Theme: &message.Themes[0], + }) actual = GetPrompt(u) expected = "[\033[38;05;88mfoo\033[0m] " if actual != expected {