]> Sergey Matveev's repositories - mmc.git/blobdiff - cmd/rd/main.go
internal package
[mmc.git] / cmd / rd / main.go
index 0d7e70b65e9280fef79d140c3ff71deb134e62fe..535332ff64a2c57f10f6e9d4956c4b8d59c5e3f6 100644 (file)
@@ -1,5 +1,5 @@
 // mmc -- Mattermost client
-// Copyright (C) 2023 Sergey Matveev <stargrave@stargrave.org>
+// Copyright (C) 2023-2024 Sergey Matveev <stargrave@stargrave.org>
 //
 // This program is free software: you can redistribute it and/or modify
 // it under the terms of the GNU Affero General Public License as
@@ -23,13 +23,29 @@ import (
        "log"
        "os"
        "path"
+       "strings"
+       "unicode/utf8"
 
        "github.com/fsnotify/fsnotify"
        "github.com/mattermost/mattermost-server/v6/model"
        "go.cypherpunks.ru/recfile"
-       "go.stargrave.org/mmc"
+       "go.stargrave.org/mmc/internal"
 )
 
+var Threads = make(map[string]string)
+
+func rememberPost(post map[string][]string) {
+       text := strings.Split(post["Text"][0], "\n")[0]
+       i := 60
+       if len(text) > i {
+               for i > 0 && !utf8.Valid([]byte{text[i]}) {
+                       i--
+               }
+               text = text[:i] + "..."
+       }
+       Threads[post["Id"][0]] = text
+}
+
 func printPost(m map[string][]string) {
        var tag string
        if len(m["Event"]) > 0 {
@@ -40,13 +56,22 @@ func printPost(m map[string][]string) {
                        tag += "[DEL] "
                }
        }
-       fmt.Printf("\a%s <%s> %s%s\n", m["Created"][0], m["Sender"][0], tag, m["Text"][0])
+       var re string
+       if len(m["RootId"]) > 0 {
+               thread := Threads[m["RootId"][0]]
+               if thread == "" {
+                       thread = m["RootId"][0]
+               }
+               re = " [RE]: " + thread
+       }
+       fmt.Printf(
+               "\a%s <%s> %s%s%s\n",
+               m["Created"][0], m["Sender"][0],
+               tag, m["Text"][0], re,
+       )
        tag += "[FILE] "
        for i, fileId := range m["File"] {
-               fmt.Printf(
-                       "\a%s <%s> %s%s (%s)\n",
-                       m["Created"][0], m["Sender"][0], tag, fileId, m["FileName"][i],
-               )
+               fmt.Printf("\a%s%s (%s)\n", tag, fileId, m["FileName"][i])
        }
 }
 
@@ -78,6 +103,9 @@ func main() {
                ms = append(ms, m)
        }
        unlock()
+       for _, m := range ms {
+               rememberPost(m)
+       }
        if len(ms) > *lastNum {
                ms = ms[len(ms)-*lastNum:]
        }
@@ -114,6 +142,7 @@ func main() {
                                }
                                log.Fatalln(err)
                        }
+                       rememberPost(m)
                        printPost(m)
                }
                unlock()