From 99964ccf695c7f5eadd2150fdea4983dddc33a86 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Mon, 1 Aug 2022 19:43:49 -0400 Subject: [PATCH] feat: add functions to retrieve message history and check if node is started (#5) --- .../com/wakureactnative/ReactNativeModule.kt | 5 ++ example/src/App.tsx | 19 +++++-- go-waku.VERSION | 2 +- ios/ReactNative.m | 10 ++++ ios/ReactNative.swift | 10 ++++ package.json | 2 +- src/index.tsx | 51 ++++++++++++++++--- 7 files changed, 87 insertions(+), 12 deletions(-) diff --git a/android/src/main/java/com/wakureactnative/ReactNativeModule.kt b/android/src/main/java/com/wakureactnative/ReactNativeModule.kt index 3ffb718..acdff73 100644 --- a/android/src/main/java/com/wakureactnative/ReactNativeModule.kt +++ b/android/src/main/java/com/wakureactnative/ReactNativeModule.kt @@ -64,6 +64,11 @@ class ReactNativeModule(reactContext: ReactApplicationContext) : ReactContextBas promise.resolve(Gowaku.stop()) } + @ReactMethod + fun isStarted(promise: Promise) { + promise.resolve(Gowaku.isStarted()) + } + @ReactMethod fun peerID(promise: Promise) { promise.resolve(Gowaku.peerID()) diff --git a/example/src/App.tsx b/example/src/App.tsx index 84996d4..15a5680 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import { StyleSheet, View, Text } from 'react-native'; -import { defaultPubsubTopic, newNode, start, stop, peerID, relayEnoughPeers, listenAddresses, connect, peerCnt, peers, relayPublish, relayUnsubscribe, relaySubscribe, WakuMessage, onMessage } from '@waku/react-native'; +import { defaultPubsubTopic, newNode, start, isStarted, stop, peerID, relayEnoughPeers, listenAddresses, connect, peerCnt, peers, relayPublish, relayUnsubscribe, relaySubscribe, WakuMessage, onMessage, storeQuery, StoreQuery } from '@waku/react-native'; export default function App() { const [result, setResult] = React.useState(); @@ -10,9 +10,12 @@ export default function App() { React.useEffect(() => { (async () => { - await newNode(null); // TODO: This must be called only once - await start(); // // TODO: This must be called only once + const nodeStarted = await isStarted(); + if (!nodeStarted) { + await newNode(null); + await start(); + } console.log("The node ID:", await peerID()) await relaySubscribe() @@ -27,7 +30,7 @@ export default function App() { await connect("/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.statusim.net/tcp/30303/p2p/16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm", 5000) - console.log("connected!") + console.log("connected!") console.log("PeerCNT", await peerCnt()) console.log("Peers", await peers()) @@ -42,6 +45,14 @@ export default function App() { console.log("The messageID", messageID) + /* + console.log("Retrieving messages from store node") + const query = new StoreQuery(); + //query.contentFilters.push("/toy-chat/2/luzhou/proto") + const queryResult = await storeQuery(query, "16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm") + console.log(queryResult) + */ + // await delay(5000) // Waiting 5s before unsubscribing // console.log("Unsubscribing and stopping node...") diff --git a/go-waku.VERSION b/go-waku.VERSION index 6c6aa7c..341cf11 100644 --- a/go-waku.VERSION +++ b/go-waku.VERSION @@ -1 +1 @@ -0.1.0 \ No newline at end of file +0.2.0 \ No newline at end of file diff --git a/ios/ReactNative.m b/ios/ReactNative.m index 6c0a1a3..ad2f03e 100644 --- a/ios/ReactNative.m +++ b/ios/ReactNative.m @@ -21,6 +21,10 @@ RCT_EXTERN_METHOD(stop: (RCTPromiseResolveBlock)resolve withRejecter:(RCTPromiseRejectBlock)reject) +RCT_EXTERN_METHOD(isStarted: + (RCTPromiseResolveBlock)resolve + withRejecter:(RCTPromiseRejectBlock)reject) + RCT_EXTERN_METHOD(peerID: (RCTPromiseResolveBlock)resolve withRejecter:(RCTPromiseRejectBlock)reject) @@ -125,4 +129,10 @@ RCT_EXTERN_METHOD(decodeAsymmetric:(NSString *)msg withResolver:(RCTPromiseResolveBlock)resolve withRejecter:(RCTPromiseRejectBlock)reject) +RCT_EXTERN_METHOD(storeQuery:(NSString *)query + withPeerID:(NSString *)peerID + withMs:(nonnull NSNumber *)ms + withResolver:(RCTPromiseResolveBlock)resolve + withRejecter:(RCTPromiseRejectBlock)reject) + @end diff --git a/ios/ReactNative.swift b/ios/ReactNative.swift index fdaaf6d..23fb534 100644 --- a/ios/ReactNative.swift +++ b/ios/ReactNative.swift @@ -62,6 +62,11 @@ class ReactNative: RCTEventEmitter { resolve(GowakuStop()) } + @objc(isStarted:withRejecter:) + func isStarted(_ resolve:RCTPromiseResolveBlock, withRejecter reject:RCTPromiseRejectBlock) -> Void { + resolve(GowakuIsStarted()) + } + @objc(peerID:withRejecter:) func peerID(_ resolve:RCTPromiseResolveBlock, withRejecter reject:RCTPromiseRejectBlock) -> Void { resolve(GowakuPeerID()) @@ -157,4 +162,9 @@ class ReactNative: RCTEventEmitter { resolve(GowakuDecodeAsymmetric(msg, privateKey)) } + @objc(storeQuery:withPeerID:withMs:withResolver:withRejecter:) + func storeQuery(queryJSON: String, peerID: String, ms: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void { + resolve(GowakuStoreQuery(queryJSON, peerID, ms)) + } + } diff --git a/package.json b/package.json index 7a93e21..b872dd9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@waku/react-native", - "version": "0.0.2", + "version": "0.0.4", "description": "Waku React Native", "author": "Status Research & Development GMBH", "authors": [ diff --git a/src/index.tsx b/src/index.tsx index b846653..52a2e70 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -93,6 +93,17 @@ export function stop(): Promise { }); } +export function isStarted(): Promise { + return new Promise(async (resolve, reject) => { + let response = JSON.parse(await ReactNative.isStarted()); + if(response.error){ + reject(response.error); + } else { + resolve(response.result); + } + }); +} + export function peerID(): Promise { return new Promise(async (resolve, reject) => { let response = JSON.parse(await ReactNative.peerID()); @@ -295,11 +306,6 @@ export function relayUnsubscribe(topic: String = ""): Promise { }); } - - - - - export function lightpushPublish(msg: WakuMessage, topic: String = "", peerID: String = "", ms: Number = 0): Promise { return new Promise(async (resolve, reject) => { let messageJSON = JSON.stringify(msg) @@ -361,4 +367,37 @@ export function peers(): Promise> { }) } -// TODO: storeQuery +export class Index { + digest: Uint8Array = new Uint8Array(); + receiverTime: Number = 0 + senderTime: Number = 0 + pubsubTopic: String = "" +} +export class PagingOptions { + pageSize: Number = 0 + cursor: Index | null = null + forward: Boolean = false +} +export class StoreQuery { + pubsubTopic: String = "" + contentFilters: Array = Array() + startTime: Number = 0 + endTime: Number = 0 + pagingOptions: PagingOptions | null = null +} + +export function storeQuery(query: StoreQuery, peerID: String = "", ms: Number = 0): Promise { + return new Promise(async (resolve, reject) => { + let queryJSON = JSON.stringify(query) + let response = JSON.parse(await ReactNative.storeQuery(queryJSON, peerID, ms)); + + console.log("STORE RESPONSE:") + console.log(response) + + if(response.error){ + reject(response.error); + } else { + resolve(response.result); + } + }); +} \ No newline at end of file