1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-12-02 17:02:31 +03:00

initial implementation of verification in DMs

This commit is contained in:
Hubert Chathi
2019-10-08 15:44:51 -04:00
parent 8cae00407a
commit d8e8dddd25
6 changed files with 359 additions and 55 deletions

View File

@@ -47,42 +47,54 @@ export default class VerificationBase extends EventEmitter {
*
* @param {string} transactionId the transaction ID to be used when sending events
*
* @param {object} startEvent the m.key.verification.start event that
* @param {string} [roomId] the room to use for verification
*
* @param {object} [startEvent] the m.key.verification.start event that
* initiated this verification, if any
*
* @param {object} request the key verification request object related to
* @param {object} [request] the key verification request object related to
* this verification, if any
*
* @param {object} parent parent verification for this verification, if any
*/
constructor(baseApis, userId, deviceId, transactionId, startEvent, request, parent) {
constructor(baseApis, userId, deviceId, transactionId, roomId, startEvent, request) {
super();
this._baseApis = baseApis;
this.userId = userId;
this.deviceId = deviceId;
this.transactionId = transactionId;
this.startEvent = startEvent;
this.request = request;
if (typeof(roomId) === "string" || roomId instanceof String) {
this.roomId = roomId;
this.startEvent = startEvent;
this.request = request;
} else {
// if room ID was omitted, but start event and request were not
this.startEvent= roomId;
this.request = startEvent;
}
this.cancelled = false;
this._parent = parent;
this._done = false;
this._promise = null;
this._transactionTimeoutTimer = null;
// At this point, the verification request was received so start the timeout timer.
this._resetTimer();
if (this.roomId) {
this._send = this._sendMessage;
} else {
this._send = this._sendToDevice;
}
}
_resetTimer() {
console.log("Refreshing/starting the verification transaction timeout timer");
logger.info("Refreshing/starting the verification transaction timeout timer");
if (this._transactionTimeoutTimer !== null) {
clearTimeout(this._transactionTimeoutTimer);
}
this._transactionTimeoutTimer = setTimeout(() => {
if (!this._done && !this.cancelled) {
console.log("Triggering verification timeout");
this.cancel(timeoutException);
}
if (!this._done && !this.cancelled) {
logger.info("Triggering verification timeout");
this.cancel(timeoutException);
}
}, 10 * 60 * 1000); // 10 minutes
}
@@ -93,6 +105,8 @@ export default class VerificationBase extends EventEmitter {
}
}
/* send a message to the other participant, using to-device messages
*/
_sendToDevice(type, content) {
if (this._done) {
return Promise.reject(new Error("Verification is already done"));
@@ -103,6 +117,21 @@ export default class VerificationBase extends EventEmitter {
});
}
/* send a message to the other participant, using in-roomm messages
*/
_sendMessage(type, content) {
if (this._done) {
return Promise.reject(new Error("Verification is already done"));
}
// FIXME: only use one of m.relationship/m.relates_to, once MSC1849
// decides which one to use
content["m.relationship"] = content["m.relates_to"] = {
rel_type: "m.reference",
event_id: this.transactionId,
};
return this._baseApis.sendEvent(this.roomId, type, content);
}
_waitForEvent(type) {
if (this._done) {
return Promise.reject(new Error("Verification is already done"));
@@ -153,7 +182,7 @@ export default class VerificationBase extends EventEmitter {
// cancelled by the other user)
if (e === timeoutException) {
const timeoutEvent = newTimeoutError();
this._sendToDevice(timeoutEvent.getType(), timeoutEvent.getContent());
this._send(timeoutEvent.getType(), timeoutEvent.getContent());
} else if (e instanceof MatrixEvent) {
const sender = e.getSender();
if (sender !== this.userId) {
@@ -163,9 +192,9 @@ export default class VerificationBase extends EventEmitter {
content.reason = content.reason || content.body
|| "Unknown reason";
content.transaction_id = this.transactionId;
this._sendToDevice("m.key.verification.cancel", content);
this._send("m.key.verification.cancel", content);
} else {
this._sendToDevice("m.key.verification.cancel", {
this._send("m.key.verification.cancel", {
code: "m.unknown",
reason: content.body || "Unknown reason",
transaction_id: this.transactionId,
@@ -173,7 +202,7 @@ export default class VerificationBase extends EventEmitter {
}
}
} else {
this._sendToDevice("m.key.verification.cancel", {
this._send("m.key.verification.cancel", {
code: "m.unknown",
reason: e.toString(),
transaction_id: this.transactionId,
@@ -206,11 +235,17 @@ export default class VerificationBase extends EventEmitter {
this._resolve = (...args) => {
this._done = true;
this._endTimer();
if (this.handler) {
this._baseApis.off("event", this.handler);
}
resolve(...args);
};
this._reject = (...args) => {
this._done = true;
this._endTimer();
if (this.handler) {
this._baseApis.off("event", this.handler);
}
reject(...args);
};
});