mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-12 23:27:17 +03:00
chat: When a user leaves, add time since they joined to the exit message.
* Show the connection duration upon departure * humantime: ugly sub-second precision replaces grammatically incorrect "1 seconds" message * simplify function name * humantime: repair unit test * move: util/humantime -> internal/humantime * go fmt everything
This commit is contained in:
parent
60f3202818
commit
40bf204058
@ -59,6 +59,10 @@ func NewUserScreen(identity Identifier, screen io.WriteCloser) *User {
|
||||
return u
|
||||
}
|
||||
|
||||
func (u *User) Joined() time.Time {
|
||||
return u.joined
|
||||
}
|
||||
|
||||
func (u *User) Config() UserConfig {
|
||||
u.mu.Lock()
|
||||
defer u.mu.Unlock()
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/shazow/ssh-chat/chat/message"
|
||||
"github.com/shazow/ssh-chat/internal/humantime"
|
||||
"github.com/shazow/ssh-chat/set"
|
||||
)
|
||||
|
||||
@ -159,13 +160,13 @@ func (r *Room) Join(u *message.User) (*Member, error) {
|
||||
}
|
||||
|
||||
// Leave the room as a user, will announce. Mostly used during setup.
|
||||
func (r *Room) Leave(u message.Identifier) error {
|
||||
func (r *Room) Leave(u *message.User) error {
|
||||
err := r.Members.Remove(u.ID())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.Ops.Remove(u.ID())
|
||||
s := fmt.Sprintf("%s left.", u.Name())
|
||||
s := fmt.Sprintf("%s left. (Connected %s)", u.Name(), humantime.Since(u.Joined()))
|
||||
r.Send(message.NewAnnounceMsg(s))
|
||||
return nil
|
||||
}
|
||||
|
3
host.go
3
host.go
@ -12,6 +12,7 @@ import (
|
||||
"github.com/shazow/rateio"
|
||||
"github.com/shazow/ssh-chat/chat"
|
||||
"github.com/shazow/ssh-chat/chat/message"
|
||||
"github.com/shazow/ssh-chat/internal/humantime"
|
||||
"github.com/shazow/ssh-chat/set"
|
||||
"github.com/shazow/ssh-chat/sshd"
|
||||
)
|
||||
@ -382,7 +383,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
|
||||
c.Add(chat.Command{
|
||||
Prefix: "/uptime",
|
||||
Handler: func(room *chat.Room, msg message.CommandMsg) error {
|
||||
room.Send(message.NewSystemMsg(humanSince(time.Since(timeStarted)), msg.From()))
|
||||
room.Send(message.NewSystemMsg(humantime.Since(timeStarted), msg.From()))
|
||||
return nil
|
||||
},
|
||||
})
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/shazow/ssh-chat/chat/message"
|
||||
"github.com/shazow/ssh-chat/internal/humantime"
|
||||
"github.com/shazow/ssh-chat/internal/sanitize"
|
||||
"github.com/shazow/ssh-chat/sshd"
|
||||
)
|
||||
@ -50,7 +51,7 @@ func (i Identity) Whois() string {
|
||||
return "name: " + i.Name() + message.Newline +
|
||||
" > fingerprint: " + fingerprint + message.Newline +
|
||||
" > client: " + sanitize.Data(string(i.ClientVersion()), 64) + message.Newline +
|
||||
" > joined: " + humanSince(time.Since(i.created)) + " ago"
|
||||
" > joined: " + humantime.Since(i.created) + " ago"
|
||||
}
|
||||
|
||||
// WhoisAdmin returns a whois description for admin users.
|
||||
@ -64,5 +65,5 @@ func (i Identity) WhoisAdmin() string {
|
||||
" > ip: " + ip + message.Newline +
|
||||
" > fingerprint: " + fingerprint + message.Newline +
|
||||
" > client: " + sanitize.Data(string(i.ClientVersion()), 64) + message.Newline +
|
||||
" > joined: " + humanSince(time.Since(i.created)) + " ago"
|
||||
" > joined: " + humantime.Since(i.created) + " ago"
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package sshchat
|
||||
package humantime
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -6,8 +6,12 @@ import (
|
||||
)
|
||||
|
||||
// humanSince returns a human-friendly relative time string
|
||||
func humanSince(d time.Duration) string {
|
||||
func Since(t time.Time) string {
|
||||
d := time.Since(t)
|
||||
switch {
|
||||
case d < time.Second*2:
|
||||
//e.g. "516.971µs", "535.412009ms", "1.880689686s"
|
||||
return d.String()
|
||||
case d < time.Minute*2:
|
||||
return fmt.Sprintf("%0.f seconds", d.Seconds())
|
||||
case d < time.Hour*2:
|
@ -1,4 +1,4 @@
|
||||
package sshchat
|
||||
package humantime
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@ -33,7 +33,8 @@ func TestHumanSince(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if actual, expected := humanSince(test.input), test.expected; actual != expected {
|
||||
absolute := time.Now().Add(test.input * -1)
|
||||
if actual, expected := Since(absolute), test.expected; actual != expected {
|
||||
t.Errorf("Got: %q; Expected: %q", actual, expected)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user