2019-10-31 13:19:54 -06:00
/*
2021-04-06 12:26:50 +01:00
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
2019-10-31 13:19:54 -06:00
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.
*/
2023-02-13 11:39:16 +00:00
import React , { ChangeEvent , SyntheticEvent } from "react" ;
2021-10-22 17:23:32 -05:00
import { logger } from "matrix-js-sdk/src/logger" ;
2021-06-29 13:11:58 +01:00
import { _t } from "../../../../../languageHandler" ;
2020-07-10 19:07:11 +01:00
import SdkConfig from "../../../../../SdkConfig" ;
2021-06-29 13:11:58 +01:00
import { Mjolnir } from "../../../../../mjolnir/Mjolnir" ;
import { ListRule } from "../../../../../mjolnir/ListRule" ;
import { BanList , RULE_SERVER , RULE_USER } from "../../../../../mjolnir/BanList" ;
2019-10-31 14:24:51 -06:00
import Modal from "../../../../../Modal" ;
2021-06-29 13:11:58 +01:00
import { MatrixClientPeg } from "../../../../../MatrixClientPeg" ;
2021-07-03 10:06:42 +02:00
import ErrorDialog from "../../../dialogs/ErrorDialog" ;
import QuestionDialog from "../../../dialogs/QuestionDialog" ;
import AccessibleButton from "../../../elements/AccessibleButton" ;
import Field from "../../../elements/Field" ;
2023-05-31 10:42:53 +12:00
import SettingsTab from "../SettingsTab" ;
import { SettingsSection } from "../../shared/SettingsSection" ;
import SettingsSubsection , { SettingsSubsectionText } from "../../shared/SettingsSubsection" ;
2019-10-31 13:19:54 -06:00
2021-04-06 12:26:50 +01:00
interface IState {
busy : boolean ;
newPersonalRule : string ;
newList : string ;
}
export default class MjolnirUserSettingsTab extends React . Component < {}, IState > {
2023-02-13 11:39:16 +00:00
public constructor ( props : {}) {
2021-04-06 12:26:50 +01:00
super ( props );
2019-10-31 14:24:51 -06:00
this . state = {
busy : false ,
newPersonalRule : "" ,
2019-10-31 15:53:18 -06:00
newList : "" ,
2019-10-31 14:24:51 -06:00
};
}
2023-02-13 11:39:16 +00:00
private onPersonalRuleChanged = ( e : ChangeEvent < HTMLInputElement >) : void => {
2021-06-29 13:11:58 +01:00
this . setState ({ newPersonalRule : e.target.value });
2019-10-31 14:24:51 -06:00
};
2023-02-13 11:39:16 +00:00
private onNewListChanged = ( e : ChangeEvent < HTMLInputElement >) : void => {
2021-06-29 13:11:58 +01:00
this . setState ({ newList : e.target.value });
2019-10-31 15:53:18 -06:00
};
2023-02-13 11:39:16 +00:00
private onAddPersonalRule = async ( e : SyntheticEvent ) : Promise < void > => {
2019-10-31 14:24:51 -06:00
e . preventDefault ();
e . stopPropagation ();
let kind = RULE_SERVER ;
if ( this . state . newPersonalRule . startsWith ( "@" )) {
kind = RULE_USER ;
}
2021-06-29 13:11:58 +01:00
this . setState ({ busy : true });
2019-10-31 14:24:51 -06:00
try {
const list = await Mjolnir . sharedInstance (). getOrCreatePersonalList ();
await list . banEntity ( kind , this . state . newPersonalRule , _t ( "Ignored/Blocked" ));
2021-06-29 13:11:58 +01:00
this . setState ({ newPersonalRule : "" }); // this will also cause the new rule to be rendered
2019-10-31 14:24:51 -06:00
} catch ( e ) {
2021-10-15 16:30:53 +02:00
logger . error ( e );
2019-10-31 14:24:51 -06:00
2022-06-14 17:51:51 +01:00
Modal . createDialog ( ErrorDialog , {
2019-10-31 15:53:18 -06:00
title : _t ( "Error adding ignored user/server" ),
2019-10-31 14:24:51 -06:00
description : _t ( "Something went wrong. Please try again or view your console for hints." ),
});
} finally {
2021-06-29 13:11:58 +01:00
this . setState ({ busy : false });
2019-10-31 14:24:51 -06:00
}
};
2023-02-13 11:39:16 +00:00
private onSubscribeList = async ( e : SyntheticEvent ) : Promise < void > => {
2019-10-31 15:53:18 -06:00
e . preventDefault ();
e . stopPropagation ();
2021-06-29 13:11:58 +01:00
this . setState ({ busy : true });
2019-10-31 15:53:18 -06:00
try {
2023-06-21 17:29:44 +01:00
const room = await MatrixClientPeg . safeGet (). joinRoom ( this . state . newList );
2019-10-31 15:53:18 -06:00
await Mjolnir . sharedInstance (). subscribeToList ( room . roomId );
2021-06-29 13:11:58 +01:00
this . setState ({ newList : "" }); // this will also cause the new rule to be rendered
2019-10-31 15:53:18 -06:00
} catch ( e ) {
2021-10-15 16:30:53 +02:00
logger . error ( e );
2019-10-31 15:53:18 -06:00
2022-06-14 17:51:51 +01:00
Modal . createDialog ( ErrorDialog , {
2019-10-31 15:53:18 -06:00
title : _t ( "Error subscribing to list" ),
2020-04-14 10:06:57 +01:00
description : _t ( "Please verify the room ID or address and try again." ),
2019-10-31 15:53:18 -06:00
});
} finally {
2021-06-29 13:11:58 +01:00
this . setState ({ busy : false });
2019-10-31 15:53:18 -06:00
}
};
2023-01-12 13:25:14 +00:00
private async removePersonalRule ( rule : ListRule ) : Promise < void > {
2021-06-29 13:11:58 +01:00
this . setState ({ busy : true });
2019-10-31 14:24:51 -06:00
try {
const list = Mjolnir . sharedInstance (). getPersonalList ();
2023-02-16 09:38:44 +00:00
await list ! . unbanEntity ( rule . kind , rule . entity );
2019-10-31 14:24:51 -06:00
} catch ( e ) {
2021-10-15 16:30:53 +02:00
logger . error ( e );
2019-10-31 14:24:51 -06:00
2022-06-14 17:51:51 +01:00
Modal . createDialog ( ErrorDialog , {
2019-10-31 14:24:51 -06:00
title : _t ( "Error removing ignored user/server" ),
description : _t ( "Something went wrong. Please try again or view your console for hints." ),
});
} finally {
2021-06-29 13:11:58 +01:00
this . setState ({ busy : false });
2019-10-31 14:24:51 -06:00
}
}
2023-01-12 13:25:14 +00:00
private async unsubscribeFromList ( list : BanList ) : Promise < void > {
2021-06-29 13:11:58 +01:00
this . setState ({ busy : true });
2019-10-31 15:53:18 -06:00
try {
await Mjolnir . sharedInstance (). unsubscribeFromList ( list . roomId );
2023-06-21 17:29:44 +01:00
await MatrixClientPeg . safeGet (). leave ( list . roomId );
2019-10-31 15:53:18 -06:00
} catch ( e ) {
2021-10-15 16:30:53 +02:00
logger . error ( e );
2019-10-31 15:53:18 -06:00
2022-06-14 17:51:51 +01:00
Modal . createDialog ( ErrorDialog , {
2019-10-31 15:53:18 -06:00
title : _t ( "Error unsubscribing from list" ),
description : _t ( "Please try again or view your console for hints." ),
});
} finally {
2021-06-29 13:11:58 +01:00
this . setState ({ busy : false });
2019-10-31 15:53:18 -06:00
}
}
2023-01-12 13:25:14 +00:00
private viewListRules ( list : BanList ) : void {
2023-06-21 17:29:44 +01:00
const room = MatrixClientPeg . safeGet (). getRoom ( list . roomId );
2019-10-31 16:00:31 -06:00
const name = room ? room.name : list.roomId ;
2023-01-12 13:25:14 +00:00
const renderRules = ( rules : ListRule []) : JSX . Element => {
2021-07-19 22:43:11 +01:00
if ( rules . length === 0 ) return < i >{ _t ( "None" )}</ i >;
2019-10-31 16:00:31 -06:00
2023-02-16 09:38:44 +00:00
const tiles : JSX.Element [] = [];
2019-10-31 16:00:31 -06:00
for ( const rule of rules ) {
2021-07-19 22:43:11 +01:00
tiles . push (
< li key = { rule . kind + rule . entity }>
< code >{ rule . entity }</ code >
</ li >,
);
2019-10-31 16:00:31 -06:00
}
2021-07-19 22:43:11 +01:00
return < ul >{ tiles }</ ul >;
2019-10-31 16:00:31 -06:00
};
2022-06-14 17:51:51 +01:00
Modal . createDialog ( QuestionDialog , {
2021-06-29 13:11:58 +01:00
title : _t ( "Ban list rules - %(roomName)s" , { roomName : name }),
2019-10-31 16:00:31 -06:00
description : (
< div >
2021-07-19 22:43:11 +01:00
< h3 >{ _t ( "Server rules" )}</ h3 >
{ renderRules ( list . serverRules )}
< h3 >{ _t ( "User rules" )}</ h3 >
{ renderRules ( list . userRules )}
2019-10-31 16:00:31 -06:00
</ div >
),
button : _t ( "Close" ),
hasCancelButton : false ,
});
2019-10-31 15:53:18 -06:00
}
2023-01-12 13:25:14 +00:00
private renderPersonalBanListRules () : JSX . Element {
2019-10-31 14:24:51 -06:00
const list = Mjolnir . sharedInstance (). getPersonalList ();
const rules = list ? [... list . userRules , ... list . serverRules ] : [];
2021-07-19 22:43:11 +01:00
if ( ! list || rules . length <= 0 ) return < i >{ _t ( "You have not ignored anyone." )}</ i >;
2019-10-31 14:24:51 -06:00
2023-02-16 09:38:44 +00:00
const tiles : JSX.Element [] = [];
2019-10-31 14:24:51 -06:00
for ( const rule of rules ) {
tiles . push (
2019-10-31 15:53:18 -06:00
< li key = { rule . entity } className = "mx_MjolnirUserSettingsTab_listItem" >
< AccessibleButton
kind = "danger_sm"
2021-04-26 14:02:53 +01:00
onClick = {() => this . removePersonalRule ( rule )}
2019-10-31 15:53:18 -06:00
disabled = { this . state . busy }
>
2021-07-19 22:43:11 +01:00
{ _t ( "Remove" )}
2019-10-31 14:24:51 -06:00
</ AccessibleButton >
& nbsp ;
2021-07-19 22:43:11 +01:00
< code >{ rule . entity }</ code >
2019-10-31 14:24:51 -06:00
</ li >,
);
}
2019-10-31 15:53:18 -06:00
return (
< div >
2021-07-19 22:43:11 +01:00
< p >{ _t ( "You are currently ignoring:" )}</ p >
< ul >{ tiles }</ ul >
2019-10-31 15:53:18 -06:00
</ div >
);
}
2023-01-12 13:25:14 +00:00
private renderSubscribedBanLists () : JSX . Element {
2019-10-31 15:53:18 -06:00
const personalList = Mjolnir . sharedInstance (). getPersonalList ();
2019-10-31 16:27:45 -06:00
const lists = Mjolnir . sharedInstance (). lists . filter (( b ) => {
return personalList ? personalList . roomId !== b.roomId : true ;
});
2021-07-19 22:43:11 +01:00
if ( ! lists || lists . length <= 0 ) return < i >{ _t ( "You are not subscribed to any lists" )}</ i >;
2019-10-31 15:53:18 -06:00
2023-02-16 09:38:44 +00:00
const tiles : JSX.Element [] = [];
2019-10-31 15:53:18 -06:00
for ( const list of lists ) {
2023-06-21 17:29:44 +01:00
const room = MatrixClientPeg . safeGet (). getRoom ( list . roomId );
2021-07-19 22:43:11 +01:00
const name = room ? (
< span >
{ room . name } (< code >{ list . roomId }</ code >)
</ span >
) : (
< code > list . roomId </ code >
);
2019-10-31 15:53:18 -06:00
tiles . push (
< li key = { list . roomId } className = "mx_MjolnirUserSettingsTab_listItem" >
< AccessibleButton
kind = "danger_sm"
2021-04-26 14:02:53 +01:00
onClick = {() => this . unsubscribeFromList ( list )}
2019-10-31 15:53:18 -06:00
disabled = { this . state . busy }
>
2021-07-19 22:43:11 +01:00
{ _t ( "Unsubscribe" )}
2019-10-31 15:53:18 -06:00
</ AccessibleButton >
& nbsp ;
< AccessibleButton
kind = "primary_sm"
2021-04-26 14:02:53 +01:00
onClick = {() => this . viewListRules ( list )}
2019-10-31 15:53:18 -06:00
disabled = { this . state . busy }
>
2021-07-19 22:43:11 +01:00
{ _t ( "View rules" )}
2019-10-31 15:53:18 -06:00
</ AccessibleButton >
& nbsp ;
2021-07-19 22:43:11 +01:00
{ name }
2019-10-31 15:53:18 -06:00
</ li >,
);
}
return (
< div >
2021-07-19 22:43:11 +01:00
< p >{ _t ( "You are currently subscribed to:" )}</ p >
< ul >{ tiles }</ ul >
2019-10-31 15:53:18 -06:00
</ div >
);
2019-10-31 13:19:54 -06:00
}
2023-02-13 17:01:43 +00:00
public render () : React . ReactNode {
2020-07-10 19:07:11 +01:00
const brand = SdkConfig . get (). brand ;
2019-10-31 14:24:51 -06:00
2019-10-31 13:19:54 -06:00
return (
2023-05-31 10:42:53 +12:00
< SettingsTab >
< SettingsSection heading = { _t ( "Ignored users" )}>
< SettingsSubsectionText >
2021-07-19 22:43:11 +01:00
< span className = "warning" >{ _t ( "⚠ These settings are meant for advanced users." )}</ span >
2023-05-31 10:42:53 +12:00
< p >
{ _t (
2023-08-22 16:32:05 +01:00
"Add users and servers you want to ignore here. Use asterisks to have %(brand)s match any characters. For example, <code>@bot:*</code> would ignore all users that have the name 'bot' on any server." ,
2023-05-31 10:42:53 +12:00
{ brand },
{ code : ( s ) => < code >{ s }</ code > },
)}
</ p >
< p >
{ _t (
2023-08-22 16:32:05 +01:00
"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." ,
2023-05-31 10:42:53 +12:00
)}
</ p >
</ SettingsSubsectionText >
< SettingsSubsection
heading = { _t ( "Personal ban list" )}
description = { _t (
2023-08-22 16:32:05 +01:00
"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 '%(myBanList)s' - stay in this room to keep the ban list in effect." ,
2023-02-13 15:19:02 +00:00
{
myBanList : _t ( "My Ban List" ),
},
2021-07-19 22:43:11 +01:00
)}
2023-05-31 10:42:53 +12:00
>
{ this . renderPersonalBanListRules ()}
2021-04-26 14:02:53 +01:00
< form onSubmit = { this . onAddPersonalRule } autoComplete = "off" >
2019-10-31 14:24:51 -06:00
< Field
type = "text"
label = { _t ( "Server or user ID to ignore" )}
placeholder = { _t ( "eg: @bot:* or example.org" )}
value = { this . state . newPersonalRule }
2021-04-26 14:02:53 +01:00
onChange = { this . onPersonalRuleChanged }
2019-10-31 14:24:51 -06:00
/>
2019-10-31 15:53:18 -06:00
< AccessibleButton
type = "submit"
kind = "primary"
2021-04-26 14:02:53 +01:00
onClick = { this . onAddPersonalRule }
2019-10-31 15:53:18 -06:00
disabled = { this . state . busy }
>
2021-07-19 22:43:11 +01:00
{ _t ( "Ignore" )}
2019-10-31 14:24:51 -06:00
</ AccessibleButton >
</ form >
2023-05-31 10:42:53 +12:00
</ SettingsSubsection >
< SettingsSubsection
heading = { _t ( "Subscribed lists" )}
description = {
<>
< span className = "warning" >
{ _t ( "Subscribing to a ban list will cause you to join it!" )}
</ span >
& nbsp ;
< span >
{ _t ( "If this isn't what you want, please use a different tool to ignore users." )}
</ span >
</>
}
>
{ this . renderSubscribedBanLists ()}
2021-04-26 14:02:53 +01:00
< form onSubmit = { this . onSubscribeList } autoComplete = "off" >
2019-10-31 15:53:18 -06:00
< Field
type = "text"
2020-04-14 10:06:57 +01:00
label = { _t ( "Room ID or address of ban list" )}
2019-10-31 15:53:18 -06:00
value = { this . state . newList }
2021-04-26 14:02:53 +01:00
onChange = { this . onNewListChanged }
2019-10-31 15:53:18 -06:00
/>
< AccessibleButton
type = "submit"
kind = "primary"
2021-04-26 14:02:53 +01:00
onClick = { this . onSubscribeList }
2019-10-31 15:53:18 -06:00
disabled = { this . state . busy }
>
2021-07-19 22:43:11 +01:00
{ _t ( "Subscribe" )}
2019-10-31 15:53:18 -06:00
</ AccessibleButton >
</ form >
2023-05-31 10:42:53 +12:00
</ SettingsSubsection >
</ SettingsSection >
</ SettingsTab >
2019-10-31 13:19:54 -06:00
);
}
}