forked from textileio/go-textile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contacts.go
121 lines (104 loc) · 2.82 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package core
import (
"time"
"github.com/textileio/textile-go/ipfs"
"github.com/textileio/textile-go/repo"
)
// ContactInfo display info about a contact
type ContactInfo struct {
Id string `json:"id"`
Address string `json:"address"`
Username string `json:"username"`
ThreadIds []string `json:"thread_ids"`
Added time.Time `json:"added"`
}
// AddContact adds a contact for the first time
// Note: Existing contacts will not be overwritten
func (t *Textile) AddContact(id string, address string, username string) error {
return t.datastore.Contacts().Add(&repo.Contact{
Id: id,
Address: address,
Username: username,
Added: time.Now(),
})
}
// Contact looks up a contact by peer id
func (t *Textile) Contact(id string) *ContactInfo {
model := t.datastore.Contacts().Get(id)
if model == nil {
return nil
}
threads := make([]string, 0)
for _, peer := range t.datastore.ThreadPeers().ListById(id) {
threads = append(threads, peer.ThreadId)
}
return &ContactInfo{
Id: model.Id,
Address: model.Address,
Username: toUsername(model),
ThreadIds: threads,
Added: model.Added,
}
}
// Contacts returns all contacts this peer has interacted with
func (t *Textile) Contacts() ([]ContactInfo, error) {
contacts := make([]ContactInfo, 0)
for _, model := range t.datastore.Contacts().List() {
threads := make([]string, 0)
for _, peer := range t.datastore.ThreadPeers().ListById(model.Id) {
threads = append(threads, peer.ThreadId)
}
contacts = append(contacts, ContactInfo{
Id: model.Id,
Address: model.Address,
Username: toUsername(&model),
ThreadIds: threads,
Added: model.Added,
})
}
return contacts, nil
}
// ContactUsername returns the username for the peer id if known
func (t *Textile) ContactUsername(id string) string {
if id == t.node.Identity.Pretty() {
username, err := t.Username()
if err == nil && username != nil && *username != "" {
return *username
}
return ipfs.ShortenID(id)
}
contact := t.datastore.Contacts().Get(id)
if contact == nil {
return ipfs.ShortenID(id)
}
return toUsername(contact)
}
// ContactThreads returns all threads with the given peer
func (t *Textile) ContactThreads(id string) ([]ThreadInfo, error) {
peers := t.datastore.ThreadPeers().ListById(id)
if len(peers) == 0 {
return nil, nil
}
var infos []ThreadInfo
for _, peer := range peers {
info, err := t.ThreadInfo(peer.ThreadId)
if err != nil {
return nil, err
}
infos = append(infos, *info)
}
return infos, nil
}
// toUsername returns a contact's username or trimmed peer id
func toUsername(contact *repo.Contact) string {
if contact == nil || contact.Id == "" {
return ""
}
if contact.Username != "" {
return contact.Username
}
if len(contact.Id) >= 7 {
return contact.Id[len(contact.Id)-7:]
}
return ""
}