sendingnetwork-bot-sdk
Version:
TypeScript/JavaScript SDK for SDN bots
113 lines • 4.42 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RoomTracker = void 0;
// noinspection ES6RedundantAwait
/**
* Tracks room encryption status for a SDNClient.
* @category Encryption
*/
class RoomTracker {
constructor(client) {
this.client = client;
}
/**
* Handles a room join
* @internal
* @param roomId The room ID.
*/
onRoomJoin(roomId) {
return __awaiter(this, void 0, void 0, function* () {
yield this.queueRoomCheck(roomId);
});
}
/**
* Handles a room event.
* @internal
* @param roomId The room ID.
* @param event The event.
*/
onRoomEvent(roomId, event) {
return __awaiter(this, void 0, void 0, function* () {
if (event['state_key'] !== '')
return; // we don't care about anything else
if (event['type'] === 'm.room.encryption' || event['type'] === 'm.room.history_visibility') {
yield this.queueRoomCheck(roomId);
}
});
}
/**
* Prepares the room tracker to track the given rooms.
* @param {string[]} roomIds The room IDs to track. This should be the joined rooms set.
*/
prepare(roomIds) {
return __awaiter(this, void 0, void 0, function* () {
for (const roomId of roomIds) {
yield this.queueRoomCheck(roomId);
}
});
}
/**
* Queues a room check for the tracker. If the room needs an update to the store, an
* update will be made.
* @param {string} roomId The room ID to check.
*/
queueRoomCheck(roomId) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const config = yield this.client.cryptoStore.getRoom(roomId);
if (config) {
if (config.algorithm !== undefined) {
return; // assume no change to encryption config
}
}
let encEvent;
try {
encEvent = yield this.client.getRoomStateEvent(roomId, "m.room.encryption", "");
encEvent.algorithm = (_a = encEvent.algorithm) !== null && _a !== void 0 ? _a : 'UNKNOWN';
}
catch (e) {
return; // failure == no encryption
}
// Pick out the history visibility setting too
let historyVisibility;
try {
const ev = yield this.client.getRoomStateEvent(roomId, "m.room.history_visibility", "");
historyVisibility = ev.history_visibility;
}
catch (e) {
// ignore - we'll just treat history visibility as normal
}
yield this.client.cryptoStore.storeRoom(roomId, Object.assign(Object.assign({}, encEvent), { historyVisibility }));
});
}
/**
* Gets the room's crypto configuration, as known by the underlying store. If the room is
* not encrypted then this will return an empty object.
* @param {string} roomId The room ID to get the config for.
* @returns {Promise<ICryptoRoomInformation>} Resolves to the encryption config.
*/
getRoomCryptoConfig(roomId) {
return __awaiter(this, void 0, void 0, function* () {
let config = yield this.client.cryptoStore.getRoom(roomId);
if (!config) {
yield this.queueRoomCheck(roomId);
config = yield this.client.cryptoStore.getRoom(roomId);
}
if (!config) {
return {};
}
return config;
});
}
}
exports.RoomTracker = RoomTracker;
//# sourceMappingURL=RoomTracker.js.map