Skip to content

Commit

Permalink
Add 1:1 messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Nov 1, 2024
1 parent 77f4e99 commit 48368e6
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sdks/browser-sdk/src/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,10 @@ export class Conversation {
state,
});
}

async dmPeerInboxId() {
return this.#client.sendMessage("getDmPeerInboxId", {
id: this.#id,
});
}
}
30 changes: 30 additions & 0 deletions sdks/browser-sdk/src/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -39,6 +45,22 @@ export class Conversations {
);
}

async listGroups(
options?: Omit<SafeListConversationsOptions, "conversation_type">,
) {
return this.#client.sendMessage("getGroups", {
options,
});
}

async listDms(
options?: Omit<SafeListConversationsOptions, "conversation_type">,
) {
return this.#client.sendMessage("getDms", {
options,
});
}

async newGroup(accountAddresses: string[], options?: SafeCreateGroupOptions) {
const conversation = await this.#client.sendMessage("newGroup", {
accountAddresses,
Expand All @@ -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);
}
}
4 changes: 4 additions & 0 deletions sdks/browser-sdk/src/WorkerConversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,8 @@ export class WorkerConversation {
updateConsentState(state: WasmConsentState) {
this.#group.update_consent_state(state);
}

dmPeerInboxId() {
return this.#group.dm_peer_inbox_id();
}
}
32 changes: 32 additions & 0 deletions sdks/browser-sdk/src/WorkerConversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,50 @@ 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,
)) as WasmGroup[];
return groups.map((group) => new WorkerConversation(this.#client, group));
}

async listGroups(
options?: Omit<SafeListConversationsOptions, "conversation_type">,
) {
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<SafeListConversationsOptions, "conversation_type">,
) {
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,
options ? fromSafeCreateGroupOptions(options) : undefined,
);
return new WorkerConversation(this.#client, group);
}

async newDm(accountAddress: string) {
const group = await this.#conversations.create_dm(accountAddress);
return new WorkerConversation(this.#client, group);
}
}
40 changes: 40 additions & 0 deletions sdks/browser-sdk/src/types/clientEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ export type ClientEvents =
id: string;
};
}
| {
action: "getDmByInboxId";
id: string;
result: SafeConversation | undefined;
data: {
inboxId: string;
};
}
| {
action: "getConversations";
id: string;
Expand All @@ -172,6 +180,22 @@ export type ClientEvents =
options?: SafeListConversationsOptions;
};
}
| {
action: "getGroups";
id: string;
result: SafeConversation[];
data: {
options?: Omit<SafeListConversationsOptions, "conversation_type">;
};
}
| {
action: "getDms";
id: string;
result: SafeConversation[];
data: {
options?: Omit<SafeListConversationsOptions, "conversation_type">;
};
}
| {
action: "newGroup";
id: string;
Expand All @@ -181,6 +205,14 @@ export type ClientEvents =
options?: SafeCreateGroupOptions;
};
}
| {
action: "newDm";
id: string;
result: SafeConversation;
data: {
accountAddress: string;
};
}
| {
action: "syncConversations";
id: string;
Expand Down Expand Up @@ -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"];
Expand Down
61 changes: 61 additions & 0 deletions sdks/browser-sdk/src/workers/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,30 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
});
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,
Expand All @@ -215,6 +239,17 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
});
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({
Expand Down Expand Up @@ -242,6 +277,15 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
});
break;
}
case "getDmByInboxId": {
const conversation = client.conversations.getDmByInboxId(data.inboxId);
postMessage({
id,
action,
result: conversation ? toSafeConversation(conversation) : undefined,
});
break;
}
/**
* Group actions
*/
Expand Down Expand Up @@ -678,6 +722,23 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
}
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({
Expand Down

0 comments on commit 48368e6

Please sign in to comment.