]> Sergey Matveev's repositories - mmc.git/blob - common.go
Move dlpane's tmux-related command outside
[mmc.git] / common.go
1 // mmc -- Mattermost client
2 // Copyright (C) 2023 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 package mmc
18
19 import (
20         "os"
21         "strings"
22         "time"
23
24         "github.com/davecgh/go-spew/spew"
25         "github.com/mattermost/mattermost-server/v6/model"
26         "go.cypherpunks.ru/recfile"
27 )
28
29 const (
30         PerPage    = 100
31         OutRec     = "out.rec"
32         OutRecLock = "out.rec.lock"
33         Last       = "last"
34 )
35
36 var SleepTime = 250 * time.Millisecond
37
38 type Post struct {
39         P *model.Post
40         E string
41 }
42
43 func PostToRec(w *recfile.Writer, users map[string]*model.User, post Post) error {
44         _, err := w.RecordStart()
45         if err != nil {
46                 return err
47         }
48         created := time.Unix(post.P.CreateAt/1000, 0)
49         user := users[post.P.UserId]
50         sender := "unknown"
51         if user != nil {
52                 sender = user.Username
53         }
54         fields := []recfile.Field{
55                 {Name: "Id", Value: post.P.Id},
56                 {Name: "Created", Value: created.Format("2006-01-02 15:04:05")},
57                 {Name: "Sender", Value: sender},
58         }
59         if post.E != model.WebsocketEventPosted {
60                 fields = append(fields, recfile.Field{Name: "Event", Value: post.E})
61         }
62         if post.P.RootId != "" {
63                 fields = append(fields, recfile.Field{Name: "RootId", Value: post.P.RootId})
64         }
65         if post.P.Metadata != nil {
66                 for _, fi := range post.P.Metadata.Files {
67                         fields = append(
68                                 fields,
69                                 recfile.Field{Name: "File", Value: fi.Id},
70                                 recfile.Field{Name: "FileName", Value: fi.Name},
71                         )
72                 }
73         }
74         if _, err = w.WriteFields(fields...); err != nil {
75                 return err
76         }
77         lines := strings.Split(post.P.Message, "\n")
78         for i, line := range lines {
79                 if strings.HasSuffix(line, "\\") {
80                         lines[i] += " "
81                 }
82         }
83         _, err = w.WriteFieldMultiline("Text", lines)
84         return err
85 }
86
87 func GetUsers(c *model.Client4, debugFd *os.File) (map[string]*model.User, error) {
88         users := make(map[string]*model.User)
89         for n := 0; ; n++ {
90                 time.Sleep(SleepTime)
91                 page, resp, err := c.GetUsers(n, PerPage, "")
92                 if err != nil {
93                         if debugFd != nil {
94                                 spew.Fdump(debugFd, resp)
95                         }
96                         return nil, err
97                 }
98                 if debugFd != nil {
99                         spew.Fdump(debugFd, page)
100                 }
101                 for _, u := range page {
102                         users[u.Id] = u
103                 }
104                 if len(page) < PerPage {
105                         break
106                 }
107         }
108         return users, nil
109 }
110
111 func GetEntrypoint() string {
112         s := os.Getenv("MMC_ENTRYPOINT")
113         if s == "" {
114                 return "mm.invalid"
115         }
116         return s
117 }