mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-06-05 01:43:20 +03:00
/timestamp: Toggle timestamps after 30min of inactivity
Closes #244 CC #194, #99, #242
This commit is contained in:
parent
66fee99a81
commit
87dd859fed
@ -275,6 +275,26 @@ func InitCommands(c *Commands) {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
c.Add(Command{
|
||||||
|
Prefix: "/timestamp",
|
||||||
|
Help: "Timestamps after 30min of inactivity.",
|
||||||
|
Handler: func(room *Room, msg message.CommandMsg) error {
|
||||||
|
u := msg.From()
|
||||||
|
cfg := u.Config()
|
||||||
|
cfg.Timestamp = !cfg.Timestamp
|
||||||
|
u.SetConfig(cfg)
|
||||||
|
|
||||||
|
var body string
|
||||||
|
if cfg.Timestamp {
|
||||||
|
body = "Timestamp is toggled ON"
|
||||||
|
} else {
|
||||||
|
body = "Timestamp is toggled OFF"
|
||||||
|
}
|
||||||
|
room.Send(message.NewSystemMsg(body, u))
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
c.Add(Command{
|
c.Add(Command{
|
||||||
Prefix: "/ignore",
|
Prefix: "/ignore",
|
||||||
PrefixHelp: "[USER]",
|
PrefixHelp: "[USER]",
|
||||||
|
@ -15,6 +15,8 @@ import (
|
|||||||
const messageBuffer = 5
|
const messageBuffer = 5
|
||||||
const messageTimeout = 5 * time.Second
|
const messageTimeout = 5 * time.Second
|
||||||
const reHighlight = `\b(%s)\b`
|
const reHighlight = `\b(%s)\b`
|
||||||
|
const timestampTimeout = 30 * time.Minute
|
||||||
|
const timestampLayout = "2006-01-02 15:04:05 UTC"
|
||||||
|
|
||||||
var ErrUserClosed = errors.New("user closed")
|
var ErrUserClosed = errors.New("user closed")
|
||||||
|
|
||||||
@ -33,6 +35,7 @@ type User struct {
|
|||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
config UserConfig
|
config UserConfig
|
||||||
replyTo *User // Set when user gets a /msg, for replying.
|
replyTo *User // Set when user gets a /msg, for replying.
|
||||||
|
lastMsg time.Time // When the last message was rendered
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUser(identity Identifier) *User {
|
func NewUser(identity Identifier) *User {
|
||||||
@ -164,8 +167,9 @@ func (u *User) render(m Message) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleMsg will render the message to the screen, blocking.
|
// writeMsg renders the message and attempts to write it, will Close the user
|
||||||
func (u *User) HandleMsg(m Message) error {
|
// if it fails.
|
||||||
|
func (u *User) writeMsg(m Message) error {
|
||||||
r := u.render(m)
|
r := u.render(m)
|
||||||
_, err := u.screen.Write([]byte(r))
|
_, err := u.screen.Write([]byte(r))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -175,6 +179,26 @@ func (u *User) HandleMsg(m Message) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HandleMsg will render the message to the screen, blocking.
|
||||||
|
func (u *User) HandleMsg(m Message) error {
|
||||||
|
u.mu.Lock()
|
||||||
|
cfg := u.config
|
||||||
|
lastMsg := u.lastMsg
|
||||||
|
u.lastMsg = m.Timestamp()
|
||||||
|
injectTimestamp := !lastMsg.IsZero() && cfg.Timestamp && u.lastMsg.Sub(lastMsg) > timestampTimeout
|
||||||
|
u.mu.Unlock()
|
||||||
|
|
||||||
|
if injectTimestamp {
|
||||||
|
// Inject a timestamp at most once every timestampTimeout between message intervals
|
||||||
|
ts := NewSystemMsg(fmt.Sprintf("Timestamp: %s", m.Timestamp().UTC().Format(timestampLayout)), u)
|
||||||
|
if err := u.writeMsg(ts); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return u.writeMsg(m)
|
||||||
|
}
|
||||||
|
|
||||||
// Add message to consume by user
|
// Add message to consume by user
|
||||||
func (u *User) Send(m Message) error {
|
func (u *User) Send(m Message) error {
|
||||||
select {
|
select {
|
||||||
@ -194,6 +218,7 @@ type UserConfig struct {
|
|||||||
Highlight *regexp.Regexp
|
Highlight *regexp.Regexp
|
||||||
Bell bool
|
Bell bool
|
||||||
Quiet bool
|
Quiet bool
|
||||||
|
Timestamp bool
|
||||||
Theme *Theme
|
Theme *Theme
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,6 +229,7 @@ func init() {
|
|||||||
DefaultUserConfig = UserConfig{
|
DefaultUserConfig = UserConfig{
|
||||||
Bell: true,
|
Bell: true,
|
||||||
Quiet: false,
|
Quiet: false,
|
||||||
|
Timestamp: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Seed random?
|
// TODO: Seed random?
|
||||||
|
Loading…
x
Reference in New Issue
Block a user