Move `contentTopics` out the `WakuStore.queryHistory`'s optional params

This commit is contained in:
Franck Royer 2021-08-04 11:59:53 +10:00
parent 55a36f2263
commit b422c9a10b
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
6 changed files with 36 additions and 40 deletions

View File

@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- **Breaking**: The `WakuMessage` APIs have been changed to move `contentTopic` out of the optional parameters.
- **Breaking**: Move `contentTopics` out the `WakuStore.queryHistory`'s optional parameters.
### Removed
- Examples (web-chat): Remove broken `/fleet` command.

View File

@ -118,20 +118,19 @@ Query a waku store peer to check historical messages:
```ts
// Process messages once they are all retrieved
const messages = await waku.store.queryHistory({ contentTopics: ["/my-cool-app/1/my-use-case/proto"] });
const messages = await waku.store.queryHistory(['/my-cool-app/1/my-use-case/proto']);
messages.forEach((msg) => {
console.log("Message retrieved:", msg.payloadAsUtf8)
})
console.log('Message retrieved:', msg.payloadAsUtf8);
});
// Or, pass a callback function to be executed as pages are received:
waku.store.queryHistory({
contentTopics: ["/my-cool-app/1/my-use-case/proto"],
callback: (messages) => {
messages.forEach((msg) => {
console.log("Message retrieved:", msg.payloadAsUtf8);
});
}
});
waku.store.queryHistory(['/my-cool-app/1/my-use-case/proto'], {
callback: (messages) => {
messages.forEach((msg) => {
console.log('Message retrieved:', msg.payloadAsUtf8);
});
}
});
```
### Encryption & Signature
@ -212,9 +211,8 @@ Keys can be removed using `WakuMessage.deleteDecryptionKey`.
##### Waku Store
```ts
const messages = await waku.store.queryHistory({
contentTopics: [],
decryptionKeys: [privateKey, symKey],
const messages = await waku.store.queryHistory([], {
decryptionKeys: [privateKey, symKey]
});
```
@ -228,20 +226,20 @@ In the case where your app does not need encryption then you could use symmetric
Signature keys can be generated the same way asymmetric keys for encryption are:
```ts
import { generatePrivateKey, getPublicKey, WakuMessage } from "js-waku";
import { generatePrivateKey, getPublicKey, WakuMessage } from 'js-waku';
const signPrivateKey = generatePrivateKey();
// Asymmetric Encryption
const message1 = await WakuMessage.fromBytes(payload, myAppContentTopic, {
encPublicKey: recipientPublicKey,
sigPrivKey: signPrivateKey,
sigPrivKey: signPrivateKey
});
// Symmetric Encryption
const message2 = await WakuMessage.fromBytes(payload, myAppContentTopic, {
encPublicKey: symKey,
sigPrivKey: signPrivateKey,
sigPrivKey: signPrivateKey
});
```

View File

@ -62,8 +62,7 @@ async function retrieveStoreMessages(
setArchivedMessages(messages);
};
const res = await waku.store.queryHistory({
contentTopics: [ChatContentTopic],
const res = await waku.store.queryHistory([ChatContentTopic], {
pageSize: 5,
direction: Direction.FORWARD,
callback,

View File

@ -8,7 +8,7 @@ export enum Direction {
FORWARD = 'forward',
}
export interface Options {
export interface Params {
contentTopics: string[];
cursor?: proto.Index;
pubsubTopic: string;
@ -22,22 +22,22 @@ export class HistoryRPC {
/**
* Create History Query.
*/
static createQuery(options: Options): HistoryRPC {
const direction = directionToProto(options.direction);
static createQuery(params: Params): HistoryRPC {
const direction = directionToProto(params.direction);
const pagingInfo = {
pageSize: options.pageSize,
cursor: options.cursor,
pageSize: params.pageSize,
cursor: params.cursor,
direction,
};
const contentFilters = options.contentTopics.map((contentTopic) => {
const contentFilters = params.contentTopics.map((contentTopic) => {
return { contentTopic };
});
return new HistoryRPC({
requestId: uuid(),
query: {
pubsubTopic: options.pubsubTopic,
pubsubTopic: params.pubsubTopic,
contentFilters,
pagingInfo,
startTime: undefined,

View File

@ -55,9 +55,7 @@ describe('Waku Store', () => {
waku.libp2p.peerStore.once('change:protocols', resolve);
});
const messages = await waku.store.queryHistory({
contentTopics: [],
});
const messages = await waku.store.queryHistory([]);
expect(messages?.length).eq(2);
const result = messages?.findIndex((msg) => {
@ -91,8 +89,7 @@ describe('Waku Store', () => {
waku.libp2p.peerStore.once('change:protocols', resolve);
});
const messages = await waku.store.queryHistory({
contentTopics: [],
const messages = await waku.store.queryHistory([], {
direction: Direction.FORWARD,
});
@ -136,9 +133,8 @@ describe('Waku Store', () => {
const nimPeerId = await nimWaku.getPeerId();
const messages = await waku.store.queryHistory({
const messages = await waku.store.queryHistory([], {
peerId: nimPeerId,
contentTopics: [],
});
expect(messages?.length).eq(2);
@ -237,8 +233,7 @@ describe('Waku Store', () => {
}
dbg('Retrieve messages from store');
const messages = await waku2.store.queryHistory({
contentTopics: [],
const messages = await waku2.store.queryHistory([], {
decryptionKeys: [privateKey, symKey],
});

View File

@ -33,7 +33,6 @@ export interface CreateOptions {
export interface QueryOptions {
peerId?: PeerId;
contentTopics: string[];
pubsubTopic?: string;
direction?: Direction;
pageSize?: number;
@ -58,10 +57,10 @@ export class WakuStore {
/**
* Query given peer using Waku Store.
*
* @param contentTopics The content topics to pass to the query, leave empty to
* retrieve all messages.
* @param options
* @param options.peerId The peer to query.Options
* @param options.contentTopics The content topics to pass to the query, leave empty to
* retrieve all messages.
* @param options.pubsubTopic The pubsub topic to pass to the query. Defaults
* to the value set at creation. See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/).
* @param options.callback Callback called on page of stored messages as they are retrieved
@ -70,14 +69,18 @@ export class WakuStore {
* methods.
* @throws If not able to reach the peer to query.
*/
async queryHistory(options: QueryOptions): Promise<WakuMessage[] | null> {
async queryHistory(
contentTopics: string[],
options?: QueryOptions
): Promise<WakuMessage[] | null> {
const opts = Object.assign(
{
pubsubTopic: this.pubsubTopic,
direction: Direction.BACKWARD,
pageSize: 10,
},
options
options,
{ contentTopics }
);
dbg('Querying history with the following options', options);