diff --git a/packages/status-communities/src/chat.ts b/packages/status-communities/src/chat.ts index 54ee5da2..8727fe32 100644 --- a/packages/status-communities/src/chat.ts +++ b/packages/status-communities/src/chat.ts @@ -1,6 +1,7 @@ import { idToContentTopic } from "./contentTopic"; import { createSymKeyFromPassword } from "./encryption"; import { ChatMessage, Content } from "./wire/chat_message"; +import { CommunityChat } from "./wire/community_chat"; /** * Represent a chat room. Only public chats are currently supported. @@ -9,16 +10,23 @@ export class Chat { private lastClockValue?: number; private lastMessage?: ChatMessage; - private constructor(public id: string, public symKey: Uint8Array) {} + private constructor( + public id: string, + public symKey: Uint8Array, + public communityChat?: CommunityChat + ) {} /** * Create a public chat room. * [[Community.instantiateChat]] MUST be used for chats belonging to a community. */ - public static async create(id: string): Promise { + public static async create( + id: string, + communityChat?: CommunityChat + ): Promise { const symKey = await createSymKeyFromPassword(id); - return new Chat(id, symKey); + return new Chat(id, symKey, communityChat); } public get contentTopic(): string { diff --git a/packages/status-communities/src/community.spec.ts b/packages/status-communities/src/community.spec.ts index 770e77a8..bd3490d5 100644 --- a/packages/status-communities/src/community.spec.ts +++ b/packages/status-communities/src/community.spec.ts @@ -26,9 +26,16 @@ describe("Community live data", () => { expect(desc).to.not.be.undefined; expect(desc.identity?.displayName).to.eq("DappConnect Test"); - const chats = Array.from(desc.chats.values()).map( + + const descChats = Array.from(desc.chats.values()).map( (chat) => chat?.identity?.displayName ); + expect(descChats).to.include("foobar"); + expect(descChats).to.include("another-channel!"); + + const chats = Array.from(community.chats.values()).map( + (chat) => chat?.communityChat?.identity?.displayName + ); expect(chats).to.include("foobar"); expect(chats).to.include("another-channel!"); }); diff --git a/packages/status-communities/src/community.ts b/packages/status-communities/src/community.ts index 4ffac4f6..424bd8ac 100644 --- a/packages/status-communities/src/community.ts +++ b/packages/status-communities/src/community.ts @@ -11,12 +11,13 @@ const dbg = debug("communities:community"); export class Community { public publicKey: Uint8Array; private waku: Waku; - + public chats: Map; // Chat id, Chat public description?: CommunityDescription; constructor(publicKey: Uint8Array, waku: Waku) { this.publicKey = publicKey; this.waku = waku; + this.chats = new Map(); } /** @@ -60,6 +61,12 @@ export class Community { } this.description = desc; + + await Promise.all( + Array.from(this.description.chats).map(([chatUuid, communityChat]) => { + return this.instantiateChat(chatUuid, communityChat); + }) + ); } /** @@ -68,28 +75,18 @@ export class Community { * * @throws string If the Community Description is unavailable or the chat is not found; */ - public async instantiateChat(chatName: string): Promise { - if (!this.description) { - await this.refreshCommunityDescription(); - if (!this.description) - throw "Failed to retrieve community description, cannot instantiate chat"; - } + private async instantiateChat( + chatUuid: string, + communityChat: CommunityChat + ): Promise { + if (!this.description) + throw "Failed to retrieve community description, cannot instantiate chat"; - let communityChat: CommunityChat | undefined; - let chatUuid: string | undefined; + const chatId = this.publicKeyStr + chatUuid; + if (this.chats.get(chatId)) return; - this.description.chats.forEach((_chat, _id) => { - if (chatUuid) return; + const chat = await Chat.create(chatId, communityChat); - if (_chat.identity?.displayName === chatName) { - chatUuid = _id; - communityChat = _chat; - } - }); - - if (!communityChat || !chatUuid) - throw `Failed to retrieve community community chat with name ${chatName}`; - - return Chat.create(this.publicKeyStr + chatUuid); + this.chats.set(chatId, chat); } }