Add an option to protect all joined rooms

This commit is contained in:
Travis Ralston 2020-01-21 15:19:03 -07:00
parent 97d02b3816
commit c0365416fa
6 changed files with 45 additions and 9 deletions

View File

@ -74,3 +74,11 @@ automaticallyRedactForReasons:
# A list of rooms to protect (matrix.to URLs) # A list of rooms to protect (matrix.to URLs)
protectedRooms: protectedRooms:
- "https://matrix.to/#/#yourroom:example.org" - "https://matrix.to/#/#yourroom:example.org"
# Set this option to true to protect every room the bot is joined to. Note that
# this effectively makes the protectedRooms and associated commands useless because
# the bot by nature must be joined to the room to protect it.
#
# Note: the management room is *excluded* from this condition. Add it to the
# protected rooms to protect it.
protectAllJoinedRooms: false

View File

@ -27,6 +27,6 @@
"config": "^3.2.2", "config": "^3.2.2",
"escape-html": "^1.0.3", "escape-html": "^1.0.3",
"js-yaml": "^3.13.1", "js-yaml": "^3.13.1",
"matrix-bot-sdk": "^0.4.0" "matrix-bot-sdk": "^0.4.1"
} }
} }

View File

@ -44,6 +44,7 @@ export class Mjolnir {
private protections: IProtection[] = []; private protections: IProtection[] = [];
private redactionQueue = new AutomaticRedactionQueue(); private redactionQueue = new AutomaticRedactionQueue();
private automaticRedactionReasons: MatrixGlob[] = []; private automaticRedactionReasons: MatrixGlob[] = [];
private protectedJoinedRoomIds: string[] = [];
constructor( constructor(
public readonly client: MatrixClient, public readonly client: MatrixClient,
@ -75,6 +76,15 @@ export class Mjolnir {
} }
}); });
client.on("room.join", (roomId: string, event: any) => {
LogService.info("Mjolnir", `Joined ${roomId}`);
return this.resyncJoinedRooms();
});
client.on("room.leave", (roomId: string, event: any) => {
LogService.info("Mjolnir", `Left ${roomId}`);
return this.resyncJoinedRooms();
});
client.getUserId().then(userId => { client.getUserId().then(userId => {
this.localpart = userId.split(':')[0].substring(1); this.localpart = userId.split(':')[0].substring(1);
return client.getUserProfile(userId); return client.getUserProfile(userId);
@ -115,6 +125,7 @@ export class Mjolnir {
}).then(async () => { }).then(async () => {
this.currentState = STATE_SYNCING; this.currentState = STATE_SYNCING;
await logMessage(LogLevel.DEBUG, "Mjolnir@startup", "Loading protected rooms..."); await logMessage(LogLevel.DEBUG, "Mjolnir@startup", "Loading protected rooms...");
await this.resyncJoinedRooms(false);
try { try {
const data = await this.client.getAccountData(PROTECTED_ROOMS_EVENT_TYPE); const data = await this.client.getAccountData(PROTECTED_ROOMS_EVENT_TYPE);
if (data && data['rooms']) { if (data && data['rooms']) {
@ -138,8 +149,7 @@ export class Mjolnir {
} }
public async addProtectedRoom(roomId: string) { public async addProtectedRoom(roomId: string) {
const permalink = Permalinks.forRoom(roomId); this.protectedRooms[roomId] = Permalinks.forRoom(roomId);
this.protectedRooms[roomId] = permalink;
let additionalProtectedRooms; let additionalProtectedRooms;
try { try {
@ -167,6 +177,23 @@ export class Mjolnir {
await this.client.setAccountData(PROTECTED_ROOMS_EVENT_TYPE, additionalProtectedRooms); await this.client.setAccountData(PROTECTED_ROOMS_EVENT_TYPE, additionalProtectedRooms);
} }
private async resyncJoinedRooms(withSync = true) {
if (!config.protectAllJoinedRooms) return;
const joinedRoomIds = (await this.client.getJoinedRooms()).filter(r => r !== config.managementRoom);
for (const roomId of this.protectedJoinedRoomIds) {
delete this.protectedRooms[roomId];
}
this.protectedJoinedRoomIds = joinedRoomIds;
for (const roomId of joinedRoomIds) {
this.protectedRooms[roomId] = Permalinks.forRoom(roomId);
}
if (withSync) {
await this.syncLists(config.verboseLogging);
}
}
private async getEnabledProtections() { private async getEnabledProtections() {
let enabled: string[] = []; let enabled: string[] = [];
try { try {

View File

@ -19,8 +19,8 @@ import { RichReply } from "matrix-bot-sdk";
// !mjolnir rooms // !mjolnir rooms
export async function execListProtectedRooms(roomId: string, event: any, mjolnir: Mjolnir) { export async function execListProtectedRooms(roomId: string, event: any, mjolnir: Mjolnir) {
let html = "<b>Protected rooms:</b><br/><ul>"; let html = `<b>Protected rooms (${Object.keys(mjolnir.protectedRooms).length}):</b><br/><ul>`;
let text = "Protected rooms:\n"; let text = `Protected rooms (${Object.keys(mjolnir.protectedRooms).length}):\n`;
let hasRooms = false; let hasRooms = false;
for (const roomId in mjolnir.protectedRooms) { for (const roomId in mjolnir.protectedRooms) {

View File

@ -37,6 +37,7 @@ interface IConfig {
protectedRooms: string[]; // matrix.to urls protectedRooms: string[]; // matrix.to urls
fasterMembershipChecks: boolean; fasterMembershipChecks: boolean;
automaticallyRedactForReasons: string[]; // case-insensitive globs automaticallyRedactForReasons: string[]; // case-insensitive globs
protectAllJoinedRooms: boolean;
/** /**
* Config options only set at runtime. Try to avoid using the objects * Config options only set at runtime. Try to avoid using the objects

View File

@ -1534,10 +1534,10 @@ map-visit@^1.0.0:
dependencies: dependencies:
object-visit "^1.0.0" object-visit "^1.0.0"
matrix-bot-sdk@^0.4.0: matrix-bot-sdk@^0.4.1:
version "0.4.0" version "0.4.1"
resolved "https://registry.yarnpkg.com/matrix-bot-sdk/-/matrix-bot-sdk-0.4.0.tgz#a2189e9cdb01d34b2a9c56003167eb33b1a12752" resolved "https://registry.yarnpkg.com/matrix-bot-sdk/-/matrix-bot-sdk-0.4.1.tgz#4a0a5d5c20a8444612f760c689adba0e7dfe5424"
integrity sha512-ZIICFEYDsSX3emPnVRTV1FIV22zkt7KiJbTYN4rHQ3Z/rk66RB7Y+TMHxrkJCqPs3xVdaGmGkh5m+hNi4fibRg== integrity sha512-ePsmL6ZOVLsSY06fqS3c/3ZWXtfoCk3vUcHyciU+tgRrJqPP2wV15vbs8gHdavIWXmwno3wK95Xc45oH6Uibhg==
dependencies: dependencies:
"@types/express" "^4.17.2" "@types/express" "^4.17.2"
bluebird "^3.7.1" bluebird "^3.7.1"