This repository was archived by the owner on Mar 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathcontacts.go
104 lines (82 loc) · 2 KB
/
contacts.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
package mobile
import (
"github.com/golang/protobuf/proto"
"github.com/textileio/go-textile/core"
"github.com/textileio/go-textile/pb"
)
// AddContact calls core AddContact
func (m *Mobile) AddContact(contact []byte) error {
if !m.node.Started() {
return core.ErrStopped
}
model := new(pb.Contact)
if err := proto.Unmarshal(contact, model); err != nil {
return err
}
err := m.node.AddContact(model)
if err != nil {
return err
}
m.node.FlushCafes()
return nil
}
// Contact calls core Contact
func (m *Mobile) Contact(address string) ([]byte, error) {
if !m.node.Started() {
return nil, core.ErrStopped
}
contact := m.node.Contact(address)
if contact == nil {
return nil, nil
}
return proto.Marshal(contact)
}
// Contacts calls core Contacts
func (m *Mobile) Contacts() ([]byte, error) {
if !m.node.Started() {
return nil, core.ErrStopped
}
return proto.Marshal(m.node.Contacts())
}
// RemoveContact calls core RemoveContact
func (m *Mobile) RemoveContact(address string) error {
if !m.node.Started() {
return core.ErrStopped
}
err := m.node.RemoveContact(address)
if err != nil {
return err
}
m.node.FlushCafes()
return nil
}
// ContactThreads calls core ContactThreads
func (m *Mobile) ContactThreads(address string) ([]byte, error) {
if !m.node.Started() {
return nil, core.ErrStopped
}
thrds, err := m.node.ContactThreads(address)
if err != nil {
return nil, err
}
return proto.Marshal(thrds)
}
// SearchContacts calls core SearchContacts
func (m *Mobile) SearchContacts(query []byte, options []byte) (*SearchHandle, error) {
if !m.node.Online() {
return nil, core.ErrOffline
}
mquery := new(pb.ContactQuery)
if err := proto.Unmarshal(query, mquery); err != nil {
return nil, err
}
moptions := new(pb.QueryOptions)
if err := proto.Unmarshal(options, moptions); err != nil {
return nil, err
}
resCh, errCh, cancel, err := m.node.SearchContacts(mquery, moptions)
if err != nil {
return nil, err
}
return m.handleSearchStream(resCh, errCh, cancel)
}