Convert matrix-utils to typescript
This commit is contained in:
parent
5c4bab2a8a
commit
a05501a909
3 changed files with 33 additions and 22 deletions
7
src/@types/global.d.ts
vendored
7
src/@types/global.d.ts
vendored
|
@ -15,3 +15,10 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import "matrix-js-sdk/src/@types/global";
|
import "matrix-js-sdk/src/@types/global";
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface Window {
|
||||||
|
// TODO: https://gitlab.matrix.org/matrix-org/olm/-/issues/10
|
||||||
|
OLM_OPTIONS: Record<string, string>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,20 +1,24 @@
|
||||||
import matrix from "matrix-js-sdk/src/browser-index";
|
|
||||||
import {
|
|
||||||
GroupCallIntent,
|
|
||||||
GroupCallType,
|
|
||||||
} from "matrix-js-sdk/src/browser-index";
|
|
||||||
import IndexedDBWorker from "./IndexedDBWorker?worker";
|
|
||||||
import Olm from "@matrix-org/olm";
|
import Olm from "@matrix-org/olm";
|
||||||
import olmWasmPath from "@matrix-org/olm/olm.wasm?url";
|
import olmWasmPath from "@matrix-org/olm/olm.wasm?url";
|
||||||
|
import { IndexedDBStore } from "matrix-js-sdk/src/store/indexeddb";
|
||||||
|
import { WebStorageSessionStore } from "matrix-js-sdk/src/store/session/webstorage";
|
||||||
|
import { MemoryStore } from "matrix-js-sdk/src/store/memory";
|
||||||
|
import { IndexedDBCryptoStore } from "matrix-js-sdk/src/crypto/store/indexeddb-crypto-store";
|
||||||
|
import { createClient, MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||||
|
import { ICreateClientOpts } from "matrix-js-sdk/src/matrix";
|
||||||
|
import { Visibility, Preset, GroupCallIntent } from "matrix-js-sdk";
|
||||||
|
import { GroupCallType } from "matrix-js-sdk";
|
||||||
|
|
||||||
|
import IndexedDBWorker from "./IndexedDBWorker?worker";
|
||||||
|
|
||||||
export const defaultHomeserver =
|
export const defaultHomeserver =
|
||||||
import.meta.env.VITE_DEFAULT_HOMESERVER ||
|
(import.meta.env.VITE_DEFAULT_HOMESERVER as string) ??
|
||||||
`${window.location.protocol}//${window.location.host}`;
|
`${window.location.protocol}//${window.location.host}`;
|
||||||
|
|
||||||
export const defaultHomeserverHost = new URL(defaultHomeserver).host;
|
export const defaultHomeserverHost = new URL(defaultHomeserver).host;
|
||||||
|
|
||||||
function waitForSync(client) {
|
function waitForSync(client) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
const onSync = (state, _old, data) => {
|
const onSync = (state, _old, data) => {
|
||||||
if (state === "PREPARED") {
|
if (state === "PREPARED") {
|
||||||
resolve();
|
resolve();
|
||||||
|
@ -28,7 +32,7 @@ function waitForSync(client) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function initClient(clientOptions) {
|
export async function initClient(clientOptions: ICreateClientOpts) {
|
||||||
// TODO: https://gitlab.matrix.org/matrix-org/olm/-/issues/10
|
// TODO: https://gitlab.matrix.org/matrix-org/olm/-/issues/10
|
||||||
window.OLM_OPTIONS = {};
|
window.OLM_OPTIONS = {};
|
||||||
await Olm.init({ locateFile: () => olmWasmPath });
|
await Olm.init({ locateFile: () => olmWasmPath });
|
||||||
|
@ -39,10 +43,10 @@ export async function initClient(clientOptions) {
|
||||||
indexedDB = window.indexedDB;
|
indexedDB = window.indexedDB;
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
|
|
||||||
const storeOpts = {};
|
const storeOpts = {} as ICreateClientOpts;
|
||||||
|
|
||||||
if (indexedDB && localStorage && !import.meta.env.DEV) {
|
if (indexedDB && localStorage && !import.meta.env.DEV) {
|
||||||
storeOpts.store = new matrix.IndexedDBStore({
|
storeOpts.store = new IndexedDBStore({
|
||||||
indexedDB: window.indexedDB,
|
indexedDB: window.indexedDB,
|
||||||
localStorage: window.localStorage,
|
localStorage: window.localStorage,
|
||||||
dbName: "element-call-sync",
|
dbName: "element-call-sync",
|
||||||
|
@ -51,17 +55,17 @@ export async function initClient(clientOptions) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (localStorage) {
|
if (localStorage) {
|
||||||
storeOpts.sessionStore = new matrix.WebStorageSessionStore(localStorage);
|
storeOpts.sessionStore = new WebStorageSessionStore(localStorage);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (indexedDB) {
|
if (indexedDB) {
|
||||||
storeOpts.cryptoStore = new matrix.IndexedDBCryptoStore(
|
storeOpts.cryptoStore = new IndexedDBCryptoStore(
|
||||||
indexedDB,
|
indexedDB,
|
||||||
"matrix-js-sdk:crypto"
|
"matrix-js-sdk:crypto"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = matrix.createClient({
|
const client = createClient({
|
||||||
...storeOpts,
|
...storeOpts,
|
||||||
...clientOptions,
|
...clientOptions,
|
||||||
useAuthorizationHeader: true,
|
useAuthorizationHeader: true,
|
||||||
|
@ -74,7 +78,7 @@ export async function initClient(clientOptions) {
|
||||||
"Error starting matrix client store. Falling back to memory store.",
|
"Error starting matrix client store. Falling back to memory store.",
|
||||||
error
|
error
|
||||||
);
|
);
|
||||||
client.store = new matrix.MemoryStore({ localStorage });
|
client.store = new MemoryStore({ localStorage });
|
||||||
await client.store.startup();
|
await client.store.startup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,10 +131,10 @@ export function isLocalRoomId(roomId) {
|
||||||
return parts[1] === defaultHomeserverHost;
|
return parts[1] === defaultHomeserverHost;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createRoom(client, name, isPtt = false) {
|
export async function createRoom(client: MatrixClient, name, isPtt = false) {
|
||||||
const { room_id, room_alias } = await client.createRoom({
|
const createRoomResult = await client.createRoom({
|
||||||
visibility: "private",
|
visibility: Visibility.Private,
|
||||||
preset: "public_chat",
|
preset: Preset.PublicChat,
|
||||||
name,
|
name,
|
||||||
room_alias_name: roomAliasFromRoomName(name),
|
room_alias_name: roomAliasFromRoomName(name),
|
||||||
power_level_content_override: {
|
power_level_content_override: {
|
||||||
|
@ -161,13 +165,13 @@ export async function createRoom(client, name, isPtt = false) {
|
||||||
console.log({ isPtt });
|
console.log({ isPtt });
|
||||||
|
|
||||||
await client.createGroupCall(
|
await client.createGroupCall(
|
||||||
room_id,
|
createRoomResult.room_id,
|
||||||
isPtt ? GroupCallType.Voice : GroupCallType.Video,
|
isPtt ? GroupCallType.Voice : GroupCallType.Video,
|
||||||
isPtt,
|
isPtt,
|
||||||
GroupCallIntent.Prompt
|
GroupCallIntent.Prompt
|
||||||
);
|
);
|
||||||
|
|
||||||
return room_alias || room_id;
|
return createRoomResult.room_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getRoomUrl(roomId) {
|
export function getRoomUrl(roomId) {
|
|
@ -2,7 +2,7 @@
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "es2016",
|
"target": "es2016",
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"module": "commonjs",
|
"module": "es2020",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
"noEmit": true,
|
"noEmit": true,
|
||||||
"noImplicitAny": false,
|
"noImplicitAny": false,
|
||||||
|
|
Loading…
Add table
Reference in a new issue