-
Notifications
You must be signed in to change notification settings - Fork 9
/
chat.go
266 lines (234 loc) · 5.3 KB
/
chat.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
package handshake
import (
"bytes"
"crypto/rand"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"math/big"
"sort"
"strings"
)
const (
maxMessageSize = 250000 // ~2 Megabytes
defaultChatTTL = 604800 // 7 days in seconds
)
type lookup map[string][]byte
type chatLog map[string]chatLogEntry
type chatLogEntry struct {
ID string `json:"id,omitempty"`
Sender string `json:"sender,omitempty"`
Sent int64 `json:"sent,omitempty"`
Received int64 `json:"received,omitempty"`
TTL int64 `json:"ttl,omitempty"`
Data chatData `json:"data"`
}
func (cl chatLog) SortedJSON() ([]byte, error) {
return json.Marshal(cl.Sorted())
}
func (cl chatLog) Sorted() []chatLogEntry {
var entries []chatLogEntry
var keys []string
for k := range cl {
keys = append(keys, k)
}
sort.Strings(keys)
for _, v := range keys {
entries = append(entries, cl[v])
}
return entries
}
func (cl *chatLog) AddEntry(entry chatLogEntry) error {
if (entry.Sent == 0) && (entry.Received == 0) {
return errors.New("no valid timestamp found")
}
timestamp := entry.Sent
if timestamp == 0 {
timestamp = entry.Received
}
key := fmt.Sprintf("%v-%v", timestamp, entry.ID)
(*cl)[key] = entry
return nil
}
func (cl chatLog) HashInLog(hash string) bool {
for entry := range cl {
if strings.Contains(entry, hash) {
return true
}
}
return false
}
type chatData struct {
Parent string `json:"parent,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
Media []string `json:"media,omitempty"`
Message string `json:"message,omitempty"`
TTL int64 `json:"ttl,omitempty"`
}
type chat struct {
ID string
PeerID string
LastSent string
Peers map[string]chatPeer
Settings chatSettings
}
// a chatConfig allows safe encoding of a chat
type chatConfig struct {
ID string
PeerID string
LastSent string
Peers map[string]chatPeerConfig
Settings chatSettings
}
type chatSettings struct {
MaxTTL int64
}
// uniqueChatIDsFromPaths takes a lists of paths from and a profile ID and strips out unique ChatID
// entries and resturns a slice of strings
func uniqueChatIDsFromPaths(list []string, profileID string) (ids []string) {
idMap := make(map[string]struct{})
for _, l := range list {
if strings.Contains(l, profileID) {
s := strings.Split(l, "/")
idMap[s[1]] = struct{}{}
}
}
for id := range idMap {
ids = append(ids, id)
}
return
}
func newLookupFromGob(b []byte) (lookup, error) {
l := make(lookup)
var buffer bytes.Buffer
buffer.Write(b)
err := gob.NewDecoder(&buffer).Decode(&l)
return l, err
}
func newChatFromGob(b []byte) (chat, error) {
var config chatConfig
var buffer bytes.Buffer
buffer.Write(b)
err := gob.NewDecoder(&buffer).Decode(&config)
if err != nil {
return chat{}, err
}
return config.Chat()
}
func newChatLogFromGob(b []byte) (chatLog, error) {
var cl chatLog
var buffer bytes.Buffer
buffer.Write(b)
err := gob.NewDecoder(&buffer).Decode(&cl)
if err != nil {
return chatLog{}, err
}
return cl, nil
}
func (l lookup) get(key string) []byte {
return l[key]
}
func (l *lookup) popKey(key string) []byte {
v := l.get(key)
delete(*l, key)
return v
}
func (l *lookup) popRandom() (string, []byte) {
k, v := l.getRandom()
delete(*l, k)
return k, v
}
func (l lookup) getRandom() (string, []byte) {
x, _ := rand.Int(rand.Reader, big.NewInt(int64(len(l))))
i := x.Int64()
c := int64(0)
for k, v := range l {
if c == i {
return k, v
}
c++
}
return "", []byte{}
}
// TODO:
// - get chatLog
// - retrieve Chatmessages (this should query all peer's endpoints)
// -- this should be recursive and query chats until all either a hash match
// -- or the lookup hash doesn't exist
// -- or no parent exists
// - postToChat (this should post a message to chat)
// -
func (config chatConfig) Chat() (chat, error) {
c := chat{
ID: config.ID,
PeerID: config.PeerID,
LastSent: config.LastSent,
Peers: make(map[string]chatPeer),
Settings: config.Settings,
}
for _, peerConfig := range config.Peers {
peer, err := peerConfig.Peer()
if err != nil {
return chat{}, err
}
c.Peers[peer.ID] = peer
}
return c, nil
}
func (c chat) TTL() int64 {
if c.Settings.MaxTTL <= 0 {
return defaultChatTTL
}
return c.Settings.MaxTTL
}
func (c chat) Config() (chatConfig, error) {
config := chatConfig{
ID: c.ID,
PeerID: c.PeerID,
LastSent: c.LastSent,
Peers: make(map[string]chatPeerConfig),
Settings: c.Settings,
}
for _, peer := range c.Peers {
peerConfig, err := peer.Config()
if err != nil {
return chatConfig{}, err
}
config.Peers[peer.ID] = peerConfig
}
return config, nil
}
type chatPeer struct {
ID string
Alias string
Strategy strategy
}
type chatPeerConfig struct {
ID string
Alias string
Strategy strategyConfig
}
// Peer converts a chatPeerConfig into a chatPeer
func (config chatPeerConfig) Peer() (chatPeer, error) {
peer := chatPeer{
ID: config.ID,
Alias: config.Alias,
}
s, err := strategyFromConfig(config.Strategy)
if err != nil {
return chatPeer{}, err
}
peer.Strategy = s
return peer, nil
}
// Config returns a storage-safe chatPeerConfig and an error
func (c chatPeer) Config() (chatPeerConfig, error) {
config := chatPeerConfig{
ID: c.ID,
Alias: c.Alias,
}
s, err := c.Strategy.Export()
config.Strategy = s
return config, err
}