Compare commits
177 Commits
v9.5.0
...
v9.9.0-rc.1
| Author | SHA1 | Date | |
|---|---|---|---|
| 0ffdf7c0f1 | |||
| 13b6db8eb4 | |||
| 481acb2a1a | |||
| 683092140d | |||
| 1bb8c2d1a5 | |||
| 60fd3b0786 | |||
| cd4abc4e9b | |||
| e6a21cc487 | |||
| 6c5fc153bf | |||
| 07f15b41a2 | |||
| 8375638d76 | |||
| bed7543b46 | |||
| dc55236263 | |||
| 5f3427c5d1 | |||
| 66e5af185d | |||
| 0ff611e033 | |||
| 737cadaabc | |||
| 51e817a3a2 | |||
| 59c93b59bf | |||
| c18ef051fc | |||
| 1ac5c9acbd | |||
| a034ca171e | |||
| f630a9f297 | |||
| 2f71c93b53 | |||
| 92032a17a8 | |||
| e531456d42 | |||
| f0b2d2fe4d | |||
| 427500220d | |||
| 32e19ead74 | |||
| d11adb6f43 | |||
| f6155a50f6 | |||
| 4efee9445d | |||
| 20746a433f | |||
| 31dacc4206 | |||
| 88e5c59a85 | |||
| cf74920b36 | |||
| 972c900b58 | |||
| 12d5fd79f7 | |||
| a29f6979b2 | |||
| 3a7146c77b | |||
| b178d8f629 | |||
| 0c94ee62a3 | |||
| e7562898cd | |||
| fb73ab6878 | |||
| 38f978791d | |||
| 5dd60de57d | |||
| 5efbfc2dba | |||
| fcd1dbad89 | |||
| ad521bf4c2 | |||
| 8152fa44e0 | |||
| e217bf9e37 | |||
| bfad21f811 | |||
| 81e68abce3 | |||
| 7963bb352d | |||
| 14d3882059 | |||
| dede508e89 | |||
| ea39b69f65 | |||
| eafecd36bc | |||
| 198c9a2507 | |||
| d07563013b | |||
| 9e967832cd | |||
| bfe1987cd9 | |||
| 1045538f1f | |||
| fccf08edcf | |||
| f43fe366b5 | |||
| 0f75f2ef9c | |||
| 2cdc68f9c3 | |||
| 6a7d58e22e | |||
| 203829c1cd | |||
| b55e6c4ef0 | |||
| 8d779e8aec | |||
| dd1d48f688 | |||
| 8d14dc9ee3 | |||
| 5849ea8e63 | |||
| 20afebf339 | |||
| 20eaba191e | |||
| ba58d3c544 | |||
| a8b9d8e3ae | |||
| 83d1e61b2f | |||
| 8e0fc8d460 | |||
| f547fa732f | |||
| e24b1519a4 | |||
| 3028fe9c87 | |||
| 0b970b05b6 | |||
| f7bfb1e49e | |||
| 371ca009e9 | |||
| 4a0f848551 | |||
| 5e8b7b2a62 | |||
| 0f27b703bd | |||
| 61e19c30cb | |||
| c82bc35202 | |||
| 65934227c3 | |||
| 7519becd43 | |||
| fe83c15bc6 | |||
| 07e6b47fa7 | |||
| b026e1c2f7 | |||
| f8194d9418 | |||
| 1ecd7f274f | |||
| 66bf0ec7af | |||
| 1b22df2b7b | |||
| 9f993f1f67 | |||
| 975518bd88 | |||
| 66a863456c | |||
| 91290c0d25 | |||
| 8a23e89c87 | |||
| 9e9cf85ba1 | |||
| 3dd365bbea | |||
| 33a824b980 | |||
| 8571884304 | |||
| 4f1067e66c | |||
| 7b5b851db0 | |||
| ed0be0cf84 | |||
| d3775e5cb1 | |||
| 2c8f658810 | |||
| 5c52f5f579 | |||
| 0a81bb3fdc | |||
| f33196bc51 | |||
| 6beb90a835 | |||
| 9d45e6acd6 | |||
| 516c464458 | |||
| 6ad3fb16b3 | |||
| 277fdd9b8c | |||
| 7d56993b39 | |||
| 4e1442fcf6 | |||
| 4777bf3e75 | |||
| 14cd37ec56 | |||
| 8bcdfd50c9 | |||
| 6776df8e80 | |||
| fbec079c9b | |||
| 93f6bc3780 | |||
| dde8f23cc3 | |||
| 7cfbd0da95 | |||
| a27ddfaaaf | |||
| b53f616015 | |||
| 22dc175879 | |||
| 5f23e4699c | |||
| a1bd258a7b | |||
| 39a9c54589 | |||
| 5f68370e07 | |||
| dae2de703d | |||
| fa19c40868 | |||
| 1df69d259a | |||
| 90dda0ca68 | |||
| e2d138cac6 | |||
| 15f968d5f8 | |||
| 4a3b68de8f | |||
| f6aec7f763 | |||
| 212b6c3a0f | |||
| 820256d451 | |||
| 3aba538db3 | |||
| 4820cf8cac | |||
| c289effba0 | |||
| 3edccf496a | |||
| 97b4171b3e | |||
| 4a073a7ba5 | |||
| 9f275d57a9 | |||
| d23bbaeb06 | |||
| c64f7a9ec4 | |||
| 214a9df382 | |||
| 90f6620f1e | |||
| f6e8048d9e | |||
| 5afca17d27 | |||
| 2d7f5ae279 | |||
| 458384d658 | |||
| 159b98132d | |||
| 349bb2730a | |||
| c13813348d | |||
| 26e70d6b30 | |||
| f6d3b50b08 | |||
| 5b1fdb7b37 | |||
| c701bf279f | |||
| f8f76f6806 | |||
| c4e7c149a4 | |||
| f91edfabbb | |||
| f410004d45 | |||
| 49e238d580 | |||
| 22713d8f89 |
@@ -35,6 +35,8 @@ module.exports = {
|
||||
"files": ["src/**/*.ts"],
|
||||
"extends": ["matrix-org/ts"],
|
||||
"rules": {
|
||||
// We're okay being explicit at the moment
|
||||
"@typescript-eslint/no-empty-interface": "off",
|
||||
// While we're converting to ts we make heavy use of this
|
||||
"@typescript-eslint/no-explicit-any": "off",
|
||||
"quotes": "off",
|
||||
|
||||
+150
@@ -1,3 +1,153 @@
|
||||
Changes in [9.9.0-rc.1](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v9.9.0-rc.1) (2021-03-10)
|
||||
==========================================================================================================
|
||||
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v9.8.0...v9.9.0-rc.1)
|
||||
|
||||
* Remove detailed Olm session logging
|
||||
[\#1638](https://github.com/matrix-org/matrix-js-sdk/pull/1638)
|
||||
* Add space summary suggested only param
|
||||
[\#1637](https://github.com/matrix-org/matrix-js-sdk/pull/1637)
|
||||
* Check TURN servers periodically, and at start of calls
|
||||
[\#1634](https://github.com/matrix-org/matrix-js-sdk/pull/1634)
|
||||
* Support sending invite reasons
|
||||
[\#1624](https://github.com/matrix-org/matrix-js-sdk/pull/1624)
|
||||
* Bump elliptic from 6.5.3 to 6.5.4
|
||||
[\#1636](https://github.com/matrix-org/matrix-js-sdk/pull/1636)
|
||||
* Add a function to get a room's MXC URI
|
||||
[\#1635](https://github.com/matrix-org/matrix-js-sdk/pull/1635)
|
||||
* Stop streams if the call has ended
|
||||
[\#1633](https://github.com/matrix-org/matrix-js-sdk/pull/1633)
|
||||
* Remove export keyword from global.d.ts
|
||||
[\#1631](https://github.com/matrix-org/matrix-js-sdk/pull/1631)
|
||||
* Fix IndexedDB store creation example
|
||||
[\#1445](https://github.com/matrix-org/matrix-js-sdk/pull/1445)
|
||||
* An attempt to cleanup how constraints are handled in calls
|
||||
[\#1613](https://github.com/matrix-org/matrix-js-sdk/pull/1613)
|
||||
* Extract display name patterns to constants
|
||||
[\#1628](https://github.com/matrix-org/matrix-js-sdk/pull/1628)
|
||||
* Bump pug-code-gen from 2.0.2 to 2.0.3
|
||||
[\#1630](https://github.com/matrix-org/matrix-js-sdk/pull/1630)
|
||||
* Avoid deadlocks when ensuring Olm sessions for devices
|
||||
[\#1627](https://github.com/matrix-org/matrix-js-sdk/pull/1627)
|
||||
* Filter out edits from other senders in history
|
||||
[\#1626](https://github.com/matrix-org/matrix-js-sdk/pull/1626)
|
||||
* Fix ContentHelpers export
|
||||
[\#1618](https://github.com/matrix-org/matrix-js-sdk/pull/1618)
|
||||
* Add logging to in progress Olm sessions
|
||||
[\#1621](https://github.com/matrix-org/matrix-js-sdk/pull/1621)
|
||||
* Don't ignore ICE candidates received before offer/answer
|
||||
[\#1623](https://github.com/matrix-org/matrix-js-sdk/pull/1623)
|
||||
* Better handling of send failures on VoIP events
|
||||
[\#1622](https://github.com/matrix-org/matrix-js-sdk/pull/1622)
|
||||
* Log when turn creds expire
|
||||
[\#1620](https://github.com/matrix-org/matrix-js-sdk/pull/1620)
|
||||
* Initial Spaces [MSC1772] support
|
||||
[\#1563](https://github.com/matrix-org/matrix-js-sdk/pull/1563)
|
||||
* Add logging to crypto store transactions
|
||||
[\#1617](https://github.com/matrix-org/matrix-js-sdk/pull/1617)
|
||||
* Room helper for getting type and checking if it is a space room
|
||||
[\#1610](https://github.com/matrix-org/matrix-js-sdk/pull/1610)
|
||||
|
||||
Changes in [9.8.0](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v9.8.0) (2021-03-01)
|
||||
================================================================================================
|
||||
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v9.8.0-rc.1...v9.8.0)
|
||||
|
||||
* No changes since rc.1
|
||||
|
||||
Changes in [9.8.0-rc.1](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v9.8.0-rc.1) (2021-02-24)
|
||||
==========================================================================================================
|
||||
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v9.7.0...v9.8.0-rc.1)
|
||||
|
||||
* Optimise prefixed logger
|
||||
[\#1615](https://github.com/matrix-org/matrix-js-sdk/pull/1615)
|
||||
* Add debug logs to encryption prep, take 3
|
||||
[\#1614](https://github.com/matrix-org/matrix-js-sdk/pull/1614)
|
||||
* Add functions for upper & lowercase random strings
|
||||
[\#1612](https://github.com/matrix-org/matrix-js-sdk/pull/1612)
|
||||
* Room helpers for invite permissions and join rules
|
||||
[\#1609](https://github.com/matrix-org/matrix-js-sdk/pull/1609)
|
||||
* Fixed wording in "Adding video track with id" log
|
||||
[\#1606](https://github.com/matrix-org/matrix-js-sdk/pull/1606)
|
||||
* Add more debug logs to encryption prep
|
||||
[\#1605](https://github.com/matrix-org/matrix-js-sdk/pull/1605)
|
||||
* Add option to set ice candidate pool size
|
||||
[\#1604](https://github.com/matrix-org/matrix-js-sdk/pull/1604)
|
||||
* Cancel call if no source was selected
|
||||
[\#1601](https://github.com/matrix-org/matrix-js-sdk/pull/1601)
|
||||
|
||||
Changes in [9.7.0](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v9.7.0) (2021-02-16)
|
||||
================================================================================================
|
||||
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v9.7.0-rc.1...v9.7.0)
|
||||
|
||||
* No changes since rc.1
|
||||
|
||||
Changes in [9.7.0-rc.1](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v9.7.0-rc.1) (2021-02-10)
|
||||
==========================================================================================================
|
||||
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v9.6.0...v9.7.0-rc.1)
|
||||
|
||||
* Handle undefined peerconn
|
||||
[\#1600](https://github.com/matrix-org/matrix-js-sdk/pull/1600)
|
||||
* ReEmitter: Don't throw if no error handler is attached
|
||||
[\#1599](https://github.com/matrix-org/matrix-js-sdk/pull/1599)
|
||||
* Convert ReEmitter to TS
|
||||
[\#1598](https://github.com/matrix-org/matrix-js-sdk/pull/1598)
|
||||
* Fix typo in main readme
|
||||
[\#1597](https://github.com/matrix-org/matrix-js-sdk/pull/1597)
|
||||
* Remove rogue plus character
|
||||
[\#1596](https://github.com/matrix-org/matrix-js-sdk/pull/1596)
|
||||
* Fix call ID NaN
|
||||
[\#1595](https://github.com/matrix-org/matrix-js-sdk/pull/1595)
|
||||
* Fix Electron type merging
|
||||
[\#1594](https://github.com/matrix-org/matrix-js-sdk/pull/1594)
|
||||
* Fix browser screen share
|
||||
[\#1593](https://github.com/matrix-org/matrix-js-sdk/pull/1593)
|
||||
* Fix desktop Matrix screen sharing
|
||||
[\#1570](https://github.com/matrix-org/matrix-js-sdk/pull/1570)
|
||||
* Guard against confused server retry times
|
||||
[\#1591](https://github.com/matrix-org/matrix-js-sdk/pull/1591)
|
||||
* Decrypt redaction events
|
||||
[\#1589](https://github.com/matrix-org/matrix-js-sdk/pull/1589)
|
||||
* Fix edge cases with peeking where a room is re-peeked
|
||||
[\#1587](https://github.com/matrix-org/matrix-js-sdk/pull/1587)
|
||||
|
||||
Changes in [9.6.0](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v9.6.0) (2021-02-03)
|
||||
================================================================================================
|
||||
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v9.6.0-rc.1...v9.6.0)
|
||||
|
||||
* [Release] Fix edge cases with peeking where a room is re-peeked
|
||||
[\#1588](https://github.com/matrix-org/matrix-js-sdk/pull/1588)
|
||||
|
||||
Changes in [9.6.0-rc.1](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v9.6.0-rc.1) (2021-01-29)
|
||||
==========================================================================================================
|
||||
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v9.5.1...v9.6.0-rc.1)
|
||||
|
||||
* Add support for getting call stats
|
||||
[\#1584](https://github.com/matrix-org/matrix-js-sdk/pull/1584)
|
||||
* Fix compatibility with v0 calls
|
||||
[\#1583](https://github.com/matrix-org/matrix-js-sdk/pull/1583)
|
||||
* Upgrade deps 2021-01
|
||||
[\#1582](https://github.com/matrix-org/matrix-js-sdk/pull/1582)
|
||||
* Log the call ID when logging that we've received VoIP events
|
||||
[\#1581](https://github.com/matrix-org/matrix-js-sdk/pull/1581)
|
||||
* Fix extra negotiate message in Firefox
|
||||
[\#1579](https://github.com/matrix-org/matrix-js-sdk/pull/1579)
|
||||
* Add debug logs to encryption prep
|
||||
[\#1580](https://github.com/matrix-org/matrix-js-sdk/pull/1580)
|
||||
* Expose getPresence endpoint
|
||||
[\#1578](https://github.com/matrix-org/matrix-js-sdk/pull/1578)
|
||||
* Queue keys for backup even if backup isn't enabled yet
|
||||
[\#1577](https://github.com/matrix-org/matrix-js-sdk/pull/1577)
|
||||
* Stop retrying TURN access when forbidden
|
||||
[\#1576](https://github.com/matrix-org/matrix-js-sdk/pull/1576)
|
||||
* Add DTMF sending support
|
||||
[\#1573](https://github.com/matrix-org/matrix-js-sdk/pull/1573)
|
||||
|
||||
Changes in [9.5.1](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v9.5.1) (2021-01-26)
|
||||
================================================================================================
|
||||
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v9.5.0...v9.5.1)
|
||||
|
||||
* [Release] Fix compatibility with v0 calls
|
||||
[\#1585](https://github.com/matrix-org/matrix-js-sdk/pull/1585)
|
||||
|
||||
Changes in [9.5.0](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v9.5.0) (2021-01-18)
|
||||
================================================================================================
|
||||
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v9.5.0-rc.1...v9.5.0)
|
||||
|
||||
@@ -307,7 +307,7 @@ The SDK supports end-to-end encryption via the Olm and Megolm protocols, using
|
||||
[libolm](https://gitlab.matrix.org/matrix-org/olm). It is left up to the
|
||||
application to make libolm available, via the ``Olm`` global.
|
||||
|
||||
It is also necessry to call ``matrixClient.initCrypto()`` after creating a new
|
||||
It is also necessary to call ``matrixClient.initCrypto()`` after creating a new
|
||||
``MatrixClient`` (but **before** calling ``matrixClient.startClient()``) to
|
||||
initialise the crypto layer.
|
||||
|
||||
|
||||
+14
-14
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "matrix-js-sdk",
|
||||
"version": "9.5.0",
|
||||
"version": "9.9.0-rc.1",
|
||||
"description": "Matrix Client-Server SDK for Javascript",
|
||||
"scripts": {
|
||||
"prepublishOnly": "yarn build",
|
||||
@@ -15,7 +15,7 @@
|
||||
"build:minify-browser": "terser dist/browser-matrix.js --compress --mangle --source-map --output dist/browser-matrix.min.js",
|
||||
"gendoc": "jsdoc -c jsdoc.json -P package.json",
|
||||
"lint": "yarn lint:types && yarn lint:js",
|
||||
"lint:js": "eslint --max-warnings 73 src spec",
|
||||
"lint:js": "eslint --max-warnings 72 src spec",
|
||||
"lint:types": "tsc --noEmit",
|
||||
"test": "jest spec/ --coverage --testEnvironment node",
|
||||
"test:watch": "jest spec/ --coverage --testEnvironment node --watch"
|
||||
@@ -54,22 +54,22 @@
|
||||
"bs58": "^4.0.1",
|
||||
"content-type": "^1.0.4",
|
||||
"loglevel": "^1.7.1",
|
||||
"qs": "^6.9.4",
|
||||
"qs": "^6.9.6",
|
||||
"request": "^2.88.2",
|
||||
"unhomoglyph": "^1.0.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.12.8",
|
||||
"@babel/core": "^7.12.9",
|
||||
"@babel/cli": "^7.12.10",
|
||||
"@babel/core": "^7.12.10",
|
||||
"@babel/plugin-proposal-class-properties": "^7.12.1",
|
||||
"@babel/plugin-proposal-numeric-separator": "^7.12.7",
|
||||
"@babel/plugin-proposal-object-rest-spread": "^7.12.1",
|
||||
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
|
||||
"@babel/plugin-transform-runtime": "^7.12.1",
|
||||
"@babel/preset-env": "^7.12.7",
|
||||
"@babel/plugin-transform-runtime": "^7.12.10",
|
||||
"@babel/preset-env": "^7.12.11",
|
||||
"@babel/preset-typescript": "^7.12.7",
|
||||
"@babel/register": "^7.12.1",
|
||||
"@types/jest": "^26.0.15",
|
||||
"@babel/register": "^7.12.10",
|
||||
"@types/jest": "^26.0.20",
|
||||
"@types/node": "12",
|
||||
"@types/request": "^2.48.5",
|
||||
"babel-eslint": "^10.1.0",
|
||||
@@ -78,20 +78,20 @@
|
||||
"better-docs": "^2.3.2",
|
||||
"browserify": "^17.0.0",
|
||||
"docdash": "^1.2.0",
|
||||
"eslint": "7.14.0",
|
||||
"eslint-config-matrix-org": "^0.1.2",
|
||||
"eslint": "7.18.0",
|
||||
"eslint-config-matrix-org": "^0.2.0",
|
||||
"eslint-plugin-babel": "^5.3.1",
|
||||
"exorcist": "^1.0.1",
|
||||
"fake-indexeddb": "^3.1.2",
|
||||
"jest": "^26.6.3",
|
||||
"jest-localstorage-mock": "^2.4.3",
|
||||
"jest-localstorage-mock": "^2.4.6",
|
||||
"jsdoc": "^3.6.6",
|
||||
"matrix-mock-request": "^1.2.3",
|
||||
"olm": "https://packages.matrix.org/npm/olm/olm-3.2.1.tgz",
|
||||
"rimraf": "^3.0.2",
|
||||
"terser": "^5.5.0",
|
||||
"terser": "^5.5.1",
|
||||
"tsify": "^5.0.2",
|
||||
"typescript": "^4.1.2"
|
||||
"typescript": "^4.1.3"
|
||||
},
|
||||
"jest": {
|
||||
"testEnvironment": "node"
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
Copyright 2021 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { EventEmitter } from "events";
|
||||
import { ReEmitter } from "../../src/ReEmitter";
|
||||
|
||||
const EVENTNAME = "UnknownEntry";
|
||||
|
||||
class EventSource extends EventEmitter {
|
||||
doTheThing() {
|
||||
this.emit(EVENTNAME, "foo", "bar");
|
||||
}
|
||||
|
||||
doAnError() {
|
||||
this.emit('error');
|
||||
}
|
||||
}
|
||||
|
||||
class EventTarget extends EventEmitter {
|
||||
|
||||
}
|
||||
|
||||
describe("ReEmitter", function() {
|
||||
it("Re-Emits events with the same args", function() {
|
||||
const src = new EventSource();
|
||||
const tgt = new EventTarget();
|
||||
|
||||
const handler = jest.fn();
|
||||
tgt.on(EVENTNAME, handler);
|
||||
|
||||
const reEmitter = new ReEmitter(tgt);
|
||||
reEmitter.reEmit(src, [EVENTNAME]);
|
||||
|
||||
src.doTheThing();
|
||||
|
||||
// Args should be the args passed to 'emit' after the event name, and
|
||||
// also the source object of the event which re-emitter adds
|
||||
expect(handler).toHaveBeenCalledWith("foo", "bar", src);
|
||||
});
|
||||
|
||||
it("Doesn't throw if no handler for 'error' event", function() {
|
||||
const src = new EventSource();
|
||||
const tgt = new EventTarget();
|
||||
|
||||
const reEmitter = new ReEmitter(tgt);
|
||||
reEmitter.reEmit(src, ['error']);
|
||||
|
||||
// without the workaround in ReEmitter, this would throw
|
||||
src.doAnError();
|
||||
|
||||
const handler = jest.fn();
|
||||
tgt.on('error', handler);
|
||||
|
||||
src.doAnError();
|
||||
|
||||
// Now we've attached an error handler, it should be called
|
||||
expect(handler).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -190,5 +190,91 @@ describe("OlmDevice", function() {
|
||||
// new session and will have called claimOneTimeKeys
|
||||
expect(count).toBe(2);
|
||||
});
|
||||
|
||||
it("avoids deadlocks when two tasks are ensuring the same devices", async function() {
|
||||
// This test checks whether `ensureOlmSessionsForDevices` properly
|
||||
// handles multiple tasks in flight ensuring some set of devices in
|
||||
// common without deadlocks.
|
||||
|
||||
let claimRequestCount = 0;
|
||||
const baseApis = {
|
||||
claimOneTimeKeys: () => {
|
||||
// simulate a very slow server (.5 seconds to respond)
|
||||
claimRequestCount++;
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(reject, 500);
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
const deviceBobA = DeviceInfo.fromStorage({
|
||||
keys: {
|
||||
"curve25519:BOB-A": "akey",
|
||||
},
|
||||
}, "BOB-A");
|
||||
const deviceBobB = DeviceInfo.fromStorage({
|
||||
keys: {
|
||||
"curve25519:BOB-B": "bkey",
|
||||
},
|
||||
}, "BOB-B");
|
||||
|
||||
// There's no required ordering of devices per user, so here we
|
||||
// create two different orderings so that each task reserves a
|
||||
// device the other task needs before continuing.
|
||||
const devicesByUserAB = {
|
||||
"@bob:example.com": [
|
||||
deviceBobA,
|
||||
deviceBobB,
|
||||
],
|
||||
};
|
||||
const devicesByUserBA = {
|
||||
"@bob:example.com": [
|
||||
deviceBobB,
|
||||
deviceBobA,
|
||||
],
|
||||
};
|
||||
|
||||
function alwaysSucceed(promise) {
|
||||
// swallow any exception thrown by a promise, so that
|
||||
// Promise.all doesn't abort
|
||||
return promise.catch(() => {});
|
||||
}
|
||||
|
||||
const task1 = alwaysSucceed(olmlib.ensureOlmSessionsForDevices(
|
||||
aliceOlmDevice, baseApis, devicesByUserAB,
|
||||
));
|
||||
|
||||
// After a single tick through the first task, it should have
|
||||
// claimed ownership of all devices to avoid deadlocking others.
|
||||
expect(Object.keys(aliceOlmDevice._sessionsInProgress).length).toBe(2);
|
||||
|
||||
const task2 = alwaysSucceed(olmlib.ensureOlmSessionsForDevices(
|
||||
aliceOlmDevice, baseApis, devicesByUserBA,
|
||||
));
|
||||
|
||||
// The second task should not have changed the ownership count, as
|
||||
// it's waiting on the first task.
|
||||
expect(Object.keys(aliceOlmDevice._sessionsInProgress).length).toBe(2);
|
||||
|
||||
// Track the tasks, but don't await them yet.
|
||||
const promises = Promise.all([
|
||||
task1,
|
||||
task2,
|
||||
]);
|
||||
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(resolve, 200);
|
||||
});
|
||||
|
||||
// After .2s, the first task should have made an initial claim request.
|
||||
expect(claimRequestCount).toBe(1);
|
||||
|
||||
await promises;
|
||||
|
||||
// After waiting for both tasks to complete, the first task should
|
||||
// have failed, so the second task should have tried to create a
|
||||
// new session and will have called claimOneTimeKeys
|
||||
expect(claimRequestCount).toBe(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -521,4 +521,19 @@ describe("MatrixClient", function() {
|
||||
xit("should be able to peek into a room using peekInRoom", function(done) {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getPresence", function() {
|
||||
it("should send a presence HTTP GET", function() {
|
||||
httpLookups = [{
|
||||
method: "GET",
|
||||
path: `/presence/${encodeURIComponent(userId)}/status`,
|
||||
data: {
|
||||
"presence": "unavailable",
|
||||
"last_active_ago": 420845,
|
||||
},
|
||||
}];
|
||||
client.getPresence(userId);
|
||||
expect(httpLookups.length).toEqual(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -79,6 +79,7 @@ class MockRTCPeerConnection {
|
||||
return Promise.resolve();
|
||||
}
|
||||
close() {}
|
||||
getStats() { return []; }
|
||||
}
|
||||
|
||||
describe('Call', function() {
|
||||
@@ -122,6 +123,7 @@ describe('Call', function() {
|
||||
// We just stub out sendEvent: we're not interested in testing the client's
|
||||
// event sending code here
|
||||
client.client.sendEvent = () => {};
|
||||
client.httpBackend.when("GET", "/voip/turnServer").respond(200, {});
|
||||
call = new MatrixCall({
|
||||
client: client.client,
|
||||
roomId: '!foo:bar',
|
||||
@@ -138,7 +140,9 @@ describe('Call', function() {
|
||||
});
|
||||
|
||||
it('should ignore candidate events from non-matching party ID', async function() {
|
||||
await call.placeVoiceCall();
|
||||
const callPromise = call.placeVoiceCall();
|
||||
await client.httpBackend.flush();
|
||||
await callPromise;
|
||||
await call.onAnswerReceived({
|
||||
getContent: () => {
|
||||
return {
|
||||
@@ -190,4 +194,64 @@ describe('Call', function() {
|
||||
// Hangup to stop timers
|
||||
call.hangup(CallErrorCode.UserHangup, true);
|
||||
});
|
||||
|
||||
it('should add candidates received before answer if party ID is correct', async function() {
|
||||
const callPromise = call.placeVoiceCall();
|
||||
await client.httpBackend.flush();
|
||||
await callPromise;
|
||||
call.peerConn.addIceCandidate = jest.fn();
|
||||
|
||||
call.onRemoteIceCandidatesReceived({
|
||||
getContent: () => {
|
||||
return {
|
||||
version: 1,
|
||||
call_id: call.callId,
|
||||
party_id: 'the_correct_party_id',
|
||||
candidates: [
|
||||
{
|
||||
candidate: 'the_correct_candidate',
|
||||
sdpMid: '',
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
call.onRemoteIceCandidatesReceived({
|
||||
getContent: () => {
|
||||
return {
|
||||
version: 1,
|
||||
call_id: call.callId,
|
||||
party_id: 'some_other_party_id',
|
||||
candidates: [
|
||||
{
|
||||
candidate: 'the_wrong_candidate',
|
||||
sdpMid: '',
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
expect(call.peerConn.addIceCandidate.mock.calls.length).toBe(0);
|
||||
|
||||
await call.onAnswerReceived({
|
||||
getContent: () => {
|
||||
return {
|
||||
version: 1,
|
||||
call_id: call.callId,
|
||||
party_id: 'the_correct_party_id',
|
||||
answer: {
|
||||
sdp: DUMMY_SDP,
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
expect(call.peerConn.addIceCandidate.mock.calls.length).toBe(1);
|
||||
expect(call.peerConn.addIceCandidate).toHaveBeenCalledWith({
|
||||
candidate: 'the_correct_candidate',
|
||||
sdpMid: '',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -36,6 +36,10 @@ export enum EventType {
|
||||
*/
|
||||
RoomAliases = "m.room.aliases", // deprecated https://matrix.org/docs/spec/client_server/r0.6.1#historical-events
|
||||
|
||||
// Spaces MSC1772
|
||||
SpaceChild = "org.matrix.msc1772.space.child",
|
||||
SpaceParent = "org.matrix.msc1772.space.parent",
|
||||
|
||||
// Room timeline events
|
||||
RoomRedaction = "m.room.redaction",
|
||||
RoomMessage = "m.room.message",
|
||||
@@ -87,3 +91,9 @@ export enum MsgType {
|
||||
Location = "m.location",
|
||||
Video = "m.video",
|
||||
}
|
||||
|
||||
export const RoomCreateTypeField = "org.matrix.msc1772.type"; // Spaces MSC1772
|
||||
|
||||
export enum RoomType {
|
||||
Space = "org.matrix.msc1772.space", // Spaces MSC1772
|
||||
}
|
||||
|
||||
Vendored
+40
-1
@@ -26,10 +26,49 @@ declare global {
|
||||
}
|
||||
}
|
||||
|
||||
interface Window {
|
||||
electron?: Electron;
|
||||
}
|
||||
|
||||
interface Electron {
|
||||
getDesktopCapturerSources(options: GetSourcesOptions): Promise<Array<DesktopCapturerSource>>;
|
||||
}
|
||||
|
||||
interface MediaDevices {
|
||||
// This is experimental and types don't know about it yet
|
||||
// https://github.com/microsoft/TypeScript/issues/33232
|
||||
getDisplayMedia(constraints: MediaStreamConstraints): Promise<MediaStream>;
|
||||
getDisplayMedia(constraints: MediaStreamConstraints | DesktopCapturerConstraints): Promise<MediaStream>;
|
||||
getUserMedia(constraints: MediaStreamConstraints | DesktopCapturerConstraints): Promise<MediaStream>;
|
||||
}
|
||||
|
||||
interface DesktopCapturerConstraints {
|
||||
audio: boolean | {
|
||||
mandatory: {
|
||||
chromeMediaSource: string;
|
||||
chromeMediaSourceId: string;
|
||||
};
|
||||
};
|
||||
video: boolean | {
|
||||
mandatory: {
|
||||
chromeMediaSource: string;
|
||||
chromeMediaSourceId: string;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
interface DesktopCapturerSource {
|
||||
id: string;
|
||||
name: string;
|
||||
thumbnailURL: string;
|
||||
}
|
||||
|
||||
interface GetSourcesOptions {
|
||||
types: Array<string>;
|
||||
thumbnailSize?: {
|
||||
height: number;
|
||||
width: number;
|
||||
};
|
||||
fetchWindowIcons?: boolean;
|
||||
}
|
||||
|
||||
interface HTMLAudioElement {
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
Copyright 2015, 2016 OpenMarket Ltd
|
||||
Copyright 2017 Vector Creations Ltd
|
||||
Copyright 2017 New Vector Ltd
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @module
|
||||
*/
|
||||
|
||||
export class ReEmitter {
|
||||
constructor(target) {
|
||||
this.target = target;
|
||||
|
||||
// We keep one bound event handler for each event name so we know
|
||||
// what event is arriving
|
||||
this.boundHandlers = {};
|
||||
}
|
||||
|
||||
_handleEvent(eventName, ...args) {
|
||||
this.target.emit(eventName, ...args);
|
||||
}
|
||||
|
||||
reEmit(source, eventNames) {
|
||||
// We include the source as the last argument for event handlers which may need it,
|
||||
// such as read receipt listeners on the client class which won't have the context
|
||||
// of the room.
|
||||
const forSource = (handler, ...args) => {
|
||||
handler(...args, source);
|
||||
};
|
||||
for (const eventName of eventNames) {
|
||||
if (this.boundHandlers[eventName] === undefined) {
|
||||
this.boundHandlers[eventName] = this._handleEvent.bind(this, eventName);
|
||||
}
|
||||
|
||||
const boundHandler = forSource.bind(this, this.boundHandlers[eventName]);
|
||||
source.on(eventName, boundHandler);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
Copyright 2015, 2016 OpenMarket Ltd
|
||||
Copyright 2017 Vector Creations Ltd
|
||||
Copyright 2017 New Vector Ltd
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { EventEmitter } from "events";
|
||||
|
||||
export class ReEmitter {
|
||||
private target: EventEmitter;
|
||||
|
||||
constructor(target: EventEmitter) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
reEmit(source: EventEmitter, eventNames: string[]) {
|
||||
for (const eventName of eventNames) {
|
||||
// We include the source as the last argument for event handlers which may need it,
|
||||
// such as read receipt listeners on the client class which won't have the context
|
||||
// of the room.
|
||||
const forSource = (...args) => {
|
||||
// EventEmitter special cases 'error' to make the emit function throw if no
|
||||
// handler is attached, which sort of makes sense for making sure that something
|
||||
// handles an error, but for re-emitting, there could be a listener on the original
|
||||
// source object so the test doesn't really work. We *could* try to replicate the
|
||||
// same logic and throw if there is no listener on either the source or the target,
|
||||
// but this behaviour is fairly undesireable for us anyway: the main place we throw
|
||||
// 'error' events is for calls, where error events are usually emitted some time
|
||||
// later by a different part of the code where 'emit' throwing because the app hasn't
|
||||
// added an error handler isn't terribly helpful. (A better fix in retrospect may
|
||||
// have been to just avoid using the event name 'error', but backwards compat...)
|
||||
if (eventName === 'error' && this.target.listenerCount('error') === 0) return;
|
||||
this.target.emit(eventName, ...args, source);
|
||||
};
|
||||
source.on(eventName, forSource);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2374,3 +2374,35 @@ MatrixBaseApis.prototype.reportEvent = function(roomId, eventId, score, reason)
|
||||
return this._http.authedRequest(undefined, "POST", path, null, {score, reason});
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetches or paginates a summary of a space as defined by MSC2946
|
||||
* @param {string} roomId The ID of the space-room to use as the root of the summary.
|
||||
* @param {number?} maxRoomsPerSpace The maximum number of rooms to return per subspace.
|
||||
* @param {boolean?} suggestedOnly Whether to only return rooms with suggested=true.
|
||||
* @param {boolean?} autoJoinOnly Whether to only return rooms with auto_join=true.
|
||||
* @param {number?} limit The maximum number of rooms to return in total.
|
||||
* @param {string?} batch The opaque token to paginate a previous summary request.
|
||||
* @returns {Promise} the response, with next_batch, rooms, events fields.
|
||||
*/
|
||||
MatrixBaseApis.prototype.getSpaceSummary = function(
|
||||
roomId,
|
||||
maxRoomsPerSpace,
|
||||
suggestedOnly,
|
||||
autoJoinOnly,
|
||||
limit,
|
||||
batch,
|
||||
) {
|
||||
const path = utils.encodeUri("/rooms/$roomId/spaces", {
|
||||
$roomId: roomId,
|
||||
});
|
||||
|
||||
return this._http.authedRequest(undefined, "POST", path, null, {
|
||||
max_rooms_per_space: maxRoomsPerSpace,
|
||||
suggested_only: suggestedOnly,
|
||||
auto_join_only: autoJoinOnly,
|
||||
limit,
|
||||
batch,
|
||||
}, {
|
||||
prefix: "/_matrix/client/unstable/org.matrix.msc2946",
|
||||
});
|
||||
};
|
||||
|
||||
+88
-34
@@ -61,6 +61,7 @@ import {DEHYDRATION_ALGORITHM} from "./crypto/dehydration";
|
||||
const SCROLLBACK_DELAY_MS = 3000;
|
||||
export const CRYPTO_ENABLED = isCryptoAvailable();
|
||||
const CAPABILITIES_CACHE_MS = 21600000; // 6 hours - an arbitrary value
|
||||
const TURN_CHECK_INTERVAL = 10 * 60 * 1000; // poll for turn credentials every 10 minutes
|
||||
|
||||
function keysFromRecoverySession(sessions, decryptionKey, roomId) {
|
||||
const keys = [];
|
||||
@@ -180,6 +181,11 @@ function keyFromRecoverySession(session, decryptionKey) {
|
||||
* @param {boolean} [opts.forceTURN]
|
||||
* Optional. Whether relaying calls through a TURN server should be forced.
|
||||
*
|
||||
* * @param {boolean} [opts.iceCandidatePoolSize]
|
||||
* Optional. Up to this many ICE candidates will be gathered when an incoming call arrives.
|
||||
* Gathering does not send data to the caller, but will communicate with the configured TURN
|
||||
* server. Default 0.
|
||||
*
|
||||
* @param {boolean} [opts.supportsCallTransfer]
|
||||
* Optional. True to advertise support for call transfers to other parties on Matrix calls.
|
||||
*
|
||||
@@ -367,6 +373,7 @@ export function MatrixClient(opts) {
|
||||
this._cryptoCallbacks = opts.cryptoCallbacks || {};
|
||||
|
||||
this._forceTURN = opts.forceTURN || false;
|
||||
this._iceCandidatePoolSize = opts.iceCandidatePoolSize === undefined ? 0 : opts.iceCandidatePoolSize;
|
||||
this._supportsCallTransfer = opts.supportsCallTransfer || false;
|
||||
this._fallbackICEServerAllowed = opts.fallbackICEServerAllowed || false;
|
||||
|
||||
@@ -387,6 +394,10 @@ export function MatrixClient(opts) {
|
||||
this._clientWellKnown = undefined;
|
||||
this._clientWellKnownPromise = undefined;
|
||||
|
||||
this._turnServers = [];
|
||||
this._turnServersExpiry = 0;
|
||||
this._checkTurnServersIntervalID = null;
|
||||
|
||||
// The SDK doesn't really provide a clean way for events to recalculate the push
|
||||
// actions for themselves, so we have to kinda help them out when they are encrypted.
|
||||
// We do this so that push rules are correctly executed on events in their decrypted
|
||||
@@ -3441,11 +3452,12 @@ MatrixClient.prototype.getRoomUpgradeHistory = function(roomId, verifyLinks=fals
|
||||
* @param {string} roomId
|
||||
* @param {string} userId
|
||||
* @param {module:client.callback} callback Optional.
|
||||
* @param {string} reason Optional.
|
||||
* @return {Promise} Resolves: TODO
|
||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||
*/
|
||||
MatrixClient.prototype.invite = function(roomId, userId, callback) {
|
||||
return _membershipChange(this, roomId, userId, "invite", undefined,
|
||||
MatrixClient.prototype.invite = function(roomId, userId, callback, reason) {
|
||||
return _membershipChange(this, roomId, userId, "invite", reason,
|
||||
callback);
|
||||
};
|
||||
|
||||
@@ -3841,6 +3853,19 @@ MatrixClient.prototype.setPresence = function(opts, callback) {
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} userId The user to get presence for
|
||||
* @param {module:client.callback} callback Optional.
|
||||
* @return {Promise} Resolves: The presence state for this user.
|
||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||
*/
|
||||
MatrixClient.prototype.getPresence = function(userId, callback) {
|
||||
const path = utils.encodeUri("/presence/$userId/status", {
|
||||
$userId: userId,
|
||||
});
|
||||
|
||||
return this._http.authedRequest(callback, "GET", path, undefined, undefined);
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve older messages from the given room and put them in the timeline.
|
||||
@@ -4923,6 +4948,57 @@ MatrixClient.prototype.getTurnServers = function() {
|
||||
return this._turnServers || [];
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the unix timestamp (in seconds) at which the current
|
||||
* TURN credentials (from getTurnServers) expire
|
||||
* @return {number} The expiry timestamp, in seconds, or null if no credentials
|
||||
*/
|
||||
MatrixClient.prototype.getTurnServersExpiry = function() {
|
||||
return this._turnServersExpiry;
|
||||
};
|
||||
|
||||
MatrixClient.prototype._checkTurnServers = async function() {
|
||||
if (!this._supportsVoip) {
|
||||
return;
|
||||
}
|
||||
|
||||
let credentialsGood = false;
|
||||
const remainingTime = this._turnServersExpiry - Date.now();
|
||||
if (remainingTime > TURN_CHECK_INTERVAL) {
|
||||
logger.debug("TURN creds are valid for another " + remainingTime + " ms: not fetching new ones.");
|
||||
credentialsGood = true;
|
||||
} else {
|
||||
logger.debug("Fetching new TURN credentials");
|
||||
try {
|
||||
const res = await this.turnServer();
|
||||
if (res.uris) {
|
||||
logger.log("Got TURN URIs: " + res.uris + " refresh in " + res.ttl + " secs");
|
||||
// map the response to a format that can be fed to RTCPeerConnection
|
||||
const servers = {
|
||||
urls: res.uris,
|
||||
username: res.username,
|
||||
credential: res.password,
|
||||
};
|
||||
this._turnServers = [servers];
|
||||
// The TTL is in seconds but we work in ms
|
||||
this._turnServersExpiry = Date.now() + (res.ttl * 1000);
|
||||
credentialsGood = true;
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error("Failed to get TURN URIs", err);
|
||||
// If we get a 403, there's no point in looping forever.
|
||||
if (err.httpStatus === 403) {
|
||||
logger.info("TURN access unavailable for this account: stopping credentials checks");
|
||||
if (this._checkTurnServersIntervalID !== null) global.clearInterval(this._checkTurnServersIntervalID);
|
||||
this._checkTurnServersIntervalID = null;
|
||||
}
|
||||
}
|
||||
// otherwise, if we failed for whatever reason, try again the next time we're called.
|
||||
}
|
||||
|
||||
return credentialsGood;
|
||||
};
|
||||
|
||||
/**
|
||||
* Set whether to allow a fallback ICE server should be used for negotiating a
|
||||
* WebRTC connection if the homeserver doesn't provide any servers. Defaults to
|
||||
@@ -5075,7 +5151,12 @@ MatrixClient.prototype.startClient = async function(opts) {
|
||||
}
|
||||
|
||||
// periodically poll for turn servers if we support voip
|
||||
checkTurnServers(this);
|
||||
if (this._supportsVoip) {
|
||||
this._checkTurnServersIntervalID = setInterval(() => {
|
||||
this._checkTurnServers();
|
||||
}, TURN_CHECK_INTERVAL);
|
||||
this._checkTurnServers();
|
||||
}
|
||||
|
||||
if (this._syncApi) {
|
||||
// This shouldn't happen since we thought the client was not running
|
||||
@@ -5187,7 +5268,7 @@ MatrixClient.prototype.stopClient = function() {
|
||||
this._callEventHandler = null;
|
||||
}
|
||||
|
||||
global.clearTimeout(this._checkTurnServersTimeoutID);
|
||||
global.clearInterval(this._checkTurnServersIntervalID);
|
||||
if (this._clientWellKnownIntervalID !== undefined) {
|
||||
global.clearInterval(this._clientWellKnownIntervalID);
|
||||
}
|
||||
@@ -5394,6 +5475,9 @@ async function(roomId, eventId, relationType, eventType, opts = {}) {
|
||||
}));
|
||||
events = events.filter(e => e.getType() === eventType);
|
||||
}
|
||||
if (originalEvent && relationType === "m.replace") {
|
||||
events = events.filter(e => e.getSender() === originalEvent.getSender());
|
||||
}
|
||||
return {
|
||||
originalEvent,
|
||||
events,
|
||||
@@ -5401,36 +5485,6 @@ async function(roomId, eventId, relationType, eventType, opts = {}) {
|
||||
};
|
||||
};
|
||||
|
||||
function checkTurnServers(client) {
|
||||
if (!client._supportsVoip) {
|
||||
return;
|
||||
}
|
||||
|
||||
client.turnServer().then(function(res) {
|
||||
if (res.uris) {
|
||||
logger.log("Got TURN URIs: " + res.uris + " refresh in " +
|
||||
res.ttl + " secs");
|
||||
// map the response to a format that can be fed to
|
||||
// RTCPeerConnection
|
||||
const servers = {
|
||||
urls: res.uris,
|
||||
username: res.username,
|
||||
credential: res.password,
|
||||
};
|
||||
client._turnServers = [servers];
|
||||
// re-fetch when we're about to reach the TTL
|
||||
client._checkTurnServersTimeoutID = setTimeout(() => {
|
||||
checkTurnServers(client);
|
||||
}, (res.ttl || (60 * 60)) * 1000 * 0.9);
|
||||
}
|
||||
}, function(err) {
|
||||
logger.error("Failed to get TURN URIs");
|
||||
client._checkTurnServersTimeoutID = setTimeout(function() {
|
||||
checkTurnServers(client);
|
||||
}, 60000);
|
||||
});
|
||||
}
|
||||
|
||||
function _reject(callback, reject, err) {
|
||||
if (callback) {
|
||||
callback(err);
|
||||
|
||||
+24
-5
@@ -545,6 +545,7 @@ OlmDevice.prototype.createOutboundSession = async function(
|
||||
}
|
||||
});
|
||||
},
|
||||
logger.withPrefix("[createOutboundSession]"),
|
||||
);
|
||||
return newSessionId;
|
||||
};
|
||||
@@ -605,6 +606,7 @@ OlmDevice.prototype.createInboundSession = async function(
|
||||
}
|
||||
});
|
||||
},
|
||||
logger.withPrefix("[createInboundSession]"),
|
||||
);
|
||||
|
||||
return result;
|
||||
@@ -619,8 +621,10 @@ OlmDevice.prototype.createInboundSession = async function(
|
||||
* @return {Promise<string[]>} a list of known session ids for the device
|
||||
*/
|
||||
OlmDevice.prototype.getSessionIdsForDevice = async function(theirDeviceIdentityKey) {
|
||||
const log = logger.withPrefix("[getSessionIdsForDevice]");
|
||||
|
||||
if (this._sessionsInProgress[theirDeviceIdentityKey]) {
|
||||
logger.log("waiting for olm session to be created");
|
||||
log.debug(`Waiting for Olm session for ${theirDeviceIdentityKey} to be created`);
|
||||
try {
|
||||
await this._sessionsInProgress[theirDeviceIdentityKey];
|
||||
} catch (e) {
|
||||
@@ -638,6 +642,7 @@ OlmDevice.prototype.getSessionIdsForDevice = async function(theirDeviceIdentityK
|
||||
},
|
||||
);
|
||||
},
|
||||
log,
|
||||
);
|
||||
|
||||
return sessionIds;
|
||||
@@ -651,13 +656,14 @@ OlmDevice.prototype.getSessionIdsForDevice = async function(theirDeviceIdentityK
|
||||
* @param {boolean} nowait Don't wait for an in-progress session to complete.
|
||||
* This should only be set to true of the calling function is the function
|
||||
* that marked the session as being in-progress.
|
||||
* @param {Logger} [log] A possibly customised log
|
||||
* @return {Promise<?string>} session id, or null if no established session
|
||||
*/
|
||||
OlmDevice.prototype.getSessionIdForDevice = async function(
|
||||
theirDeviceIdentityKey, nowait,
|
||||
theirDeviceIdentityKey, nowait, log,
|
||||
) {
|
||||
const sessionInfos = await this.getSessionInfoForDevice(
|
||||
theirDeviceIdentityKey, nowait,
|
||||
theirDeviceIdentityKey, nowait, log,
|
||||
);
|
||||
|
||||
if (sessionInfos.length === 0) {
|
||||
@@ -697,11 +703,16 @@ OlmDevice.prototype.getSessionIdForDevice = async function(
|
||||
* @param {boolean} nowait Don't wait for an in-progress session to complete.
|
||||
* This should only be set to true of the calling function is the function
|
||||
* that marked the session as being in-progress.
|
||||
* @param {Logger} [log] A possibly customised log
|
||||
* @return {Array.<{sessionId: string, hasReceivedMessage: Boolean}>}
|
||||
*/
|
||||
OlmDevice.prototype.getSessionInfoForDevice = async function(deviceIdentityKey, nowait) {
|
||||
OlmDevice.prototype.getSessionInfoForDevice = async function(
|
||||
deviceIdentityKey, nowait, log = logger,
|
||||
) {
|
||||
log = log.withPrefix("[getSessionInfoForDevice]");
|
||||
|
||||
if (this._sessionsInProgress[deviceIdentityKey] && !nowait) {
|
||||
logger.log("waiting for olm session to be created");
|
||||
log.debug(`Waiting for Olm session for ${deviceIdentityKey} to be created`);
|
||||
try {
|
||||
await this._sessionsInProgress[deviceIdentityKey];
|
||||
} catch (e) {
|
||||
@@ -727,6 +738,7 @@ OlmDevice.prototype.getSessionInfoForDevice = async function(deviceIdentityKey,
|
||||
}
|
||||
});
|
||||
},
|
||||
log,
|
||||
);
|
||||
|
||||
return info;
|
||||
@@ -761,6 +773,7 @@ OlmDevice.prototype.encryptMessage = async function(
|
||||
this._saveSession(theirDeviceIdentityKey, sessionInfo, txn);
|
||||
});
|
||||
},
|
||||
logger.withPrefix("[encryptMessage]"),
|
||||
);
|
||||
return res;
|
||||
};
|
||||
@@ -794,6 +807,7 @@ OlmDevice.prototype.decryptMessage = async function(
|
||||
this._saveSession(theirDeviceIdentityKey, sessionInfo, txn);
|
||||
});
|
||||
},
|
||||
logger.withPrefix("[decryptMessage]"),
|
||||
);
|
||||
return payloadString;
|
||||
};
|
||||
@@ -825,6 +839,7 @@ OlmDevice.prototype.matchesSession = async function(
|
||||
matches = sessionInfo.session.matches_inbound(ciphertext);
|
||||
});
|
||||
},
|
||||
logger.withPrefix("[matchesSession]"),
|
||||
);
|
||||
return matches;
|
||||
};
|
||||
@@ -1095,6 +1110,7 @@ OlmDevice.prototype.addInboundGroupSession = async function(
|
||||
},
|
||||
);
|
||||
},
|
||||
logger.withPrefix("[addInboundGroupSession]"),
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1265,6 +1281,7 @@ OlmDevice.prototype.decryptGroupMessage = async function(
|
||||
},
|
||||
);
|
||||
},
|
||||
logger.withPrefix("[decryptGroupMessage]"),
|
||||
);
|
||||
|
||||
if (error) {
|
||||
@@ -1310,6 +1327,7 @@ OlmDevice.prototype.hasInboundSessionKeys = async function(roomId, senderKey, se
|
||||
},
|
||||
);
|
||||
},
|
||||
logger.withPrefix("[hasInboundSessionKeys]"),
|
||||
);
|
||||
|
||||
return result;
|
||||
@@ -1369,6 +1387,7 @@ OlmDevice.prototype.getInboundGroupSessionKey = async function(
|
||||
},
|
||||
);
|
||||
},
|
||||
logger.withPrefix("[getInboundGroupSessionKey]"),
|
||||
);
|
||||
|
||||
return result;
|
||||
|
||||
@@ -264,11 +264,14 @@ MegolmEncryption.prototype._ensureOutboundSession = async function(
|
||||
await Promise.all([
|
||||
(async () => {
|
||||
// share keys with devices that we already have a session for
|
||||
logger.debug(`Sharing keys with existing Olm sessions in ${this._roomId}`);
|
||||
await this._shareKeyWithOlmSessions(
|
||||
session, key, payload, olmSessions,
|
||||
);
|
||||
logger.debug(`Shared keys with existing Olm sessions in ${this._roomId}`);
|
||||
})(),
|
||||
(async () => {
|
||||
logger.debug(`Sharing keys (start phase 1) with new Olm sessions in ${this._roomId}`);
|
||||
const errorDevices = [];
|
||||
|
||||
// meanwhile, establish olm sessions for devices that we don't
|
||||
@@ -282,6 +285,7 @@ MegolmEncryption.prototype._ensureOutboundSession = async function(
|
||||
session, key, payload, devicesWithoutSession, errorDevices,
|
||||
singleOlmCreationPhase ? 10000 : 2000, failedServers,
|
||||
);
|
||||
logger.debug(`Shared keys (end phase 1) with new Olm sessions in ${this._roomId}`);
|
||||
|
||||
if (!singleOlmCreationPhase && (Date.now() - start < 10000)) {
|
||||
// perform the second phase of olm session creation if requested,
|
||||
@@ -310,19 +314,24 @@ MegolmEncryption.prototype._ensureOutboundSession = async function(
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug(`Sharing keys (start phase 2) with new Olm sessions in ${this._roomId}`);
|
||||
await this._shareKeyWithDevices(
|
||||
session, key, payload, retryDevices, failedDevices, 30000,
|
||||
);
|
||||
logger.debug(`Shared keys (end phase 2) with new Olm sessions in ${this._roomId}`);
|
||||
|
||||
await this._notifyFailedOlmDevices(session, key, failedDevices);
|
||||
})();
|
||||
} else {
|
||||
await this._notifyFailedOlmDevices(session, key, errorDevices);
|
||||
}
|
||||
logger.debug(`Shared keys (all phases done) with new Olm sessions in ${this._roomId}`);
|
||||
})(),
|
||||
(async () => {
|
||||
logger.debug(`Notifying blocked devices in ${this._roomId}`);
|
||||
// also, notify blocked devices that they're blocked
|
||||
const blockedMap = {};
|
||||
let blockedCount = 0;
|
||||
for (const [userId, userBlockedDevices] of Object.entries(blocked)) {
|
||||
for (const [deviceId, device] of Object.entries(userBlockedDevices)) {
|
||||
if (
|
||||
@@ -330,12 +339,14 @@ MegolmEncryption.prototype._ensureOutboundSession = async function(
|
||||
session.blockedDevicesNotified[userId][deviceId] === undefined
|
||||
) {
|
||||
blockedMap[userId] = blockedMap[userId] || {};
|
||||
blockedMap[userId][deviceId] = {device};
|
||||
blockedMap[userId][deviceId] = { device };
|
||||
blockedCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await this._notifyBlockedDevices(session, blockedMap);
|
||||
logger.debug(`Notified ${blockedCount} blocked devices in ${this._roomId}`);
|
||||
})(),
|
||||
]);
|
||||
};
|
||||
@@ -348,6 +359,11 @@ MegolmEncryption.prototype._ensureOutboundSession = async function(
|
||||
// first wait for the previous share to complete
|
||||
const prom = this._setupPromise.then(prepareSession);
|
||||
|
||||
// Ensure any failures are logged for debugging
|
||||
prom.catch(e => {
|
||||
logger.error(`Failed to ensure outbound session in ${this._roomId}`, e);
|
||||
});
|
||||
|
||||
// _setupPromise resolves to `session` whether or not the share succeeds
|
||||
this._setupPromise = prom.then(returnSession, returnSession);
|
||||
|
||||
@@ -369,17 +385,11 @@ MegolmEncryption.prototype._prepareNewSession = async function() {
|
||||
key.key, {ed25519: this._olmDevice.deviceEd25519Key},
|
||||
);
|
||||
|
||||
if (this._crypto.backupInfo) {
|
||||
// don't wait for it to complete
|
||||
this._crypto.backupGroupSession(
|
||||
this._roomId, this._olmDevice.deviceCurve25519Key, [],
|
||||
sessionId, key.key,
|
||||
).catch((e) => {
|
||||
// This throws if the upload failed, but this is fine
|
||||
// since it will have written it to the db and will retry.
|
||||
logger.log("Failed to back up megolm session", e);
|
||||
});
|
||||
}
|
||||
// don't wait for it to complete
|
||||
this._crypto.backupGroupSession(
|
||||
this._roomId, this._olmDevice.deviceCurve25519Key, [],
|
||||
sessionId, key.key,
|
||||
);
|
||||
|
||||
return new OutboundSessionInfo(sessionId);
|
||||
};
|
||||
@@ -723,13 +733,18 @@ MegolmEncryption.prototype.reshareKeyWithDevice = async function(
|
||||
MegolmEncryption.prototype._shareKeyWithDevices = async function(
|
||||
session, key, payload, devicesByUser, errorDevices, otkTimeout, failedServers,
|
||||
) {
|
||||
logger.debug(`Ensuring Olm sessions for devices in ${this._roomId}`);
|
||||
const devicemap = await olmlib.ensureOlmSessionsForDevices(
|
||||
this._olmDevice, this._baseApis, devicesByUser, otkTimeout, failedServers,
|
||||
logger.withPrefix(`[${this._roomId}]`),
|
||||
);
|
||||
logger.debug(`Ensured Olm sessions for devices in ${this._roomId}`);
|
||||
|
||||
this._getDevicesWithoutSessions(devicemap, devicesByUser, errorDevices);
|
||||
|
||||
logger.debug(`Sharing keys with Olm sessions in ${this._roomId}`);
|
||||
await this._shareKeyWithOlmSessions(session, key, payload, devicemap);
|
||||
logger.debug(`Shared keys with Olm sessions in ${this._roomId}`);
|
||||
};
|
||||
|
||||
MegolmEncryption.prototype._shareKeyWithOlmSessions = async function(
|
||||
@@ -738,16 +753,17 @@ MegolmEncryption.prototype._shareKeyWithOlmSessions = async function(
|
||||
const userDeviceMaps = this._splitDevices(devicemap);
|
||||
|
||||
for (let i = 0; i < userDeviceMaps.length; i++) {
|
||||
const taskDetail =
|
||||
`megolm keys for ${session.sessionId} ` +
|
||||
`in ${this._roomId} (slice ${i + 1}/${userDeviceMaps.length})`;
|
||||
try {
|
||||
logger.debug(`Sharing ${taskDetail}`);
|
||||
await this._encryptAndSendKeysToDevices(
|
||||
session, key.chain_index, userDeviceMaps[i], payload,
|
||||
);
|
||||
logger.log(`Completed megolm keyshare for ${session.sessionId} `
|
||||
+ `in ${this._roomId} (slice ${i + 1}/${userDeviceMaps.length})`);
|
||||
logger.debug(`Shared ${taskDetail}`);
|
||||
} catch (e) {
|
||||
logger.log(`megolm keyshare for ${session.sessionId} in ${this._roomId} `
|
||||
+ `(slice ${i + 1}/${userDeviceMaps.length}) failed`);
|
||||
|
||||
logger.error(`Failed to share ${taskDetail}`);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
@@ -766,6 +782,11 @@ MegolmEncryption.prototype._shareKeyWithOlmSessions = async function(
|
||||
MegolmEncryption.prototype._notifyFailedOlmDevices = async function(
|
||||
session, key, failedDevices,
|
||||
) {
|
||||
logger.debug(
|
||||
`Notifying ${failedDevices.length} devices we failed to ` +
|
||||
`create Olm sessions in ${this._roomId}`,
|
||||
);
|
||||
|
||||
// mark the devices that failed as "handled" because we don't want to try
|
||||
// to claim a one-time-key for dead devices on every message.
|
||||
for (const {userId, deviceInfo} of failedDevices) {
|
||||
@@ -780,6 +801,10 @@ MegolmEncryption.prototype._notifyFailedOlmDevices = async function(
|
||||
await this._olmDevice.filterOutNotifiedErrorDevices(
|
||||
failedDevices,
|
||||
);
|
||||
logger.debug(
|
||||
`Filtered down to ${filteredFailedDevices.length} error devices ` +
|
||||
`in ${this._roomId}`,
|
||||
);
|
||||
const blockedMap = {};
|
||||
for (const {userId, deviceInfo} of filteredFailedDevices) {
|
||||
blockedMap[userId] = blockedMap[userId] || {};
|
||||
@@ -797,6 +822,10 @@ MegolmEncryption.prototype._notifyFailedOlmDevices = async function(
|
||||
|
||||
// send the notifications
|
||||
await this._notifyBlockedDevices(session, blockedMap);
|
||||
logger.debug(
|
||||
`Notified ${filteredFailedDevices.length} devices we failed to ` +
|
||||
`create Olm sessions in ${this._roomId}`,
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -846,24 +875,41 @@ MegolmEncryption.prototype.prepareToEncrypt = function(room) {
|
||||
// We're already preparing something, so don't do anything else.
|
||||
// FIXME: check if we need to restart
|
||||
// (https://github.com/matrix-org/matrix-js-sdk/issues/1255)
|
||||
const elapsedTime = Date.now() - this.encryptionPreparationMetadata.startTime;
|
||||
logger.debug(
|
||||
`Already started preparing to encrypt for ${this._roomId} ` +
|
||||
`${elapsedTime} ms ago, skipping`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
logger.debug(`Preparing to encrypt events for ${this._roomId}`);
|
||||
|
||||
this.encryptionPreparationMetadata = {
|
||||
startTime: Date.now(),
|
||||
};
|
||||
this.encryptionPreparation = (async () => {
|
||||
const [devicesInRoom, blocked] = await this._getDevicesInRoom(room);
|
||||
try {
|
||||
logger.debug(`Getting devices in ${this._roomId}`);
|
||||
const [devicesInRoom, blocked] = await this._getDevicesInRoom(room);
|
||||
|
||||
if (this._crypto.getGlobalErrorOnUnknownDevices()) {
|
||||
// Drop unknown devices for now. When the message gets sent, we'll
|
||||
// throw an error, but we'll still be prepared to send to the known
|
||||
// devices.
|
||||
this._removeUnknownDevices(devicesInRoom);
|
||||
if (this._crypto.getGlobalErrorOnUnknownDevices()) {
|
||||
// Drop unknown devices for now. When the message gets sent, we'll
|
||||
// throw an error, but we'll still be prepared to send to the known
|
||||
// devices.
|
||||
this._removeUnknownDevices(devicesInRoom);
|
||||
}
|
||||
|
||||
logger.debug(`Ensuring outbound session in ${this._roomId}`);
|
||||
await this._ensureOutboundSession(devicesInRoom, blocked, true);
|
||||
|
||||
logger.debug(`Ready to encrypt events for ${this._roomId}`);
|
||||
} catch (e) {
|
||||
logger.error(`Failed to prepare to encrypt events for ${this._roomId}`, e);
|
||||
} finally {
|
||||
delete this.encryptionPreparationMetadata;
|
||||
delete this.encryptionPreparation;
|
||||
}
|
||||
|
||||
await this._ensureOutboundSession(devicesInRoom, blocked, true);
|
||||
|
||||
delete this.encryptionPreparation;
|
||||
})();
|
||||
};
|
||||
|
||||
@@ -1347,18 +1393,12 @@ MegolmDecryption.prototype.onRoomKeyEvent = function(event) {
|
||||
}
|
||||
});
|
||||
}).then(() => {
|
||||
if (this._crypto.backupInfo) {
|
||||
// don't wait for the keys to be backed up for the server
|
||||
this._crypto.backupGroupSession(
|
||||
content.room_id, senderKey, forwardingKeyChain,
|
||||
content.session_id, content.session_key, keysClaimed,
|
||||
exportFormat,
|
||||
).catch((e) => {
|
||||
// This throws if the upload failed, but this is fine
|
||||
// since it will have written it to the db and will retry.
|
||||
logger.log("Failed to back up megolm session", e);
|
||||
});
|
||||
}
|
||||
// don't wait for the keys to be backed up for the server
|
||||
this._crypto.backupGroupSession(
|
||||
content.room_id, senderKey, forwardingKeyChain,
|
||||
content.session_id, content.session_key, keysClaimed,
|
||||
exportFormat,
|
||||
);
|
||||
}).catch((e) => {
|
||||
logger.error(`Error handling m.room_key_event: ${e}`);
|
||||
});
|
||||
@@ -1564,7 +1604,7 @@ MegolmDecryption.prototype.importRoomKey = function(session, opts = {}) {
|
||||
true,
|
||||
opts.untrusted ? { untrusted: opts.untrusted } : {},
|
||||
).then(() => {
|
||||
if (this._crypto.backupInfo && opts.source !== "backup") {
|
||||
if (opts.source !== "backup") {
|
||||
// don't wait for it to complete
|
||||
this._crypto.backupGroupSession(
|
||||
session.room_id,
|
||||
|
||||
+21
-13
@@ -57,6 +57,7 @@ import {IllegalMethod} from "./verification/IllegalMethod";
|
||||
import {KeySignatureUploadError} from "../errors";
|
||||
import {decryptAES, encryptAES} from './aes';
|
||||
import {DehydrationManager} from './dehydration';
|
||||
import { MatrixEvent } from "../models/event";
|
||||
|
||||
const DeviceVerification = DeviceInfo.DeviceVerification;
|
||||
|
||||
@@ -2884,18 +2885,18 @@ Crypto.prototype.backupGroupSession = async function(
|
||||
sessionId, sessionKey, keysClaimed,
|
||||
exportFormat,
|
||||
) {
|
||||
if (!this.backupInfo) {
|
||||
throw new Error("Key backups are not enabled");
|
||||
}
|
||||
|
||||
await this._cryptoStore.markSessionsNeedingBackup([{
|
||||
senderKey: senderKey,
|
||||
sessionId: sessionId,
|
||||
}]);
|
||||
|
||||
// don't wait for this to complete: it will delay so
|
||||
// happens in the background
|
||||
this.scheduleKeyBackupSend();
|
||||
if (this.backupInfo) {
|
||||
// don't wait for this to complete: it will delay so
|
||||
// happens in the background
|
||||
this.scheduleKeyBackupSend();
|
||||
}
|
||||
// if this.backupInfo is not set, then the keys will be backed up when
|
||||
// client.enableKeyBackup is called
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -3028,19 +3029,26 @@ Crypto.prototype.encryptEvent = async function(event, room) {
|
||||
* finished decrypting. Rejects with an `algorithms.DecryptionError` if there
|
||||
* is a problem decrypting the event.
|
||||
*/
|
||||
Crypto.prototype.decryptEvent = function(event) {
|
||||
Crypto.prototype.decryptEvent = async function(event) {
|
||||
if (event.isRedacted()) {
|
||||
return Promise.resolve({
|
||||
const redactionEvent = new MatrixEvent(event.getUnsigned().redacted_because);
|
||||
const decryptedEvent = await this.decryptEvent(redactionEvent);
|
||||
|
||||
return {
|
||||
clearEvent: {
|
||||
room_id: event.getRoomId(),
|
||||
type: "m.room.message",
|
||||
content: {},
|
||||
unsigned: {
|
||||
redacted_because: decryptedEvent.clearEvent,
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
} else {
|
||||
const content = event.getWireContent();
|
||||
const alg = this._getRoomDecryptor(event.getRoomId(), content.algorithm);
|
||||
return await alg.decryptEvent(event);
|
||||
}
|
||||
const content = event.getWireContent();
|
||||
const alg = this._getRoomDecryptor(event.getRoomId(), content.algorithm);
|
||||
return alg.decryptEvent(event);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
+61
-39
@@ -183,18 +183,24 @@ export async function getExistingOlmSessions(
|
||||
* @param {Array} [failedServers] An array to fill with remote servers that
|
||||
* failed to respond to one-time-key requests.
|
||||
*
|
||||
* @param {Logger} [log] A possibly customised log
|
||||
*
|
||||
* @return {Promise} resolves once the sessions are complete, to
|
||||
* an Object mapping from userId to deviceId to
|
||||
* {@link module:crypto~OlmSessionResult}
|
||||
*/
|
||||
export async function ensureOlmSessionsForDevices(
|
||||
olmDevice, baseApis, devicesByUser, force, otkTimeout, failedServers,
|
||||
olmDevice, baseApis, devicesByUser, force, otkTimeout, failedServers, log,
|
||||
) {
|
||||
if (typeof force === "number") {
|
||||
log = failedServers;
|
||||
failedServers = otkTimeout;
|
||||
otkTimeout = force;
|
||||
force = false;
|
||||
}
|
||||
if (!log) {
|
||||
log = logger;
|
||||
}
|
||||
|
||||
const devicesWithoutSession = [
|
||||
// [userId, deviceId], ...
|
||||
@@ -202,6 +208,35 @@ export async function ensureOlmSessionsForDevices(
|
||||
const result = {};
|
||||
const resolveSession = {};
|
||||
|
||||
// Mark all sessions this task intends to update as in progress. It is
|
||||
// important to do this for all devices this task cares about in a single
|
||||
// synchronous operation, as otherwise it is possible to have deadlocks
|
||||
// where multiple tasks wait indefinitely on another task to update some set
|
||||
// of common devices.
|
||||
for (const [, devices] of Object.entries(devicesByUser)) {
|
||||
for (const deviceInfo of devices) {
|
||||
const key = deviceInfo.getIdentityKey();
|
||||
|
||||
if (key === olmDevice.deviceCurve25519Key) {
|
||||
// We don't start sessions with ourself, so there's no need to
|
||||
// mark it in progress.
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!olmDevice._sessionsInProgress[key]) {
|
||||
// pre-emptively mark the session as in-progress to avoid race
|
||||
// conditions. If we find that we already have a session, then
|
||||
// we'll resolve
|
||||
olmDevice._sessionsInProgress[key] = new Promise(resolve => {
|
||||
resolveSession[key] = (...args) => {
|
||||
delete olmDevice._sessionsInProgress[key];
|
||||
resolve(...args);
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const [userId, devices] of Object.entries(devicesByUser)) {
|
||||
result[userId] = {};
|
||||
for (const deviceInfo of devices) {
|
||||
@@ -216,7 +251,7 @@ export async function ensureOlmSessionsForDevices(
|
||||
// new chain when this side has an active sender chain.
|
||||
// If you see this message being logged in the wild, we should find
|
||||
// the thing that is trying to send Olm messages to itself and fix it.
|
||||
logger.info("Attempted to start session with ourself! Ignoring");
|
||||
log.info("Attempted to start session with ourself! Ignoring");
|
||||
// We must fill in the section in the return value though, as callers
|
||||
// expect it to be there.
|
||||
result[userId][deviceId] = {
|
||||
@@ -226,41 +261,21 @@ export async function ensureOlmSessionsForDevices(
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!olmDevice._sessionsInProgress[key]) {
|
||||
// pre-emptively mark the session as in-progress to avoid race
|
||||
// conditions. If we find that we already have a session, then
|
||||
// we'll resolve
|
||||
olmDevice._sessionsInProgress[key] = new Promise(
|
||||
(resolve, reject) => {
|
||||
resolveSession[key] = {
|
||||
resolve: (...args) => {
|
||||
delete olmDevice._sessionsInProgress[key];
|
||||
resolve(...args);
|
||||
},
|
||||
reject: (...args) => {
|
||||
delete olmDevice._sessionsInProgress[key];
|
||||
reject(...args);
|
||||
},
|
||||
};
|
||||
},
|
||||
);
|
||||
}
|
||||
const forWhom = `for ${key} (${userId}:${deviceId})`;
|
||||
const sessionId = await olmDevice.getSessionIdForDevice(
|
||||
key, resolveSession[key],
|
||||
key, resolveSession[key], log,
|
||||
);
|
||||
if (sessionId !== null && resolveSession[key]) {
|
||||
// we found a session, but we had marked the session as
|
||||
// in-progress, so unmark it and unblock anything that was
|
||||
// waiting
|
||||
delete olmDevice._sessionsInProgress[key];
|
||||
resolveSession[key].resolve();
|
||||
delete resolveSession[key];
|
||||
// in-progress, so resolve it now, which will unmark it and
|
||||
// unblock anything that was waiting
|
||||
resolveSession[key]();
|
||||
}
|
||||
if (sessionId === null || force) {
|
||||
if (force) {
|
||||
logger.info("Forcing new Olm session for " + userId + ":" + deviceId);
|
||||
log.info(`Forcing new Olm session ${forWhom}`);
|
||||
} else {
|
||||
logger.info("Making new Olm session for " + userId + ":" + deviceId);
|
||||
log.info(`Making new Olm session ${forWhom}`);
|
||||
}
|
||||
devicesWithoutSession.push([userId, deviceId]);
|
||||
}
|
||||
@@ -277,15 +292,18 @@ export async function ensureOlmSessionsForDevices(
|
||||
|
||||
const oneTimeKeyAlgorithm = "signed_curve25519";
|
||||
let res;
|
||||
let taskDetail = `one-time keys for ${devicesWithoutSession.length} devices`;
|
||||
try {
|
||||
log.debug(`Claiming ${taskDetail}`);
|
||||
res = await baseApis.claimOneTimeKeys(
|
||||
devicesWithoutSession, oneTimeKeyAlgorithm, otkTimeout,
|
||||
);
|
||||
log.debug(`Claimed ${taskDetail}`);
|
||||
} catch (e) {
|
||||
for (const resolver of Object.values(resolveSession)) {
|
||||
resolver.resolve();
|
||||
resolver();
|
||||
}
|
||||
logger.log("failed to claim one-time keys", e, devicesWithoutSession);
|
||||
log.log(`Failed to claim ${taskDetail}`, e, devicesWithoutSession);
|
||||
throw e;
|
||||
}
|
||||
|
||||
@@ -293,10 +311,10 @@ export async function ensureOlmSessionsForDevices(
|
||||
failedServers.push(...Object.keys(res.failures));
|
||||
}
|
||||
|
||||
const otk_res = res.one_time_keys || {};
|
||||
const otkResult = res.one_time_keys || {};
|
||||
const promises = [];
|
||||
for (const [userId, devices] of Object.entries(devicesByUser)) {
|
||||
const userRes = otk_res[userId] || {};
|
||||
const userRes = otkResult[userId] || {};
|
||||
for (let j = 0; j < devices.length; j++) {
|
||||
const deviceInfo = devices[j];
|
||||
const deviceId = deviceInfo.deviceId;
|
||||
@@ -323,11 +341,12 @@ export async function ensureOlmSessionsForDevices(
|
||||
}
|
||||
|
||||
if (!oneTimeKey) {
|
||||
const msg = "No one-time keys (alg=" + oneTimeKeyAlgorithm +
|
||||
") for device " + userId + ":" + deviceId;
|
||||
logger.warn(msg);
|
||||
log.warn(
|
||||
`No one-time keys (alg=${oneTimeKeyAlgorithm}) ` +
|
||||
`for device ${userId}:${deviceId}`,
|
||||
);
|
||||
if (resolveSession[key]) {
|
||||
resolveSession[key].resolve();
|
||||
resolveSession[key]();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -337,12 +356,12 @@ export async function ensureOlmSessionsForDevices(
|
||||
olmDevice, oneTimeKey, userId, deviceInfo,
|
||||
).then((sid) => {
|
||||
if (resolveSession[key]) {
|
||||
resolveSession[key].resolve(sid);
|
||||
resolveSession[key](sid);
|
||||
}
|
||||
result[userId][deviceId].sessionId = sid;
|
||||
}, (e) => {
|
||||
if (resolveSession[key]) {
|
||||
resolveSession[key].resolve();
|
||||
resolveSession[key]();
|
||||
}
|
||||
throw e;
|
||||
}),
|
||||
@@ -350,7 +369,10 @@ export async function ensureOlmSessionsForDevices(
|
||||
}
|
||||
}
|
||||
|
||||
taskDetail = `Olm sessions for ${promises.length} devices`;
|
||||
log.debug(`Starting ${taskDetail}`);
|
||||
await Promise.all(promises);
|
||||
log.debug(`Started ${taskDetail}`);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import {logger} from '../../logger';
|
||||
import * as utils from "../../utils";
|
||||
|
||||
export const VERSION = 9;
|
||||
const PROFILE_TRANSACTIONS = false;
|
||||
|
||||
/**
|
||||
* Implementation of a CryptoStore which is backed by an existing
|
||||
@@ -34,6 +35,7 @@ export class Backend {
|
||||
*/
|
||||
constructor(db) {
|
||||
this._db = db;
|
||||
this._nextTxnId = 0;
|
||||
|
||||
// make sure we close the db on `onversionchange` - otherwise
|
||||
// attempts to delete the database will block (and subsequent
|
||||
@@ -757,10 +759,27 @@ export class Backend {
|
||||
}));
|
||||
}
|
||||
|
||||
doTxn(mode, stores, func) {
|
||||
doTxn(mode, stores, func, log = logger) {
|
||||
let startTime;
|
||||
let description;
|
||||
if (PROFILE_TRANSACTIONS) {
|
||||
const txnId = this._nextTxnId++;
|
||||
startTime = Date.now();
|
||||
description = `${mode} crypto store transaction ${txnId} in ${stores}`;
|
||||
log.debug(`Starting ${description}`);
|
||||
}
|
||||
const txn = this._db.transaction(stores, mode);
|
||||
const promise = promiseifyTxn(txn);
|
||||
const result = func(txn);
|
||||
if (PROFILE_TRANSACTIONS) {
|
||||
promise.then(() => {
|
||||
const elapsedTime = Date.now() - startTime;
|
||||
log.debug(`Finished ${description}, took ${elapsedTime} ms`);
|
||||
}, () => {
|
||||
const elapsedTime = Date.now() - startTime;
|
||||
log.error(`Failed ${description}, took ${elapsedTime} ms`);
|
||||
});
|
||||
}
|
||||
return promise.then(() => {
|
||||
return result;
|
||||
});
|
||||
|
||||
@@ -596,6 +596,7 @@ export class IndexedDBCryptoStore {
|
||||
* @param {function(*)} func Function called with the
|
||||
* transaction object: an opaque object that should be passed
|
||||
* to store functions.
|
||||
* @param {Logger} [log] A possibly customised log
|
||||
* @return {Promise} Promise that resolves with the result of the `func`
|
||||
* when the transaction is complete. If the backend is
|
||||
* async (ie. the indexeddb backend) any of the callback
|
||||
@@ -603,8 +604,8 @@ export class IndexedDBCryptoStore {
|
||||
* reject with that exception. On synchronous backends, the
|
||||
* exception will propagate to the caller of the getFoo method.
|
||||
*/
|
||||
doTxn(mode, stores, func) {
|
||||
return this._backend.doTxn(mode, stores, func);
|
||||
doTxn(mode, stores, func, log) {
|
||||
return this._backend.doTxn(mode, stores, func, log);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+32
-3
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Copyright 2018 André Jaenisch
|
||||
Copyright 2019 The Matrix.org Foundation C.I.C.
|
||||
Copyright 2019, 2021 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
@@ -19,7 +19,7 @@ limitations under the License.
|
||||
* @module logger
|
||||
*/
|
||||
|
||||
import log from "loglevel";
|
||||
import log, { Logger } from "loglevel";
|
||||
|
||||
// This is to demonstrate, that you can use any namespace you want.
|
||||
// Namespaces allow you to turn on/off the logging for specific parts of the
|
||||
@@ -36,6 +36,11 @@ const DEFAULT_NAMESPACE = "matrix";
|
||||
// when logging so we always get the current value of console methods.
|
||||
log.methodFactory = function(methodName, logLevel, loggerName) {
|
||||
return function(...args) {
|
||||
/* eslint-disable babel/no-invalid-this */
|
||||
if (this.prefix) {
|
||||
args.unshift(this.prefix);
|
||||
}
|
||||
/* eslint-enable babel/no-invalid-this */
|
||||
const supportedByConsole = methodName === "error" ||
|
||||
methodName === "warn" ||
|
||||
methodName === "trace" ||
|
||||
@@ -54,6 +59,30 @@ log.methodFactory = function(methodName, logLevel, loggerName) {
|
||||
* Drop-in replacement for <code>console</code> using {@link https://www.npmjs.com/package/loglevel|loglevel}.
|
||||
* Can be tailored down to specific use cases if needed.
|
||||
*/
|
||||
export const logger = log.getLogger(DEFAULT_NAMESPACE);
|
||||
export const logger: PrefixedLogger = log.getLogger(DEFAULT_NAMESPACE);
|
||||
logger.setLevel(log.levels.DEBUG);
|
||||
|
||||
interface PrefixedLogger extends Logger {
|
||||
withPrefix?: (prefix: string) => PrefixedLogger;
|
||||
prefix?: string;
|
||||
}
|
||||
|
||||
function extendLogger(logger: PrefixedLogger) {
|
||||
logger.withPrefix = function(prefix: string): PrefixedLogger {
|
||||
const existingPrefix = this.prefix || "";
|
||||
return getPrefixedLogger(existingPrefix + prefix);
|
||||
};
|
||||
}
|
||||
|
||||
extendLogger(logger);
|
||||
|
||||
function getPrefixedLogger(prefix): PrefixedLogger {
|
||||
const prefixLogger: PrefixedLogger = log.getLogger(`${DEFAULT_NAMESPACE}-${prefix}`);
|
||||
if (prefixLogger.prefix !== prefix) {
|
||||
// Only do this setup work the first time through, as loggers are saved by name.
|
||||
extendLogger(prefixLogger);
|
||||
prefixLogger.prefix = prefix;
|
||||
prefixLogger.setLevel(log.levels.DEBUG);
|
||||
}
|
||||
return prefixLogger;
|
||||
}
|
||||
|
||||
+2
-1
@@ -52,7 +52,7 @@ export * from "./store/session/webstorage";
|
||||
export * from "./crypto/store/memory-crypto-store";
|
||||
export * from "./crypto/store/indexeddb-crypto-store";
|
||||
export * from "./content-repo";
|
||||
export const ContentHelpers = import("./content-helpers");
|
||||
export * as ContentHelpers from "./content-helpers";
|
||||
export {
|
||||
createNewMatrixCall,
|
||||
setAudioOutput as setMatrixCallAudioOutput,
|
||||
@@ -142,6 +142,7 @@ export interface ICreateClientOpts {
|
||||
unstableClientRelationAggregation?: boolean;
|
||||
verificationMethods?: Array<any>;
|
||||
forceTURN?: boolean;
|
||||
iceCandidatePoolSize?: number,
|
||||
supportsCallTransfer?: boolean,
|
||||
fallbackICEServerAllowed?: boolean;
|
||||
cryptoCallbacks?: ICryptoCallbacks;
|
||||
|
||||
@@ -811,6 +811,24 @@ utils.extend(MatrixEvent.prototype, {
|
||||
return this.getType() === "m.room.redaction";
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the (decrypted, if necessary) redaction event JSON
|
||||
* if event was redacted
|
||||
*
|
||||
* @returns {object} The redaction event JSON, or an empty object
|
||||
*/
|
||||
getRedactionEvent: function() {
|
||||
if (!this.isRedacted()) return null;
|
||||
|
||||
if (this._clearEvent.unsigned) {
|
||||
return this._clearEvent.unsigned.redacted_because;
|
||||
} else if (this.event.unsigned.redacted_because) {
|
||||
return this.event.unsigned.redacted_because;
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the push actions, if known, for this event
|
||||
*
|
||||
|
||||
@@ -290,6 +290,9 @@ RoomMember.prototype.getMxcAvatarUrl = function() {
|
||||
return null;
|
||||
};
|
||||
|
||||
const MXID_PATTERN = /@.+:.+/;
|
||||
const LTR_RTL_PATTERN = /[\u200E\u200F\u202A-\u202F]/;
|
||||
|
||||
function calculateDisplayName(selfUserId, displayName, roomState) {
|
||||
if (!displayName || displayName === selfUserId) {
|
||||
return selfUserId;
|
||||
@@ -308,13 +311,13 @@ function calculateDisplayName(selfUserId, displayName, roomState) {
|
||||
// Next check if the name contains something that look like a mxid
|
||||
// If it does, it may be someone trying to impersonate someone else
|
||||
// Show full mxid in this case
|
||||
let disambiguate = /@.+:.+/.test(displayName);
|
||||
let disambiguate = MXID_PATTERN.test(displayName);
|
||||
|
||||
if (!disambiguate) {
|
||||
// Also show mxid if the display name contains any LTR/RTL characters as these
|
||||
// make it very difficult for us to find similar *looking* display names
|
||||
// E.g "Mark" could be cloned by writing "kraM" but in RTL.
|
||||
disambiguate = /[\u200E\u200F\u202A-\u202F]/.test(displayName);
|
||||
disambiguate = LTR_RTL_PATTERN.test(displayName);
|
||||
}
|
||||
|
||||
if (!disambiguate) {
|
||||
|
||||
@@ -23,6 +23,7 @@ import {EventEmitter} from "events";
|
||||
import {RoomMember} from "./room-member";
|
||||
import {logger} from '../logger';
|
||||
import * as utils from "../utils";
|
||||
import {EventType} from "../@types/event";
|
||||
|
||||
// possible statuses for out-of-band member loading
|
||||
const OOB_STATUS_NOTSTARTED = 1;
|
||||
@@ -718,6 +719,16 @@ RoomState.prototype.mayTriggerNotifOfType = function(notifLevelKey, userId) {
|
||||
return member.powerLevel >= notifLevel;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the join rule based on the m.room.join_rule state event, defaulting to `invite`.
|
||||
* @returns {string} the join_rule applied to this room
|
||||
*/
|
||||
RoomState.prototype.getJoinRule = function() {
|
||||
const joinRuleEvent = this.getStateEvents(EventType.RoomJoinRules, "");
|
||||
const joinRuleContent = joinRuleEvent ? joinRuleEvent.getContent() : {};
|
||||
return joinRuleContent["join_rule"] || "invite";
|
||||
};
|
||||
|
||||
|
||||
function _updateThirdPartyTokenCache(roomState, memberEvent) {
|
||||
if (!memberEvent.getContent().third_party_invite) {
|
||||
|
||||
+66
-2
@@ -30,6 +30,7 @@ import {RoomMember} from "./room-member";
|
||||
import {RoomSummary} from "./room-summary";
|
||||
import {logger} from '../logger';
|
||||
import {ReEmitter} from '../ReEmitter';
|
||||
import {EventType, RoomCreateTypeField, RoomType} from "../@types/event";
|
||||
|
||||
// These constants are used as sane defaults when the homeserver doesn't support
|
||||
// the m.room_versions capability. In practice, KNOWN_SAFE_ROOM_VERSION should be
|
||||
@@ -817,7 +818,7 @@ Room.prototype.getBlacklistUnverifiedDevices = function() {
|
||||
*/
|
||||
Room.prototype.getAvatarUrl = function(baseUrl, width, height, resizeMethod,
|
||||
allowDefault) {
|
||||
const roomAvatarEvent = this.currentState.getStateEvents("m.room.avatar", "");
|
||||
const roomAvatarEvent = this.currentState.getStateEvents(EventType.RoomAvatar, "");
|
||||
if (allowDefault === undefined) {
|
||||
allowDefault = true;
|
||||
}
|
||||
@@ -835,6 +836,15 @@ Room.prototype.getAvatarUrl = function(baseUrl, width, height, resizeMethod,
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the mxc avatar url for the room, if one was set.
|
||||
* @return {string} the mxc avatar url or falsy
|
||||
*/
|
||||
Room.prototype.getMxcAvatarUrl = function() {
|
||||
const roomAvatarEvent = this.currentState.getStateEvents(EventType.RoomAvatar, "");
|
||||
return roomAvatarEvent ? roomAvatarEvent.getContent().url : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the aliases this room has according to the room's state
|
||||
* The aliases returned by this function may not necessarily
|
||||
@@ -1822,7 +1832,7 @@ Room.prototype.getAccountData = function(type) {
|
||||
|
||||
|
||||
/**
|
||||
* Returns wheter the syncing user has permission to send a message in the room
|
||||
* Returns whether the syncing user has permission to send a message in the room
|
||||
* @return {boolean} true if the user should be permitted to send
|
||||
* message events into the room.
|
||||
*/
|
||||
@@ -1831,6 +1841,51 @@ Room.prototype.maySendMessage = function() {
|
||||
this.currentState.maySendEvent('m.room.message', this.myUserId);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns whether the given user has permissions to issue an invite for this room.
|
||||
* @param {string} userId the ID of the Matrix user to check permissions for
|
||||
* @returns {boolean} true if the user should be permitted to issue invites for this room.
|
||||
*/
|
||||
Room.prototype.canInvite = function(userId) {
|
||||
let canInvite = this.getMyMembership() === "join";
|
||||
const powerLevelsEvent = this.currentState.getStateEvents(EventType.RoomPowerLevels, "");
|
||||
const powerLevels = powerLevelsEvent && powerLevelsEvent.getContent();
|
||||
const me = this.getMember(userId);
|
||||
if (powerLevels && me && powerLevels.invite > me.powerLevel) {
|
||||
canInvite = false;
|
||||
}
|
||||
return canInvite;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the join rule based on the m.room.join_rule state event, defaulting to `invite`.
|
||||
* @returns {string} the join_rule applied to this room
|
||||
*/
|
||||
Room.prototype.getJoinRule = function() {
|
||||
return this.currentState.getJoinRule();
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the type of the room from the `m.room.create` event content or undefined if none is set
|
||||
* @returns {?string} the type of the room. Currently only RoomType.Space is known.
|
||||
*/
|
||||
Room.prototype.getType = function() {
|
||||
const createEvent = this.currentState.getStateEvents("m.room.create", "");
|
||||
if (!createEvent) {
|
||||
logger.warn("Room " + this.roomId + " does not have an m.room.create event");
|
||||
return undefined;
|
||||
}
|
||||
return createEvent.getContent()[RoomCreateTypeField];
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns whether the room is a space-room as defined by MSC1772.
|
||||
* @returns {boolean} true if the room's type is RoomType.Space
|
||||
*/
|
||||
Room.prototype.isSpaceRoom = function() {
|
||||
return this.getType() === RoomType.Space;
|
||||
};
|
||||
|
||||
/**
|
||||
* This is an internal method. Calculates the name of the room from the current
|
||||
* room state.
|
||||
@@ -2048,3 +2103,12 @@ function memberNamesToRoomName(names, count = (names.length + 1)) {
|
||||
*
|
||||
* @param {EventStatus} oldStatus The previous event status.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Fires when the logged in user's membership in the room is updated.
|
||||
*
|
||||
* @event module:models/room~Room#"Room.myMembership"
|
||||
* @param {Room} room The room in which the membership has been updated
|
||||
* @param {string} membership The new membership value
|
||||
* @param {string} prevMembership The previous membership value
|
||||
*/
|
||||
|
||||
+16
-1
@@ -15,9 +15,24 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
const LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
|
||||
const UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const DIGITS = "0123456789";
|
||||
|
||||
export function randomString(len: number): string {
|
||||
return randomStringFrom(len, UPPERCASE + LOWERCASE + DIGITS);
|
||||
}
|
||||
|
||||
export function randomLowercaseString(len: number): string {
|
||||
return randomStringFrom(len, LOWERCASE);
|
||||
}
|
||||
|
||||
export function randomUppercaseString(len: number): string {
|
||||
return randomStringFrom(len, UPPERCASE);
|
||||
}
|
||||
|
||||
function randomStringFrom(len: number, chars: string): string {
|
||||
let ret = "";
|
||||
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
||||
for (let i = 0; i < len; ++i) {
|
||||
ret += chars.charAt(Math.floor(Math.random() * chars.length));
|
||||
|
||||
+1
-1
@@ -164,7 +164,7 @@ MatrixScheduler.RETRY_BACKOFF_RATELIMIT = function(event, attempts, err) {
|
||||
|
||||
if (err.name === "M_LIMIT_EXCEEDED") {
|
||||
const waitTime = err.data.retry_after_ms;
|
||||
if (waitTime) {
|
||||
if (waitTime > 0) {
|
||||
return waitTime;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -435,7 +435,7 @@ LocalIndexedDBStoreBackend.prototype = {
|
||||
* @return {Promise} Resolves if the data was persisted.
|
||||
*/
|
||||
_persistSyncData: function(nextBatch, roomsData, groupsData) {
|
||||
logger.log("Persisting sync data up to ", nextBatch);
|
||||
logger.log("Persisting sync data up to", nextBatch);
|
||||
return utils.promiseTry(() => {
|
||||
const txn = this.db.transaction(["sync"], "readwrite");
|
||||
const store = txn.objectStore("sync");
|
||||
|
||||
@@ -51,8 +51,8 @@ const WRITE_DELAY_MS = 1000 * 60 * 5; // once every 5 minutes
|
||||
* sync from the server is not required. This does not reduce memory usage as all
|
||||
* the data is eagerly fetched when <code>startup()</code> is called.
|
||||
* <pre>
|
||||
* let opts = { localStorage: window.localStorage };
|
||||
* let store = new IndexedDBStore();
|
||||
* let opts = { indexedDB: window.indexedDB, localStorage: window.localStorage };
|
||||
* let store = new IndexedDBStore(opts);
|
||||
* await store.startup(); // load from indexed db
|
||||
* let client = sdk.createClient({
|
||||
* store: store,
|
||||
|
||||
+22
-21
@@ -93,7 +93,7 @@ export function SyncApi(client, opts) {
|
||||
};
|
||||
}
|
||||
this.opts = opts;
|
||||
this._peekRoomId = null;
|
||||
this._peekRoom = null;
|
||||
this._currentSyncRequest = null;
|
||||
this._syncState = null;
|
||||
this._syncStateData = null; // additional data (eg. error object for failed sync)
|
||||
@@ -264,17 +264,18 @@ SyncApi.prototype.syncLeftRooms = function() {
|
||||
* store.
|
||||
*/
|
||||
SyncApi.prototype.peek = function(roomId) {
|
||||
const self = this;
|
||||
if (this._peekRoom && this._peekRoom.roomId === roomId) {
|
||||
return Promise.resolve(this._peekRoom);
|
||||
}
|
||||
|
||||
const client = this.client;
|
||||
this._peekRoomId = roomId;
|
||||
return this.client.roomInitialSync(roomId, 20).then(function(response) {
|
||||
this._peekRoom = this.createRoom(roomId);
|
||||
return this.client.roomInitialSync(roomId, 20).then((response) => {
|
||||
// make sure things are init'd
|
||||
response.messages = response.messages || {};
|
||||
response.messages.chunk = response.messages.chunk || [];
|
||||
response.state = response.state || [];
|
||||
|
||||
const peekRoom = self.createRoom(roomId);
|
||||
|
||||
// FIXME: Mostly duplicated from _processRoomEvents but not entirely
|
||||
// because "state" in this API is at the BEGINNING of the chunk
|
||||
const oldStateEvents = utils.map(
|
||||
@@ -309,28 +310,28 @@ SyncApi.prototype.peek = function(roomId) {
|
||||
// fire off pagination requests in response to the Room.timeline
|
||||
// events.
|
||||
if (response.messages.start) {
|
||||
peekRoom.oldState.paginationToken = response.messages.start;
|
||||
this._peekRoom.oldState.paginationToken = response.messages.start;
|
||||
}
|
||||
|
||||
// set the state of the room to as it was after the timeline executes
|
||||
peekRoom.oldState.setStateEvents(oldStateEvents);
|
||||
peekRoom.currentState.setStateEvents(stateEvents);
|
||||
this._peekRoom.oldState.setStateEvents(oldStateEvents);
|
||||
this._peekRoom.currentState.setStateEvents(stateEvents);
|
||||
|
||||
self._resolveInvites(peekRoom);
|
||||
peekRoom.recalculate();
|
||||
this._resolveInvites(this._peekRoom);
|
||||
this._peekRoom.recalculate();
|
||||
|
||||
// roll backwards to diverge old state. addEventsToTimeline
|
||||
// will overwrite the pagination token, so make sure it overwrites
|
||||
// it with the right thing.
|
||||
peekRoom.addEventsToTimeline(messages.reverse(), true,
|
||||
peekRoom.getLiveTimeline(),
|
||||
this._peekRoom.addEventsToTimeline(messages.reverse(), true,
|
||||
this._peekRoom.getLiveTimeline(),
|
||||
response.messages.start);
|
||||
|
||||
client.store.storeRoom(peekRoom);
|
||||
client.emit("Room", peekRoom);
|
||||
client.store.storeRoom(this._peekRoom);
|
||||
client.emit("Room", this._peekRoom);
|
||||
|
||||
self._peekPoll(peekRoom);
|
||||
return peekRoom;
|
||||
this._peekPoll(this._peekRoom);
|
||||
return this._peekRoom;
|
||||
});
|
||||
};
|
||||
|
||||
@@ -339,16 +340,16 @@ SyncApi.prototype.peek = function(roomId) {
|
||||
* peeked.
|
||||
*/
|
||||
SyncApi.prototype.stopPeeking = function() {
|
||||
this._peekRoomId = null;
|
||||
this._peekRoom = null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Do a peek room poll.
|
||||
* @param {Room} peekRoom
|
||||
* @param {string} token from= token
|
||||
* @param {string?} token from= token
|
||||
*/
|
||||
SyncApi.prototype._peekPoll = function(peekRoom, token) {
|
||||
if (this._peekRoomId !== peekRoom.roomId) {
|
||||
if (this._peekRoom !== peekRoom) {
|
||||
debuglog("Stopped peeking in room %s", peekRoom.roomId);
|
||||
return;
|
||||
}
|
||||
@@ -360,7 +361,7 @@ SyncApi.prototype._peekPoll = function(peekRoom, token) {
|
||||
timeout: 30 * 1000,
|
||||
from: token,
|
||||
}, undefined, 50 * 1000).then(function(res) {
|
||||
if (self._peekRoomId !== peekRoom.roomId) {
|
||||
if (self._peekRoom !== peekRoom) {
|
||||
debuglog("Stopped peeking in room %s", peekRoom.roomId);
|
||||
return;
|
||||
}
|
||||
|
||||
+313
-110
@@ -174,6 +174,11 @@ export enum CallErrorCode {
|
||||
SignallingFailed = 'signalling_timeout',
|
||||
}
|
||||
|
||||
enum ConstraintsType {
|
||||
Audio = "audio",
|
||||
Video = "video",
|
||||
}
|
||||
|
||||
/**
|
||||
* The version field that we set in m.call.* events
|
||||
*/
|
||||
@@ -185,6 +190,21 @@ const FALLBACK_ICE_SERVER = 'stun:turn.matrix.org';
|
||||
/** The length of time a call can be ringing for. */
|
||||
const CALL_TIMEOUT_MS = 60000;
|
||||
|
||||
/** Retrieves sources from desktopCapturer */
|
||||
export function getDesktopCapturerSources(): Promise<Array<DesktopCapturerSource>> {
|
||||
const options: GetSourcesOptions = {
|
||||
thumbnailSize: {
|
||||
height: 176,
|
||||
width: 312,
|
||||
},
|
||||
types: [
|
||||
"screen",
|
||||
"window",
|
||||
],
|
||||
};
|
||||
return window.electron.getDesktopCapturerSources(options);
|
||||
}
|
||||
|
||||
export class CallError extends Error {
|
||||
code : string;
|
||||
|
||||
@@ -196,8 +216,8 @@ export class CallError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
function genCallID() {
|
||||
return Date.now() + randomString(16);
|
||||
function genCallID(): string {
|
||||
return Date.now().toString() + randomString(16);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -236,8 +256,6 @@ export class MatrixCall extends EventEmitter {
|
||||
private localAVStream: MediaStream;
|
||||
private inviteOrAnswerSent: boolean;
|
||||
private waitForLocalAVStream: boolean;
|
||||
// XXX: This is either the invite or answer from remote...
|
||||
private msg: any;
|
||||
// XXX: I don't know why this is called 'config'.
|
||||
private config: MediaStreamConstraints;
|
||||
private successor: MatrixCall;
|
||||
@@ -260,10 +278,20 @@ export class MatrixCall extends EventEmitter {
|
||||
private micMuted;
|
||||
private vidMuted;
|
||||
|
||||
// the stats for the call at the point it ended. We can't get these after we
|
||||
// tear the call down, so we just grab a snapshot before we stop the call.
|
||||
// The typescript definitions have this type as 'any' :(
|
||||
private callStatsAtEnd: any[];
|
||||
|
||||
// Perfect negotiation state: https://www.w3.org/TR/webrtc/#perfect-negotiation-example
|
||||
private makingOffer: boolean;
|
||||
private ignoreOffer: boolean;
|
||||
|
||||
// If candidates arrive before we've picked an opponent (which, in particular,
|
||||
// will happen if the opponent sends candidates eagerly before the user answers
|
||||
// the call) we buffer them up here so we can then add the ones from the party we pick
|
||||
private remoteCandidateBuffer = new Map<string, RTCIceCandidate[]>();
|
||||
|
||||
constructor(opts: CallOpts) {
|
||||
super();
|
||||
this.roomId = opts.roomId;
|
||||
@@ -305,10 +333,11 @@ export class MatrixCall extends EventEmitter {
|
||||
* Place a voice call to this room.
|
||||
* @throws If you have not specified a listener for 'error' events.
|
||||
*/
|
||||
placeVoiceCall() {
|
||||
async placeVoiceCall() {
|
||||
logger.debug("placeVoiceCall");
|
||||
this.checkForErrorListener();
|
||||
this.placeCallWithConstraints(getUserMediaVideoContraints(CallType.Voice));
|
||||
const constraints = getUserMediaContraints(ConstraintsType.Audio);
|
||||
await this.placeCallWithConstraints(constraints);
|
||||
this.type = CallType.Voice;
|
||||
}
|
||||
|
||||
@@ -320,12 +349,13 @@ export class MatrixCall extends EventEmitter {
|
||||
* to render the local camera preview.
|
||||
* @throws If you have not specified a listener for 'error' events.
|
||||
*/
|
||||
placeVideoCall(remoteVideoElement: HTMLVideoElement, localVideoElement: HTMLVideoElement) {
|
||||
async placeVideoCall(remoteVideoElement: HTMLVideoElement, localVideoElement: HTMLVideoElement) {
|
||||
logger.debug("placeVideoCall");
|
||||
this.checkForErrorListener();
|
||||
this.localVideoElement = localVideoElement;
|
||||
this.remoteVideoElement = remoteVideoElement;
|
||||
this.placeCallWithConstraints(getUserMediaVideoContraints(CallType.Video));
|
||||
const constraints = getUserMediaContraints(ConstraintsType.Video);
|
||||
await this.placeCallWithConstraints(constraints);
|
||||
this.type = CallType.Video;
|
||||
}
|
||||
|
||||
@@ -339,15 +369,31 @@ export class MatrixCall extends EventEmitter {
|
||||
* to render the local camera preview.
|
||||
* @throws If you have not specified a listener for 'error' events.
|
||||
*/
|
||||
async placeScreenSharingCall(remoteVideoElement: HTMLVideoElement, localVideoElement: HTMLVideoElement) {
|
||||
async placeScreenSharingCall(
|
||||
remoteVideoElement: HTMLVideoElement,
|
||||
localVideoElement: HTMLVideoElement,
|
||||
selectDesktopCapturerSource: () => Promise<DesktopCapturerSource>,
|
||||
) {
|
||||
logger.debug("placeScreenSharingCall");
|
||||
this.checkForErrorListener();
|
||||
this.localVideoElement = localVideoElement;
|
||||
this.remoteVideoElement = remoteVideoElement;
|
||||
|
||||
try {
|
||||
this.screenSharingStream = await navigator.mediaDevices.getDisplayMedia({'audio': false});
|
||||
const screenshareConstraints = await getScreenshareContraints(selectDesktopCapturerSource);
|
||||
if (!screenshareConstraints) return;
|
||||
if (window.electron?.getDesktopCapturerSources) {
|
||||
// We are using Electron
|
||||
logger.debug("Getting screen stream using getUserMedia()...");
|
||||
this.screenSharingStream = await navigator.mediaDevices.getUserMedia(screenshareConstraints);
|
||||
} else {
|
||||
// We are not using Electron
|
||||
logger.debug("Getting screen stream using getDisplayMedia()...");
|
||||
this.screenSharingStream = await navigator.mediaDevices.getDisplayMedia(screenshareConstraints);
|
||||
}
|
||||
|
||||
logger.debug("Got screen stream, requesting audio stream...");
|
||||
const audioConstraints = getUserMediaVideoContraints(CallType.Voice);
|
||||
const audioConstraints = getUserMediaContraints(ConstraintsType.Audio);
|
||||
this.placeCallWithConstraints(audioConstraints);
|
||||
} catch (err) {
|
||||
this.emit(CallEvent.Error,
|
||||
@@ -357,15 +403,14 @@ export class MatrixCall extends EventEmitter {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
this.type = CallType.Video;
|
||||
}
|
||||
|
||||
getOpponentMember() {
|
||||
public getOpponentMember() {
|
||||
return this.opponentMember;
|
||||
}
|
||||
|
||||
opponentCanBeTransferred() {
|
||||
public opponentCanBeTransferred() {
|
||||
return Boolean(this.opponentCaps && this.opponentCaps["m.call.transferee"]);
|
||||
}
|
||||
|
||||
@@ -373,7 +418,7 @@ export class MatrixCall extends EventEmitter {
|
||||
* Retrieve the local <code><video></code> DOM element.
|
||||
* @return {Element} The dom element
|
||||
*/
|
||||
getLocalVideoElement(): HTMLVideoElement {
|
||||
public getLocalVideoElement(): HTMLVideoElement {
|
||||
return this.localVideoElement;
|
||||
}
|
||||
|
||||
@@ -382,7 +427,7 @@ export class MatrixCall extends EventEmitter {
|
||||
* used for playing back video capable streams.
|
||||
* @return {Element} The dom element
|
||||
*/
|
||||
getRemoteVideoElement(): HTMLVideoElement {
|
||||
public getRemoteVideoElement(): HTMLVideoElement {
|
||||
return this.remoteVideoElement;
|
||||
}
|
||||
|
||||
@@ -391,7 +436,7 @@ export class MatrixCall extends EventEmitter {
|
||||
* used for playing back audio only streams.
|
||||
* @return {Element} The dom element
|
||||
*/
|
||||
getRemoteAudioElement(): HTMLAudioElement {
|
||||
public getRemoteAudioElement(): HTMLAudioElement {
|
||||
return this.remoteAudioElement;
|
||||
}
|
||||
|
||||
@@ -400,7 +445,7 @@ export class MatrixCall extends EventEmitter {
|
||||
* video will be rendered to it immediately.
|
||||
* @param {Element} element The <code><video></code> DOM element.
|
||||
*/
|
||||
async setLocalVideoElement(element : HTMLVideoElement) {
|
||||
public async setLocalVideoElement(element : HTMLVideoElement) {
|
||||
this.localVideoElement = element;
|
||||
|
||||
if (element && this.localAVStream && this.type === CallType.Video) {
|
||||
@@ -421,7 +466,7 @@ export class MatrixCall extends EventEmitter {
|
||||
* the first received video-capable stream will be rendered to it immediately.
|
||||
* @param {Element} element The <code><video></code> DOM element.
|
||||
*/
|
||||
setRemoteVideoElement(element : HTMLVideoElement) {
|
||||
public setRemoteVideoElement(element : HTMLVideoElement) {
|
||||
if (element === this.remoteVideoElement) return;
|
||||
|
||||
element.autoplay = true;
|
||||
@@ -443,7 +488,7 @@ export class MatrixCall extends EventEmitter {
|
||||
* The audio will *not* be rendered from the remoteVideoElement.
|
||||
* @param {Element} element The <code><video></code> DOM element.
|
||||
*/
|
||||
async setRemoteAudioElement(element: HTMLAudioElement) {
|
||||
public async setRemoteAudioElement(element: HTMLAudioElement) {
|
||||
if (element === this.remoteAudioElement) return;
|
||||
|
||||
this.remoteAudioElement = element;
|
||||
@@ -451,16 +496,51 @@ export class MatrixCall extends EventEmitter {
|
||||
if (this.remoteStream) this.playRemoteAudio();
|
||||
}
|
||||
|
||||
// The typescript definitions have this type as 'any' :(
|
||||
public async getCurrentCallStats(): Promise<any[]> {
|
||||
if (this.callHasEnded()) {
|
||||
return this.callStatsAtEnd;
|
||||
}
|
||||
|
||||
return this.collectCallStats();
|
||||
}
|
||||
|
||||
private async collectCallStats(): Promise<any[]> {
|
||||
// This happens when the call fails before it starts.
|
||||
// For example when we fail to get capture sources
|
||||
if (!this.peerConn) return;
|
||||
|
||||
const statsReport = await this.peerConn.getStats();
|
||||
const stats = [];
|
||||
for (const item of statsReport) {
|
||||
stats.push(item[1]);
|
||||
}
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure this call from an invite event. Used by MatrixClient.
|
||||
* @param {MatrixEvent} event The m.call.invite event
|
||||
*/
|
||||
async initWithInvite(event: MatrixEvent) {
|
||||
this.msg = event.getContent();
|
||||
const invite = event.getContent();
|
||||
this.direction = CallDirection.Inbound;
|
||||
|
||||
// make sure we have valid turn creds. Unless something's gone wrong, it should
|
||||
// poll and keep the credentials valid so this should be instant.
|
||||
const haveTurnCreds = await this.client._checkTurnServers();
|
||||
if (!haveTurnCreds) {
|
||||
logger.warn("Failed to get TURN credentials! Proceeding with call anyway...");
|
||||
}
|
||||
|
||||
this.peerConn = this.createPeerConnection();
|
||||
// we must set the party ID before await-ing on anything: the call event
|
||||
// handler will start giving us more call events (eg. candidates) so if
|
||||
// we haven't set the party ID, we'll ignore them.
|
||||
this.chooseOpponent(event);
|
||||
try {
|
||||
await this.peerConn.setRemoteDescription(this.msg.offer);
|
||||
await this.peerConn.setRemoteDescription(invite.offer);
|
||||
} catch (e) {
|
||||
logger.debug("Failed to set remote description", e);
|
||||
this.terminate(CallParty.Local, CallErrorCode.SetRemoteDescription, false);
|
||||
@@ -479,13 +559,6 @@ export class MatrixCall extends EventEmitter {
|
||||
this.type = this.remoteStream.getTracks().some(t => t.kind === 'video') ? CallType.Video : CallType.Voice;
|
||||
|
||||
this.setState(CallState.Ringing);
|
||||
this.opponentVersion = this.msg.version;
|
||||
if (this.opponentVersion !== 0) {
|
||||
// ignore party ID in v0 calls: party ID isn't a thing until v1
|
||||
this.opponentPartyId = this.msg.party_id || null;
|
||||
}
|
||||
this.opponentCaps = this.msg.capabilities || {};
|
||||
this.opponentMember = event.sender;
|
||||
|
||||
if (event.getLocalAge()) {
|
||||
setTimeout(() => {
|
||||
@@ -499,7 +572,7 @@ export class MatrixCall extends EventEmitter {
|
||||
}
|
||||
this.emit(CallEvent.Hangup);
|
||||
}
|
||||
}, this.msg.lifetime - event.getLocalAge());
|
||||
}, invite.lifetime - event.getLocalAge());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -511,7 +584,6 @@ export class MatrixCall extends EventEmitter {
|
||||
// perverse as it may seem, sometimes we want to instantiate a call with a
|
||||
// hangup message (because when getting the state of the room on load, events
|
||||
// come in reverse order and we want to remember that a call has been hung up)
|
||||
this.msg = event.getContent();
|
||||
this.setState(CallState.Ended);
|
||||
}
|
||||
|
||||
@@ -526,7 +598,11 @@ export class MatrixCall extends EventEmitter {
|
||||
logger.debug(`Answering call ${this.callId} of type ${this.type}`);
|
||||
|
||||
if (!this.localAVStream && !this.waitForLocalAVStream) {
|
||||
const constraints = getUserMediaVideoContraints(this.type);
|
||||
const constraints = getUserMediaContraints(
|
||||
this.type == CallType.Video ?
|
||||
ConstraintsType.Video:
|
||||
ConstraintsType.Audio,
|
||||
);
|
||||
logger.log("Getting user media with constraints", constraints);
|
||||
this.setState(CallState.WaitLocalMedia);
|
||||
this.waitForLocalAVStream = true;
|
||||
@@ -709,6 +785,21 @@ export class MatrixCall extends EventEmitter {
|
||||
return callOnHold;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a DTMF digit to the other party
|
||||
* @param digit The digit (nb. string - '#' and '*' are dtmf too)
|
||||
*/
|
||||
sendDtmfDigit(digit: string) {
|
||||
for (const sender of this.peerConn.getSenders()) {
|
||||
if (sender.track.kind === 'audio' && sender.dtmf) {
|
||||
sender.dtmf.insertDTMF(digit);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error("Unable to find a track to send DTMF on");
|
||||
}
|
||||
|
||||
private updateMuteStatus() {
|
||||
if (!this.localAVStream) {
|
||||
return;
|
||||
@@ -741,8 +832,11 @@ export class MatrixCall extends EventEmitter {
|
||||
return;
|
||||
}
|
||||
if (this.callHasEnded()) {
|
||||
this.stopAllMedia();
|
||||
return;
|
||||
}
|
||||
this.localAVStream = stream;
|
||||
logger.info("Got local AV stream with id " + this.localAVStream.id);
|
||||
|
||||
this.setState(CallState.CreateOffer);
|
||||
|
||||
@@ -768,25 +862,22 @@ export class MatrixCall extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
this.localAVStream = stream;
|
||||
logger.info("Got local AV stream with id " + this.localAVStream.id);
|
||||
// why do we enable audio (and only audio) tracks here? -- matthew
|
||||
setTracksEnabled(stream.getAudioTracks(), true);
|
||||
this.peerConn = this.createPeerConnection();
|
||||
|
||||
for (const audioTrack of stream.getAudioTracks()) {
|
||||
logger.info("Adding audio track with id " + audioTrack.id);
|
||||
this.peerConn.addTrack(audioTrack, stream);
|
||||
}
|
||||
for (const videoTrack of (this.screenSharingStream || stream).getVideoTracks()) {
|
||||
logger.info("Adding audio track with id " + videoTrack.id);
|
||||
logger.info("Adding video track with id " + videoTrack.id);
|
||||
this.peerConn.addTrack(videoTrack, stream);
|
||||
}
|
||||
|
||||
// Now we wait for the negotiationneeded event
|
||||
};
|
||||
|
||||
private sendAnswer() {
|
||||
private async sendAnswer() {
|
||||
const answerContent = {
|
||||
answer: {
|
||||
sdp: this.peerConn.localDescription.sdp,
|
||||
@@ -808,12 +899,12 @@ export class MatrixCall extends EventEmitter {
|
||||
logger.info(`Discarding ${this.candidateSendQueue.length} candidates that will be sent in answer`);
|
||||
this.candidateSendQueue = [];
|
||||
|
||||
this.sendVoipEvent(EventType.CallAnswer, answerContent).then(() => {
|
||||
try {
|
||||
await this.sendVoipEvent(EventType.CallAnswer, answerContent);
|
||||
// If this isn't the first time we've tried to send the answer,
|
||||
// we may have candidates queued up, so send them now.
|
||||
this.inviteOrAnswerSent = true;
|
||||
this.sendCandidateQueue();
|
||||
}).catch((error) => {
|
||||
} catch (error) {
|
||||
// We've failed to answer: back to the ringing state
|
||||
this.setState(CallState.Ringing);
|
||||
this.client.cancelPendingEvent(error.event);
|
||||
@@ -826,7 +917,11 @@ export class MatrixCall extends EventEmitter {
|
||||
}
|
||||
this.emit(CallEvent.Error, new CallError(code, message, error));
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
// error handler re-throws so this won't happen on error, but
|
||||
// we don't want the same error handling on the candidate queue
|
||||
this.sendCandidateQueue();
|
||||
}
|
||||
|
||||
private gotUserMediaForAnswer = async (stream: MediaStream) => {
|
||||
@@ -930,37 +1025,33 @@ export class MatrixCall extends EventEmitter {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.partyIdMatches(ev.getContent())) {
|
||||
logger.info(
|
||||
`Ignoring candidates from party ID ${ev.getContent().party_id}: ` +
|
||||
`we have chosen party ID ${this.opponentPartyId}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const cands = ev.getContent().candidates;
|
||||
if (!cands) {
|
||||
logger.info("Ignoring candidates event with no candidates!");
|
||||
return;
|
||||
}
|
||||
|
||||
for (const cand of cands) {
|
||||
if (
|
||||
(cand.sdpMid === null || cand.sdpMid === undefined) &&
|
||||
(cand.sdpMLineIndex === null || cand.sdpMLineIndex === undefined)
|
||||
) {
|
||||
logger.debug("Ignoring remote ICE candidate with no sdpMid or sdpMLineIndex");
|
||||
return;
|
||||
}
|
||||
logger.debug("Got remote ICE " + cand.sdpMid + " candidate: " + cand.candidate);
|
||||
try {
|
||||
this.peerConn.addIceCandidate(cand);
|
||||
} catch (err) {
|
||||
if (!this.ignoreOffer) {
|
||||
logger.info("Failed to add remore ICE candidate", err);
|
||||
}
|
||||
}
|
||||
const fromPartyId = ev.getContent().version === 0 ? null : ev.getContent().party_id || null;
|
||||
|
||||
if (this.opponentPartyId === undefined) {
|
||||
// we haven't picked an opponent yet so save the candidates
|
||||
logger.info(`Bufferring ${cands.length} candidates until we pick an opponent`);
|
||||
const bufferedCands = this.remoteCandidateBuffer.get(fromPartyId) || [];
|
||||
bufferedCands.push(...cands);
|
||||
this.remoteCandidateBuffer.set(fromPartyId, bufferedCands);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.partyIdMatches(ev.getContent())) {
|
||||
logger.info(
|
||||
`Ignoring candidates from party ID ${ev.getContent().party_id}: ` +
|
||||
`we have chosen party ID ${this.opponentPartyId}`,
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.addIceCandidates(cands);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -980,12 +1071,7 @@ export class MatrixCall extends EventEmitter {
|
||||
return;
|
||||
}
|
||||
|
||||
this.opponentVersion = event.getContent().version;
|
||||
if (this.opponentVersion !== 0) {
|
||||
this.opponentPartyId = event.getContent().party_id || null;
|
||||
}
|
||||
this.opponentCaps = event.getContent().capabilities || {};
|
||||
this.opponentMember = event.sender;
|
||||
this.chooseOpponent(event);
|
||||
|
||||
this.setState(CallState.Connecting);
|
||||
|
||||
@@ -1069,8 +1155,21 @@ export class MatrixCall extends EventEmitter {
|
||||
await this.peerConn.setRemoteDescription(description);
|
||||
|
||||
if (description.type === 'offer') {
|
||||
// First we sent the direction of the tranciever to what we'd like it to be,
|
||||
// irresepective of whether the other side has us on hold - so just whether we
|
||||
// want the call to be on hold or not. This is necessary because in a few lines,
|
||||
// we'll adjust the direction and unless we do this too, we'll never come off hold.
|
||||
for (const tranceiver of this.peerConn.getTransceivers()) {
|
||||
tranceiver.direction = this.isRemoteOnHold() ? 'inactive' : 'sendrecv';
|
||||
}
|
||||
const localDescription = await this.peerConn.createAnswer();
|
||||
await this.peerConn.setLocalDescription(localDescription);
|
||||
// Now we've got our answer, set the direction to the outcome of the negotiation.
|
||||
// We need to do this otherwise Firefox will notice that the direction is not the
|
||||
// currentDirection and try to negotiate itself off hold again.
|
||||
for (const tranceiver of this.peerConn.getTransceivers()) {
|
||||
tranceiver.direction = tranceiver.currentDirection;
|
||||
}
|
||||
|
||||
this.sendVoipEvent(EventType.CallNegotiate, {
|
||||
description: this.peerConn.localDescription,
|
||||
@@ -1147,19 +1246,9 @@ export class MatrixCall extends EventEmitter {
|
||||
|
||||
try {
|
||||
await this.sendVoipEvent(eventType, content);
|
||||
this.sendCandidateQueue();
|
||||
if (this.state === CallState.CreateOffer) {
|
||||
this.inviteOrAnswerSent = true;
|
||||
this.setState(CallState.InviteSent);
|
||||
this.inviteTimeout = setTimeout(() => {
|
||||
this.inviteTimeout = null;
|
||||
if (this.state === CallState.InviteSent) {
|
||||
this.hangup(CallErrorCode.InviteTimeout, false);
|
||||
}
|
||||
}, CALL_TIMEOUT_MS);
|
||||
}
|
||||
} catch (error) {
|
||||
this.client.cancelPendingEvent(error.event);
|
||||
logger.error("Failed to send invite", error);
|
||||
if (error.event) this.client.cancelPendingEvent(error.event);
|
||||
|
||||
let code = CallErrorCode.SignallingFailed;
|
||||
let message = "Signalling failed";
|
||||
@@ -1174,6 +1263,22 @@ export class MatrixCall extends EventEmitter {
|
||||
|
||||
this.emit(CallEvent.Error, new CallError(code, message, error));
|
||||
this.terminate(CallParty.Local, code, false);
|
||||
|
||||
// no need to carry on & send the candidate queue, but we also
|
||||
// don't want to rethrow the error
|
||||
return;
|
||||
}
|
||||
|
||||
this.sendCandidateQueue();
|
||||
if (this.state === CallState.CreateOffer) {
|
||||
this.inviteOrAnswerSent = true;
|
||||
this.setState(CallState.InviteSent);
|
||||
this.inviteTimeout = setTimeout(() => {
|
||||
this.inviteTimeout = null;
|
||||
if (this.state === CallState.InviteSent) {
|
||||
this.hangup(CallErrorCode.InviteTimeout, false);
|
||||
}
|
||||
}, CALL_TIMEOUT_MS);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1214,7 +1319,7 @@ export class MatrixCall extends EventEmitter {
|
||||
return; // because ICE can still complete as we're ending the call
|
||||
}
|
||||
logger.debug(
|
||||
"ICE connection state changed to: " + this.peerConn.iceConnectionState,
|
||||
"Call ID " + this.callId + ": ICE connection state changed to: " + this.peerConn.iceConnectionState,
|
||||
);
|
||||
// ideally we'd consider the call to be connected when we get media but
|
||||
// chrome doesn't implement any of the 'onstarted' events yet
|
||||
@@ -1332,11 +1437,11 @@ export class MatrixCall extends EventEmitter {
|
||||
}
|
||||
|
||||
onHangupReceived = (msg) => {
|
||||
logger.debug("Hangup received");
|
||||
logger.debug("Hangup received for call ID " + this.callId);
|
||||
|
||||
// party ID must match (our chosen partner hanging up the call) or be undefined (we haven't chosen
|
||||
// a partner yet but we're treating the hangup as a reject as per VoIP v0)
|
||||
if (this.partyIdMatches(msg) || this.opponentPartyId === undefined || this.state === CallState.Ringing) {
|
||||
if (this.partyIdMatches(msg) || this.state === CallState.Ringing) {
|
||||
// default reason is user_hangup
|
||||
this.terminate(CallParty.Remote, msg.reason || CallErrorCode.UserHangup, true);
|
||||
} else {
|
||||
@@ -1345,7 +1450,7 @@ export class MatrixCall extends EventEmitter {
|
||||
};
|
||||
|
||||
onRejectReceived = (msg) => {
|
||||
logger.debug("Reject received");
|
||||
logger.debug("Reject received for call ID " + this.callId);
|
||||
|
||||
// No need to check party_id for reject because if we'd received either
|
||||
// an answer or reject, we wouldn't be in state InviteSent
|
||||
@@ -1367,7 +1472,7 @@ export class MatrixCall extends EventEmitter {
|
||||
};
|
||||
|
||||
onAnsweredElsewhere = (msg) => {
|
||||
logger.debug("Answered elsewhere");
|
||||
logger.debug("Call ID " + this.callId + " answered elsewhere");
|
||||
this.terminate(CallParty.Remote, CallErrorCode.AnsweredElsewhere, true);
|
||||
};
|
||||
|
||||
@@ -1440,6 +1545,8 @@ export class MatrixCall extends EventEmitter {
|
||||
private async terminate(hangupParty: CallParty, hangupReason: CallErrorCode, shouldEmit: boolean) {
|
||||
if (this.callHasEnded()) return;
|
||||
|
||||
this.callStatsAtEnd = await this.collectCallStats();
|
||||
|
||||
if (this.inviteTimeout) {
|
||||
clearTimeout(this.inviteTimeout);
|
||||
this.inviteTimeout = null;
|
||||
@@ -1508,7 +1615,7 @@ export class MatrixCall extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
private sendCandidateQueue() {
|
||||
private async sendCandidateQueue() {
|
||||
if (this.candidateSendQueue.length === 0) {
|
||||
return;
|
||||
}
|
||||
@@ -1520,20 +1627,28 @@ export class MatrixCall extends EventEmitter {
|
||||
candidates: cands,
|
||||
};
|
||||
logger.debug("Attempting to send " + cands.length + " candidates");
|
||||
this.sendVoipEvent(EventType.CallCandidates, content).then(() => {
|
||||
this.candidateSendTries = 0;
|
||||
this.sendCandidateQueue();
|
||||
}, (error) => {
|
||||
for (let i = 0; i < cands.length; i++) {
|
||||
this.candidateSendQueue.push(cands[i]);
|
||||
}
|
||||
try {
|
||||
await this.sendVoipEvent(EventType.CallCandidates, content);
|
||||
} catch (error) {
|
||||
// don't retry this event: we'll send another one later as we might
|
||||
// have more candidates by then.
|
||||
if (error.event) this.client.cancelPendingEvent(error.event);
|
||||
|
||||
// put all the candidates we failed to send back in the queue
|
||||
this.candidateSendQueue.push(...cands);
|
||||
|
||||
if (this.candidateSendTries > 5) {
|
||||
logger.debug(
|
||||
"Failed to send candidates on attempt " + this.candidateSendTries +
|
||||
". Giving up for now.", error,
|
||||
". Giving up on this call.", error,
|
||||
);
|
||||
this.candidateSendTries = 0;
|
||||
|
||||
const code = CallErrorCode.SignallingFailed;
|
||||
const message = "Signalling failed";
|
||||
|
||||
this.emit(CallEvent.Error, new CallError(code, message, error));
|
||||
this.hangup(code, false);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1543,7 +1658,7 @@ export class MatrixCall extends EventEmitter {
|
||||
setTimeout(() => {
|
||||
this.sendCandidateQueue();
|
||||
}, delayMs);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async placeCallWithConstraints(constraints: MediaStreamConstraints) {
|
||||
@@ -1553,11 +1668,18 @@ export class MatrixCall extends EventEmitter {
|
||||
this.setState(CallState.WaitLocalMedia);
|
||||
this.direction = CallDirection.Outbound;
|
||||
this.config = constraints;
|
||||
// It would be really nice if we could start gathering candidates at this point
|
||||
// so the ICE agent could be gathering while we open our media devices: we already
|
||||
// know the type of the call and therefore what tracks we want to send.
|
||||
// Perhaps we could do this by making fake tracks now and then using replaceTrack()
|
||||
// once we have the actual tracks? (Can we make fake tracks?)
|
||||
|
||||
// make sure we have valid turn creds. Unless something's gone wrong, it should
|
||||
// poll and keep the credentials valid so this should be instant.
|
||||
const haveTurnCreds = await this.client._checkTurnServers();
|
||||
if (!haveTurnCreds) {
|
||||
logger.warn("Failed to get TURN credentials! Proceeding with call anyway...");
|
||||
}
|
||||
|
||||
// create the peer connection now so it can be gathering candidates while we get user
|
||||
// media (assuming a candidate pool size is configured)
|
||||
this.peerConn = this.createPeerConnection();
|
||||
|
||||
try {
|
||||
const mediaStream = await navigator.mediaDevices.getUserMedia(constraints);
|
||||
this.gotUserMediaForInvite(mediaStream);
|
||||
@@ -1571,6 +1693,7 @@ export class MatrixCall extends EventEmitter {
|
||||
const pc = new window.RTCPeerConnection({
|
||||
iceTransportPolicy: this.forceTURN ? 'relay' : undefined,
|
||||
iceServers: this.turnServers,
|
||||
iceCandidatePoolSize: this.client._iceCandidatePoolSize,
|
||||
});
|
||||
|
||||
// 'connectionstatechange' would be better, but firefox doesn't implement that.
|
||||
@@ -1586,9 +1709,60 @@ export class MatrixCall extends EventEmitter {
|
||||
|
||||
private partyIdMatches(msg): boolean {
|
||||
// They must either match or both be absent (in which case opponentPartyId will be null)
|
||||
const msgPartyId = msg.party_id || null;
|
||||
// Also we ignore party IDs on the invite/offer if the version is 0, so we must do the same
|
||||
// here and use null if the version is 0 (woe betide any opponent sending messages in the
|
||||
// same call with different versions)
|
||||
const msgPartyId = msg.version === 0 ? null : msg.party_id || null;
|
||||
return msgPartyId === this.opponentPartyId;
|
||||
}
|
||||
|
||||
// Commits to an opponent for the call
|
||||
// ev: An invite or answer event
|
||||
private chooseOpponent(ev: MatrixEvent) {
|
||||
// I choo-choo-choose you
|
||||
const msg = ev.getContent();
|
||||
|
||||
this.opponentVersion = msg.version;
|
||||
if (this.opponentVersion === 0) {
|
||||
// set to null to indicate that we've chosen an opponent, but because
|
||||
// they're v0 they have no party ID (even if they sent one, we're ignoring it)
|
||||
this.opponentPartyId = null;
|
||||
} else {
|
||||
// set to their party ID, or if they're naughty and didn't send one despite
|
||||
// not being v0, set it to null to indicate we picked an opponent with no
|
||||
// party ID
|
||||
this.opponentPartyId = msg.party_id || null;
|
||||
}
|
||||
this.opponentCaps = msg.capabilities || {};
|
||||
this.opponentMember = ev.sender;
|
||||
|
||||
const bufferedCands = this.remoteCandidateBuffer.get(this.opponentPartyId);
|
||||
if (bufferedCands) {
|
||||
logger.info(`Adding ${bufferedCands.length} buffered candidates for opponent ${this.opponentPartyId}`);
|
||||
this.addIceCandidates(bufferedCands);
|
||||
}
|
||||
this.remoteCandidateBuffer = null;
|
||||
}
|
||||
|
||||
private addIceCandidates(cands: RTCIceCandidate[]) {
|
||||
for (const cand of cands) {
|
||||
if (
|
||||
(cand.sdpMid === null || cand.sdpMid === undefined) &&
|
||||
(cand.sdpMLineIndex === null || cand.sdpMLineIndex === undefined)
|
||||
) {
|
||||
logger.debug("Ignoring remote ICE candidate with no sdpMid or sdpMLineIndex");
|
||||
return;
|
||||
}
|
||||
logger.debug("Got remote ICE " + cand.sdpMid + " candidate: " + cand.candidate);
|
||||
try {
|
||||
this.peerConn.addIceCandidate(cand);
|
||||
} catch (err) {
|
||||
if (!this.ignoreOffer) {
|
||||
logger.info("Failed to add remore ICE candidate", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setTracksEnabled(tracks: Array<MediaStreamTrack>, enabled: boolean) {
|
||||
@@ -1597,17 +1771,19 @@ function setTracksEnabled(tracks: Array<MediaStreamTrack>, enabled: boolean) {
|
||||
}
|
||||
}
|
||||
|
||||
function getUserMediaVideoContraints(callType: CallType) {
|
||||
function getUserMediaContraints(type: ConstraintsType) {
|
||||
const isWebkit = !!navigator.webkitGetUserMedia;
|
||||
|
||||
switch (callType) {
|
||||
case CallType.Voice:
|
||||
switch (type) {
|
||||
case ConstraintsType.Audio: {
|
||||
return {
|
||||
audio: {
|
||||
deviceId: audioInput ? {ideal: audioInput} : undefined,
|
||||
}, video: false,
|
||||
},
|
||||
video: false,
|
||||
};
|
||||
case CallType.Video:
|
||||
}
|
||||
case ConstraintsType.Video: {
|
||||
return {
|
||||
audio: {
|
||||
deviceId: audioInput ? {ideal: audioInput} : undefined,
|
||||
@@ -1622,6 +1798,33 @@ function getUserMediaVideoContraints(callType: CallType) {
|
||||
height: isWebkit ? { exact: 360 } : { ideal: 360 },
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function getScreenshareContraints(selectDesktopCapturerSource?: () => Promise<DesktopCapturerSource>) {
|
||||
if (window.electron?.getDesktopCapturerSources && selectDesktopCapturerSource) {
|
||||
// We have access to getDesktopCapturerSources()
|
||||
logger.debug("Electron getDesktopCapturerSources() is available...");
|
||||
const selectedSource = await selectDesktopCapturerSource();
|
||||
if (!selectedSource) return null;
|
||||
return {
|
||||
audio: false,
|
||||
video: {
|
||||
mandatory: {
|
||||
chromeMediaSource: "desktop",
|
||||
chromeMediaSourceId: selectedSource.id,
|
||||
},
|
||||
},
|
||||
};
|
||||
} else {
|
||||
// We do not have access to the Electron desktop capturer,
|
||||
// therefore we can assume we are on the web
|
||||
logger.debug("Electron desktopCapturer is not available...");
|
||||
return {
|
||||
audio: false,
|
||||
video: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -138,6 +138,8 @@ export class CallEventHandler {
|
||||
);
|
||||
}
|
||||
|
||||
const timeUntilTurnCresExpire = this.client.getTurnServersExpiry() - Date.now();
|
||||
logger.info("Current turn creds expire in " + timeUntilTurnCresExpire + " ms");
|
||||
call = createNewMatrixCall(this.client, event.getRoomId(), {
|
||||
forceTURN: this.client._forceTURN,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user