-
Notifications
You must be signed in to change notification settings - Fork 1
/
tx.go
124 lines (111 loc) · 3.17 KB
/
tx.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
package main
import (
"bufio"
"bytes"
"fmt"
"time"
"github.com/bwmarrin/discordgo"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
)
type Tx struct {
Ctim time.Time `json:"ctim,omitempty"`
Mtim time.Time `json:"mtim,omitempty"`
Path string `json:"path"`
FileIDs []string `json:"ids,omitempty"`
Checksums []string `json:"sums,omitempty"`
Tx TxType `json:"tx"`
Type InodeType `json:"type,omitempty"`
Size int64 `json:"size,omitempty"`
}
// getDataFile downloads an attachment and writes to buffer
func getDataFile(channelID string, fileID string, buffer []byte) (int, error) {
req := fasthttp.AcquireRequest()
req.SetRequestURI(fmt.Sprintf("https://cdn.discordapp.com/attachments/%s/%s/%s", channelID, fileID, DataChannelName))
req.Header.SetMethod(fasthttp.MethodGet)
resp := fasthttp.AcquireResponse()
err := fasthttp.Do(req, resp)
fasthttp.ReleaseRequest(req)
if err != nil {
return 0, err
}
n := copy(buffer, resp.Body())
fasthttp.ReleaseResponse(resp)
return n, nil
}
// createDeleteTx creates a delete transaction for path
func createDeleteTx(path string) Tx {
return Tx{Tx: DeleteTx, Path: path}
}
// applyMessageTxs applies transactions to DB, and writes message data to buffer if a buffer is given
func applyMessageTxs(db DB, ms []*discordgo.Message, buffer *bytes.Buffer, live bool) {
zap.S().Infof("applying %d messages with TXs", len(ms))
for _, m := range ms {
for _, file := range m.Attachments {
_, data, err := fasthttp.Get(nil, file.URL)
if err != nil {
zap.S().Warnf("%s, skipping tx batch", err)
continue
}
bytesReader := bytes.NewReader(data)
scanner := bufio.NewScanner(bytesReader)
for scanner.Scan() {
line := scanner.Text()
if len(line) == 0 {
continue
}
tx := &Tx{}
err := json.Unmarshal([]byte(line), tx)
if err != nil {
zap.S().Warnf("%s, skipping tx", err)
continue
}
switch tx.Tx {
case WriteTx:
zap.S().Debugw("Write", "path", tx.Path)
if live {
err := dsfs.ApplyLiveTx(tx.Path, tx)
if err != nil {
zap.S().Warnw("failed to apply live tx", "error", err)
}
} else {
db.Insert(tx.Path, tx)
}
case DeleteTx:
zap.S().Debugw("Delete", "path", tx.Path)
if live {
dsfs.lock.Lock()
db.Delete(tx.Path)
delete(dsfs.open, tx.Path)
dsfs.lock.Unlock()
} else {
db.Delete(tx.Path)
}
default:
zap.S().Warnf("found unknown tx type %d, skipping tx", tx.Tx)
continue
}
// Write to buffer
if buffer != nil {
buffer.WriteString(line)
buffer.WriteByte('\n')
}
}
}
}
zap.S().Info("done applying TXs")
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Not ready to accept TXs
if !dsfsReady.Load() {
return
}
// Don't handle the bot's own TXs or listen to a non-TX channel
if m.Author.ID == s.State.User.ID || m.ChannelID != dsfs.txChannel.ID {
return
}
// There is potentially some issues when doing this
// In this current state, open files will not be affected
// by any TXs broadcasted by remote clients
applyMessageTxs(dsfs.db, []*discordgo.Message{m.Message}, nil, true)
}