Skip to content

Commit 87dd859

Browse files
committed
/timestamp: Toggle timestamps after 30min of inactivity
Closes #244 CC #194, #99, #242
1 parent 66fee99 commit 87dd859

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

chat/command.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,26 @@ func InitCommands(c *Commands) {
275275
},
276276
})
277277

278+
c.Add(Command{
279+
Prefix: "/timestamp",
280+
Help: "Timestamps after 30min of inactivity.",
281+
Handler: func(room *Room, msg message.CommandMsg) error {
282+
u := msg.From()
283+
cfg := u.Config()
284+
cfg.Timestamp = !cfg.Timestamp
285+
u.SetConfig(cfg)
286+
287+
var body string
288+
if cfg.Timestamp {
289+
body = "Timestamp is toggled ON"
290+
} else {
291+
body = "Timestamp is toggled OFF"
292+
}
293+
room.Send(message.NewSystemMsg(body, u))
294+
return nil
295+
},
296+
})
297+
278298
c.Add(Command{
279299
Prefix: "/ignore",
280300
PrefixHelp: "[USER]",

chat/message/user.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
const messageBuffer = 5
1616
const messageTimeout = 5 * time.Second
1717
const reHighlight = `\b(%s)\b`
18+
const timestampTimeout = 30 * time.Minute
19+
const timestampLayout = "2006-01-02 15:04:05 UTC"
1820

1921
var ErrUserClosed = errors.New("user closed")
2022

@@ -32,7 +34,8 @@ type User struct {
3234

3335
mu sync.Mutex
3436
config UserConfig
35-
replyTo *User // Set when user gets a /msg, for replying.
37+
replyTo *User // Set when user gets a /msg, for replying.
38+
lastMsg time.Time // When the last message was rendered
3639
}
3740

3841
func NewUser(identity Identifier) *User {
@@ -164,8 +167,9 @@ func (u *User) render(m Message) string {
164167
}
165168
}
166169

167-
// HandleMsg will render the message to the screen, blocking.
168-
func (u *User) HandleMsg(m Message) error {
170+
// writeMsg renders the message and attempts to write it, will Close the user
171+
// if it fails.
172+
func (u *User) writeMsg(m Message) error {
169173
r := u.render(m)
170174
_, err := u.screen.Write([]byte(r))
171175
if err != nil {
@@ -175,6 +179,26 @@ func (u *User) HandleMsg(m Message) error {
175179
return err
176180
}
177181

182+
// HandleMsg will render the message to the screen, blocking.
183+
func (u *User) HandleMsg(m Message) error {
184+
u.mu.Lock()
185+
cfg := u.config
186+
lastMsg := u.lastMsg
187+
u.lastMsg = m.Timestamp()
188+
injectTimestamp := !lastMsg.IsZero() && cfg.Timestamp && u.lastMsg.Sub(lastMsg) > timestampTimeout
189+
u.mu.Unlock()
190+
191+
if injectTimestamp {
192+
// Inject a timestamp at most once every timestampTimeout between message intervals
193+
ts := NewSystemMsg(fmt.Sprintf("Timestamp: %s", m.Timestamp().UTC().Format(timestampLayout)), u)
194+
if err := u.writeMsg(ts); err != nil {
195+
return err
196+
}
197+
}
198+
199+
return u.writeMsg(m)
200+
}
201+
178202
// Add message to consume by user
179203
func (u *User) Send(m Message) error {
180204
select {
@@ -194,6 +218,7 @@ type UserConfig struct {
194218
Highlight *regexp.Regexp
195219
Bell bool
196220
Quiet bool
221+
Timestamp bool
197222
Theme *Theme
198223
}
199224

@@ -202,8 +227,9 @@ var DefaultUserConfig UserConfig
202227

203228
func init() {
204229
DefaultUserConfig = UserConfig{
205-
Bell: true,
206-
Quiet: false,
230+
Bell: true,
231+
Quiet: false,
232+
Timestamp: false,
207233
}
208234

209235
// TODO: Seed random?

0 commit comments

Comments
 (0)