2017-05-19 10:54:29 +02:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations 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 sdk from './index';
|
|
|
|
import Modal from './Modal';
|
|
|
|
|
|
|
|
export default class KeyRequestHandler {
|
|
|
|
constructor(matrixClient) {
|
|
|
|
this._matrixClient = matrixClient;
|
|
|
|
|
2017-06-01 18:49:24 +02:00
|
|
|
// the user/device for which we currently have a dialog open
|
|
|
|
this._currentUser = null;
|
|
|
|
this._currentDevice = null;
|
2017-05-19 10:54:29 +02:00
|
|
|
|
|
|
|
// userId -> deviceId -> [keyRequest]
|
2017-06-05 18:58:11 +02:00
|
|
|
this._pendingKeyRequests = Object.create(null);
|
2017-05-19 10:54:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyRequest(keyRequest) {
|
|
|
|
const userId = keyRequest.userId;
|
|
|
|
const deviceId = keyRequest.deviceId;
|
2017-06-01 18:49:24 +02:00
|
|
|
const requestId = keyRequest.requestId;
|
2017-05-19 10:54:29 +02:00
|
|
|
|
|
|
|
if (!this._pendingKeyRequests[userId]) {
|
2017-06-05 18:58:11 +02:00
|
|
|
this._pendingKeyRequests[userId] = Object.create(null);
|
2017-05-19 10:54:29 +02:00
|
|
|
}
|
|
|
|
if (!this._pendingKeyRequests[userId][deviceId]) {
|
|
|
|
this._pendingKeyRequests[userId][deviceId] = [];
|
|
|
|
}
|
|
|
|
|
2017-06-01 18:49:24 +02:00
|
|
|
// check if we already have this request
|
|
|
|
const requests = this._pendingKeyRequests[userId][deviceId];
|
|
|
|
if (requests.find((r) => r.requestId === requestId)) {
|
|
|
|
console.log("Already have this key request, ignoring");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
requests.push(keyRequest);
|
|
|
|
|
|
|
|
if (this._currentUser) {
|
2017-05-19 10:54:29 +02:00
|
|
|
// ignore for now
|
|
|
|
console.log("Key request, but we already have a dialog open");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._processNextRequest();
|
|
|
|
}
|
|
|
|
|
2017-06-01 18:49:24 +02:00
|
|
|
handleKeyRequestCancellation(cancellation) {
|
|
|
|
// see if we can find the request in the queue
|
|
|
|
const userId = cancellation.userId;
|
|
|
|
const deviceId = cancellation.deviceId;
|
|
|
|
const requestId = cancellation.requestId;
|
|
|
|
|
|
|
|
if (userId === this._currentUser && deviceId === this._currentDevice) {
|
|
|
|
console.log(
|
|
|
|
"room key request cancellation for the user we currently have a"
|
|
|
|
+ " dialog open for",
|
|
|
|
);
|
|
|
|
// TODO: update the dialog. For now, we just ignore the
|
|
|
|
// cancellation.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._pendingKeyRequests[userId]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const requests = this._pendingKeyRequests[userId][deviceId];
|
|
|
|
if (!requests) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const idx = requests.findIndex((r) => r.requestId === requestId);
|
|
|
|
if (idx < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log("Forgetting room key request");
|
|
|
|
requests.splice(idx, 1);
|
2017-06-05 18:26:25 +02:00
|
|
|
if (requests.length === 0) {
|
|
|
|
delete this._pendingKeyRequests[userId][deviceId];
|
|
|
|
if (Object.keys(this._pendingKeyRequests[userId]).length === 0) {
|
|
|
|
delete this._pendingKeyRequests[userId];
|
|
|
|
}
|
|
|
|
}
|
2017-06-01 18:49:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 10:54:29 +02:00
|
|
|
_processNextRequest() {
|
|
|
|
const userId = Object.keys(this._pendingKeyRequests)[0];
|
|
|
|
if (!userId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const deviceId = Object.keys(this._pendingKeyRequests[userId])[0];
|
|
|
|
if (!deviceId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log(`Starting KeyShareDialog for ${userId}:${deviceId}`);
|
|
|
|
|
|
|
|
const finished = (r) => {
|
2017-06-01 18:49:24 +02:00
|
|
|
this._currentUser = null;
|
|
|
|
this._currentDevice = null;
|
2017-05-19 10:54:29 +02:00
|
|
|
|
|
|
|
if (r) {
|
|
|
|
for (const req of this._pendingKeyRequests[userId][deviceId]) {
|
|
|
|
req.share();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete this._pendingKeyRequests[userId][deviceId];
|
|
|
|
if (Object.keys(this._pendingKeyRequests[userId]).length === 0) {
|
|
|
|
delete this._pendingKeyRequests[userId];
|
|
|
|
}
|
|
|
|
|
|
|
|
this._processNextRequest();
|
|
|
|
};
|
|
|
|
|
|
|
|
const KeyShareDialog = sdk.getComponent("dialogs.KeyShareDialog");
|
2017-07-27 18:19:18 +02:00
|
|
|
Modal.createTrackedDialog('Key Share', 'Process Next Request', KeyShareDialog, {
|
2017-05-19 10:54:29 +02:00
|
|
|
matrixClient: this._matrixClient,
|
|
|
|
userId: userId,
|
|
|
|
deviceId: deviceId,
|
|
|
|
onFinished: finished,
|
|
|
|
});
|
2017-06-01 18:49:24 +02:00
|
|
|
this._currentUser = userId;
|
|
|
|
this._currentDevice = deviceId;
|
2017-05-19 10:54:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|