Offer "alias" field in room create form
Tries its best to not allow aliases that are too long (full alias including leading #, : in the middle, and homeserver domain name must not exceed 255 bytes. Change-Id: I1e784a94cb599eca7e30736d666b20e37aad5444
This commit is contained in:
@@ -29,20 +29,73 @@ function generateRoomRecord() {
|
||||
return {
|
||||
room_name: "",
|
||||
public: true,
|
||||
alias: "",
|
||||
}
|
||||
}
|
||||
|
||||
const validateDisplayName = fieldval => fieldval === undefined ? "synapseadmin.rooms.room_name_required" : fieldval.length === 0 ? "synapseadmin.rooms.room_name_required" : undefined;
|
||||
|
||||
function approximateAliasLength(alias, homeserver) {
|
||||
/* TODO maybe handle punycode in homeserver URL */
|
||||
|
||||
var te;
|
||||
|
||||
// Support for TextEncoder is quite widespread, but the polyfill is
|
||||
// pretty large; We will only underestimate the size with the regular
|
||||
// length attribute of String, so we never prevent the user from using
|
||||
// an alias that is short enough for the server, but too long for our
|
||||
// heuristic.
|
||||
try {
|
||||
te = new TextEncoder();
|
||||
}
|
||||
catch (err) {
|
||||
if (err instanceof ReferenceError) {
|
||||
te = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
const aliasLength = te === undefined ? alias.length : te.encode(alias).length;
|
||||
|
||||
return (
|
||||
"#".length +
|
||||
aliasLength +
|
||||
":".length +
|
||||
homeserver.length
|
||||
);
|
||||
}
|
||||
|
||||
const validateAlias = fieldval => {
|
||||
if (fieldval === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
const homeserver = localStorage.getItem("home_server");
|
||||
|
||||
if (approximateAliasLength(fieldval, homeserver) > 255) {
|
||||
return "synapseadmin.rooms.alias_too_long";
|
||||
}
|
||||
}
|
||||
|
||||
const removeLeadingWhitespace = fieldVal => fieldVal === undefined ? undefined : fieldVal.trimStart();
|
||||
|
||||
const validateHasAliasIfPublic = formdata => {
|
||||
let errors = {};
|
||||
if (formdata.public) {
|
||||
if (formdata.alias === undefined || formdata.alias.trim().length === 0) {
|
||||
errors.alias = "synapseadmin.rooms.alias_required_if_public"
|
||||
}
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
|
||||
export const RoomCreate = props => (
|
||||
<Create record={generateRoomRecord()} {...props}>
|
||||
<SimpleForm>
|
||||
<SimpleForm validate={validateHasAliasIfPublic}>
|
||||
<TextInput source="room_name"
|
||||
label="synapseadmin.rooms.room_name"
|
||||
parse={removeLeadingWhitespace}
|
||||
validate={validateDisplayName}/>
|
||||
<TextInput source="alias"
|
||||
parse={removeLeadingWhitespace}
|
||||
validate={validateAlias}/>
|
||||
<BooleanInput source="public"
|
||||
label="synapseadmin.rooms.make_public"/>
|
||||
</SimpleForm>
|
||||
|
||||
Reference in New Issue
Block a user