Merge 92af4dc5fa2030a00321b2354aefa0da57445fab into 2dc875817decc3aefb35935c331c11f22b1ecd5e

This commit is contained in:
Akshay Shekher 2025-03-24 03:15:45 +00:00 committed by GitHub
commit 9a4695b2cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 61 additions and 14 deletions

View File

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"sort" "sort"
"strconv"
"strings" "strings"
"time" "time"
@ -191,13 +192,27 @@ func InitCommands(c *Commands) {
names := room.Members.ListPrefix("") names := room.Members.ListPrefix("")
sort.Slice(names, func(i, j int) bool { return names[i].Key() < names[j].Key() }) sort.Slice(names, func(i, j int) bool { return names[i].Key() < names[j].Key() })
colNames := make([]string, len(names)) activeColNames := []string{}
for i, uname := range names { awayColNames := []string{}
colNames[i] = colorize(uname.Value().(*Member).User) for _, uname := range names {
user := uname.Value().(*Member).User
colUser := colorize(user)
if isAway, _, _ := user.GetAway(); isAway {
awayColNames = append(awayColNames, colUser)
} else {
activeColNames = append(activeColNames, colUser)
}
}
numPeople := strconv.Itoa(len(names))
activePeople := strings.Join(activeColNames, ", ")
if len(awayColNames) > 0 {
awayPeople := strings.Join(awayColNames, ",")
room.Send(message.NewSystemMsgP(msg.From(), numPeople, " connected: ", activePeople, "; away: ", awayPeople))
} else {
room.Send(message.NewSystemMsgP(msg.From(), numPeople, " connected: ", activePeople))
} }
body := fmt.Sprintf("%d connected: %s", len(colNames), strings.Join(colNames, ", "))
room.Send(message.NewSystemMsg(body, msg.From()))
return nil return nil
}, },
}) })

View File

@ -203,38 +203,70 @@ func (m PrivateMsg) String() string {
return m.Render(nil) return m.Render(nil)
} }
var _ Message = &SystemMsg{}
// SystemMsg is a response sent from the server directly to a user, not shown // SystemMsg is a response sent from the server directly to a user, not shown
// to anyone else. Usually in response to something, like /help. // to anyone else. Usually in response to something, like /help.
type SystemMsg struct { type SystemMsg struct {
Msg parts []string
to *User timestamp time.Time
to *User
} }
var systemMessagePrefix = []string{"-> "}
func NewSystemMsg(body string, to *User) *SystemMsg { func NewSystemMsg(body string, to *User) *SystemMsg {
return &SystemMsg{ return &SystemMsg{
Msg: Msg{ parts: append(systemMessagePrefix, body),
body: body, timestamp: time.Now(),
timestamp: time.Now(), to: to,
},
to: to,
} }
} }
func NewSystemMsgP(to *User, parts ...string) *SystemMsg {
return &SystemMsg{
to: to,
parts: append(systemMessagePrefix, parts...),
timestamp: time.Now(),
}
}
func (m *SystemMsg) renderPlain() string {
spArgs := make([]interface{}, len(m.parts))
for i, arg := range m.parts {
spArgs[i] = arg
}
return fmt.Sprint(spArgs...)
}
func (m *SystemMsg) Render(t *Theme) string { func (m *SystemMsg) Render(t *Theme) string {
if t == nil { if t == nil {
return m.String() return m.String()
} }
return t.ColorSys(m.String())
colPart := make([]interface{}, len(m.parts))
for i, part := range m.parts {
colPart[i] = t.ColorSys(part)
}
return fmt.Sprint(colPart...)
} }
func (m *SystemMsg) String() string { func (m *SystemMsg) String() string {
return fmt.Sprintf("-> %s", m.body) return fmt.Sprintf("-> %s", m.renderPlain())
} }
func (m *SystemMsg) To() *User { func (m *SystemMsg) To() *User {
return m.to return m.to
} }
func (m *SystemMsg) Command() string {
return ""
}
func (m *SystemMsg) Timestamp() time.Time {
return m.timestamp
}
// AnnounceMsg is a message sent from the server to everyone, like a join or // AnnounceMsg is a message sent from the server to everyone, like a join or
// leave event. // leave event.
type AnnounceMsg struct { type AnnounceMsg struct {