forked from textileio/go-textile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_service.go
405 lines (362 loc) · 11.9 KB
/
thread_service.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
package core
import (
"context"
"errors"
"fmt"
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
libp2pc "gx/ipfs/QmPvyPwuCgJ7pDmrKDxRtsScJgBaM5h4EpRL2qQJsmXf4n/go-libp2p-crypto"
"gx/ipfs/QmTRhk7cgjUf2gfQ3p2M9KPECNZEW9XUrmHcFCgog4cPgB/go-libp2p-peer"
"gx/ipfs/QmUJYo4etAQqFfSS2rarFAE97eNGB8ej64YkRT2SmsYD4r/go-ipfs/core"
"gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/segmentio/ksuid"
"github.com/textileio/textile-go/crypto"
"github.com/textileio/textile-go/keypair"
"github.com/textileio/textile-go/pb"
"github.com/textileio/textile-go/repo"
"github.com/textileio/textile-go/service"
)
// ErrInvalidThreadBlock is a catch all error for malformed / invalid blocks
var ErrInvalidThreadBlock = errors.New("invalid thread block")
// ThreadService is a libp2p service for orchestrating a collection of files
// with annotations amongst a group of peers
type ThreadsService struct {
service *service.Service
datastore repo.Datastore
getThread func(id string) *Thread
addThread func(sk libp2pc.PrivKey, conf AddThreadConfig) (*Thread, error)
sendNotification func(note *repo.Notification) error
}
// NewThreadsService returns a new threads service
func NewThreadsService(
account *keypair.Full,
node *core.IpfsNode,
datastore repo.Datastore,
getThread func(id string) *Thread,
addThread func(sk libp2pc.PrivKey, conf AddThreadConfig) (*Thread, error),
sendNotification func(note *repo.Notification) error,
) *ThreadsService {
handler := &ThreadsService{
datastore: datastore,
getThread: getThread,
addThread: addThread,
sendNotification: sendNotification,
}
handler.service = service.NewService(account, handler, node)
return handler
}
// NewDummyThreadsService is used to create message envelopes
// _before_ the node is able to go online
func NewDummyThreadsService(account *keypair.Full, node *core.IpfsNode) *ThreadsService {
return &ThreadsService{service: &service.Service{Account: account, Node: node}}
}
// Protocol returns the handler protocol
func (h *ThreadsService) Protocol() protocol.ID {
return protocol.ID("/textile/threads/2.0.0")
}
// Ping pings another peer
func (h *ThreadsService) Ping(pid peer.ID) (service.PeerStatus, error) {
return h.service.Ping(pid)
}
// Handle is called by the underlying service handler method
func (h *ThreadsService) Handle(pid peer.ID, env *pb.Envelope) (*pb.Envelope, error) {
if env.Message.Type != pb.Message_THREAD_ENVELOPE {
return nil, nil
}
tenv := new(pb.ThreadEnvelope)
if err := ptypes.UnmarshalAny(env.Message.Payload, tenv); err != nil {
return nil, err
}
hash, err := mh.FromB58String(tenv.Hash)
if err != nil {
return nil, err
}
thrd := h.getThread(tenv.Thread)
if thrd == nil {
// this might be a direct invite
if err := h.handleInvite(hash, tenv); err != nil {
return nil, err
}
return nil, nil
}
block, err := thrd.handleBlock(hash, tenv.Ciphertext)
if err != nil {
return nil, err
}
if block == nil {
// exists, abort
return nil, nil
}
switch block.Type {
case pb.ThreadBlock_MERGE:
log.Debugf("handling MERGE from %s", block.Header.Author)
err = h.handleMerge(thrd, hash, block)
case pb.ThreadBlock_IGNORE:
log.Debugf("handling IGNORE from %s", block.Header.Author)
err = h.handleIgnore(thrd, hash, block)
case pb.ThreadBlock_FLAG:
log.Debugf("handling FLAG from %s", block.Header.Author)
err = h.handleFlag(thrd, hash, block)
case pb.ThreadBlock_JOIN:
log.Debugf("handling JOIN from %s", block.Header.Author)
err = h.handleJoin(thrd, hash, block)
case pb.ThreadBlock_ANNOUNCE:
log.Debugf("handling ANNOUNCE from %s", block.Header.Author)
err = h.handleAnnounce(thrd, hash, block)
case pb.ThreadBlock_LEAVE:
log.Debugf("handling LEAVE from %s", block.Header.Author)
err = h.handleLeave(thrd, hash, block)
case pb.ThreadBlock_MESSAGE:
log.Debugf("handling MESSAGE from %s", block.Header.Author)
err = h.handleMessage(thrd, hash, block)
case pb.ThreadBlock_FILES:
log.Debugf("handling FILES from %s", block.Header.Author)
err = h.handleFiles(thrd, hash, block)
case pb.ThreadBlock_COMMENT:
log.Debugf("handling COMMENT from %s", block.Header.Author)
err = h.handleComment(thrd, hash, block)
case pb.ThreadBlock_LIKE:
log.Debugf("handling LIKE from %s", block.Header.Author)
err = h.handleLike(thrd, hash, block)
default:
return nil, nil
}
if err != nil {
return nil, err
}
if err := thrd.followParents(block.Header.Parents); err != nil {
return nil, err
}
if _, err := thrd.handleHead(hash, block.Header.Parents); err != nil {
return nil, err
}
// handle newly discovered peers during back prop
if err := thrd.sendWelcome(); err != nil {
return nil, err
}
// flush cafe queue _at the very end_
go thrd.cafeOutbox.Flush()
return nil, nil
}
// SendMessage sends a message to a peer
func (h *ThreadsService) SendMessage(ctx context.Context, pid peer.ID, env *pb.Envelope) error {
return h.service.SendMessage(ctx, pid, env)
}
// NewEnvelope signs and wraps an encypted block for transport
func (h *ThreadsService) NewEnvelope(threadId string, hash mh.Multihash, ciphertext []byte) (*pb.Envelope, error) {
tenv := &pb.ThreadEnvelope{
Thread: threadId,
Hash: hash.B58String(),
Ciphertext: ciphertext,
}
return h.service.NewEnvelope(pb.Message_THREAD_ENVELOPE, tenv, nil, false)
}
// handleInvite receives an invite message
func (h *ThreadsService) handleInvite(hash mh.Multihash, tenv *pb.ThreadEnvelope) error {
plaintext, err := crypto.Decrypt(h.service.Node.PrivateKey, tenv.Ciphertext)
if err != nil {
// wasn't an invite, abort
return ErrInvalidThreadBlock
}
block := new(pb.ThreadBlock)
if err := proto.Unmarshal(plaintext, block); err != nil {
return err
}
if block.Type != pb.ThreadBlock_INVITE {
return ErrInvalidThreadBlock
}
msg := new(pb.ThreadInvite)
if err := ptypes.UnmarshalAny(block.Payload, msg); err != nil {
return err
}
log.Debugf("handling THREAD_INVITE from %s", block.Header.Author)
date, err := ptypes.Timestamp(block.Header.Date)
if err != nil {
return err
}
if err := h.datastore.ThreadInvites().Add(&repo.ThreadInvite{
Id: hash.B58String(),
Block: plaintext,
Name: msg.Name,
Inviter: block.Header.Author,
Date: date,
}); err != nil {
if !repo.ConflictError(err) {
return err
}
// exists, abort
return nil
}
notification, err := h.newNotification(block.Header, repo.InviteReceivedNotification)
if err != nil {
return err
}
notification.Subject = msg.Name
notification.SubjectId = tenv.Thread
notification.BlockId = hash.B58String() // invite block
notification.Body = "invited you to join"
return h.sendNotification(notification)
}
// handleMerge receives a merge message
func (h *ThreadsService) handleMerge(thrd *Thread, hash mh.Multihash, block *pb.ThreadBlock) error {
return thrd.handleMergeBlock(hash, block)
}
// handleIgnore receives an ignore message
func (h *ThreadsService) handleIgnore(thrd *Thread, hash mh.Multihash, block *pb.ThreadBlock) error {
if _, err := thrd.handleIgnoreBlock(hash, block); err != nil {
return err
}
return nil
}
// handleFlag receives a flag message
func (h *ThreadsService) handleFlag(thrd *Thread, hash mh.Multihash, block *pb.ThreadBlock) error {
if _, err := thrd.handleFlagBlock(hash, block); err != nil {
return err
}
return nil
}
// handleJoin receives a join message
func (h *ThreadsService) handleJoin(thrd *Thread, hash mh.Multihash, block *pb.ThreadBlock) error {
if _, err := thrd.handleJoinBlock(hash, block); err != nil {
return err
}
notification, err := h.newNotification(block.Header, repo.PeerJoinedNotification)
if err != nil {
return err
}
notification.Subject = thrd.Name
notification.SubjectId = thrd.Id
notification.BlockId = hash.B58String()
notification.Body = "joined"
return h.sendNotification(notification)
}
// handleAnnounce receives an announce message
func (h *ThreadsService) handleAnnounce(thrd *Thread, hash mh.Multihash, block *pb.ThreadBlock) error {
if _, err := thrd.handleAnnounceBlock(hash, block); err != nil {
return err
}
return nil
}
// handleLeave receives a leave message
func (h *ThreadsService) handleLeave(thrd *Thread, hash mh.Multihash, block *pb.ThreadBlock) error {
if err := thrd.handleLeaveBlock(hash, block); err != nil {
return err
}
notification, err := h.newNotification(block.Header, repo.PeerLeftNotification)
if err != nil {
return err
}
notification.Subject = thrd.Name
notification.SubjectId = thrd.Id
notification.BlockId = hash.B58String()
notification.Body = "left"
return h.sendNotification(notification)
}
// handleMessage receives a message message
func (h *ThreadsService) handleMessage(thrd *Thread, hash mh.Multihash, block *pb.ThreadBlock) error {
msg, err := thrd.handleMessageBlock(hash, block)
if err != nil {
return err
}
notification, err := h.newNotification(block.Header, repo.MessageAddedNotification)
if err != nil {
return err
}
notification.Body = msg.Body
notification.BlockId = hash.B58String()
notification.Subject = thrd.Name
notification.SubjectId = thrd.Id
return h.sendNotification(notification)
}
// handleData receives a files message
func (h *ThreadsService) handleFiles(thrd *Thread, hash mh.Multihash, block *pb.ThreadBlock) error {
msg, err := thrd.handleFilesBlock(hash, block)
if err != nil {
return err
}
notification, err := h.newNotification(block.Header, repo.FilesAddedNotification)
if err != nil {
return err
}
notification.Target = msg.Target
notification.Body = "added a " + threadSubject(thrd.Schema.Name)
notification.BlockId = hash.B58String()
notification.Subject = thrd.Name
notification.SubjectId = thrd.Id
return h.sendNotification(notification)
}
// handleComment receives a comment message
func (h *ThreadsService) handleComment(thrd *Thread, hash mh.Multihash, block *pb.ThreadBlock) error {
msg, err := thrd.handleCommentBlock(hash, block)
if err != nil {
return err
}
target := h.datastore.Blocks().Get(msg.Target)
if target == nil {
return nil
}
var desc string
if target.AuthorId == h.service.Node.Identity.Pretty() {
desc = "your " + threadSubject(thrd.Schema.Name)
} else {
desc = "a " + threadSubject(thrd.Schema.Name)
}
notification, err := h.newNotification(block.Header, repo.CommentAddedNotification)
if err != nil {
return err
}
notification.Body = fmt.Sprintf("commented on %s: \"%s\"", desc, msg.Body)
notification.BlockId = hash.B58String()
notification.Target = target.Target
notification.Subject = thrd.Name
notification.SubjectId = thrd.Id
return h.sendNotification(notification)
}
// handleLike receives a like message
func (h *ThreadsService) handleLike(thrd *Thread, hash mh.Multihash, block *pb.ThreadBlock) error {
msg, err := thrd.handleLikeBlock(hash, block)
if err != nil {
return err
}
target := h.datastore.Blocks().Get(msg.Target)
if target == nil {
return nil
}
var desc string
if target.AuthorId == h.service.Node.Identity.Pretty() {
desc = "your " + threadSubject(thrd.Schema.Name)
} else {
desc = "a " + threadSubject(thrd.Schema.Name)
}
notification, err := h.newNotification(block.Header, repo.LikeAddedNotification)
if err != nil {
return err
}
notification.Body = "liked " + desc
notification.BlockId = hash.B58String()
notification.Target = target.Target
notification.Subject = thrd.Name
notification.SubjectId = thrd.Id
return h.sendNotification(notification)
}
// newNotification returns new thread notification
func (h *ThreadsService) newNotification(header *pb.ThreadBlockHeader, ntype repo.NotificationType) (*repo.Notification, error) {
date, err := ptypes.Timestamp(header.Date)
if err != nil {
return nil, err
}
return &repo.Notification{
Id: ksuid.New().String(),
Date: date,
ActorId: header.Author,
Type: ntype,
}, nil
}
// threadSubject returns the thread subject
func threadSubject(name string) string {
if name == "" {
return "file"
}
return name
}