diff --git a/sdks/browser-sdk/src/Conversation.ts b/sdks/browser-sdk/src/Conversation.ts index 01e73b382..5f46256e1 100644 --- a/sdks/browser-sdk/src/Conversation.ts +++ b/sdks/browser-sdk/src/Conversation.ts @@ -282,4 +282,10 @@ export class Conversation { state, }); } + + async dmPeerInboxId() { + return this.#client.sendMessage("getDmPeerInboxId", { + id: this.#id, + }); + } } diff --git a/sdks/browser-sdk/src/Conversations.ts b/sdks/browser-sdk/src/Conversations.ts index f2c3316f4..75e4b8dc6 100644 --- a/sdks/browser-sdk/src/Conversations.ts +++ b/sdks/browser-sdk/src/Conversations.ts @@ -28,6 +28,12 @@ export class Conversations { }); } + async getDmByInboxId(inboxId: string) { + return this.#client.sendMessage("getDmByInboxId", { + inboxId, + }); + } + async list(options?: SafeListConversationsOptions) { const conversations = await this.#client.sendMessage("getConversations", { options, @@ -39,6 +45,22 @@ export class Conversations { ); } + async listGroups( + options?: Omit, + ) { + return this.#client.sendMessage("getGroups", { + options, + }); + } + + async listDms( + options?: Omit, + ) { + return this.#client.sendMessage("getDms", { + options, + }); + } + async newGroup(accountAddresses: string[], options?: SafeCreateGroupOptions) { const conversation = await this.#client.sendMessage("newGroup", { accountAddresses, @@ -47,4 +69,12 @@ export class Conversations { return new Conversation(this.#client, conversation.id, conversation); } + + async newDm(accountAddress: string) { + const conversation = await this.#client.sendMessage("newDm", { + accountAddress, + }); + + return new Conversation(this.#client, conversation.id, conversation); + } } diff --git a/sdks/browser-sdk/src/WorkerConversation.ts b/sdks/browser-sdk/src/WorkerConversation.ts index b92abcb2c..8a2bc9700 100644 --- a/sdks/browser-sdk/src/WorkerConversation.ts +++ b/sdks/browser-sdk/src/WorkerConversation.ts @@ -166,4 +166,8 @@ export class WorkerConversation { updateConsentState(state: WasmConsentState) { this.#group.update_consent_state(state); } + + dmPeerInboxId() { + return this.#group.dm_peer_inbox_id(); + } } diff --git a/sdks/browser-sdk/src/WorkerConversations.ts b/sdks/browser-sdk/src/WorkerConversations.ts index d7a631b82..1f2109de1 100644 --- a/sdks/browser-sdk/src/WorkerConversations.ts +++ b/sdks/browser-sdk/src/WorkerConversations.ts @@ -43,6 +43,15 @@ export class WorkerConversations { } } + getDmByInboxId(inboxId: string) { + try { + const group = this.#conversations.find_dm_by_target_inbox_id(inboxId); + return new WorkerConversation(this.#client, group); + } catch { + return undefined; + } + } + async list(options?: SafeListConversationsOptions) { const groups = (await this.#conversations.list( options ? fromSafeListConversationsOptions(options) : undefined, @@ -50,6 +59,24 @@ export class WorkerConversations { return groups.map((group) => new WorkerConversation(this.#client, group)); } + async listGroups( + options?: Omit, + ) { + const groups = (await this.#conversations.list_groups( + options ? fromSafeListConversationsOptions(options) : undefined, + )) as WasmGroup[]; + return groups.map((group) => new WorkerConversation(this.#client, group)); + } + + async listDms( + options?: Omit, + ) { + const groups = (await this.#conversations.list_dms( + options ? fromSafeListConversationsOptions(options) : undefined, + )) as WasmGroup[]; + return groups.map((group) => new WorkerConversation(this.#client, group)); + } + async newGroup(accountAddresses: string[], options?: SafeCreateGroupOptions) { const group = await this.#conversations.create_group( accountAddresses, @@ -57,4 +84,9 @@ export class WorkerConversations { ); return new WorkerConversation(this.#client, group); } + + async newDm(accountAddress: string) { + const group = await this.#conversations.create_dm(accountAddress); + return new WorkerConversation(this.#client, group); + } } diff --git a/sdks/browser-sdk/src/types/clientEvents.ts b/sdks/browser-sdk/src/types/clientEvents.ts index 1cddf0344..c61e3f995 100644 --- a/sdks/browser-sdk/src/types/clientEvents.ts +++ b/sdks/browser-sdk/src/types/clientEvents.ts @@ -164,6 +164,14 @@ export type ClientEvents = id: string; }; } + | { + action: "getDmByInboxId"; + id: string; + result: SafeConversation | undefined; + data: { + inboxId: string; + }; + } | { action: "getConversations"; id: string; @@ -172,6 +180,22 @@ export type ClientEvents = options?: SafeListConversationsOptions; }; } + | { + action: "getGroups"; + id: string; + result: SafeConversation[]; + data: { + options?: Omit; + }; + } + | { + action: "getDms"; + id: string; + result: SafeConversation[]; + data: { + options?: Omit; + }; + } | { action: "newGroup"; id: string; @@ -181,6 +205,14 @@ export type ClientEvents = options?: SafeCreateGroupOptions; }; } + | { + action: "newDm"; + id: string; + result: SafeConversation; + data: { + accountAddress: string; + }; + } | { action: "syncConversations"; id: string; @@ -399,6 +431,14 @@ export type ClientEvents = id: string; state: WasmConsentState; }; + } + | { + action: "getDmPeerInboxId"; + id: string; + result: string; + data: { + id: string; + }; }; export type ClientEventsActions = ClientEvents["action"]; diff --git a/sdks/browser-sdk/src/workers/client.ts b/sdks/browser-sdk/src/workers/client.ts index 56799cf02..ea18abf92 100644 --- a/sdks/browser-sdk/src/workers/client.ts +++ b/sdks/browser-sdk/src/workers/client.ts @@ -203,6 +203,30 @@ self.onmessage = async (event: MessageEvent) => { }); break; } + case "getGroups": { + const conversations = await client.conversations.listGroups( + data.options, + ); + postMessage({ + id, + action, + result: conversations.map((conversation) => + toSafeConversation(conversation), + ), + }); + break; + } + case "getDms": { + const conversations = await client.conversations.listDms(data.options); + postMessage({ + id, + action, + result: conversations.map((conversation) => + toSafeConversation(conversation), + ), + }); + break; + } case "newGroup": { const conversation = await client.conversations.newGroup( data.accountAddresses, @@ -215,6 +239,17 @@ self.onmessage = async (event: MessageEvent) => { }); break; } + case "newDm": { + const conversation = await client.conversations.newDm( + data.accountAddress, + ); + postMessage({ + id, + action, + result: toSafeConversation(conversation), + }); + break; + } case "syncConversations": { await client.conversations.sync(); postMessage({ @@ -242,6 +277,15 @@ self.onmessage = async (event: MessageEvent) => { }); break; } + case "getDmByInboxId": { + const conversation = client.conversations.getDmByInboxId(data.inboxId); + postMessage({ + id, + action, + result: conversation ? toSafeConversation(conversation) : undefined, + }); + break; + } /** * Group actions */ @@ -678,6 +722,23 @@ self.onmessage = async (event: MessageEvent) => { } break; } + case "getDmPeerInboxId": { + const group = client.conversations.getConversationById(data.id); + if (group) { + const result = group.dmPeerInboxId(); + postMessage({ + id, + action, + result, + }); + } else { + postMessageError({ + id, + action, + error: "Group not found", + }); + } + } } } catch (e) { postMessageError({