diff --git a/packages/react-chat-example/.eslintrc.json b/packages/react-chat-example/.eslintrc.json index 0e42b7d9..b8a87ffa 100644 --- a/packages/react-chat-example/.eslintrc.json +++ b/packages/react-chat-example/.eslintrc.json @@ -14,7 +14,6 @@ ], "globals": { "BigInt": true, "console": true, "WebAssembly": true }, "rules": { - "@typescript-eslint/explicit-function-return-type": ["warn"], "@typescript-eslint/explicit-module-boundary-types": "off", "eslint-comments/disable-enable-pair": [ "error", diff --git a/packages/react-chat/.eslintrc.json b/packages/react-chat/.eslintrc.json index 0e42b7d9..b8a87ffa 100644 --- a/packages/react-chat/.eslintrc.json +++ b/packages/react-chat/.eslintrc.json @@ -14,7 +14,6 @@ ], "globals": { "BigInt": true, "console": true, "WebAssembly": true }, "rules": { - "@typescript-eslint/explicit-function-return-type": ["warn"], "@typescript-eslint/explicit-module-boundary-types": "off", "eslint-comments/disable-enable-pair": [ "error", diff --git a/packages/react-chat/src/hooks/useMessenger.ts b/packages/react-chat/src/hooks/useMessenger.ts index f68d8464..71fc93c9 100644 --- a/packages/react-chat/src/hooks/useMessenger.ts +++ b/packages/react-chat/src/hooks/useMessenger.ts @@ -1,6 +1,12 @@ // import { StoreCodec } from "js-waku"; +import { getBootstrapNodes, StoreCodec } from "js-waku"; import { useCallback, useEffect, useState } from "react"; -import { Identity, Messenger } from "status-communities/dist/cjs"; +import { + ChatMessage as ApiChatMessage, + ApplicationMetadataMessage, + Identity, + Messenger, +} from "status-communities/dist/cjs"; import { ChatMessage } from "../models/ChatMessage"; @@ -46,14 +52,12 @@ export function useMessenger(chatId: string, chatIdList: string[]) { }; } }); - if (chatId != id) { - setNotifications((prevNotifications) => { - return { - ...prevNotifications, - [id]: prevNotifications[id] + 1, - }; - }); - } + setNotifications((prevNotifications) => { + return { + ...prevNotifications, + [id]: prevNotifications[id] + 1, + }; + }); }, [] ); @@ -61,15 +65,63 @@ export function useMessenger(chatId: string, chatIdList: string[]) { useEffect(() => { const createMessenger = async () => { const identity = Identity.generate(); - const messenger = await Messenger.create(identity); - await new Promise((resolve) => - messenger.waku.libp2p.pubsub.once("pubsub:subscription-change", () => - resolve(null) - ) - ); + + const messenger = await Messenger.create(identity, { + bootstrap: getBootstrapNodes.bind({}, [ + "fleets", + "wakuv2.prod", + "waku-websocket", + ]), + }); + await new Promise((resolve) => { + messenger.waku.libp2p.peerStore.on( + "change:protocols", + ({ protocols }) => { + if (protocols.includes(StoreCodec)) { + resolve(""); + } + } + ); + }); + await Promise.all( chatIdList.map(async (id) => { await messenger.joinChat(id); + const chat = messenger.chatsById.get(id); + if (chat) { + const messages = await messenger.waku.store.queryHistory( + [chat.contentTopic], + { decryptionKeys: [chat.symKey] } + ); + messages.sort((a, b) => + (a?.timestamp?.getTime() ?? 0) > (b?.timestamp?.getTime() ?? 0) + ? 1 + : -1 + ); + messages.forEach((message) => { + if (message.payload) { + const metadata = ApplicationMetadataMessage.decode( + message.payload + ); + if (metadata.payload) { + const chatMessage = ApiChatMessage.decode(metadata.payload); + if ( + metadata.signer && + chatMessage.text && + chatMessage.proto.timestamp + ) { + addNewMessage( + metadata.signer, + chatMessage.text, + new Date(chatMessage.proto.timestamp ?? 0), + id + ); + } + } + } + return undefined; + }); + } clearNotifications(id); messenger.addObserver((message) => { addNewMessage( diff --git a/packages/status-communities/src/index.ts b/packages/status-communities/src/index.ts index 9607dd41..7e9747af 100644 --- a/packages/status-communities/src/index.ts +++ b/packages/status-communities/src/index.ts @@ -1,4 +1,5 @@ +import { ApplicationMetadataMessage } from "./application_metadata_message"; +import { ChatMessage } from "./chat_message"; import { Identity } from "./identity"; import { Messenger } from "./messenger"; - -export { Messenger, Identity }; +export { Messenger, Identity, ApplicationMetadataMessage, ChatMessage };