Move list watching into Mjolnir class

This commit is contained in:
Travis Ralston 2019-10-08 18:25:57 +01:00
parent 9056634124
commit 1f449a9fc1
3 changed files with 56 additions and 18 deletions

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { LogService, MatrixClient } from "matrix-bot-sdk";
import { LogService, MatrixClient, Permalinks } from "matrix-bot-sdk";
import BanList, { ALL_RULE_TYPES } from "./models/BanList";
import { applyServerAcls } from "./actions/ApplyAcl";
import { RoomUpdateError } from "./models/RoomUpdateError";
@ -37,7 +37,7 @@ export class Mjolnir {
public readonly client: MatrixClient,
public readonly managementRoomId: string,
public readonly protectedRooms: { [roomId: string]: string },
public readonly banLists: BanList[],
private banLists: BanList[],
) {
client.on("room.event", this.handleEvent.bind(this));
@ -69,6 +69,10 @@ export class Mjolnir {
})
}
public get lists(): BanList[] {
return this.banLists;
}
public get state(): string {
return this.currentState;
}
@ -88,6 +92,7 @@ export class Mjolnir {
if (config.verboseLogging) {
await this.client.sendNotice(this.managementRoomId, "Syncing lists...");
}
await this.buildWatchedBanLists();
await this.syncLists(config.verboseLogging);
}
}).then(async () => {
@ -98,6 +103,52 @@ export class Mjolnir {
});
}
public async watchList(roomRef: string) {
const joinedRooms = await this.client.getJoinedRooms();
const permalink = Permalinks.parseUrl(roomRef);
if (!permalink.roomIdOrAlias) return;
const roomId = await this.client.resolveRoom(permalink.roomIdOrAlias);
if (!joinedRooms.includes(roomId)) {
await this.client.joinRoom(permalink.roomIdOrAlias, permalink.viaServers);
}
if (this.banLists.find(b => b.roomId === roomId)) return;
const list = new BanList(roomId, roomRef, this.client);
await list.updateList();
this.banLists.push(list);
}
public async unwatchList(roomRef: string) {
const permalink = Permalinks.parseUrl(roomRef);
if (!permalink.roomIdOrAlias) return;
const roomId = await this.client.resolveRoom(permalink.roomIdOrAlias);
const list = this.banLists.find(b => b.roomId === roomId);
if (list) this.banLists.splice(this.banLists.indexOf(list), 1);
}
public async buildWatchedBanLists() {
const banLists: BanList[] = [];
const joinedRooms = await this.client.getJoinedRooms();
for (const roomRef of config.banLists) {
const permalink = Permalinks.parseUrl(roomRef);
if (!permalink.roomIdOrAlias) continue;
const roomId = await this.client.resolveRoom(permalink.roomIdOrAlias);
if (!joinedRooms.includes(roomId)) {
await this.client.joinRoom(permalink.roomIdOrAlias, permalink.viaServers);
}
const list = new BanList(roomId, roomRef, this.client);
await list.updateList();
banLists.push(list);
}
this.banLists = banLists;
}
public async verifyPermissions(verbose: boolean = true) {
const errors: RoomUpdateError[] = [];
for (const roomId of Object.keys(this.protectedRooms)) {

View File

@ -54,9 +54,10 @@ export async function execCreateListCommand(roomId: string, event: any, mjolnir:
});
const roomRef = Permalinks.forRoom(listRoomId);
await mjolnir.watchList(roomRef);
const html = `Created new list (<a href="${roomRef}">${listRoomId}</a>). It is not tracked - you will have to add this to the Mjolnir config to use it.`;
const text = `Created new list (${roomRef}). It is not tracked - you will have to add this to the Mjolnir config to use it.`;
const html = `Created new list (<a href="${roomRef}">${listRoomId}</a>). This list is now being watched.`;
const text = `Created new list (${roomRef}). This list is now being watched.`;
const reply = RichReply.createFor(roomId, event, text, html);
reply["msgtype"] = "m.notice";
await mjolnir.client.sendMessage(roomId, reply);

View File

@ -48,21 +48,7 @@ LogService.setLogger(new RichConsoleLogger());
const banLists: BanList[] = [];
const protectedRooms: { [roomId: string]: string } = {};
// Ensure we're in all the rooms we expect to be in
const joinedRooms = await client.getJoinedRooms();
for (const roomRef of config.banLists) {
const permalink = Permalinks.parseUrl(roomRef);
if (!permalink.roomIdOrAlias) continue;
const roomId = await client.resolveRoom(permalink.roomIdOrAlias);
if (!joinedRooms.includes(roomId)) {
await client.joinRoom(permalink.roomIdOrAlias, permalink.viaServers);
}
const list = new BanList(roomId, roomRef, client);
await list.updateList();
banLists.push(list);
}
// Ensure we're also joined to the rooms we're protecting
for (const roomRef of config.protectedRooms) {