]> Sergey Matveev's repositories - mmc.git/blob - internal/post.go
Verify SPKI hash
[mmc.git] / internal / post.go
1 // mmc -- Mattermost client
2 // Copyright (C) 2023-2024 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         "strings"
21         "time"
22
23         "github.com/mattermost/mattermost-server/v6/model"
24         "go.cypherpunks.ru/recfile"
25 )
26
27 type Post struct {
28         P *model.Post
29         E string
30 }
31
32 func PostToRec(w *recfile.Writer, users map[string]*model.User, post Post) error {
33         _, err := w.RecordStart()
34         if err != nil {
35                 return err
36         }
37         created := time.Unix(post.P.CreateAt/1000, 0)
38         user := users[post.P.UserId]
39         sender := "unknown"
40         if user != nil {
41                 sender = user.Username
42         }
43         fields := []recfile.Field{
44                 {Name: "Id", Value: post.P.Id},
45                 {Name: "Created", Value: created.Format("2006-01-02 15:04:05")},
46                 {Name: "Sender", Value: sender},
47         }
48         if post.E != model.WebsocketEventPosted {
49                 fields = append(fields, recfile.Field{Name: "Event", Value: post.E})
50         }
51         if post.P.RootId != "" {
52                 fields = append(fields, recfile.Field{Name: "RootId", Value: post.P.RootId})
53         }
54         if post.P.Metadata != nil {
55                 for _, fi := range post.P.Metadata.Files {
56                         fields = append(
57                                 fields,
58                                 recfile.Field{Name: "File", Value: fi.Id},
59                                 recfile.Field{Name: "FileName", Value: fi.Name},
60                         )
61                 }
62         }
63         if _, err = w.WriteFields(fields...); err != nil {
64                 return err
65         }
66         _, err = w.WriteFieldMultiline("Text", strings.Split(post.P.Message, "\n"))
67         return err
68 }