chat: /away tweaks

This commit is contained in:
Andrey Petrov 2021-03-15 10:23:33 -04:00 committed by Andrey Petrov
parent 0338cb824d
commit 9329227403
3 changed files with 26 additions and 39 deletions

View File

@ -461,26 +461,19 @@ func InitCommands(c *Commands) {
c.Add(Command{
Prefix: "/away",
PrefixHelp: "[AWAY MESSAGE]",
PrefixHelp: "[REASON]",
Help: "Set away reason, or empty to unset.",
Handler: func(room *Room, msg message.CommandMsg) error {
awayMsg := strings.TrimSpace(strings.TrimLeft(msg.Body(), "/away"))
isAway, _, _ := msg.From().GetAway()
if awayMsg == "" {
if isAway {
msg.From().SetActive()
room.Send(message.NewSystemMsg("You are marked as active, welcome back!", msg.From()))
room.Send(message.NewEmoteMsg("is back", msg.From()))
return nil
}
room.Send(message.NewSystemMsg("Not away. Add an away message to set away.", msg.From()))
return nil
}
msg.From().SetAway(awayMsg)
room.Send(message.NewSystemMsg("You are marked as away, enjoy your excursion!", msg.From()))
room.Send(message.NewEmoteMsg("has gone away: "+awayMsg, msg.From()))
if awayMsg != "" {
room.Send(message.NewEmoteMsg("has gone away: "+awayMsg, msg.From()))
} else if !isAway {
room.Send(message.NewSystemMsg("Not away. Append a reason message to set away.", msg.From()))
} else {
room.Send(message.NewEmoteMsg("is back.", msg.From()))
}
return nil
},
})

View File

@ -33,13 +33,12 @@ type User struct {
screen io.WriteCloser
closeOnce sync.Once
mu sync.Mutex
config UserConfig
replyTo *User // Set when user gets a /msg, for replying.
lastMsg time.Time // When the last message was rendered
awayStatus string // user's away status
awaySince time.Time
mu sync.Mutex
config UserConfig
replyTo *User // Set when user gets a /msg, for replying.
lastMsg time.Time // When the last message was rendered.
awayReason string // Away reason, "" when not away.
awaySince time.Time // When away was set, 0 when not away.
}
func NewUser(identity Identifier) *User {
@ -74,31 +73,24 @@ func (u *User) LastMsg() time.Time {
return u.lastMsg
}
// SetAway sets the users availablity state
// SetAway sets the users away reason and state.
func (u *User) SetAway(msg string) {
u.mu.Lock()
defer u.mu.Unlock()
u.awayStatus = msg
if msg != "" {
u.awayReason = msg
if msg == "" {
u.awaySince = time.Time{}
} else {
// Reset away timer even if already away
u.awaySince = time.Now()
}
}
// SetActive sets the users as active
func (u *User) SetActive() {
u.mu.Lock()
defer u.mu.Unlock()
u.awayStatus = ""
}
// GetAway returns if the user is away, when they went away and if they set a message
// GetAway returns if the user is away, when they went away, and the reason.
func (u *User) GetAway() (bool, time.Time, string) {
u.mu.Lock()
defer u.mu.Unlock()
if u.awayStatus == "" {
return false, time.Time{}, ""
}
return true, u.awaySince, u.awayStatus
return u.awayReason != "", u.awaySince, u.awayReason
}
func (u *User) Config() UserConfig {

View File

@ -63,11 +63,13 @@ func (i Identity) Whois(room *chat.Room) string {
if i.PublicKey() != nil {
fingerprint = sshd.Fingerprint(i.PublicKey())
}
// TODO: Rewrite this using strings.Builder like WhoisAdmin
awayMsg := ""
if m, ok := room.MemberByID(i.ID()); ok {
isAway, awaySince, awayMessage := m.GetAway()
if isAway {
awayMsg = fmt.Sprintf("%s > away since: (%s) %s", message.Newline, humantime.Since(awaySince), awayMessage)
awayMsg = fmt.Sprintf("%s > away: (%s ago) %s", message.Newline, humantime.Since(awaySince), awayMessage)
}
}
return "name: " + i.Name() + message.Newline +