chat/message: Fix RecentActiveUsers sort order
[ssh-chat] / chat / message / user.go
index 7635161d74279da0d6b9615bdd5c091d3d6ca6de..d4cc304c539c995076dcc27e02dba1adefc84538 100644 (file)
@@ -161,6 +161,9 @@ func (u *User) render(m Message) string {
        switch m := m.(type) {
        case PublicMsg:
                if u == m.From() {
+                       if !cfg.Echo {
+                               return ""
+                       }
                        out += m.RenderSelf(cfg)
                } else {
                        out += m.RenderFor(cfg)
@@ -170,17 +173,19 @@ func (u *User) render(m Message) string {
                if cfg.Bell {
                        out += Bel
                }
+       case *CommandMsg:
+               out += m.RenderSelf(cfg)
        default:
                out += m.Render(cfg.Theme)
        }
-       if cfg.Timestamp {
+       if cfg.Timeformat != nil {
                ts := m.Timestamp()
                if cfg.Timezone != nil {
                        ts = ts.In(cfg.Timezone)
                } else {
                        ts = ts.UTC()
                }
-               return cfg.Theme.Timestamp(ts) + "  " + out + Newline
+               return cfg.Theme.Timestamp(ts.Format(*cfg.Timeformat)) + "  " + out + Newline
        }
        return out + Newline
 }
@@ -221,12 +226,13 @@ func (u *User) Send(m Message) error {
 
 // Container for per-user configurations.
 type UserConfig struct {
-       Highlight *regexp.Regexp
-       Bell      bool
-       Quiet     bool
-       Timestamp bool
-       Timezone  *time.Location
-       Theme     *Theme
+       Highlight  *regexp.Regexp
+       Bell       bool
+       Quiet      bool
+       Echo       bool // Echo shows your own messages after sending, disabled for bots
+       Timeformat *string
+       Timezone   *time.Location
+       Theme      *Theme
 }
 
 // Default user configuration to use
@@ -234,10 +240,29 @@ var DefaultUserConfig UserConfig
 
 func init() {
        DefaultUserConfig = UserConfig{
-               Bell:      true,
-               Quiet:     false,
-               Timestamp: false,
+               Bell:  true,
+               Echo:  true,
+               Quiet: false,
        }
 
        // TODO: Seed random?
 }
+
+// RecentActiveUsers is a slice of *Users that knows how to be sorted by the time of the last message.
+type RecentActiveUsers []*User
+
+func (a RecentActiveUsers) Len() int      { return len(a) }
+func (a RecentActiveUsers) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
+func (a RecentActiveUsers) Less(i, j int) bool {
+       a[i].mu.Lock()
+       defer a[i].mu.Unlock()
+       a[j].mu.Lock()
+       defer a[j].mu.Unlock()
+
+       if a[i].lastMsg.IsZero() {
+               return a[i].joined.Before(a[j].joined)
+       } else {
+               return a[i].lastMsg.Before(a[j].lastMsg)
+       }
+
+}