Skip to content

Commit cdcc4a9

Browse files
committed
refactor: User.Config -> User.Config() and User.SetConfig(UserConfig)
1 parent 4547774 commit cdcc4a9

File tree

6 files changed

+45
-34
lines changed

6 files changed

+45
-34
lines changed

chat/command.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,11 @@ func InitCommands(c *Commands) {
184184
Handler: func(room *Room, msg message.CommandMsg) error {
185185
user := msg.From()
186186
args := msg.Args()
187+
cfg := user.Config()
187188
if len(args) == 0 {
188189
theme := "plain"
189-
if user.Config.Theme != nil {
190-
theme = user.Config.Theme.ID()
190+
if cfg.Theme != nil {
191+
theme = cfg.Theme.ID()
191192
}
192193
body := fmt.Sprintf("Current theme: %s", theme)
193194
room.Send(message.NewSystemMsg(body, user))
@@ -197,7 +198,8 @@ func InitCommands(c *Commands) {
197198
id := args[0]
198199
for _, t := range message.Themes {
199200
if t.ID() == id {
200-
user.Config.Theme = &t
201+
cfg.Theme = &t
202+
user.SetConfig(cfg)
201203
body := fmt.Sprintf("Set theme: %s", id)
202204
room.Send(message.NewSystemMsg(body, user))
203205
return nil
@@ -212,10 +214,12 @@ func InitCommands(c *Commands) {
212214
Help: "Silence room announcements.",
213215
Handler: func(room *Room, msg message.CommandMsg) error {
214216
u := msg.From()
215-
u.ToggleQuietMode()
217+
cfg := u.Config()
218+
cfg.Quiet = !cfg.Quiet
219+
u.SetConfig(cfg)
216220

217221
var body string
218-
if u.Config.Quiet {
222+
if cfg.Quiet {
219223
body = "Quiet mode is toggled ON"
220224
} else {
221225
body = "Quiet mode is toggled OFF"

chat/message/user.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ type User struct {
3131
closeOnce sync.Once
3232

3333
mu sync.Mutex
34-
Config UserConfig
34+
config UserConfig
3535
replyTo *User // Set when user gets a /msg, for replying.
3636
}
3737

3838
func NewUser(identity Identifier) *User {
3939
u := User{
4040
Identifier: identity,
41-
Config: DefaultUserConfig,
41+
config: DefaultUserConfig,
4242
joined: time.Now(),
4343
msg: make(chan Message, messageBuffer),
4444
done: make(chan struct{}),
@@ -56,6 +56,18 @@ func NewUserScreen(identity Identifier, screen io.WriteCloser) *User {
5656
return u
5757
}
5858

59+
func (u *User) Config() UserConfig {
60+
u.mu.Lock()
61+
defer u.mu.Unlock()
62+
return u.config
63+
}
64+
65+
func (u *User) SetConfig(cfg UserConfig) {
66+
u.mu.Lock()
67+
u.config = cfg
68+
u.mu.Unlock()
69+
}
70+
5971
// Rename the user with a new Identifier.
6072
func (u *User) SetID(id string) {
6173
u.Identifier.SetID(id)
@@ -76,24 +88,12 @@ func (u *User) SetReplyTo(user *User) {
7688
u.replyTo = user
7789
}
7890

79-
// ToggleQuietMode will toggle whether or not quiet mode is enabled
80-
func (u *User) ToggleQuietMode() {
81-
u.mu.Lock()
82-
defer u.mu.Unlock()
83-
u.Config.Quiet = !u.Config.Quiet
84-
}
85-
8691
// setColorIdx will set the colorIdx to a specific value, primarily used for
8792
// testing.
8893
func (u *User) setColorIdx(idx int) {
8994
u.colorIdx = idx
9095
}
9196

92-
// Block until user is closed
93-
func (u *User) Wait() {
94-
<-u.done
95-
}
96-
9797
// Disconnect user, stop accepting messages
9898
func (u *User) Close() {
9999
u.closeOnce.Do(func() {
@@ -144,15 +144,13 @@ func (u *User) SetHighlight(s string) error {
144144
return err
145145
}
146146
u.mu.Lock()
147-
u.Config.Highlight = re
147+
u.config.Highlight = re
148148
u.mu.Unlock()
149149
return nil
150150
}
151151

152152
func (u *User) render(m Message) string {
153-
u.mu.Lock()
154-
cfg := u.Config
155-
u.mu.Unlock()
153+
cfg := u.Config()
156154
switch m := m.(type) {
157155
case PublicMsg:
158156
return m.RenderFor(cfg) + Newline

chat/room.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (r *Room) HandleMsg(m message.Message) {
110110
return
111111
}
112112
if _, ok := m.(*message.AnnounceMsg); ok {
113-
if user.Config.Quiet {
113+
if user.Config().Quiet {
114114
// Skip announcements
115115
return
116116
}

chat/room_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ func TestRoomJoin(t *testing.T) {
213213

214214
func TestRoomDoesntBroadcastAnnounceMessagesWhenQuiet(t *testing.T) {
215215
u := message.NewUser(message.SimpleID("foo"))
216-
u.Config = message.UserConfig{
216+
u.SetConfig(message.UserConfig{
217217
Quiet: true,
218-
}
218+
})
219219

220220
ch := NewRoom()
221221
defer ch.Close()
@@ -252,9 +252,9 @@ func TestRoomDoesntBroadcastAnnounceMessagesWhenQuiet(t *testing.T) {
252252

253253
func TestRoomQuietToggleBroadcasts(t *testing.T) {
254254
u := message.NewUser(message.SimpleID("foo"))
255-
u.Config = message.UserConfig{
255+
u.SetConfig(message.UserConfig{
256256
Quiet: true,
257-
}
257+
})
258258

259259
ch := NewRoom()
260260
defer ch.Close()
@@ -267,7 +267,9 @@ func TestRoomQuietToggleBroadcasts(t *testing.T) {
267267
// Drain the initial Join message
268268
<-ch.broadcast
269269

270-
u.ToggleQuietMode()
270+
u.SetConfig(message.UserConfig{
271+
Quiet: false,
272+
})
271273

272274
expectedMsg := message.NewAnnounceMsg("Ignored")
273275
ch.HandleMsg(expectedMsg)
@@ -276,7 +278,9 @@ func TestRoomQuietToggleBroadcasts(t *testing.T) {
276278
t.Errorf("Got: `%T`; Expected: `%T`", msg, expectedMsg)
277279
}
278280

279-
u.ToggleQuietMode()
281+
u.SetConfig(message.UserConfig{
282+
Quiet: true,
283+
})
280284

281285
ch.HandleMsg(message.NewAnnounceMsg("Ignored"))
282286
ch.HandleMsg(message.NewSystemMsg("hello", u))

host.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ const maxInputLength int = 1024
2121
// GetPrompt will render the terminal prompt string based on the user.
2222
func GetPrompt(user *message.User) string {
2323
name := user.Name()
24-
if user.Config.Theme != nil {
25-
name = user.Config.Theme.ColorName(user)
24+
cfg := user.Config()
25+
if cfg.Theme != nil {
26+
name = cfg.Theme.ColorName(user)
2627
}
2728
return fmt.Sprintf("[%s] ", name)
2829
}
@@ -91,7 +92,9 @@ func (h *Host) isOp(conn sshd.Connection) bool {
9192
func (h *Host) Connect(term *sshd.Terminal) {
9293
id := NewIdentity(term.Conn)
9394
user := message.NewUserScreen(id, term)
94-
user.Config.Theme = &h.theme
95+
cfg := user.Config()
96+
cfg.Theme = &h.theme
97+
user.SetConfig(cfg)
9598
go user.Consume()
9699

97100
// Close term once user is closed.

host_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ func TestHostGetPrompt(t *testing.T) {
3535
t.Errorf("Got: %q; Expected: %q", actual, expected)
3636
}
3737

38-
u.Config.Theme = &message.Themes[0]
38+
u.SetConfig(message.UserConfig{
39+
Theme: &message.Themes[0],
40+
})
3941
actual = GetPrompt(u)
4042
expected = "[\033[38;05;88mfoo\033[0m] "
4143
if actual != expected {

0 commit comments

Comments
 (0)