1
1
package main
2
2
3
3
import (
4
+ "fmt"
5
+ "io"
6
+ "net/http"
4
7
"os"
5
8
"regexp"
6
9
"sort"
@@ -15,9 +18,18 @@ import (
15
18
"github.com/spf13/viper"
16
19
)
17
20
21
+ type MsgAttachment struct {
22
+ ID snowflake.ID
23
+ Filename string
24
+ ContentType string
25
+ URL string
26
+ Size int
27
+ }
28
+
18
29
type Msg struct {
19
- Content string
20
- ID snowflake.ID
30
+ Content string
31
+ ID snowflake.ID
32
+ Attachments []MsgAttachment
21
33
}
22
34
23
35
type MsgBlock struct {
@@ -45,25 +57,32 @@ func (t *Transcript) AddMessage(m discord.Message) {
45
57
// we get messages in the reverse order they were send,
46
58
// so we need to add them in the reverse order we get them
47
59
60
+ msg := Msg {
61
+ Content : m .Content ,
62
+ ID : m .ID ,
63
+ Attachments : []MsgAttachment {},
64
+ }
65
+
66
+ if len (m .Attachments ) > 0 {
67
+ for _ , a := range m .Attachments {
68
+ msg .Attachments = append (msg .Attachments , MsgAttachment {
69
+ ID : a .ID ,
70
+ Filename : a .Filename ,
71
+ ContentType : * a .ContentType ,
72
+ URL : a .URL ,
73
+ Size : a .Size ,
74
+ })
75
+ }
76
+ }
77
+
48
78
// if first block is the same user
49
79
if len (t .Blocks ) > 0 && t .Blocks [0 ].UserId == m .Author .ID {
50
80
// just put the message in the first block
51
-
52
- // create a new message
53
- message := Msg {
54
- Content : m .Content ,
55
- ID : m .ID ,
56
- }
57
- t .Blocks [0 ].Messages = append ([]Msg {message }, t .Blocks [0 ].Messages ... )
81
+ t .Blocks [0 ].Messages = append ([]Msg {msg }, t .Blocks [0 ].Messages ... )
58
82
59
83
return
60
84
}
61
85
62
- msg := Msg {
63
- Content : m .Content ,
64
- ID : m .ID ,
65
- }
66
-
67
86
var name string
68
87
69
88
// check if we have a name override
@@ -197,10 +216,13 @@ func (t *Transcript) PrintTranscript() {
197
216
198
217
// writes transcript to a file
199
218
func (t * Transcript ) SaveTranscript () {
200
- folderPath := "transcripts"
219
+ log .Info ("Saving transcript..." )
220
+
201
221
trialName := viper .GetString ("TRIAL_NAME" )
222
+ // format trial name for use as filename
223
+ trailNameFormated := t .FormatFileName (trialName )
202
224
203
- fileName := t . FormatFileName ( trialName )
225
+ folderPath := fmt . Sprintf ( "transcripts/%s" , trailNameFormated )
204
226
205
227
// check if folder exists
206
228
if _ , err := os .Stat (folderPath ); eris .Is (err , os .ErrNotExist ) {
@@ -214,7 +236,7 @@ func (t *Transcript) SaveTranscript() {
214
236
}
215
237
216
238
// create file
217
- f , err := os .Create (folderPath + "/" + fileName + ".md" )
239
+ f , err := os .Create (folderPath + "/" + trailNameFormated + ".md" )
218
240
if err != nil {
219
241
eris .Wrap (err , "failed to create file" )
220
242
log .Panic (err )
@@ -236,15 +258,67 @@ func (t *Transcript) SaveTranscript() {
236
258
// println(b.Name)
237
259
for _ , m := range b .Messages {
238
260
261
+ attachments := ""
262
+
263
+ for _ , a := range m .Attachments {
264
+ // construct attachment path for use on the website
265
+ attachmentPath := fmt .Sprintf ("../../assets/judiciary/%s/%s" , trailNameFormated , a .Filename )
266
+
267
+ attachments += fmt .Sprintf ("\n " , a .Filename , attachmentPath )
268
+ t .saveAttachment (a , folderPath )
269
+ }
270
+
239
271
// preprocess message to handle newlines
240
272
content := strings .ReplaceAll (m .Content , "\n " , "\n > " )
241
273
content = t .replaceMentions (content )
242
274
243
275
writeToFile (f , "> " + content )
244
- writeToFile (f , "" )
276
+ // writeToFile(f, "")
277
+
278
+ // only write attachments if there are any
279
+ if len (attachments ) > 0 {
280
+ writeToFile (f , "" )
281
+ writeToFile (f , attachments )
282
+ }
245
283
}
246
284
writeToFile (f , "" )
247
285
}
286
+
287
+ log .Info ("Transcript saved!" )
288
+ }
289
+
290
+ func (t * Transcript ) saveAttachment (attachment MsgAttachment , folderPath string ) {
291
+ // Create blank file
292
+ file , err := os .Create (folderPath + "/" + attachment .Filename )
293
+ if err != nil {
294
+ err := eris .Wrap (err , "failed to create attachment file" )
295
+ log .Fatal (err )
296
+ }
297
+ defer file .Close ()
298
+
299
+ // http client to download attachment
300
+ client := http.Client {
301
+ CheckRedirect : func (r * http.Request , via []* http.Request ) error {
302
+ r .URL .Opaque = r .URL .Path
303
+ return nil
304
+ },
305
+ }
306
+
307
+ // Put content on file
308
+ resp , err := client .Get (attachment .URL )
309
+ if err != nil {
310
+ err := eris .Wrap (err , "failed to download attachment" )
311
+ log .Fatal (err )
312
+ }
313
+ defer resp .Body .Close ()
314
+
315
+ size , err := io .Copy (file , resp .Body )
316
+ if err != nil {
317
+ err := eris .Wrap (err , "failed to write attachment to file" )
318
+ log .Fatal (err )
319
+ }
320
+
321
+ log .Debugf ("Saved attachment: %s (%d)" , attachment .Filename , size )
248
322
}
249
323
250
324
// creates a new transcript
0 commit comments