From 5601c261e0f59001cb7bffebd40b1fee1e065b5e Mon Sep 17 00:00:00 2001 From: Haoliang Yu Date: Tue, 5 Dec 2017 12:15:28 -0500 Subject: [PATCH] fix(pubsub.peers): remove the requirement for a topic (#1125) * fix(pubsub.peers): remove the requirement for a topic * shorter code --- src/core/components/pubsub.js | 7 ++++++- src/http/api/resources/pubsub.js | 10 +++++----- test/http-api/spec/pubsub.js | 6 +++--- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/core/components/pubsub.js b/src/core/components/pubsub.js index 0639654725..9796faf479 100644 --- a/src/core/components/pubsub.js +++ b/src/core/components/pubsub.js @@ -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)) diff --git a/src/http/api/resources/pubsub.js b/src/http/api/resources/pubsub.js index a0dd21f748..065746c62e 100644 --- a/src/http/api/resources/pubsub.js +++ b/src/http/api/resources/pubsub.js @@ -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}) diff --git a/test/http-api/spec/pubsub.js b/test/http-api/spec/pubsub.js index bbb8b56e66..2248a660af 100644 --- a/test/http-api/spec/pubsub.js +++ b/test/http-api/spec/pubsub.js @@ -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() }) })