/* Copyright 2019-2021 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. */ import React from 'react'; import { logger } from "matrix-js-sdk/src/logger"; import { _t } from "../../../../../languageHandler"; import SdkConfig from "../../../../../SdkConfig"; import { Mjolnir } from "../../../../../mjolnir/Mjolnir"; import { ListRule } from "../../../../../mjolnir/ListRule"; import { BanList, RULE_SERVER, RULE_USER } from "../../../../../mjolnir/BanList"; import Modal from "../../../../../Modal"; import { MatrixClientPeg } from "../../../../../MatrixClientPeg"; import { replaceableComponent } from "../../../../../utils/replaceableComponent"; import ErrorDialog from "../../../dialogs/ErrorDialog"; import QuestionDialog from "../../../dialogs/QuestionDialog"; import AccessibleButton from "../../../elements/AccessibleButton"; import Field from "../../../elements/Field"; interface IState { busy: boolean; newPersonalRule: string; newList: string; } @replaceableComponent("views.settings.tabs.user.MjolnirUserSettingsTab") export default class MjolnirUserSettingsTab extends React.Component<{}, IState> { constructor(props) { super(props); this.state = { busy: false, newPersonalRule: "", newList: "", }; } private onPersonalRuleChanged = (e) => { this.setState({ newPersonalRule: e.target.value }); }; private onNewListChanged = (e) => { this.setState({ newList: e.target.value }); }; private onAddPersonalRule = async (e) => { e.preventDefault(); e.stopPropagation(); let kind = RULE_SERVER; if (this.state.newPersonalRule.startsWith("@")) { kind = RULE_USER; } this.setState({ busy: true }); try { const list = await Mjolnir.sharedInstance().getOrCreatePersonalList(); await list.banEntity(kind, this.state.newPersonalRule, _t("Ignored/Blocked")); this.setState({ newPersonalRule: "" }); // this will also cause the new rule to be rendered } catch (e) { logger.error(e); Modal.createTrackedDialog('Failed to add Mjolnir rule', '', ErrorDialog, { title: _t('Error adding ignored user/server'), description: _t('Something went wrong. Please try again or view your console for hints.'), }); } finally { this.setState({ busy: false }); } }; private onSubscribeList = async (e) => { e.preventDefault(); e.stopPropagation(); this.setState({ busy: true }); try { const room = await MatrixClientPeg.get().joinRoom(this.state.newList); await Mjolnir.sharedInstance().subscribeToList(room.roomId); this.setState({ newList: "" }); // this will also cause the new rule to be rendered } catch (e) { logger.error(e); Modal.createTrackedDialog('Failed to subscribe to Mjolnir list', '', ErrorDialog, { title: _t('Error subscribing to list'), description: _t('Please verify the room ID or address and try again.'), }); } finally { this.setState({ busy: false }); } }; private async removePersonalRule(rule: ListRule) { this.setState({ busy: true }); try { const list = Mjolnir.sharedInstance().getPersonalList(); await list.unbanEntity(rule.kind, rule.entity); } catch (e) { logger.error(e); Modal.createTrackedDialog('Failed to remove Mjolnir rule', '', ErrorDialog, { title: _t('Error removing ignored user/server'), description: _t('Something went wrong. Please try again or view your console for hints.'), }); } finally { this.setState({ busy: false }); } } private async unsubscribeFromList(list: BanList) { this.setState({ busy: true }); try { await Mjolnir.sharedInstance().unsubscribeFromList(list.roomId); await MatrixClientPeg.get().leave(list.roomId); } catch (e) { logger.error(e); Modal.createTrackedDialog('Failed to unsubscribe from Mjolnir list', '', ErrorDialog, { title: _t('Error unsubscribing from list'), description: _t('Please try again or view your console for hints.'), }); } finally { this.setState({ busy: false }); } } private viewListRules(list: BanList) { const room = MatrixClientPeg.get().getRoom(list.roomId); const name = room ? room.name : list.roomId; const renderRules = (rules: ListRule[]) => { if (rules.length === 0) return { _t("None") }; const tiles = []; for (const rule of rules) { tiles.push(
  • { rule.entity }
  • ); } return ; }; Modal.createTrackedDialog('View Mjolnir list rules', '', QuestionDialog, { title: _t("Ban list rules - %(roomName)s", { roomName: name }), description: (

    { _t("Server rules") }

    { renderRules(list.serverRules) }

    { _t("User rules") }

    { renderRules(list.userRules) }
    ), button: _t("Close"), hasCancelButton: false, }); } private renderPersonalBanListRules() { const list = Mjolnir.sharedInstance().getPersonalList(); const rules = list ? [...list.userRules, ...list.serverRules] : []; if (!list || rules.length <= 0) return { _t("You have not ignored anyone.") }; const tiles = []; for (const rule of rules) { tiles.push(
  • this.removePersonalRule(rule)} disabled={this.state.busy} > { _t("Remove") }   { rule.entity }
  • , ); } return (

    { _t("You are currently ignoring:") }

    ); } private renderSubscribedBanLists() { const personalList = Mjolnir.sharedInstance().getPersonalList(); const lists = Mjolnir.sharedInstance().lists.filter(b => { return personalList? personalList.roomId !== b.roomId : true; }); if (!lists || lists.length <= 0) return { _t("You are not subscribed to any lists") }; const tiles = []; for (const list of lists) { const room = MatrixClientPeg.get().getRoom(list.roomId); const name = room ? { room.name } ({ list.roomId }) : list.roomId; tiles.push(
  • this.unsubscribeFromList(list)} disabled={this.state.busy} > { _t("Unsubscribe") }   this.viewListRules(list)} disabled={this.state.busy} > { _t("View rules") }   { name }
  • , ); } return (

    { _t("You are currently subscribed to:") }

    ); } render() { const brand = SdkConfig.get().brand; return (
    { _t("Ignored users") }
    { _t("⚠ These settings are meant for advanced users.") }

    { _t( "Add users and servers you want to ignore here. Use asterisks " + "to have %(brand)s match any characters. For example, @bot:* " + "would ignore all users that have the name 'bot' on any server.", { brand }, { code: (s) => { s } }, ) }

    { _t( "Ignoring people is done through ban lists which contain rules for " + "who to ban. Subscribing to a ban list means the users/servers blocked by " + "that list will be hidden from you.", ) }
    { _t("Personal ban list") }
    { _t( "Your personal ban list holds all the users/servers you personally don't " + "want to see messages from. After ignoring your first user/server, a new room " + "will show up in your room list named 'My Ban List' - stay in this room to keep " + "the ban list in effect.", ) }
    { this.renderPersonalBanListRules() }
    { _t("Ignore") }
    { _t("Subscribed lists") }
    { _t("Subscribing to a ban list will cause you to join it!") }   { _t( "If this isn't what you want, please use a different tool to ignore users.", ) }
    { this.renderSubscribedBanLists() }
    { _t("Subscribe") }
    ); } }