This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
/
invites.go
123 lines (108 loc) · 2.48 KB
/
invites.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
package cmd
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/golang/protobuf/ptypes"
"github.com/textileio/go-textile/pb"
)
func InviteCreate(threadID string, address string, wait int) error {
if address != "" {
contact, _, _ := getContact(address)
if contact != nil {
return createInvite(threadID, address)
}
output("Could not find contact locally, searching network...")
results := handleSearchStream("contacts/search", params{
opts: map[string]string{
"address": address,
"limit": strconv.Itoa(10),
"wait": strconv.Itoa(wait),
},
})
if len(results) == 0 {
output("Could not find contact")
return nil
}
remote := make(map[string]pb.QueryResult)
for _, res := range results {
if !res.Local {
remote[res.Id] = res // overwrite with newer / more complete result
}
}
result, ok := remote[address]
if !ok {
output("Could not find contact")
return nil
}
if !confirm(fmt.Sprintf("Add and invite %s?", result.Id)) {
return nil
}
contact = new(pb.Contact)
if err := ptypes.UnmarshalAny(result.Value, contact); err != nil {
return err
}
data, err := pbMarshaler.MarshalToString(result.Value)
if err != nil {
return err
}
res, err := executeStringCmd(http.MethodPut, "contacts/"+contact.Address, params{
payload: strings.NewReader(data),
ctype: "application/json",
})
if err != nil {
return err
}
if res == "" {
output("added " + result.Id)
} else {
return fmt.Errorf("error adding %s: %s", result.Id, res)
}
}
return createInvite(threadID, address)
}
func createInvite(threadID string, address string) error {
res, err := executeJsonCmd(http.MethodPost, "invites", params{
opts: map[string]string{
"thread": threadID,
"address": address,
},
}, nil)
if err != nil {
return err
}
output(res)
return nil
}
func InviteList() error {
res, err := executeJsonCmd(http.MethodGet, "invites", params{}, nil)
if err != nil {
return err
}
output(res)
return nil
}
func InviteAccept(inviteID string, key string) error {
res, err := executeJsonCmd(http.MethodPost, "invites/"+inviteID+"/accept", params{
args: []string{inviteID},
opts: map[string]string{
"key": key,
},
}, nil)
if err != nil {
return err
}
output(res)
return nil
}
func InviteIgnore(inviteID string) error {
res, err := executeStringCmd(http.MethodPost, "invites/"+inviteID+"/ignore", params{
args: []string{inviteID},
})
if err != nil {
return err
}
output(res)
return nil
}