Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for to-device messages via OLM #303

Merged
merged 5 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"dependencies": {
"@juggle/resize-observer": "^3.3.1",
"@matrix-org/olm": "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.8.tgz",
"@react-aria/button": "^3.3.4",
"@react-aria/dialog": "^3.1.4",
"@react-aria/focus": "^3.5.0",
Expand Down
5 changes: 5 additions & 0 deletions src/IndexedDBWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IndexedDBStoreWorker } from "matrix-js-sdk/src/indexeddb-worker";

const remoteWorker = new IndexedDBStoreWorker(self.postMessage);

self.onmessage = remoteWorker.onMessage;
51 changes: 51 additions & 0 deletions src/matrix-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
GroupCallIntent,
GroupCallType,
} from "matrix-js-sdk/src/browser-index";
import IndexedDBWorker from "./IndexedDBWorker?worker";
import Olm from "@matrix-org/olm";
import olmWasmPath from "@matrix-org/olm/olm.wasm?url";

export const defaultHomeserver =
import.meta.env.VITE_DEFAULT_HOMESERVER ||
Expand All @@ -26,11 +29,59 @@ function waitForSync(client) {
}

export async function initClient(clientOptions) {
// TODO: https://gitlab.matrix.org/matrix-org/olm/-/issues/10
window.OLM_OPTIONS = {};
await Olm.init({ locateFile: () => olmWasmPath });

let indexedDB;

try {
indexedDB = window.indexedDB;
} catch (e) {}

const storeOpts = {};

if (indexedDB && localStorage && !import.meta.env.DEV) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fwiw I think the bit you need is the crypto store below - this shouldn't really be necessary to get crypto working (although it will speed up your subsequent loads).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah Matthew and I discussed adding this to Element Call so I figured it was good to do it here in addition to the crypto store.

storeOpts.store = new matrix.IndexedDBStore({
indexedDB: window.indexedDB,
localStorage: window.localStorage,
dbName: "element-call-sync",
workerFactory: () => new IndexedDBWorker(),
});
}

if (localStorage) {
storeOpts.sessionStore = new matrix.WebStorageSessionStore(localStorage);
}

if (indexedDB) {
storeOpts.cryptoStore = new matrix.IndexedDBCryptoStore(
indexedDB,
"matrix-js-sdk:crypto"
);
}

const client = matrix.createClient({
...storeOpts,
...clientOptions,
useAuthorizationHeader: true,
});

try {
await client.store.startup();
} catch (error) {
console.error(
"Error starting matrix client store. Falling back to memory store.",
error
);
client.store = new matrix.MemoryStore({ localStorage });
await client.store.startup();
}

if (client.initCrypto) {
await client.initCrypto();
}

await client.startClient({
// dirty hack to reduce chance of gappy syncs
// should be fixed by spotting gaps and backpaginating
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,10 @@
resolved "https://registry.yarnpkg.com/@juggle/resize-observer/-/resize-observer-3.3.1.tgz#b50a781709c81e10701004214340f25475a171a0"
integrity sha512-zMM9Ds+SawiUkakS7y94Ymqx+S0ORzpG3frZirN3l+UlXUmSUR7hF4wxCVqW+ei94JzV5kt0uXBcoOEAuiydrw==

"@matrix-org/olm@https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.8.tgz":
version "3.2.8"
resolved "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.8.tgz#8d53636d045e1776e2a2ec6613e57330dd9ce856"

"@mdx-js/mdx@^1.6.22":
version "1.6.22"
resolved "https://registry.yarnpkg.com/@mdx-js/mdx/-/mdx-1.6.22.tgz#8a723157bf90e78f17dc0f27995398e6c731f1ba"
Expand Down