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

Commit

Permalink
fix(pubsub.peers): remove the requirement for a topic (#1125)
Browse files Browse the repository at this point in the history
* fix(pubsub.peers): remove the requirement for a topic

* shorter code
  • Loading branch information
haoliangyu authored and daviddias committed Dec 5, 2017
1 parent 5d7e1f2 commit 5601c26
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
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)
.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

0 comments on commit 5601c26

Please sign in to comment.