/
client.go
142 lines (130 loc) · 3.61 KB
/
client.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
package boards
import (
"io"
"fmt"
"bytes"
"strings"
"io/ioutil"
"encoding/json"
ipfs "github.com/ipfs/go-ipfs-api"
)
type Client struct {
shell *ipfs.Shell
}
func NewClient() (*Client, error) {
sh := ipfs.NewLocalShell()
if sh == nil {
err := fmt.Errorf("boards: ipfs daemon is offline")
return nil, err
}
return &Client{sh}, nil
}
func drainReader(r io.ReadCloser) {
io.Copy(ioutil.Discard, r)
r.Close()
}
func (c *Client) AdvertiseThreads(threads map[string][]string) {
if len(threads) == 0 {
return
}
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(threads)
if err != nil {
return
}
ref, err := c.shell.Add(&buf)
if err != nil {
return
}
c.shell.PubSubPublish(PubsubSyncThreads, ref)
}
func (c *Client) GetPost(ref string) (*Post, error) {
r, err := c.shell.Cat(ref)
if err != nil {
err = fmt.Errorf("boards: failed to cat ipfs post: %w", err)
return nil, err
}
defer drainReader(r)
var p Post
err = json.NewDecoder(r).Decode(&p)
if err != nil {
err = fmt.Errorf("boards: failed to decode post from json: %w", err)
return nil, err
}
r, err = c.shell.Cat(p.Content)
if err != nil {
err = fmt.Errorf("boards: failed to cat ipfs post content: %w", err)
return nil, err
}
defer drainReader(r)
content, err := ioutil.ReadAll(r)
if err != nil {
err = fmt.Errorf("boards: failed to read ipfs post content: %w", err)
return nil, err
}
p.Content = string(content)
return &p, nil
}
func (c *Client) PutPost(p Post) (string, error) {
err := p.validate()
if err != nil {
return "", err
}
if p.Thread != "" {
_, _, err = c.shell.BlockStat(p.Thread)
if err != nil {
err = fmt.Errorf("boards: failed to get thread: %w", err)
return "", err
}
}
_, _, err = c.shell.BlockStat(p.Content)
if err != nil {
ref, err := c.putContent(p.Content)
if err != nil {
return "", err
}
p.Content = ref
}
var buf bytes.Buffer
err = json.NewEncoder(&buf).Encode(&p)
if err != nil {
err = fmt.Errorf("boards: failed to encode post into json: %w", err)
return "", err
}
ref, err := c.shell.Add(&buf)
if err != nil {
err = fmt.Errorf("boards: failed to add post to ipfs: %w", err)
return "", err
}
c.advertise(p.Topic, p.Thread, ref)
return ref, nil
}
func (c *Client) advertise(topic, thread, ref string) {
if thread == "" {
c.advertiseNewThread(topic, ref)
} else {
c.advertiseNewPost(thread, ref)
}
}
func (c *Client) advertiseNewThread(topic, ref string) {
subTopics := append([]string{PubsubThreadsPrefix}, strings.Split(topic, "/")...)
for n := 1; n <= len(subTopics); n++ {
payload := fmt.Sprintf(`{"Topic":"%s","Ref":"%s"}`, topic, ref)
c.shell.PubSubPublish(strings.Join(subTopics[:n], "/"), payload)
}
}
func (c *Client) advertiseNewPost(thread, ref string) {
subTopics := []string{PubsubPostsPrefix, thread}
for n := 1; n <= len(subTopics); n++ {
payload := fmt.Sprintf(`{"Thread":"%s","Ref":"%s"}`, thread, ref)
c.shell.PubSubPublish(strings.Join(subTopics[:n], "/"), payload)
}
}
func (c *Client) putContent(content string) (string, error) {
ref, err := c.shell.Add(strings.NewReader(content))
if err != nil {
err = fmt.Errorf("boards: failed to add content to ipfs: %w", err)
return "", err
}
return ref, nil
}