Skip to content

Commit 41ed5ff

Browse files
authored
Merge pull request #153 from status-im/disable-button
2 parents 6dbfa3e + 89b35eb commit 41ed5ff

File tree

7 files changed

+38
-17
lines changed

7 files changed

+38
-17
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
- `WakuRelay.getPeers` method.
12+
- Use `WakuRelay.getPeers` in web chat app example to disable send button.
13+
1014
### Changed
1115
- Enable passing `string`s to `addPeerToAddressBook`.
1216
- Use `addPeerToAddressBook` in examples and usage doc.
1317
- Settle on `js-waku` name across the board.
18+
- **Breaking**: `RelayDefaultTopic` renamed to `DefaultPubsubTopic`.
1419

1520
## [0.1.0] - 2021-05-12
1621

examples/web-chat/src/MessageInput.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ export default function MessageInput(props: Props) {
3434
}
3535
};
3636

37+
// Enable the button if there are relay peers available or the user is sending a command
38+
const activeButton =
39+
(waku && waku.relay.getPeers().size !== 0) || inputText.startsWith('/');
40+
3741
return (
3842
<TextComposer
3943
onKeyDown={keyPressHandler}
4044
onChange={messageHandler}
41-
active={!!waku}
45+
active={activeButton}
4246
onButtonClick={sendMessage}
4347
>
4448
<Row align="center">

src/lib/waku_relay/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const RelayCodec = '/vac/waku/relay/2.0.0-beta2';
99
/**
1010
* RelayDefaultTopic is the default gossipsub topic to use for waku relay
1111
*/
12-
export const RelayDefaultTopic = '/waku/2/default-waku/proto';
12+
export const DefaultPubsubTopic = '/waku/2/default-waku/proto';
1313

1414
/**
1515
* RelayGossipFactor affects how many peers we will emit gossip to at each heartbeat.

src/lib/waku_relay/index.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { delay } from '../delay';
1212
import { Waku } from '../waku';
1313
import { WakuMessage } from '../waku_message';
1414

15-
import { RelayCodec, RelayDefaultTopic } from './index';
15+
import { DefaultPubsubTopic, RelayCodec } from './index';
1616

1717
const log = debug('waku:test');
1818

@@ -57,8 +57,8 @@ describe('Waku Relay', () => {
5757
});
5858

5959
it('Subscribe', async function () {
60-
const subscribers1 = waku1.libp2p.pubsub.getSubscribers(RelayDefaultTopic);
61-
const subscribers2 = waku2.libp2p.pubsub.getSubscribers(RelayDefaultTopic);
60+
const subscribers1 = waku1.libp2p.pubsub.getSubscribers(DefaultPubsubTopic);
61+
const subscribers2 = waku2.libp2p.pubsub.getSubscribers(DefaultPubsubTopic);
6262

6363
expect(subscribers1).to.contain(waku2.libp2p.peerId.toB58String());
6464
expect(subscribers2).to.contain(waku1.libp2p.peerId.toB58String());
@@ -160,7 +160,7 @@ describe('Waku Relay', () => {
160160
it('nim subscribes to js', async function () {
161161
const nimPeerId = await nimWaku.getPeerId();
162162
const subscribers = waku.libp2p.pubsub.getSubscribers(
163-
RelayDefaultTopic
163+
DefaultPubsubTopic
164164
);
165165

166166
expect(subscribers).to.contain(nimPeerId.toB58String());
@@ -244,7 +244,7 @@ describe('Waku Relay', () => {
244244

245245
while (subscribers.length === 0) {
246246
await delay(200);
247-
subscribers = waku.libp2p.pubsub.getSubscribers(RelayDefaultTopic);
247+
subscribers = waku.libp2p.pubsub.getSubscribers(DefaultPubsubTopic);
248248
}
249249

250250
const nimPeerId = await nimWaku.getPeerId();

src/lib/waku_relay/index.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import PeerId from 'peer-id';
1919
import { WakuMessage } from '../waku_message';
2020

2121
import * as constants from './constants';
22-
import { RelayCodec, RelayDefaultTopic } from './constants';
22+
import { DefaultPubsubTopic, RelayCodec } from './constants';
2323
import { getRelayPeers } from './get_relay_peers';
2424
import { RelayHeartbeat } from './relay_heartbeat';
2525

26-
export { RelayCodec, RelayDefaultTopic };
26+
export { RelayCodec, DefaultPubsubTopic };
2727

2828
/**
2929
* See {GossipOptions} from libp2p-gossipsub
@@ -94,7 +94,7 @@ export class WakuRelay extends Gossipsub implements Pubsub {
9494
* @returns {void}
9595
*/
9696
public start(): void {
97-
this.on(constants.RelayDefaultTopic, (event) => {
97+
this.on(constants.DefaultPubsubTopic, (event) => {
9898
const wakuMsg = WakuMessage.decode(event.data);
9999
if (this.observers['']) {
100100
this.observers[''].forEach((callbackFn) => {
@@ -111,7 +111,7 @@ export class WakuRelay extends Gossipsub implements Pubsub {
111111
});
112112

113113
super.start();
114-
super.subscribe(constants.RelayDefaultTopic);
114+
super.subscribe(constants.DefaultPubsubTopic);
115115
}
116116

117117
/**
@@ -122,7 +122,7 @@ export class WakuRelay extends Gossipsub implements Pubsub {
122122
*/
123123
public async send(message: WakuMessage): Promise<void> {
124124
const msg = message.encode();
125-
await super.publish(constants.RelayDefaultTopic, Buffer.from(msg));
125+
await super.publish(constants.DefaultPubsubTopic, Buffer.from(msg));
126126
}
127127

128128
/**
@@ -152,6 +152,18 @@ export class WakuRelay extends Gossipsub implements Pubsub {
152152
}
153153
}
154154

155+
/**
156+
* Return the relay peers we are connected to and we would publish a message to
157+
*/
158+
getPeers(): Set<string> {
159+
return getRelayPeers(this, DefaultPubsubTopic, this._options.D, (id) => {
160+
// Filter peers we would not publish to
161+
return (
162+
this.score.score(id) >= this._options.scoreThresholds.publishThreshold
163+
);
164+
});
165+
}
166+
155167
/**
156168
* Join pubsub topic.
157169
* This is present to override the behavior of Gossipsub and should not

src/lib/waku_store/history_rpc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { v4 as uuid } from 'uuid';
33

44
import * as proto from '../../proto/waku/v2/store';
55
import { DefaultContentTopic } from '../waku_message';
6-
import { RelayDefaultTopic } from '../waku_relay';
6+
import { DefaultPubsubTopic } from '../waku_relay';
77

88
export class HistoryRPC {
99
public constructor(public proto: proto.HistoryRPC) {}
1010

1111
static createQuery(
1212
contentTopics: string[] = [DefaultContentTopic],
1313
cursor?: proto.Index,
14-
pubsubTopic: string = RelayDefaultTopic
14+
pubsubTopic: string = DefaultPubsubTopic
1515
): HistoryRPC {
1616
const pagingInfo = {
1717
pageSize: 10,

src/test_utils/nim_waku.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Multiaddr, multiaddr } from 'multiaddr';
1313
import PeerId from 'peer-id';
1414

1515
import { WakuMessage } from '../lib/waku_message';
16-
import { RelayDefaultTopic } from '../lib/waku_relay';
16+
import { DefaultPubsubTopic } from '../lib/waku_relay';
1717
import * as proto from '../proto/waku/v2/message';
1818

1919
import { existsAsync, mkdirAsync, openAsync } from './async_fs';
@@ -167,7 +167,7 @@ export class NimWaku {
167167
};
168168

169169
return this.rpcCall<boolean>('post_waku_v2_relay_v1_message', [
170-
RelayDefaultTopic,
170+
DefaultPubsubTopic,
171171
rpcMessage,
172172
]);
173173
}
@@ -176,7 +176,7 @@ export class NimWaku {
176176
this.checkProcess();
177177

178178
return this.rpcCall<proto.WakuMessage[]>('get_waku_v2_relay_v1_messages', [
179-
RelayDefaultTopic,
179+
DefaultPubsubTopic,
180180
]).then((msgs) => msgs.map((protoMsg) => new WakuMessage(protoMsg)));
181181
}
182182

0 commit comments

Comments
 (0)