Merge pull request #153 from status-im/disable-button

This commit is contained in:
Franck Royer 2021-05-14 09:30:47 +10:00 committed by GitHub
commit 41ed5ffe66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 17 deletions

View File

@ -7,10 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Added
- `WakuRelay.getPeers` method.
- Use `WakuRelay.getPeers` in web chat app example to disable send button.
### Changed ### Changed
- Enable passing `string`s to `addPeerToAddressBook`. - Enable passing `string`s to `addPeerToAddressBook`.
- Use `addPeerToAddressBook` in examples and usage doc. - Use `addPeerToAddressBook` in examples and usage doc.
- Settle on `js-waku` name across the board. - Settle on `js-waku` name across the board.
- **Breaking**: `RelayDefaultTopic` renamed to `DefaultPubsubTopic`.
## [0.1.0] - 2021-05-12 ## [0.1.0] - 2021-05-12

View File

@ -34,11 +34,15 @@ export default function MessageInput(props: Props) {
} }
}; };
// Enable the button if there are relay peers available or the user is sending a command
const activeButton =
(waku && waku.relay.getPeers().size !== 0) || inputText.startsWith('/');
return ( return (
<TextComposer <TextComposer
onKeyDown={keyPressHandler} onKeyDown={keyPressHandler}
onChange={messageHandler} onChange={messageHandler}
active={!!waku} active={activeButton}
onButtonClick={sendMessage} onButtonClick={sendMessage}
> >
<Row align="center"> <Row align="center">

View File

@ -9,7 +9,7 @@ export const RelayCodec = '/vac/waku/relay/2.0.0-beta2';
/** /**
* RelayDefaultTopic is the default gossipsub topic to use for waku relay * RelayDefaultTopic is the default gossipsub topic to use for waku relay
*/ */
export const RelayDefaultTopic = '/waku/2/default-waku/proto'; export const DefaultPubsubTopic = '/waku/2/default-waku/proto';
/** /**
* RelayGossipFactor affects how many peers we will emit gossip to at each heartbeat. * RelayGossipFactor affects how many peers we will emit gossip to at each heartbeat.

View File

@ -12,7 +12,7 @@ import { delay } from '../delay';
import { Waku } from '../waku'; import { Waku } from '../waku';
import { WakuMessage } from '../waku_message'; import { WakuMessage } from '../waku_message';
import { RelayCodec, RelayDefaultTopic } from './index'; import { DefaultPubsubTopic, RelayCodec } from './index';
const log = debug('waku:test'); const log = debug('waku:test');
@ -57,8 +57,8 @@ describe('Waku Relay', () => {
}); });
it('Subscribe', async function () { it('Subscribe', async function () {
const subscribers1 = waku1.libp2p.pubsub.getSubscribers(RelayDefaultTopic); const subscribers1 = waku1.libp2p.pubsub.getSubscribers(DefaultPubsubTopic);
const subscribers2 = waku2.libp2p.pubsub.getSubscribers(RelayDefaultTopic); const subscribers2 = waku2.libp2p.pubsub.getSubscribers(DefaultPubsubTopic);
expect(subscribers1).to.contain(waku2.libp2p.peerId.toB58String()); expect(subscribers1).to.contain(waku2.libp2p.peerId.toB58String());
expect(subscribers2).to.contain(waku1.libp2p.peerId.toB58String()); expect(subscribers2).to.contain(waku1.libp2p.peerId.toB58String());
@ -160,7 +160,7 @@ describe('Waku Relay', () => {
it('nim subscribes to js', async function () { it('nim subscribes to js', async function () {
const nimPeerId = await nimWaku.getPeerId(); const nimPeerId = await nimWaku.getPeerId();
const subscribers = waku.libp2p.pubsub.getSubscribers( const subscribers = waku.libp2p.pubsub.getSubscribers(
RelayDefaultTopic DefaultPubsubTopic
); );
expect(subscribers).to.contain(nimPeerId.toB58String()); expect(subscribers).to.contain(nimPeerId.toB58String());
@ -244,7 +244,7 @@ describe('Waku Relay', () => {
while (subscribers.length === 0) { while (subscribers.length === 0) {
await delay(200); await delay(200);
subscribers = waku.libp2p.pubsub.getSubscribers(RelayDefaultTopic); subscribers = waku.libp2p.pubsub.getSubscribers(DefaultPubsubTopic);
} }
const nimPeerId = await nimWaku.getPeerId(); const nimPeerId = await nimWaku.getPeerId();

View File

@ -19,11 +19,11 @@ import PeerId from 'peer-id';
import { WakuMessage } from '../waku_message'; import { WakuMessage } from '../waku_message';
import * as constants from './constants'; import * as constants from './constants';
import { RelayCodec, RelayDefaultTopic } from './constants'; import { DefaultPubsubTopic, RelayCodec } from './constants';
import { getRelayPeers } from './get_relay_peers'; import { getRelayPeers } from './get_relay_peers';
import { RelayHeartbeat } from './relay_heartbeat'; import { RelayHeartbeat } from './relay_heartbeat';
export { RelayCodec, RelayDefaultTopic }; export { RelayCodec, DefaultPubsubTopic };
/** /**
* See {GossipOptions} from libp2p-gossipsub * See {GossipOptions} from libp2p-gossipsub
@ -94,7 +94,7 @@ export class WakuRelay extends Gossipsub implements Pubsub {
* @returns {void} * @returns {void}
*/ */
public start(): void { public start(): void {
this.on(constants.RelayDefaultTopic, (event) => { this.on(constants.DefaultPubsubTopic, (event) => {
const wakuMsg = WakuMessage.decode(event.data); const wakuMsg = WakuMessage.decode(event.data);
if (this.observers['']) { if (this.observers['']) {
this.observers[''].forEach((callbackFn) => { this.observers[''].forEach((callbackFn) => {
@ -111,7 +111,7 @@ export class WakuRelay extends Gossipsub implements Pubsub {
}); });
super.start(); super.start();
super.subscribe(constants.RelayDefaultTopic); super.subscribe(constants.DefaultPubsubTopic);
} }
/** /**
@ -122,7 +122,7 @@ export class WakuRelay extends Gossipsub implements Pubsub {
*/ */
public async send(message: WakuMessage): Promise<void> { public async send(message: WakuMessage): Promise<void> {
const msg = message.encode(); const msg = message.encode();
await super.publish(constants.RelayDefaultTopic, Buffer.from(msg)); await super.publish(constants.DefaultPubsubTopic, Buffer.from(msg));
} }
/** /**
@ -152,6 +152,18 @@ export class WakuRelay extends Gossipsub implements Pubsub {
} }
} }
/**
* Return the relay peers we are connected to and we would publish a message to
*/
getPeers(): Set<string> {
return getRelayPeers(this, DefaultPubsubTopic, this._options.D, (id) => {
// Filter peers we would not publish to
return (
this.score.score(id) >= this._options.scoreThresholds.publishThreshold
);
});
}
/** /**
* Join pubsub topic. * Join pubsub topic.
* This is present to override the behavior of Gossipsub and should not * This is present to override the behavior of Gossipsub and should not

View File

@ -3,7 +3,7 @@ import { v4 as uuid } from 'uuid';
import * as proto from '../../proto/waku/v2/store'; import * as proto from '../../proto/waku/v2/store';
import { DefaultContentTopic } from '../waku_message'; import { DefaultContentTopic } from '../waku_message';
import { RelayDefaultTopic } from '../waku_relay'; import { DefaultPubsubTopic } from '../waku_relay';
export class HistoryRPC { export class HistoryRPC {
public constructor(public proto: proto.HistoryRPC) {} public constructor(public proto: proto.HistoryRPC) {}
@ -11,7 +11,7 @@ export class HistoryRPC {
static createQuery( static createQuery(
contentTopics: string[] = [DefaultContentTopic], contentTopics: string[] = [DefaultContentTopic],
cursor?: proto.Index, cursor?: proto.Index,
pubsubTopic: string = RelayDefaultTopic pubsubTopic: string = DefaultPubsubTopic
): HistoryRPC { ): HistoryRPC {
const pagingInfo = { const pagingInfo = {
pageSize: 10, pageSize: 10,

View File

@ -13,7 +13,7 @@ import { Multiaddr, multiaddr } from 'multiaddr';
import PeerId from 'peer-id'; import PeerId from 'peer-id';
import { WakuMessage } from '../lib/waku_message'; import { WakuMessage } from '../lib/waku_message';
import { RelayDefaultTopic } from '../lib/waku_relay'; import { DefaultPubsubTopic } from '../lib/waku_relay';
import * as proto from '../proto/waku/v2/message'; import * as proto from '../proto/waku/v2/message';
import { existsAsync, mkdirAsync, openAsync } from './async_fs'; import { existsAsync, mkdirAsync, openAsync } from './async_fs';
@ -167,7 +167,7 @@ export class NimWaku {
}; };
return this.rpcCall<boolean>('post_waku_v2_relay_v1_message', [ return this.rpcCall<boolean>('post_waku_v2_relay_v1_message', [
RelayDefaultTopic, DefaultPubsubTopic,
rpcMessage, rpcMessage,
]); ]);
} }
@ -176,7 +176,7 @@ export class NimWaku {
this.checkProcess(); this.checkProcess();
return this.rpcCall<proto.WakuMessage[]>('get_waku_v2_relay_v1_messages', [ return this.rpcCall<proto.WakuMessage[]>('get_waku_v2_relay_v1_messages', [
RelayDefaultTopic, DefaultPubsubTopic,
]).then((msgs) => msgs.map((protoMsg) => new WakuMessage(protoMsg))); ]).then((msgs) => msgs.map((protoMsg) => new WakuMessage(protoMsg)));
} }