2019-10-31 20:20:08 +01:00
|
|
|
/*
|
|
|
|
Copyright 2019 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.
|
|
|
|
*/
|
|
|
|
|
2021-06-29 14:11:58 +02:00
|
|
|
import { MatrixClientPeg } from "../MatrixClientPeg";
|
|
|
|
import { ALL_RULE_TYPES, BanList } from "./BanList";
|
2020-07-28 19:53:43 +02:00
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
2021-06-29 14:11:58 +02:00
|
|
|
import { _t } from "../languageHandler";
|
2020-05-14 04:41:41 +02:00
|
|
|
import dis from "../dispatcher/dispatcher";
|
2021-06-29 14:11:58 +02:00
|
|
|
import { SettingLevel } from "../settings/SettingLevel";
|
2021-06-14 21:31:58 +02:00
|
|
|
import { Preset } from "matrix-js-sdk/src/@types/partials";
|
2019-10-31 20:20:08 +01:00
|
|
|
|
|
|
|
// TODO: Move this and related files to the js-sdk or something once finalized.
|
|
|
|
|
|
|
|
export class Mjolnir {
|
|
|
|
static _instance: Mjolnir = null;
|
|
|
|
|
|
|
|
_lists: BanList[] = [];
|
|
|
|
_roomIds: string[] = [];
|
|
|
|
_mjolnirWatchRef = null;
|
2019-10-31 21:24:51 +01:00
|
|
|
_dispatcherRef = null;
|
2019-10-31 20:20:08 +01:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
2019-10-31 22:53:18 +01:00
|
|
|
get roomIds(): string[] {
|
|
|
|
return this._roomIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
get lists(): BanList[] {
|
|
|
|
return this._lists;
|
|
|
|
}
|
|
|
|
|
2019-10-31 20:20:08 +01:00
|
|
|
start() {
|
|
|
|
this._mjolnirWatchRef = SettingsStore.watchSetting("mjolnirRooms", null, this._onListsChanged.bind(this));
|
|
|
|
|
2019-10-31 21:24:51 +01:00
|
|
|
this._dispatcherRef = dis.register(this._onAction);
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'do_after_sync_prepared',
|
2021-06-29 14:11:58 +02:00
|
|
|
deferred_action: { action: 'setup_mjolnir' },
|
2019-10-31 21:24:51 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_onAction = (payload) => {
|
|
|
|
if (payload['action'] === 'setup_mjolnir') {
|
|
|
|
console.log("Setting up Mjolnir: after sync");
|
|
|
|
this.setup();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
setup() {
|
|
|
|
if (!MatrixClientPeg.get()) return;
|
|
|
|
this._updateLists(SettingsStore.getValue("mjolnirRooms"));
|
2020-02-20 03:35:30 +01:00
|
|
|
MatrixClientPeg.get().on("RoomState.events", this._onEvent);
|
2019-10-31 20:20:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
2020-03-09 16:24:55 +01:00
|
|
|
if (this._mjolnirWatchRef) {
|
|
|
|
SettingsStore.unwatchSetting(this._mjolnirWatchRef);
|
|
|
|
this._mjolnirWatchRef = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._dispatcherRef) {
|
|
|
|
dis.unregister(this._dispatcherRef);
|
|
|
|
this._dispatcherRef = null;
|
2019-10-31 23:43:03 +01:00
|
|
|
}
|
2019-10-31 21:24:51 +01:00
|
|
|
|
|
|
|
if (!MatrixClientPeg.get()) return;
|
2020-02-20 03:35:30 +01:00
|
|
|
MatrixClientPeg.get().removeListener("RoomState.events", this._onEvent);
|
2019-10-31 20:20:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async getOrCreatePersonalList(): Promise<BanList> {
|
|
|
|
let personalRoomId = SettingsStore.getValue("mjolnirPersonalRoom");
|
|
|
|
if (!personalRoomId) {
|
|
|
|
const resp = await MatrixClientPeg.get().createRoom({
|
|
|
|
name: _t("My Ban List"),
|
|
|
|
topic: _t("This is your list of users/servers you have blocked - don't leave the room!"),
|
2021-06-14 21:31:58 +02:00
|
|
|
preset: Preset.PrivateChat,
|
2019-10-31 20:20:08 +01:00
|
|
|
});
|
|
|
|
personalRoomId = resp['room_id'];
|
2019-10-31 23:27:45 +01:00
|
|
|
await SettingsStore.setValue(
|
|
|
|
"mjolnirPersonalRoom", null, SettingLevel.ACCOUNT, personalRoomId);
|
|
|
|
await SettingsStore.setValue(
|
|
|
|
"mjolnirRooms", null, SettingLevel.ACCOUNT, [personalRoomId, ...this._roomIds]);
|
2019-10-31 20:20:08 +01:00
|
|
|
}
|
|
|
|
if (!personalRoomId) {
|
|
|
|
throw new Error("Error finding a room ID to use");
|
|
|
|
}
|
|
|
|
|
|
|
|
let list = this._lists.find(b => b.roomId === personalRoomId);
|
|
|
|
if (!list) list = new BanList(personalRoomId);
|
|
|
|
// we don't append the list to the tracked rooms because it should already be there.
|
|
|
|
// we're just trying to get the caller some utility access to the list
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2019-10-31 21:24:51 +01:00
|
|
|
// get without creating the list
|
|
|
|
getPersonalList(): BanList {
|
|
|
|
const personalRoomId = SettingsStore.getValue("mjolnirPersonalRoom");
|
|
|
|
if (!personalRoomId) return null;
|
|
|
|
|
|
|
|
let list = this._lists.find(b => b.roomId === personalRoomId);
|
|
|
|
if (!list) list = new BanList(personalRoomId);
|
|
|
|
// we don't append the list to the tracked rooms because it should already be there.
|
|
|
|
// we're just trying to get the caller some utility access to the list
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2019-10-31 22:53:18 +01:00
|
|
|
async subscribeToList(roomId: string) {
|
|
|
|
const roomIds = [...this._roomIds, roomId];
|
|
|
|
await SettingsStore.setValue("mjolnirRooms", null, SettingLevel.ACCOUNT, roomIds);
|
|
|
|
this._lists.push(new BanList(roomId));
|
|
|
|
}
|
|
|
|
|
|
|
|
async unsubscribeFromList(roomId: string) {
|
|
|
|
const roomIds = this._roomIds.filter(r => r !== roomId);
|
|
|
|
await SettingsStore.setValue("mjolnirRooms", null, SettingLevel.ACCOUNT, roomIds);
|
|
|
|
this._lists = this._lists.filter(b => b.roomId !== roomId);
|
|
|
|
}
|
|
|
|
|
2020-02-20 03:35:30 +01:00
|
|
|
_onEvent = (event) => {
|
2019-10-31 21:24:51 +01:00
|
|
|
if (!MatrixClientPeg.get()) return;
|
2019-10-31 20:20:08 +01:00
|
|
|
if (!this._roomIds.includes(event.getRoomId())) return;
|
|
|
|
if (!ALL_RULE_TYPES.includes(event.getType())) return;
|
|
|
|
|
|
|
|
this._updateLists(this._roomIds);
|
2020-02-20 03:35:30 +01:00
|
|
|
};
|
2019-10-31 20:20:08 +01:00
|
|
|
|
|
|
|
_onListsChanged(settingName, roomId, atLevel, newValue) {
|
|
|
|
// We know that ban lists are only recorded at one level so we don't need to re-eval them
|
|
|
|
this._updateLists(newValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateLists(listRoomIds: string[]) {
|
2019-10-31 21:24:51 +01:00
|
|
|
if (!MatrixClientPeg.get()) return;
|
|
|
|
|
|
|
|
console.log("Updating Mjolnir ban lists to: " + listRoomIds);
|
2019-10-31 20:20:08 +01:00
|
|
|
this._lists = [];
|
|
|
|
this._roomIds = listRoomIds || [];
|
|
|
|
if (!listRoomIds) return;
|
|
|
|
|
|
|
|
for (const roomId of listRoomIds) {
|
|
|
|
// Creating the list updates it
|
|
|
|
this._lists.push(new BanList(roomId));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isServerBanned(serverName: string): boolean {
|
|
|
|
for (const list of this._lists) {
|
|
|
|
for (const rule of list.serverRules) {
|
|
|
|
if (rule.isMatch(serverName)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
isUserBanned(userId: string): boolean {
|
|
|
|
for (const list of this._lists) {
|
|
|
|
for (const rule of list.userRules) {
|
|
|
|
if (rule.isMatch(userId)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sharedInstance(): Mjolnir {
|
|
|
|
if (!Mjolnir._instance) {
|
|
|
|
Mjolnir._instance = new Mjolnir();
|
|
|
|
}
|
|
|
|
return Mjolnir._instance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|