2020-09-03 19:06:17 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 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 {groupBy} from "../../utils/groupBy.js";
|
|
|
|
import {makeTxnId} from "../common.js";
|
|
|
|
|
2020-09-03 21:20:28 +05:30
|
|
|
const ENCRYPTED_TYPE = "m.room.encrypted";
|
2020-09-03 19:06:17 +05:30
|
|
|
|
|
|
|
export class RoomEncryption {
|
2020-09-03 21:20:28 +05:30
|
|
|
constructor({room, deviceTracker, olmEncryption, megolmEncryption, encryptionParams}) {
|
2020-09-03 19:06:17 +05:30
|
|
|
this._room = room;
|
|
|
|
this._deviceTracker = deviceTracker;
|
|
|
|
this._olmEncryption = olmEncryption;
|
2020-09-03 21:20:28 +05:30
|
|
|
this._megolmEncryption = megolmEncryption;
|
2020-09-03 19:06:17 +05:30
|
|
|
// content of the m.room.encryption event
|
2020-09-03 21:20:28 +05:30
|
|
|
this._encryptionParams = encryptionParams;
|
2020-09-04 15:39:19 +05:30
|
|
|
|
|
|
|
this._megolmBackfillCache = this._megolmDecryption.createSessionCache();
|
|
|
|
this._megolmSyncCache = this._megolmDecryption.createSessionCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyTimelineClosed() {
|
|
|
|
// empty the backfill cache when closing the timeline
|
|
|
|
this._megolmBackfillCache.dispose();
|
|
|
|
this._megolmBackfillCache = this._megolmDecryption.createSessionCache();
|
2020-09-03 19:06:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async writeMemberChanges(memberChanges, txn) {
|
|
|
|
return await this._deviceTracker.writeMemberChanges(this._room, memberChanges, txn);
|
|
|
|
}
|
|
|
|
|
2020-09-04 15:39:19 +05:30
|
|
|
async decryptNewSyncEvent(id, event, txn) {
|
|
|
|
const payload = await this._megolmDecryption.decryptNewEvent(
|
|
|
|
this._room.id, event, this._megolmSyncCache, txn);
|
|
|
|
return payload;
|
|
|
|
}
|
|
|
|
|
|
|
|
async decryptNewGapEvent(id, event, txn) {
|
|
|
|
const payload = await this._megolmDecryption.decryptNewEvent(
|
|
|
|
this._room.id, event, this._megolmBackfillCache, txn);
|
|
|
|
return payload;
|
|
|
|
}
|
|
|
|
|
|
|
|
async decryptStoredEvent(id, event, txn) {
|
|
|
|
const payload = await this._megolmDecryption.decryptStoredEvent(
|
|
|
|
this._room.id, event, this._megolmBackfillCache, txn);
|
|
|
|
return payload;
|
|
|
|
}
|
|
|
|
|
2020-09-03 19:06:17 +05:30
|
|
|
async encrypt(type, content, hsApi) {
|
2020-09-03 21:20:28 +05:30
|
|
|
const megolmResult = await this._megolmEncryption.encrypt(this._room.id, type, content, this._encryptionParams);
|
|
|
|
// share the new megolm session if needed
|
|
|
|
if (megolmResult.roomKeyMessage) {
|
|
|
|
await this._deviceTracker.trackRoom(this._room);
|
|
|
|
const devices = await this._deviceTracker.deviceIdentitiesForTrackedRoom(this._room.id, hsApi);
|
|
|
|
const messages = await this._olmEncryption.encrypt(
|
|
|
|
"m.room_key", megolmResult.roomKeyMessage, devices, hsApi);
|
|
|
|
await this._sendMessagesToDevices(ENCRYPTED_TYPE, messages, hsApi);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
type: ENCRYPTED_TYPE,
|
|
|
|
content: megolmResult.content
|
|
|
|
};
|
2020-09-03 19:06:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async _sendMessagesToDevices(type, messages, hsApi) {
|
|
|
|
const messagesByUser = groupBy(messages, message => message.device.userId);
|
|
|
|
const payload = {
|
|
|
|
messages: Array.from(messagesByUser.entries()).reduce((userMap, [userId, messages]) => {
|
|
|
|
userMap[userId] = messages.reduce((deviceMap, message) => {
|
|
|
|
deviceMap[message.device.deviceId] = message.content;
|
|
|
|
return deviceMap;
|
|
|
|
}, {});
|
|
|
|
return userMap;
|
|
|
|
}, {})
|
|
|
|
};
|
|
|
|
const txnId = makeTxnId();
|
|
|
|
await hsApi.sendToDevice(type, payload, txnId).response();
|
|
|
|
}
|
|
|
|
}
|