Instantiate all Chats when importing community

This commit is contained in:
Franck Royer 2021-10-19 14:17:51 +11:00
parent 647eaac722
commit a3ade13a6f
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 37 additions and 25 deletions

View File

@ -1,6 +1,7 @@
import { idToContentTopic } from "./contentTopic"; import { idToContentTopic } from "./contentTopic";
import { createSymKeyFromPassword } from "./encryption"; import { createSymKeyFromPassword } from "./encryption";
import { ChatMessage, Content } from "./wire/chat_message"; import { ChatMessage, Content } from "./wire/chat_message";
import { CommunityChat } from "./wire/community_chat";
/** /**
* Represent a chat room. Only public chats are currently supported. * Represent a chat room. Only public chats are currently supported.
@ -9,16 +10,23 @@ export class Chat {
private lastClockValue?: number; private lastClockValue?: number;
private lastMessage?: ChatMessage; 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. * Create a public chat room.
* [[Community.instantiateChat]] MUST be used for chats belonging to a community. * [[Community.instantiateChat]] MUST be used for chats belonging to a community.
*/ */
public static async create(id: string): Promise<Chat> { public static async create(
id: string,
communityChat?: CommunityChat
): Promise<Chat> {
const symKey = await createSymKeyFromPassword(id); const symKey = await createSymKeyFromPassword(id);
return new Chat(id, symKey); return new Chat(id, symKey, communityChat);
} }
public get contentTopic(): string { public get contentTopic(): string {

View File

@ -26,9 +26,16 @@ describe("Community live data", () => {
expect(desc).to.not.be.undefined; expect(desc).to.not.be.undefined;
expect(desc.identity?.displayName).to.eq("DappConnect Test"); 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 (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("foobar");
expect(chats).to.include("another-channel!"); expect(chats).to.include("another-channel!");
}); });

View File

@ -11,12 +11,13 @@ const dbg = debug("communities:community");
export class Community { export class Community {
public publicKey: Uint8Array; public publicKey: Uint8Array;
private waku: Waku; private waku: Waku;
public chats: Map<string, Chat>; // Chat id, Chat
public description?: CommunityDescription; public description?: CommunityDescription;
constructor(publicKey: Uint8Array, waku: Waku) { constructor(publicKey: Uint8Array, waku: Waku) {
this.publicKey = publicKey; this.publicKey = publicKey;
this.waku = waku; this.waku = waku;
this.chats = new Map();
} }
/** /**
@ -60,6 +61,12 @@ export class Community {
} }
this.description = desc; 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; * @throws string If the Community Description is unavailable or the chat is not found;
*/ */
public async instantiateChat(chatName: string): Promise<Chat> { private async instantiateChat(
if (!this.description) { chatUuid: string,
await this.refreshCommunityDescription(); communityChat: CommunityChat
if (!this.description) ): Promise<void> {
throw "Failed to retrieve community description, cannot instantiate chat"; if (!this.description)
} throw "Failed to retrieve community description, cannot instantiate chat";
let communityChat: CommunityChat | undefined; const chatId = this.publicKeyStr + chatUuid;
let chatUuid: string | undefined; if (this.chats.get(chatId)) return;
this.description.chats.forEach((_chat, _id) => { const chat = await Chat.create(chatId, communityChat);
if (chatUuid) return;
if (_chat.identity?.displayName === chatName) { this.chats.set(chatId, chat);
chatUuid = _id;
communityChat = _chat;
}
});
if (!communityChat || !chatUuid)
throw `Failed to retrieve community community chat with name ${chatName}`;
return Chat.create(this.publicKeyStr + chatUuid);
} }
} }