Skip to content

Commit 4542056

Browse files
committed
feat(online): Set Online flag for peers when using lib.Info. Cleanups.
When getting Info for a peer, set the Online flag appropriately, to match the behavior of lib.List (peers.go). Do this by checking the other connected peers that are known to be online, and also checking if you yourself are online. Fixes part 2 of #577.
1 parent 107c0dc commit 4542056

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed

actions/peers.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/qri-io/qri/config"
77
"github.com/qri-io/qri/p2p"
8+
"github.com/qri-io/qri/repo/profile"
89
)
910

1011
// ListPeers lists Peers on the qri network
@@ -15,14 +16,17 @@ func ListPeers(node *p2p.QriNode, limit, offset int, onlineOnly bool) ([]*config
1516
return nil, err
1617
}
1718

18-
peers := make([]*config.ProfilePod, limit)
19-
online := []*config.ProfilePod{}
20-
if online, err = ConnectedQriProfiles(node, limit); err != nil {
19+
peers := make([]*config.ProfilePod, 0, limit)
20+
connected, err := ConnectedQriProfiles(node)
21+
if err != nil {
2122
return nil, err
2223
}
2324

2425
if onlineOnly {
25-
return online, nil
26+
for _, p := range connected {
27+
peers = append(peers, p)
28+
}
29+
return peers, nil
2630
}
2731

2832
ps, err := r.Profiles().List()
@@ -34,38 +38,29 @@ func ListPeers(node *p2p.QriNode, limit, offset int, onlineOnly bool) ([]*config
3438
return []*config.ProfilePod{}, nil
3539
}
3640

37-
i := 0
3841
for _, pro := range ps {
39-
if i >= limit {
42+
if len(peers) >= limit {
4043
break
4144
}
4245
if pro == nil || pro.ID == user.ID {
4346
continue
4447
}
4548

46-
// TODO - this is dumb use a map
47-
for _, olp := range online {
48-
if pro.ID.String() == olp.ID {
49-
pro.Online = true
50-
}
49+
if _, ok := connected[pro.ID]; ok {
50+
pro.Online = true
5151
}
5252

53-
peers[i], err = pro.Encode()
53+
p, err := pro.Encode()
5454
if err != nil {
5555
return nil, err
5656
}
57-
58-
i++
57+
peers = append(peers, p)
5958
}
6059

6160
return peers, nil
6261
}
6362

64-
// ConnectedQriProfiles gives any currently connected qri profiles to this node
65-
func ConnectedQriProfiles(node *p2p.QriNode, limit int) ([]*config.ProfilePod, error) {
66-
parsed := []*config.ProfilePod{}
67-
for _, p := range node.ConnectedQriProfiles() {
68-
parsed = append(parsed, p)
69-
}
70-
return parsed, nil
63+
// ConnectedQriProfiles returns a map from ProfileIDs to profiles for each connected node
64+
func ConnectedQriProfiles(node *p2p.QriNode) (map[profile.ID]*config.ProfilePod, error) {
65+
return node.ConnectedQriProfiles(), nil
7166
}

actions/peers_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ func TestListPeers(t *testing.T) {
3838
func TestConnectedQriProfiles(t *testing.T) {
3939
node := newTestNode(t)
4040

41-
_, err := ConnectedQriProfiles(node, 100)
41+
peers, err := ConnectedQriProfiles(node)
4242
if err != nil {
4343
t.Error(err.Error())
4444
}
45+
46+
if len(peers) != 0 {
47+
t.Errorf("expected 0 connected peers, got %d", len(peers))
48+
}
49+
50+
// TODO: Test for node with at least 1 connected peer
4551
}

lib/peers.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,20 @@ func (d *PeerRequests) ConnectedQriProfiles(limit *int, peers *[]*config.Profile
7777
return d.cli.Call("PeerRequests.ConnectedQriProfiles", limit, peers)
7878
}
7979

80-
*peers, err = actions.ConnectedQriProfiles(d.qriNode, *limit)
81-
return err
80+
connected, err := actions.ConnectedQriProfiles(d.qriNode)
81+
if err != nil {
82+
return err
83+
}
84+
85+
build := make([]*config.ProfilePod, intMin(len(connected), *limit))
86+
for _, p := range connected {
87+
build = append(build, p)
88+
if len(build) >= *limit {
89+
break
90+
}
91+
}
92+
*peers = build
93+
return nil
8294
}
8395

8496
// PeerConnectionParamsPod defines parameters for defining a connection
@@ -197,6 +209,7 @@ func (d *PeerRequests) Info(p *PeerInfoParams, res *config.ProfilePod) error {
197209
return d.cli.Call("PeerRequests.Info", p, res)
198210
}
199211

212+
// TODO: Move most / all of this to actions package, perhaps.
200213
r := d.qriNode.Repo
201214

202215
profiles, err := r.Profiles().List()
@@ -215,7 +228,21 @@ func (d *PeerRequests) Info(p *PeerInfoParams, res *config.ProfilePod) error {
215228

216229
prof, err := pro.Encode()
217230
*res = *prof
218-
return err
231+
232+
connected, err := actions.ConnectedQriProfiles(d.qriNode)
233+
if err != nil {
234+
return err
235+
}
236+
// If the requested profileID is in the list of connected peers, set Online flag.
237+
if _, ok := connected[pro.ID]; ok {
238+
res.Online = true
239+
}
240+
// If the requested profileID is myself and I'm Online, set Online flag.
241+
if peer.ID(pro.ID) == d.qriNode.ID && d.qriNode.Online {
242+
res.Online = true
243+
}
244+
return nil
245+
219246
}
220247
}
221248

@@ -253,3 +280,10 @@ func (d *PeerRequests) GetReferences(p *PeerRefsParams, res *[]repo.DatasetRef)
253280
*res = refs
254281
return err
255282
}
283+
284+
func intMin(a, b int) int {
285+
if a < b {
286+
return a
287+
}
288+
return b
289+
}

0 commit comments

Comments
 (0)