From 964f322ee25e0ea79eef158233ff0272ba270ae6 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 8 Oct 2021 14:54:59 +1100 Subject: [PATCH] Add interface to join chat using `Chat` --- packages/react-chat/src/hooks/useMessenger.ts | 2 +- .../status-communities/src/messenger.spec.ts | 8 +++---- packages/status-communities/src/messenger.ts | 23 ++++++++++++++----- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/packages/react-chat/src/hooks/useMessenger.ts b/packages/react-chat/src/hooks/useMessenger.ts index c8dc47a4..6d069c9c 100644 --- a/packages/react-chat/src/hooks/useMessenger.ts +++ b/packages/react-chat/src/hooks/useMessenger.ts @@ -149,7 +149,7 @@ export function useMessenger(chatId: string, chatIdList: string[]) { await Promise.all( chatIdList.map(async (id) => { - await messenger.joinChat(id); + await messenger.joinChatById(id); setLastLoadTime((prev) => { return { ...prev, diff --git a/packages/status-communities/src/messenger.spec.ts b/packages/status-communities/src/messenger.spec.ts index 1cf734d2..2d15495a 100644 --- a/packages/status-communities/src/messenger.spec.ts +++ b/packages/status-communities/src/messenger.spec.ts @@ -62,8 +62,8 @@ describe("Messenger", () => { it("Sends & Receive public chat messages", async function () { this.timeout(10_000); - await messengerAlice.joinChat(testChatId); - await messengerBob.joinChat(testChatId); + await messengerAlice.joinChatById(testChatId); + await messengerBob.joinChatById(testChatId); const text = "This is a message."; @@ -87,8 +87,8 @@ describe("Messenger", () => { it("public chat messages have signers", async function () { this.timeout(10_000); - await messengerAlice.joinChat(testChatId); - await messengerBob.joinChat(testChatId); + await messengerAlice.joinChatById(testChatId); + await messengerBob.joinChatById(testChatId); const text = "This is a message."; diff --git a/packages/status-communities/src/messenger.ts b/packages/status-communities/src/messenger.ts index cab1dd78..375c49cd 100644 --- a/packages/status-communities/src/messenger.ts +++ b/packages/status-communities/src/messenger.ts @@ -36,16 +36,27 @@ export class Messenger { return new Messenger(identity, waku); } + /** + * Joins a public chat using its id. + * + * For community chats, prefer [[joinChat]]. + * + * Use `addListener` to get messages received on this chat. + */ + public async joinChatById(chatId: string): Promise { + const chat = await Chat.create(chatId); + + await this.joinChat(chat); + } + /** * Joins a public chat. * * Use `addListener` to get messages received on this chat. */ - public async joinChat(chatId: string): Promise { - if (this.chatsById.has(chatId)) - throw `Failed to join chat, it is already joined: ${chatId}`; - - const chat = await Chat.create(chatId); + public async joinChat(chat: Chat): Promise { + if (this.chatsById.has(chat.id)) + throw `Failed to join chat, it is already joined: ${chat.id}`; this.waku.addDecryptionKey(chat.symKey); @@ -66,7 +77,7 @@ export class Messenger { [chat.contentTopic] ); - this.chatsById.set(chatId, chat); + this.chatsById.set(chat.id, chat); } /**