From a722e8527aa10734e1748cecec8123d1a77634f6 Mon Sep 17 00:00:00 2001 From: Szymon Szlachtowicz <38212223+Szymx95@users.noreply.github.com> Date: Fri, 8 Oct 2021 16:01:37 +0200 Subject: [PATCH] Add emit self pubsub config (#59) --- packages/react-chat/src/hooks/useMessenger.ts | 75 ++++++++----------- 1 file changed, 33 insertions(+), 42 deletions(-) diff --git a/packages/react-chat/src/hooks/useMessenger.ts b/packages/react-chat/src/hooks/useMessenger.ts index a29c9a84..92984782 100644 --- a/packages/react-chat/src/hooks/useMessenger.ts +++ b/packages/react-chat/src/hooks/useMessenger.ts @@ -62,37 +62,6 @@ export function useMessenger(chatId: string, chatIdList: string[]) { }); }, []); - const addNewMessageRaw = useCallback( - ( - signer: Uint8Array, - content: string, - date: Date, - id: string, - image?: string - ) => { - const sender = signer.reduce((p, c) => p + c.toString(16), "0x"); - const newMessage = { sender, content, date, image }; - setMessages((prev) => { - return { - ...prev, - [id]: binarySetInsert( - prev?.[id] ?? [], - newMessage, - (a, b) => a.date < b.date, - (a, b) => a.date.getTime() === b.date.getTime() - ), - }; - }); - setNotifications((prev) => { - return { - ...prev, - [id]: prev[id] + 1, - }; - }); - }, - [] - ); - const addNewMessage = useCallback( (msg: ApplicationMetadataMessage, id: string, date: Date) => { if ( @@ -101,14 +70,35 @@ export function useMessenger(chatId: string, chatIdList: string[]) { msg.chatMessage.clock ) { const content = msg.chatMessage.text ?? ""; - let img: string | undefined = undefined; + let image: string | undefined = undefined; if (msg.chatMessage?.image) { - img = uintToImgUrl(msg.chatMessage?.image.payload); + image = uintToImgUrl(msg.chatMessage?.image.payload); } - addNewMessageRaw(msg.signer, content, date, id, img); + const sender = msg.signer.reduce( + (p: string, c: number): string => p + c.toString(16), + "0x" + ); + const newMessage = { sender, content, date, image }; + setMessages((prev) => { + return { + ...prev, + [id]: binarySetInsert( + prev?.[id] ?? [], + newMessage, + (a, b) => a.date < b.date, + (a, b) => a.date.getTime() === b.date.getTime() + ), + }; + }); + setNotifications((prev) => { + return { + ...prev, + [id]: prev[id] + 1, + }; + }); } }, - [addNewMessageRaw] + [] ); const loadNextDay = useCallback( @@ -147,6 +137,14 @@ export function useMessenger(chatId: string, chatIdList: string[]) { const identity = Identity.generate(); const messenger = await Messenger.create(identity, { + libp2p: { + config: { + pubsub: { + enabled: true, + emitSelf: true, + }, + }, + }, bootstrap: getBootstrapNodes.bind({}, [ "fleets", "wakuv2.test", @@ -200,13 +198,6 @@ export function useMessenger(chatId: string, chatIdList: string[]) { contentType: 0, }; await messenger?.sendMessage(chatId, content); - addNewMessageRaw( - messenger?.identity.publicKey ?? new Uint8Array(), - messageText, - new Date(), - chatId, - image ? uintToImgUrl(image) : undefined - ); }, [chatId, messenger] );