forked from textileio/go-textile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_ignores.go
105 lines (82 loc) · 2.33 KB
/
thread_ignores.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
package core
import (
"fmt"
"strings"
"github.com/textileio/textile-go/ipfs"
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
"github.com/golang/protobuf/ptypes"
"github.com/textileio/textile-go/pb"
"github.com/textileio/textile-go/repo"
)
// AddIgnore adds an outgoing ignore block targeted at another block to ignore
func (t *Thread) AddIgnore(block string) (mh.Multihash, error) {
t.mux.Lock()
defer t.mux.Unlock()
// adding an ignore specific prefix here to ensure future flexibility
target := fmt.Sprintf("ignore-%s", block)
msg := &pb.ThreadIgnore{
Target: target,
}
res, err := t.commitBlock(msg, pb.ThreadBlock_IGNORE, nil)
if err != nil {
return nil, err
}
if err := t.indexBlock(res, repo.IgnoreBlock, target, ""); err != nil {
return nil, err
}
rblock := t.datastore.Blocks().Get(block)
if err := t.ignoreBlockTarget(rblock); 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
}
// cleanup
if err := t.datastore.Notifications().DeleteByBlock(block); err != nil {
return nil, err
}
log.Debugf("added IGNORE to %s: %s", t.Id, res.hash.B58String())
return res.hash, nil
}
// handleIgnoreBlock handles an incoming ignore block
func (t *Thread) handleIgnoreBlock(hash mh.Multihash, block *pb.ThreadBlock) (*pb.ThreadIgnore, error) {
msg := new(pb.ThreadIgnore)
if err := ptypes.UnmarshalAny(block.Payload, msg); err != nil {
return nil, err
}
// cleanup
blockId := strings.Replace(msg.Target, "ignore-", "", 1)
if err := t.datastore.Notifications().DeleteByBlock(blockId); err != nil {
return nil, err
}
if err := t.indexBlock(&commitResult{
hash: hash,
header: block.Header,
}, repo.IgnoreBlock, msg.Target, ""); err != nil {
return nil, err
}
rblock := t.datastore.Blocks().Get(blockId)
if err := t.ignoreBlockTarget(rblock); err != nil {
return nil, err
}
return msg, nil
}
// ignoreBlockTarget conditionally removes block target and files
func (t *Thread) ignoreBlockTarget(block *repo.Block) error {
if block == nil || block.Target == "" {
return nil
}
switch block.Type {
case repo.FilesBlock:
node, err := ipfs.NodeAtPath(t.node(), block.Target)
if err != nil {
return err
}
return t.removeFiles(node)
default:
return nil
}
}