forked from textileio/go-textile
/
thread_comments.go
57 lines (45 loc) · 1.3 KB
/
thread_comments.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
package core
import (
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
"github.com/golang/protobuf/ptypes"
"github.com/textileio/textile-go/pb"
"github.com/textileio/textile-go/repo"
)
// AddComment adds an outgoing comment block
func (t *Thread) AddComment(target string, body string) (mh.Multihash, error) {
t.mux.Lock()
defer t.mux.Unlock()
msg := &pb.ThreadComment{
Target: target,
Body: body,
}
res, err := t.commitBlock(msg, pb.ThreadBlock_COMMENT, nil)
if err != nil {
return nil, err
}
if err := t.indexBlock(res, repo.CommentBlock, target, body); err != nil {
return nil, err
}
if err := t.updateHead(res.hash); err != nil {
return nil, err
}
if err := t.post(res, t.Peers()); err != nil {
return nil, err
}
log.Debugf("added COMMENT to %s: %s", t.Id, res.hash.B58String())
return res.hash, nil
}
// handleCommentBlock handles an incoming comment block
func (t *Thread) handleCommentBlock(hash mh.Multihash, block *pb.ThreadBlock) (*pb.ThreadComment, error) {
msg := new(pb.ThreadComment)
if err := ptypes.UnmarshalAny(block.Payload, msg); err != nil {
return nil, err
}
if err := t.indexBlock(&commitResult{
hash: hash,
header: block.Header,
}, repo.CommentBlock, msg.Target, msg.Body); err != nil {
return nil, err
}
return msg, nil
}