-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpreview.go
701 lines (611 loc) · 18.5 KB
/
preview.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
package main
import (
"embed"
"fmt"
"html/template"
"io/fs"
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"unicode"
"unicode/utf16"
"github.com/ansel1/merry/v2"
)
//go:embed preview_templates/*.html
var templatesFS embed.FS
//go:embed preview_static/*
var staticFS embed.FS
type Server struct {
config *Config
saver *JSONFilesHistorySaver
userReader *ChatSyncReader[UserData]
chatReader *ChatSyncReader[ChatData]
chatsMsgReader *ChatsMessageReader
mux *http.ServeMux
}
type ChatPageView struct {
ChatID int64
ChatTitle string
Messages []map[string]interface{}
MessagesCountApprox int
From int
Prev int
Next int
Limit int
HasPrev bool
HasNext bool
}
type File struct {
ID int64
Name string
FullWebPath string
Index int64
Size int64
}
func (s *Server) chatsPageHandler(w http.ResponseWriter, r *http.Request) error {
type ChatWithTitle struct {
SavedChatEntry
Title string
}
chatEntries, err := s.saver.ReadSavedChatsList()
if err != nil {
return merry.Wrap(err)
}
if err := s.userReader.UpdateOffsets(); err != nil {
return merry.Wrap(err)
}
if err := s.chatReader.UpdateOffsets(); err != nil {
return merry.Wrap(err)
}
chats := make([]ChatWithTitle, len(chatEntries))
for i, chatEntry := range chatEntries {
chats[i].SavedChatEntry = chatEntry
chats[i].Title, err = s.readChatTitle(s.userReader, s.chatReader, chatEntry.ID, chatEntry.FSTitle)
if err != nil {
log.Warn("chat #%d reading error: %s", chatEntry.ID, err)
}
}
s.renderTemplate(w, "chats.html", chats)
return nil
}
func (s *Server) chatPageHandler(w http.ResponseWriter, r *http.Request) error {
chatID, err := strconv.ParseInt(r.PathValue("chatID"), 10, 64)
if err != nil {
return merry.Prepend(err, "invalid chat ID")
}
limitStr := r.URL.Query().Get("limit")
fromStr := r.URL.Query().Get("from")
limit := 10000
from := 0
if limitStr != "" {
limit, err = strconv.Atoi(limitStr)
if err != nil {
return merry.Prepend(err, "invalid limit")
}
}
if fromStr != "" {
from, err = strconv.Atoi(fromStr)
if err != nil {
return merry.Prepend(err, "invalid from parameter")
}
}
chatEntries, err := s.saver.ReadSavedChatsList()
if err != nil {
return merry.Wrap(err)
}
var chatEntry SavedChatEntry
for _, chat := range chatEntries {
if chat.ID == chatID {
chatEntry = chat
break
}
}
if chatEntry.ID == 0 {
return merry.Prepend(err, "couldn't load chat")
}
if err := s.userReader.UpdateOffsets(); err != nil {
return merry.Wrap(err)
}
if err := s.chatReader.UpdateOffsets(); err != nil {
return merry.Wrap(err)
}
userReader := &ChatCachedReader[UserData]{reader: s.userReader}
chatReader := &ChatCachedReader[ChatData]{reader: s.chatReader}
userData, err := userReader.ReadOpt(chatID)
if err != nil {
return merry.Wrap(err)
}
chatData, err := chatReader.ReadOpt(chatID)
if err != nil {
return merry.Wrap(err)
}
chatTitle, err := s.readChatTitle(userReader, chatReader, chatID, chatEntry.FSTitle)
if err != nil {
return merry.Wrap(err)
}
filesByIds, err := s.loadChatFiles(chatID)
if err != nil {
return merry.Wrap(err)
}
messages, hasNext, err := s.chatsMsgReader.Read(chatEntry.FPath, from, limit)
if err != nil {
return merry.Wrap(err)
}
for _, t := range messages {
id := int64(t["ID"].(float64))
if t["_"] == "TL_messageService" {
action := t["Action"].(map[string]interface{})
// TL_messageActionChatCreate -> "ChatCreate"
t["__ServiceMessage"] = strings.TrimPrefix(action["_"].(string), "TL_messageAction")
} else {
if files, ok := filesByIds[id]; ok {
t["__Files"] = files
}
if _, ok := t["Message"]; ok {
t["__MessageParts"] = applyEntities(t["Message"].(string), t["Entities"].([]interface{}))
}
if userData != nil {
// dialog (user <-> user)
if t["Out"].(bool) {
// this is ours message in a dialog, our ID will be in FromID.UserID
t["__FromFirstName"], t["__FromLastName"], err = s.getFirstLastNames(t, userReader, chatReader)
} else {
// this is other's message in a dialog, there should be a UserData record
if userData.IsDeleted {
t["__FromFirstName"], t["__FromLastName"] = "Deleted Account", ""
} else {
t["__FromFirstName"], t["__FromLastName"] = derefOr(userData.FirstName, ""), derefOr(userData.LastName, "")
}
}
} else if chatData != nil && chatData.IsChannel {
// channel
t["__FromFirstName"], t["__FromLastName"] = chatData.Title, ""
} else if chatData != nil && !chatData.IsChannel {
// group chat
t["__FromFirstName"], t["__FromLastName"], err = s.getFirstLastNames(t, userReader, chatReader)
}
if err != nil {
log.Error(err, "")
}
// something is wrong, data is inconsistent, trying to display at least something
if t["__FromFirstName"] == "" && t["__FromLastName"] == "" {
t["__FromFirstName"] = chatEntry.FSTitle
}
if fwdFromID, ok := t["FwdFrom"].(map[string]interface{}); ok {
t["__FwdFromFirstName"], t["__FwdFromLastName"], err = s.getFirstLastNames(fwdFromID, userReader, chatReader)
if err != nil {
log.Error(err, "")
}
}
}
}
hasPrev := from > 0
prev := from - limit
if prev < 0 || limit == 0 {
prev = 0
}
next := from + limit
msgsTotalApprox, err := s.chatsMsgReader.EstimateMessagesCount(chatEntry.FPath)
if err != nil {
return merry.Wrap(err)
}
s.renderTemplate(w, "chat.html", ChatPageView{
ChatID: chatID,
ChatTitle: chatTitle,
Messages: messages,
MessagesCountApprox: int(msgsTotalApprox),
From: from,
Prev: prev,
Next: next,
Limit: limit,
HasPrev: hasPrev,
HasNext: hasNext,
})
return nil
}
func (s *Server) getFirstLastNames(
t map[string]interface{},
userReader *ChatCachedReader[UserData],
chatReader *ChatCachedReader[ChatData],
) (firstName, lastName string, err error) {
if fromID, ok := t["FromID"].(map[string]interface{}); ok {
if fromUserIDStr, ok := fromID["UserID"].(string); ok {
fromUserID, err := strconv.ParseInt(fromUserIDStr, 10, 64)
if err != nil {
return "", "", merry.Prependf(err, "couldn't parse FromID['UserID'] for %s", fromUserIDStr)
}
fromUser, err := userReader.ReadOpt(fromUserID)
if err != nil {
return "", "", merry.Wrap(err)
}
if fromUser != nil {
return derefOr(fromUser.FirstName, ""), derefOr(fromUser.LastName, ""), nil
}
} else if fromChannelIDStr, ok := fromID["ChannelID"].(string); ok {
fromChannelID, err := strconv.ParseInt(fromChannelIDStr, 10, 64)
if err != nil {
return "", "", merry.Prependf(err, "couldn't parse FromID['ChannelID'] for %s", fromUserIDStr)
}
fromChannel, err := chatReader.ReadOpt(fromChannelID)
if err != nil {
return "", "", merry.Wrap(err)
}
if fromChannel != nil {
return fromChannel.Title, "", nil
}
}
}
return "", "", nil
}
func extractFirstTwoLetters(firstName string, lastName string) string {
if firstName != "" && lastName != "" {
return firstLetterUpper(firstName) + firstLetterUpper(lastName)
}
name := firstName
if name == "" {
name = lastName
}
words := strings.Fields(name)
if len(words) >= 2 {
return firstLetterUpper(words[0]) + firstLetterUpper(words[1])
} else if len(words) == 1 {
return firstLetterUpper(words[0])
}
return ""
}
func firstLetterUpper(str string) string {
for _, c := range str {
return string(unicode.ToUpper(c))
}
return ""
}
func applyEntities(strText string, entities []interface{}) []interface{} {
type HTMLInsertion struct {
html string
hasClosingBlock bool
}
runeText := []rune(strText)
text := utf16.Encode(runeText)
htmlInserts := make([]HTMLInsertion, len(text)+1)
// entities can include other entities, but intersection seems not allowed:
// sending message with `"entities":[{"type":"bold","offset":0,"length":6}, {"type":"italic","offset":3,"length":9}]`
// results in error: `Bad Request: entity beginning at UTF-16 offset 3 ends after the end of the text at UTF-16 offset 12`
for _, entAny := range entities {
if ent, ok := entAny.(map[string]interface{}); ok {
entOpen := ""
entClose := ""
isBlock := false
switch ent["_"] {
case "TL_messageEntityTextUrl": //type name before v0.167.0
fallthrough
case "TL_messageEntityTextURL":
var url string
if u, ok := ent["Url"]; ok { //field name before v0.167.0
url = u.(string)
} else {
url = ent["URL"].(string)
}
href := addDefaultScheme(url, "http")
entOpen = `<a href="` + href + `" target="_blank">`
entClose = `</a>`
case "TL_messageEntityUrl": //type name before v0.167.0
fallthrough
case "TL_messageEntityURL":
entOffset := int64(ent["Offset"].(float64))
entLength := int64(ent["Length"].(float64))
href := string(utf16.Decode(text[entOffset : entOffset+entLength]))
href = addDefaultScheme(href, "http")
entOpen = `<a href="` + href + `" target="_blank">`
entClose = `</a>`
case "TL_messageEntityBold":
entOpen, entClose = `<b>`, `</b>`
case "TL_messageEntityItalic":
entOpen, entClose = `<i>`, `</i>`
case "TL_messageEntityUnderline":
entOpen, entClose = `<u>`, `</u>`
case "TL_messageEntityStrike":
entOpen, entClose = `<s>`, `</s>`
case "TL_messageEntityCode":
entOpen, entClose = `<code>`, `</code>`
case "TL_messageEntityPre":
entOpen, entClose = `<pre>`, `</pre>`
isBlock = true
case "TL_messageEntityBlockquote":
entOpen, entClose = `<blockquote>`, `</blockquote>`
isBlock = true
}
if entOpen != "" {
entOffset := int64(ent["Offset"].(float64))
entLength := int64(ent["Length"].(float64))
// Have found some messages with a code entity with offset+length = len(text_in_utf16)+1.
// Android and iOS clients applied code styling on these messages
// but tdesktop showed just plain text. So this looks like a Telegram bug.
if entOffset+entLength >= int64(len(htmlInserts)) {
entLength = int64(len(htmlInserts)) - entOffset - 1
}
htmlInserts[entOffset].html += entOpen
htmlInserts[entOffset+entLength].html = entClose + htmlInserts[entOffset+entLength].html
if isBlock {
htmlInserts[entOffset+entLength].hasClosingBlock = true
}
}
}
}
var res []interface{}
lastEntityEnd := 0
lastEntityWasBlock := false
addTextLines := func(endI int) {
lines := strings.Split(string(utf16.Decode(text[lastEntityEnd:endI])), "\n")
if lastEntityWasBlock && lines[0] == "" {
lines = lines[1:] //removing empty newline after block entity (such as <pre>)
}
for i, line := range lines {
if i > 0 {
res = append(res, template.HTML("<br>"))
}
if line != "" {
res = append(res, line)
}
}
}
for i, ins := range htmlInserts {
if ins.html != "" {
if lastEntityEnd < i {
addTextLines(i)
}
res = append(res, template.HTML(ins.html))
lastEntityEnd = i
lastEntityWasBlock = ins.hasClosingBlock
}
}
addTextLines(len(text))
return res
}
// https://datatracker.ietf.org/doc/html/rfc1738#section-5
var urlSchemeRe = regexp.MustCompile(`(?i)^[a-z][a-z0-9+.\-]*:`)
// "example.com" -> "http://example.com"
func addDefaultScheme(url, defaultScheme string) string {
scheme := urlSchemeRe.FindString(url)
if scheme == "" {
if !strings.HasPrefix(url, "//") {
url = "//" + url
}
url = defaultScheme + ":" + url
}
return url
}
func derefOr[T any](val *T, defaultVal T) T {
if val == nil {
return defaultVal
}
return *val
}
type ChatReader[T any] interface {
Read(id int64) (T, bool, error)
}
func (s *Server) readChatTitle(
userReader ChatReader[UserData],
chatReader ChatReader[ChatData],
chatID int64, fallback string,
) (string, error) {
userData, found, err := userReader.Read(chatID)
if err != nil {
return fallback, merry.Wrap(err)
}
if found {
if userData.IsDeleted {
return "Deleted Account", nil
}
return strings.TrimSpace(derefOr(userData.FirstName, "") + " " + derefOr(userData.LastName, "")), nil
}
chatData, found, err := chatReader.Read(chatID)
if err != nil {
return fallback, merry.Wrap(err)
}
if found {
return chatData.Title, nil
}
return fallback, nil
}
func (s *Server) loadChatFiles(chatID int64) (map[int64][]File, error) {
filesById := make(map[int64][]File)
files, err := s.saver.ReadSavedChatFilesList(chatID)
if err != nil {
return nil, merry.Wrap(err)
}
for _, file := range files {
stat, err := os.Stat(file.FPath)
if err != nil {
return nil, merry.Wrap(err)
}
relPath, _ := filepath.Rel(s.saver.Dirpath, file.FPath)
filesById[file.MessageID] = append(filesById[file.MessageID], File{
Name: file.FName,
FullWebPath: "/" + filepath.ToSlash(relPath),
Index: file.IndexInMessage,
Size: stat.Size(),
})
}
for _, files := range filesById {
sort.Slice(files, func(i, j int) bool { return files[i].Index < files[j].Index })
}
return filesById, nil
}
func (s *Server) renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
templates := template.New("").Funcs(template.FuncMap{
"formatDate": func(date interface{}) string {
return time.Unix(int64(date.(float64)), 0).Format("02.01.2006 15:04:05")
},
"firstLetters": extractFirstTwoLetters,
"humanizeSize": func(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
prefixes := "KMGTPE"
div, exp := int64(unit), 0
for b > div*unit && exp < len(prefixes)-1 {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), prefixes[exp])
},
"pluralize": func(num int, single, plural string) string {
if num == 1 {
return single
}
return plural
},
"add": func(a, b int) int {
return a + b
},
})
// Parse the layout and the specific template
templates, err := templates.ParseFS(templatesFS, "preview_templates/layout.html", "preview_templates/"+tmpl)
if err != nil {
log.Info("Error parsing templates: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = templates.ExecuteTemplate(w, "layout.html", data)
if err != nil {
log.Info("Error rendering template %s: %v", tmpl, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.mux.ServeHTTP(w, r)
}
// ChatSyncReader is a thread-safe wrapper around JSONRecordsReader
// so it can be used with (theoretically) concurrent HTTP requests.
type ChatSyncReader[T UserData | ChatData] struct {
reader *JSONRecordsReader[T]
mutex sync.RWMutex
}
func NewChatSyncReader[T UserData | ChatData](fpath string) *ChatSyncReader[T] {
return &ChatSyncReader[T]{
reader: NewJSONRecordsReader[T](fpath),
}
}
func (r *ChatSyncReader[T]) Read(id int64) (T, bool, error) {
r.mutex.RLock()
defer r.mutex.RUnlock()
return r.reader.Read(id)
}
func (r *ChatSyncReader[T]) UpdateOffsets() error {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.reader.UpdateOffsets()
}
// ChatCachedReader is a wrapper around JSONRecordsReader with read items cache.
//
// It IS NOT thread-safe, though multiple chached readers may share same [ChatSyncReader].
//
// It is expected to be created once for each HTTP request, so multiple reads of same
// UserID (for example) will be cached for single request. And so subsequent request
// will run with an empty cache and may read updated user data.
type ChatCachedReader[T UserData | ChatData] struct {
reader *ChatSyncReader[T]
cache map[int64]T
}
func (r *ChatCachedReader[T]) Read(id int64) (T, bool, error) {
if r.cache == nil {
r.cache = make(map[int64]T)
}
item, ok := r.cache[id]
if ok {
return item, true, nil
}
item, ok, err := r.reader.Read(id)
if err != nil {
return item, false, merry.Wrap(err)
}
if ok {
r.cache[id] = item
return item, true, nil
}
return item, false, nil
}
func (r *ChatCachedReader[T]) ReadOpt(id int64) (*T, error) {
item, found, err := r.Read(id)
if err != nil {
return nil, merry.Wrap(err)
}
if !found {
return nil, nil
}
return &item, nil
}
// ChatsMessageReader is a thread-safe wrapper around multiple [JSONMessageReader]s.
// It stores a reader for each chat's history file.
type ChatsMessageReader struct {
mutex sync.Mutex
chatReaders map[string]*JSONMessageReader
}
// Read reads limit messages starting from offset message from history file at fpath.
//
// First message has offset=0.
//
// If limit=0, reads all messages till the end (offset still applies).
func (r *ChatsMessageReader) Read(fpath string, offset, limit int) ([]map[string]interface{}, bool, error) {
r.mutex.Lock()
defer r.mutex.Unlock()
if r.chatReaders == nil {
r.chatReaders = make(map[string]*JSONMessageReader)
}
reader := r.chatReaders[fpath]
if reader == nil {
reader = NewJSONMessageReader(fpath)
r.chatReaders[fpath] = reader
}
return reader.Read(offset, limit)
}
func (r *ChatsMessageReader) EstimateMessagesCount(fpath string) (int64, error) {
r.mutex.Lock()
defer r.mutex.Unlock()
if r.chatReaders == nil {
return -1, nil
}
reader := r.chatReaders[fpath]
if reader == nil {
return -1, nil
}
return reader.EstimateMessagesCount()
}
func withError(handler func(http.ResponseWriter, *http.Request) error) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if err := handler(w, r); err != nil {
log.Error(err, "while handling %s %s", r.Method, r.URL)
http.Error(w, merry.Details(err), http.StatusInternalServerError)
}
}
}
func servePreviewHttp(addr string, config *Config, saver *JSONFilesHistorySaver) error {
server := &Server{
config: config,
saver: saver,
userReader: NewChatSyncReader[UserData](saver.usersFPath()),
chatReader: NewChatSyncReader[ChatData](saver.chatsFPath()),
chatsMsgReader: &ChatsMessageReader{},
}
mux := http.NewServeMux()
server.mux = mux
mux.Handle("/", http.RedirectHandler("/chats/", http.StatusFound))
mux.HandleFunc("/chats/", withError(server.chatsPageHandler))
mux.HandleFunc("/chats/{chatID}", withError(server.chatPageHandler))
filesDir := http.Dir(config.OutDirPath + "/files")
mux.Handle("/files/", http.StripPrefix("/files/", http.FileServer(filesDir)))
staticFS, _ := fs.Sub(staticFS, "preview_static")
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS))))
log.Info("Starting server on http://%s", addr) //"http://" makes the address openable with Ctrl+Click in some terminal emulators (like GNOME Terminal)
if err := http.ListenAndServe(addr, server); err != nil {
return err
}
return nil
}