chat: Add /focus command

Only show messages from focused users
This commit is contained in:
Andrey Petrov 2020-08-03 11:32:55 -04:00
parent 8cd06e33b5
commit aa78d0eb22
2 changed files with 53 additions and 1 deletions

View File

@ -411,4 +411,51 @@ func InitCommands(c *Commands) {
return nil return nil
}, },
}) })
c.Add(Command{
Prefix: "/focus",
PrefixHelp: "[USER ...]",
Help: "Only show messages from focused users, or $ to reset.",
Handler: func(room *Room, msg message.CommandMsg) error {
ids := strings.TrimSpace(strings.TrimLeft(msg.Body(), "/focus"))
if ids == "" {
// Print focused names, if any.
var names []string
msg.From().Focused.Each(func(_ string, item set.Item) error {
names = append(names, item.Key())
return nil
})
var systemMsg string
if len(names) == 0 {
systemMsg = "Unfocused."
} else {
systemMsg = fmt.Sprintf("Focusing on %d users: %s", len(names), strings.Join(names, ", "))
}
room.Send(message.NewSystemMsg(systemMsg, msg.From()))
return nil
}
n := msg.From().Focused.Clear()
if ids == "$" {
room.Send(message.NewSystemMsg(fmt.Sprintf("Removed focus from %d users.", n), msg.From()))
return nil
}
var focused []string
for _, name := range strings.Split(ids, " ") {
id := sanitize.Name(name)
if id == "" {
continue // Skip
}
focused = append(focused, id)
if err := msg.From().Focused.Set(set.Itemize(id, set.ZeroValue)); err != nil {
return err
}
}
room.Send(message.NewSystemMsg(fmt.Sprintf("Focusing: %s", strings.Join(focused, ", ")), msg.From()))
return nil
},
})
} }

View File

@ -22,7 +22,8 @@ var ErrUserClosed = errors.New("user closed")
// User definition, implemented set Item interface and io.Writer // User definition, implemented set Item interface and io.Writer
type User struct { type User struct {
Identifier Identifier
Ignored *set.Set Ignored set.Interface
Focused set.Interface
colorIdx int colorIdx int
joined time.Time joined time.Time
msg chan Message msg chan Message
@ -45,6 +46,7 @@ func NewUser(identity Identifier) *User {
msg: make(chan Message, messageBuffer), msg: make(chan Message, messageBuffer),
done: make(chan struct{}), done: make(chan struct{}),
Ignored: set.New(), Ignored: set.New(),
Focused: set.New(),
} }
u.setColorIdx(rand.Int()) u.setColorIdx(rand.Int())
@ -175,6 +177,9 @@ func (u *User) render(m Message) string {
return "" return ""
} }
out += m.RenderSelf(cfg) out += m.RenderSelf(cfg)
} else if u.Focused.Len() > 0 && !u.Focused.In(m.From().ID()) {
// Skip message during focus
return ""
} else { } else {
out += m.RenderFor(cfg) out += m.RenderFor(cfg)
} }