2022-04-28 04:38:16 +08:00
|
|
|
import Olm from "@matrix-org/olm";
|
|
|
|
import olmWasmPath from "@matrix-org/olm/olm.wasm?url";
|
2022-05-30 17:09:13 +08:00
|
|
|
import { IndexedDBStore } from "matrix-js-sdk/src/store/indexeddb";
|
|
|
|
import { MemoryStore } from "matrix-js-sdk/src/store/memory";
|
|
|
|
import { IndexedDBCryptoStore } from "matrix-js-sdk/src/crypto/store/indexeddb-crypto-store";
|
2022-06-21 23:32:07 +08:00
|
|
|
import { LocalStorageCryptoStore } from "matrix-js-sdk/src/crypto/store/localStorage-crypto-store";
|
|
|
|
import { MemoryCryptoStore } from "matrix-js-sdk/src/crypto/store/memory-crypto-store";
|
2022-05-30 17:09:13 +08:00
|
|
|
import { createClient, MatrixClient } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { ICreateClientOpts } from "matrix-js-sdk/src/matrix";
|
2022-05-30 18:28:16 +08:00
|
|
|
import { ClientEvent } from "matrix-js-sdk/src/client";
|
2022-05-30 18:41:59 +08:00
|
|
|
import { Visibility, Preset } from "matrix-js-sdk/src/@types/partials";
|
|
|
|
import {
|
|
|
|
GroupCallIntent,
|
|
|
|
GroupCallType,
|
|
|
|
} from "matrix-js-sdk/src/webrtc/groupCall";
|
2022-05-30 18:28:16 +08:00
|
|
|
import { ISyncStateData, SyncState } from "matrix-js-sdk/src/sync";
|
2022-05-30 17:09:13 +08:00
|
|
|
|
|
|
|
import IndexedDBWorker from "./IndexedDBWorker?worker";
|
2022-01-06 09:19:03 +08:00
|
|
|
|
|
|
|
export const defaultHomeserver =
|
2022-05-30 17:09:13 +08:00
|
|
|
(import.meta.env.VITE_DEFAULT_HOMESERVER as string) ??
|
2022-01-06 09:19:03 +08:00
|
|
|
`${window.location.protocol}//${window.location.host}`;
|
|
|
|
|
|
|
|
export const defaultHomeserverHost = new URL(defaultHomeserver).host;
|
|
|
|
|
2022-05-30 18:28:16 +08:00
|
|
|
function waitForSync(client: MatrixClient) {
|
2022-05-30 17:09:13 +08:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
2022-05-30 18:28:16 +08:00
|
|
|
const onSync = (
|
|
|
|
state: SyncState,
|
|
|
|
_old: SyncState,
|
|
|
|
data: ISyncStateData
|
|
|
|
) => {
|
2022-01-06 09:19:03 +08:00
|
|
|
if (state === "PREPARED") {
|
|
|
|
resolve();
|
2022-05-30 18:28:16 +08:00
|
|
|
client.removeListener(ClientEvent.Sync, onSync);
|
2022-01-06 09:19:03 +08:00
|
|
|
} else if (state === "ERROR") {
|
|
|
|
reject(data?.error);
|
2022-05-30 18:28:16 +08:00
|
|
|
client.removeListener(ClientEvent.Sync, onSync);
|
2022-01-06 09:19:03 +08:00
|
|
|
}
|
|
|
|
};
|
2022-05-30 18:28:16 +08:00
|
|
|
client.on(ClientEvent.Sync, onSync);
|
2022-01-06 09:19:03 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-30 18:28:16 +08:00
|
|
|
export async function initClient(
|
|
|
|
clientOptions: ICreateClientOpts
|
|
|
|
): Promise<MatrixClient> {
|
2022-04-28 04:51:08 +08:00
|
|
|
// TODO: https://gitlab.matrix.org/matrix-org/olm/-/issues/10
|
2022-04-28 04:38:16 +08:00
|
|
|
window.OLM_OPTIONS = {};
|
|
|
|
await Olm.init({ locateFile: () => olmWasmPath });
|
2022-04-27 07:28:21 +08:00
|
|
|
|
2022-06-01 16:07:00 +08:00
|
|
|
let indexedDB: IDBFactory;
|
2022-04-27 06:20:06 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
indexedDB = window.indexedDB;
|
|
|
|
} catch (e) {}
|
|
|
|
|
2022-05-30 17:09:13 +08:00
|
|
|
const storeOpts = {} as ICreateClientOpts;
|
2022-04-27 06:20:06 +08:00
|
|
|
|
|
|
|
if (indexedDB && localStorage && !import.meta.env.DEV) {
|
2022-05-30 17:09:13 +08:00
|
|
|
storeOpts.store = new IndexedDBStore({
|
2022-04-27 06:20:06 +08:00
|
|
|
indexedDB: window.indexedDB,
|
2022-06-21 23:32:07 +08:00
|
|
|
localStorage,
|
2022-04-27 06:20:06 +08:00
|
|
|
dbName: "element-call-sync",
|
|
|
|
workerFactory: () => new IndexedDBWorker(),
|
|
|
|
});
|
2022-06-21 23:32:07 +08:00
|
|
|
} else if (localStorage) {
|
|
|
|
storeOpts.store = new MemoryStore({ localStorage });
|
2022-04-27 06:20:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (indexedDB) {
|
2022-05-30 17:09:13 +08:00
|
|
|
storeOpts.cryptoStore = new IndexedDBCryptoStore(
|
2022-04-27 06:20:06 +08:00
|
|
|
indexedDB,
|
|
|
|
"matrix-js-sdk:crypto"
|
|
|
|
);
|
2022-06-21 23:32:07 +08:00
|
|
|
} else if (localStorage) {
|
|
|
|
storeOpts.cryptoStore = new LocalStorageCryptoStore(localStorage);
|
|
|
|
} else {
|
|
|
|
storeOpts.cryptoStore = new MemoryCryptoStore();
|
2022-04-27 06:20:06 +08:00
|
|
|
}
|
|
|
|
|
2022-05-30 17:09:13 +08:00
|
|
|
const client = createClient({
|
2022-04-27 06:20:06 +08:00
|
|
|
...storeOpts,
|
2022-01-07 08:51:23 +08:00
|
|
|
...clientOptions,
|
|
|
|
useAuthorizationHeader: true,
|
2022-06-01 17:04:49 +08:00
|
|
|
// Use a relatively low timeout for API calls: this is a realtime application
|
2022-05-30 18:46:27 +08:00
|
|
|
// so we don't want API calls taking ages, we'd rather they just fail.
|
|
|
|
localTimeoutMs: 5000,
|
2022-01-07 08:51:23 +08:00
|
|
|
});
|
2022-01-06 09:19:03 +08:00
|
|
|
|
2022-04-27 06:20:06 +08:00
|
|
|
try {
|
|
|
|
await client.store.startup();
|
|
|
|
} catch (error) {
|
|
|
|
console.error(
|
|
|
|
"Error starting matrix client store. Falling back to memory store.",
|
|
|
|
error
|
|
|
|
);
|
2022-05-30 17:09:13 +08:00
|
|
|
client.store = new MemoryStore({ localStorage });
|
2022-04-27 06:20:06 +08:00
|
|
|
await client.store.startup();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client.initCrypto) {
|
|
|
|
await client.initCrypto();
|
|
|
|
}
|
|
|
|
|
2022-01-06 09:19:03 +08:00
|
|
|
await client.startClient({
|
|
|
|
// dirty hack to reduce chance of gappy syncs
|
|
|
|
// should be fixed by spotting gaps and backpaginating
|
|
|
|
initialSyncLimit: 50,
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitForSync(client);
|
|
|
|
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
2022-06-01 16:29:47 +08:00
|
|
|
export function roomAliasLocalpartFromRoomName(roomName: string): string {
|
2022-01-06 09:19:03 +08:00
|
|
|
return roomName
|
|
|
|
.trim()
|
|
|
|
.replace(/\s/g, "-")
|
|
|
|
.replace(/[^\w-]/g, "")
|
|
|
|
.toLowerCase();
|
|
|
|
}
|
|
|
|
|
2022-06-01 16:29:47 +08:00
|
|
|
export function fullAliasFromRoomName(
|
|
|
|
roomName: string,
|
|
|
|
client: MatrixClient
|
|
|
|
): string {
|
|
|
|
return `#${roomAliasLocalpartFromRoomName(roomName)}:${client.getDomain()}`;
|
|
|
|
}
|
|
|
|
|
2022-05-30 18:28:16 +08:00
|
|
|
export function roomNameFromRoomId(roomId: string): string {
|
2022-02-15 05:53:19 +08:00
|
|
|
return roomId
|
|
|
|
.match(/([^:]+):.*$/)[1]
|
|
|
|
.substring(1)
|
|
|
|
.split("-")
|
2022-03-04 09:09:31 +08:00
|
|
|
.map((part) =>
|
|
|
|
part.length > 0 ? part.charAt(0).toUpperCase() + part.slice(1) : part
|
|
|
|
)
|
2022-03-04 08:56:45 +08:00
|
|
|
.join(" ")
|
|
|
|
.toLowerCase();
|
2022-02-15 05:53:19 +08:00
|
|
|
}
|
|
|
|
|
2022-05-30 18:28:16 +08:00
|
|
|
export function isLocalRoomId(roomId: string): boolean {
|
2022-02-15 05:53:19 +08:00
|
|
|
if (!roomId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parts = roomId.match(/[^:]+:(.*)$/);
|
|
|
|
|
|
|
|
if (parts.length < 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts[1] === defaultHomeserverHost;
|
|
|
|
}
|
|
|
|
|
2022-05-30 18:28:16 +08:00
|
|
|
export async function createRoom(
|
|
|
|
client: MatrixClient,
|
|
|
|
name: string,
|
|
|
|
isPtt = false
|
|
|
|
): Promise<string> {
|
2022-05-30 17:09:13 +08:00
|
|
|
const createRoomResult = await client.createRoom({
|
|
|
|
visibility: Visibility.Private,
|
|
|
|
preset: Preset.PublicChat,
|
2022-01-06 09:19:03 +08:00
|
|
|
name,
|
2022-06-01 16:29:47 +08:00
|
|
|
room_alias_name: roomAliasLocalpartFromRoomName(name),
|
2022-01-06 09:19:03 +08:00
|
|
|
power_level_content_override: {
|
|
|
|
invite: 100,
|
|
|
|
kick: 100,
|
|
|
|
ban: 100,
|
|
|
|
redact: 50,
|
|
|
|
state_default: 0,
|
|
|
|
events_default: 0,
|
|
|
|
users_default: 0,
|
|
|
|
events: {
|
|
|
|
"m.room.power_levels": 100,
|
|
|
|
"m.room.history_visibility": 100,
|
|
|
|
"m.room.tombstone": 100,
|
|
|
|
"m.room.encryption": 100,
|
|
|
|
"m.room.name": 50,
|
|
|
|
"m.room.message": 0,
|
|
|
|
"m.room.encrypted": 50,
|
|
|
|
"m.sticker": 50,
|
|
|
|
"org.matrix.msc3401.call.member": 0,
|
|
|
|
},
|
|
|
|
users: {
|
|
|
|
[client.getUserId()]: 100,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-06-01 16:29:47 +08:00
|
|
|
console.log(`Creating ${isPtt ? "PTT" : "video"} group call room`);
|
2022-04-23 09:05:48 +08:00
|
|
|
|
2022-01-06 09:19:03 +08:00
|
|
|
await client.createGroupCall(
|
2022-05-30 17:09:13 +08:00
|
|
|
createRoomResult.room_id,
|
2022-04-29 02:13:20 +08:00
|
|
|
isPtt ? GroupCallType.Voice : GroupCallType.Video,
|
2022-04-23 09:05:48 +08:00
|
|
|
isPtt,
|
2022-01-06 09:19:03 +08:00
|
|
|
GroupCallIntent.Prompt
|
|
|
|
);
|
|
|
|
|
2022-06-01 16:29:47 +08:00
|
|
|
return fullAliasFromRoomName(name, client);
|
2022-01-06 09:19:03 +08:00
|
|
|
}
|
|
|
|
|
2022-05-30 18:28:16 +08:00
|
|
|
export function getRoomUrl(roomId: string): string {
|
2022-01-06 09:19:03 +08:00
|
|
|
if (roomId.startsWith("#")) {
|
|
|
|
const [localPart, host] = roomId.replace("#", "").split(":");
|
|
|
|
|
|
|
|
if (host !== defaultHomeserverHost) {
|
2022-02-17 02:52:07 +08:00
|
|
|
return `${window.location.protocol}//${window.location.host}/room/${roomId}`;
|
2022-01-06 09:19:03 +08:00
|
|
|
} else {
|
2022-02-17 02:52:07 +08:00
|
|
|
return `${window.location.protocol}//${window.location.host}/${localPart}`;
|
2022-01-06 09:19:03 +08:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-17 02:52:07 +08:00
|
|
|
return `${window.location.protocol}//${window.location.host}/room/${roomId}`;
|
2022-01-06 09:19:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 18:28:16 +08:00
|
|
|
export function getAvatarUrl(
|
|
|
|
client: MatrixClient,
|
|
|
|
mxcUrl: string,
|
|
|
|
avatarSize = 96
|
|
|
|
): string {
|
2022-01-06 09:19:03 +08:00
|
|
|
const width = Math.floor(avatarSize * window.devicePixelRatio);
|
|
|
|
const height = Math.floor(avatarSize * window.devicePixelRatio);
|
|
|
|
return mxcUrl && client.mxcUrlToHttp(mxcUrl, width, height, "crop");
|
|
|
|
}
|