Skip to content

Commit

Permalink
Merge pull request #703 from xmtp/rygine/browser-sdk-update
Browse files Browse the repository at this point in the history
Add 1:1 messaging to Browser SDK
  • Loading branch information
rygine authored Nov 1, 2024
2 parents 13430d1 + 04a8244 commit 5dd8eb0
Show file tree
Hide file tree
Showing 18 changed files with 525 additions and 35 deletions.
2 changes: 1 addition & 1 deletion sdks/browser-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"@xmtp/content-type-primitives": "^1.0.1",
"@xmtp/content-type-text": "^1.0.0",
"@xmtp/proto": "^3.70.0",
"@xmtp/wasm-bindings": "^0.0.1",
"@xmtp/wasm-bindings": "^0.0.2",
"uuid": "^10.0.0"
},
"devDependencies": {
Expand Down
14 changes: 10 additions & 4 deletions sdks/browser-sdk/src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,16 @@ export class Client extends ClientWorkerClass {
return this.sendMessage("getRevokeWalletSignatureText", { accountAddress });
}

async getRevokeInstallationsSignatureText() {
return this.sendMessage("getRevokeInstallationsSignatureText", undefined);
}

async addSignature(type: WasmSignatureRequestType, bytes: Uint8Array) {
return this.sendMessage("addSignature", { type, bytes });
}

async applySignaturesRequests() {
return this.sendMessage("applySignaturesRequests", undefined);
async applySignatures() {
return this.sendMessage("applySignatures", undefined);
}

async registerIdentity() {
Expand All @@ -119,8 +123,10 @@ export class Client extends ClientWorkerClass {
return this.sendMessage("findInboxIdByAddress", { address });
}

async inboxState(refreshFromNetwork: boolean) {
return this.sendMessage("inboxState", { refreshFromNetwork });
async inboxState(refreshFromNetwork?: boolean) {
return this.sendMessage("inboxState", {
refreshFromNetwork: refreshFromNetwork ?? false,
});
}

async getLatestInboxState(inboxId: string) {
Expand Down
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);
}
}
2 changes: 1 addition & 1 deletion sdks/browser-sdk/src/WorkerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class WorkerClient {
return this.#client.addSignature(type, bytes);
}

async applySignaturesRequests() {
async applySignatures() {
return this.#client.applySignatureRequests();
}

Expand Down
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);
}
}
42 changes: 41 additions & 1 deletion sdks/browser-sdk/src/types/clientEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export type ClientEvents =
};
}
| {
action: "applySignaturesRequests";
action: "applySignatures";
id: string;
result: undefined;
data: undefined;
Expand Down 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
12 changes: 12 additions & 0 deletions sdks/browser-sdk/src/utils/conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import {
WasmListMessagesOptions,
type WasmConsentEntityType,
type WasmConsentState,
type WasmConversationType,
type WasmDeliveryStatus,
type WasmDirection,
type WasmGroupMembershipState,
type WasmGroupMessageKind,
type WasmGroupPermissionsOptions,
type WasmInboxState,
Expand Down Expand Up @@ -141,6 +144,7 @@ export const toSafeMessage = (message: WasmMessage): SafeMessage => ({

export type SafeListMessagesOptions = {
delivery_status?: WasmDeliveryStatus;
direction?: WasmDirection;
limit?: bigint;
sent_after_ns?: bigint;
sent_before_ns?: bigint;
Expand All @@ -150,6 +154,7 @@ export const toSafeListMessagesOptions = (
options: WasmListMessagesOptions,
): SafeListMessagesOptions => ({
delivery_status: options.delivery_status,
direction: options.direction,
limit: options.limit,
sent_after_ns: options.sent_after_ns,
sent_before_ns: options.sent_before_ns,
Expand All @@ -163,9 +168,12 @@ export const fromSafeListMessagesOptions = (
options.sent_after_ns,
options.limit,
options.delivery_status,
options.direction,
);

export type SafeListConversationsOptions = {
allowed_states?: WasmGroupMembershipState[];
conversation_type?: WasmConversationType;
created_after_ns?: bigint;
created_before_ns?: bigint;
limit?: bigint;
Expand All @@ -174,6 +182,8 @@ export type SafeListConversationsOptions = {
export const toSafeListConversationsOptions = (
options: WasmListConversationsOptions,
): SafeListConversationsOptions => ({
allowed_states: options.allowed_states,
conversation_type: options.conversation_type,
created_after_ns: options.created_after_ns,
created_before_ns: options.created_before_ns,
limit: options.limit,
Expand All @@ -183,6 +193,8 @@ export const fromSafeListConversationsOptions = (
options: SafeListConversationsOptions,
): WasmListConversationsOptions =>
new WasmListConversationsOptions(
options.allowed_states,
options.conversation_type,
options.created_after_ns,
options.created_before_ns,
options.limit,
Expand Down
7 changes: 6 additions & 1 deletion sdks/browser-sdk/src/utils/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ export const createClient = async (
await init();

const host = options?.apiUrl ?? ApiUrls[options?.env ?? "dev"];
const dbPath = `xmtp-${options?.env ?? "dev"}-${accountAddress}.db3`;
// TODO: add db path validation
// - must end with .db3
// - must not contain invalid characters
// - must not start with a dot
const dbPath =
options?.dbPath ?? `xmtp-${options?.env ?? "dev"}-${accountAddress}.db3`;

const inboxId =
(await getInboxIdForAddress(host, accountAddress)) ||
Expand Down
Loading

0 comments on commit 5dd8eb0

Please sign in to comment.