|
| 1 | +/* |
| 2 | +Copyright 2019, 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import AwaitLock from 'await-lock'; |
| 18 | +import { extractRequestError, LogService, MatrixClient, Permalinks } from "matrix-bot-sdk"; |
| 19 | +import { IConfig } from "./config"; |
| 20 | +const PROTECTED_ROOMS_EVENT_TYPE = "org.matrix.mjolnir.protected_rooms"; |
| 21 | + |
| 22 | +/** |
| 23 | + * Manages the set of rooms that the user has EXPLICITLY asked to be protected. |
| 24 | + */ |
| 25 | +export default class ProtectedRoomsConfig { |
| 26 | + |
| 27 | + /** |
| 28 | + * These are rooms that we EXPLICITLY asked Mjolnir to protect, usually via the `rooms add` command. |
| 29 | + * These are NOT all of the rooms that mjolnir is protecting as with `config.protectAllJoinedRooms`. |
| 30 | + */ |
| 31 | + private explicitlyProtectedRooms = new Set</*room id*/string>(); |
| 32 | + /** This is to prevent clobbering the account data for the protected rooms if several rooms are explicitly protected concurrently. */ |
| 33 | + private accountDataLock = new AwaitLock(); |
| 34 | + |
| 35 | + constructor(private readonly client: MatrixClient) { |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Load any rooms that have been explicitly protected from a Mjolnir config. |
| 41 | + * Will also ensure we are able to join all of the rooms. |
| 42 | + * @param config The config to load the rooms from under `config.protectedRooms`. |
| 43 | + */ |
| 44 | + public async loadProtectedRoomsFromConfig(config: IConfig): Promise<void> { |
| 45 | + // Ensure we're also joined to the rooms we're protecting |
| 46 | + LogService.info("ProtectedRoomsConfig", "Resolving protected rooms..."); |
| 47 | + const joinedRooms = await this.client.getJoinedRooms(); |
| 48 | + for (const roomRef of config.protectedRooms) { |
| 49 | + const permalink = Permalinks.parseUrl(roomRef); |
| 50 | + if (!permalink.roomIdOrAlias) continue; |
| 51 | + |
| 52 | + let roomId = await this.client.resolveRoom(permalink.roomIdOrAlias); |
| 53 | + if (!joinedRooms.includes(roomId)) { |
| 54 | + roomId = await this.client.joinRoom(permalink.roomIdOrAlias, permalink.viaServers); |
| 55 | + } |
| 56 | + this.explicitlyProtectedRooms.add(roomId); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Load any rooms that have been explicitly protected from the account data of the mjolnir user. |
| 62 | + * Will not ensure we can join all the rooms. This so mjolnir can continue to operate if bogus rooms have been persisted to the account data. |
| 63 | + */ |
| 64 | + public async loadProtectedRoomsFromAccountData(): Promise<void> { |
| 65 | + LogService.debug("ProtectedRoomsConfig", "Loading protected rooms..."); |
| 66 | + try { |
| 67 | + const data: { rooms?: string[] } | null = await this.client.getAccountData(PROTECTED_ROOMS_EVENT_TYPE); |
| 68 | + if (data && data['rooms']) { |
| 69 | + for (const roomId of data['rooms']) { |
| 70 | + this.explicitlyProtectedRooms.add(roomId); |
| 71 | + } |
| 72 | + } |
| 73 | + } catch (e) { |
| 74 | + if (e.statusCode === 404) { |
| 75 | + LogService.warn("ProtectedRoomsConfig", "Couldn't find any explicitly protected rooms from Mjolnir's account data, assuming first start.", extractRequestError(e)); |
| 76 | + } else { |
| 77 | + throw e; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Save the room as explicitly protected. |
| 84 | + * @param roomId The room to persist as explicitly protected. |
| 85 | + */ |
| 86 | + public async addProtectedRoom(roomId: string): Promise<void> { |
| 87 | + this.explicitlyProtectedRooms.add(roomId); |
| 88 | + await this.saveProtectedRoomsToAccountData(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Remove the room from the explicitly protected set of rooms. |
| 93 | + * @param roomId The room that should no longer be persisted as protected. |
| 94 | + */ |
| 95 | + public async removeProtectedRoom(roomId: string): Promise<void> { |
| 96 | + this.explicitlyProtectedRooms.delete(roomId); |
| 97 | + await this.saveProtectedRoomsToAccountData([roomId]); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Get the set of explicitly protected rooms. |
| 102 | + * This will NOT be the complete set of protected rooms, if `config.protectAllJoinedRooms` is true and should never be treated as the complete set. |
| 103 | + * @returns The rooms that are marked as explicitly protected in both the config and Mjolnir's account data. |
| 104 | + */ |
| 105 | + public getExplicitlyProtectedRooms(): string[] { |
| 106 | + return [...this.explicitlyProtectedRooms.keys()] |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Persist the set of explicitly protected rooms to the client's account data. |
| 111 | + * @param excludeRooms Rooms that should not be persisted to the account data, and removed if already present. |
| 112 | + */ |
| 113 | + private async saveProtectedRoomsToAccountData(excludeRooms: string[] = []): Promise<void> { |
| 114 | + // NOTE: this stops Mjolnir from racing with itself when saving the config |
| 115 | + // but it doesn't stop a third party client on the same account racing with us instead. |
| 116 | + await this.accountDataLock.acquireAsync(); |
| 117 | + try { |
| 118 | + const additionalProtectedRooms: string[] = await this.client.getAccountData(PROTECTED_ROOMS_EVENT_TYPE) |
| 119 | + .then((rooms: {rooms?: string[]}) => Array.isArray(rooms?.rooms) ? rooms.rooms : []) |
| 120 | + .catch(e => (LogService.warn("ProtectedRoomsConfig", "Could not load protected rooms from account data", extractRequestError(e)), [])); |
| 121 | + |
| 122 | + const roomsToSave = new Set([...this.explicitlyProtectedRooms.keys(), ...additionalProtectedRooms]); |
| 123 | + excludeRooms.forEach(roomsToSave.delete, roomsToSave); |
| 124 | + await this.client.setAccountData(PROTECTED_ROOMS_EVENT_TYPE, { rooms: Array.from(roomsToSave.keys()) }); |
| 125 | + } finally { |
| 126 | + this.accountDataLock.release(); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments