Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

fix(pubsub.peers): remove the requirement for a topic #1125

Merged
merged 2 commits into from
Dec 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/core/components/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ module.exports = function pubsub (self) {
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
}

if (typeof topic === 'function') {
callback = topic
topic = null
}

const peers = Array.from(self._pubsub.peers.values())
.filter((peer) => peer.topics.has(topic))
.filter((peer) => topic ? peer.topics.has(topic) : true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why bother doing the filter if you're getting all peers anyway?

.map((peer) => peer.info.id.toB58String())

setImmediate(() => callback(null, peers))
Expand Down
10 changes: 5 additions & 5 deletions src/http/api/resources/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ exports.peers = {
const topic = request.query.arg
const ipfs = request.server.app.ipfs

if (!topic) {
return reply(new Error('Missing topic'))
}

ipfs.pubsub.peers(topic, (err, peers) => {
if (err) {
return reply(new Error(`Failed to find peers subscribed to ${topic}: ${err}`))
const message = topic
? `Failed to find peers subscribed to ${topic}: ${err}`
: `Failed to find peers: ${err}`

return reply(new Error(message))
}

reply({Strings: peers})
Expand Down
6 changes: 3 additions & 3 deletions test/http-api/spec/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ module.exports = (http) => {
})

describe('/peers', () => {
it('returns 500 if no topic is provided', (done) => {
it('returns 200 if no topic is provided', (done) => {
api.inject({
method: 'GET',
url: `/api/v0/pubsub/peers`
}, (res) => {
expect(res.statusCode).to.equal(500)
expect(res.result.Code).to.be.eql(1)
expect(res.statusCode).to.equal(200)
expect(res.result.Strings).to.be.eql([])
done()
})
})
Expand Down