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 { 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<Chat> {
public static async create(
id: string,
communityChat?: CommunityChat
): Promise<Chat> {
const symKey = await createSymKeyFromPassword(id);
return new Chat(id, symKey);
return new Chat(id, symKey, communityChat);
}
public get contentTopic(): string {

View File

@ -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!");
});

View File

@ -11,12 +11,13 @@ const dbg = debug("communities:community");
export class Community {
public publicKey: Uint8Array;
private waku: Waku;
public chats: Map<string, Chat>; // 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<Chat> {
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<void> {
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);
}
}