Add personal list management to Mjolnir section
This commit is contained in:
parent
e9c8a31e1f
commit
b93508728a
7 changed files with 197 additions and 17 deletions
|
@ -60,17 +60,23 @@ export class BanList {
|
|||
return this._rules.filter(r => r.kind === RULE_ROOM);
|
||||
}
|
||||
|
||||
banEntity(kind: string, entity: string, reason: string): Promise<any> {
|
||||
return MatrixClientPeg.get().sendStateEvent(this._roomId, ruleTypeToStable(kind, true), {
|
||||
async banEntity(kind: string, entity: string, reason: string): Promise<any> {
|
||||
await MatrixClientPeg.get().sendStateEvent(this._roomId, ruleTypeToStable(kind, true), {
|
||||
entity: entity,
|
||||
reason: reason,
|
||||
recommendation: recommendationToStable(RECOMMENDATION_BAN, true),
|
||||
}, "rule:" + entity);
|
||||
this._rules.push(new ListRule(entity, RECOMMENDATION_BAN, reason, ruleTypeToStable(kind, false)));
|
||||
}
|
||||
|
||||
unbanEntity(kind: string, entity: string): Promise<any> {
|
||||
async unbanEntity(kind: string, entity: string): Promise<any> {
|
||||
// Empty state event is effectively deleting it.
|
||||
return MatrixClientPeg.get().sendStateEvent(this._roomId, ruleTypeToStable(kind, true), {}, "rule:" + entity);
|
||||
await MatrixClientPeg.get().sendStateEvent(this._roomId, ruleTypeToStable(kind, true), {}, "rule:" + entity);
|
||||
this._rules = this._rules.filter(r => {
|
||||
if (r.kind !== ruleTypeToStable(kind, false)) return true;
|
||||
if (r.entity !== entity) return true;
|
||||
return false; // we just deleted this rule
|
||||
});
|
||||
}
|
||||
|
||||
updateList() {
|
||||
|
@ -82,7 +88,7 @@ export class BanList {
|
|||
for (const eventType of ALL_RULE_TYPES) {
|
||||
const events = room.currentState.getStateEvents(eventType, undefined);
|
||||
for (const ev of events) {
|
||||
if (!ev['state_key']) continue;
|
||||
if (!ev.getStateKey()) continue;
|
||||
|
||||
const kind = ruleTypeToStable(eventType, false);
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ import MatrixClientPeg from "../MatrixClientPeg";
|
|||
import {ALL_RULE_TYPES, BanList} from "./BanList";
|
||||
import SettingsStore, {SettingLevel} from "../settings/SettingsStore";
|
||||
import {_t} from "../languageHandler";
|
||||
import dis from "../dispatcher";
|
||||
|
||||
// TODO: Move this and related files to the js-sdk or something once finalized.
|
||||
|
||||
|
@ -27,19 +28,39 @@ export class Mjolnir {
|
|||
_lists: BanList[] = [];
|
||||
_roomIds: string[] = [];
|
||||
_mjolnirWatchRef = null;
|
||||
_dispatcherRef = null;
|
||||
|
||||
constructor() {
|
||||
}
|
||||
|
||||
start() {
|
||||
this._updateLists(SettingsStore.getValue("mjolnirRooms"));
|
||||
this._mjolnirWatchRef = SettingsStore.watchSetting("mjolnirRooms", null, this._onListsChanged.bind(this));
|
||||
|
||||
this._dispatcherRef = dis.register(this._onAction);
|
||||
dis.dispatch({
|
||||
action: 'do_after_sync_prepared',
|
||||
deferred_action: {action: 'setup_mjolnir'},
|
||||
});
|
||||
}
|
||||
|
||||
_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"));
|
||||
MatrixClientPeg.get().on("RoomState.events", this._onEvent.bind(this));
|
||||
}
|
||||
|
||||
stop() {
|
||||
SettingsStore.unwatchSetting(this._mjolnirWatchRef);
|
||||
dis.unregister(this._dispatcherRef);
|
||||
|
||||
if (!MatrixClientPeg.get()) return;
|
||||
MatrixClientPeg.get().removeListener("RoomState.events", this._onEvent.bind(this));
|
||||
}
|
||||
|
||||
|
@ -52,8 +73,8 @@ export class Mjolnir {
|
|||
preset: "private_chat"
|
||||
});
|
||||
personalRoomId = resp['room_id'];
|
||||
SettingsStore.setValue("mjolnirPersonalRoom", null, SettingLevel.ACCOUNT, personalRoomId);
|
||||
SettingsStore.setValue("mjolnirRooms", null, SettingLevel.ACCOUNT, [personalRoomId, ...this._roomIds]);
|
||||
await SettingsStore.setValue("mjolnirPersonalRoom", null, SettingLevel.ACCOUNT, personalRoomId);
|
||||
await SettingsStore.setValue("mjolnirRooms", null, SettingLevel.ACCOUNT, [personalRoomId, ...this._roomIds]);
|
||||
}
|
||||
if (!personalRoomId) {
|
||||
throw new Error("Error finding a room ID to use");
|
||||
|
@ -67,7 +88,21 @@ export class Mjolnir {
|
|||
return list;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
_onEvent(event) {
|
||||
if (!MatrixClientPeg.get()) return;
|
||||
if (!this._roomIds.includes(event.getRoomId())) return;
|
||||
if (!ALL_RULE_TYPES.includes(event.getType())) return;
|
||||
|
||||
|
@ -80,6 +115,9 @@ export class Mjolnir {
|
|||
}
|
||||
|
||||
_updateLists(listRoomIds: string[]) {
|
||||
if (!MatrixClientPeg.get()) return;
|
||||
|
||||
console.log("Updating Mjolnir ban lists to: " + listRoomIds);
|
||||
this._lists = [];
|
||||
this._roomIds = listRoomIds || [];
|
||||
if (!listRoomIds) return;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue