2020-02-07 18:19:01 +03:00
|
|
|
import React from "react";
|
2020-04-17 00:31:41 +03:00
|
|
|
import {
|
|
|
|
BooleanInput,
|
|
|
|
Create,
|
|
|
|
Datagrid,
|
|
|
|
List,
|
|
|
|
Pagination,
|
|
|
|
SimpleForm,
|
|
|
|
TextField,
|
|
|
|
TextInput,
|
|
|
|
} from "react-admin";
|
2020-03-27 23:02:37 +03:00
|
|
|
|
|
|
|
const RoomPagination = props => (
|
|
|
|
<Pagination {...props} rowsPerPageOptions={[10, 25, 50, 100, 500, 1000]} />
|
|
|
|
);
|
2020-02-07 18:19:01 +03:00
|
|
|
|
|
|
|
export const RoomList = props => (
|
2020-03-27 23:02:37 +03:00
|
|
|
<List {...props} pagination={<RoomPagination />}>
|
2020-02-07 18:19:01 +03:00
|
|
|
<Datagrid>
|
|
|
|
<TextField source="room_id" />
|
|
|
|
<TextField source="name" />
|
|
|
|
<TextField source="canonical_alias" />
|
|
|
|
<TextField source="joined_members" />
|
|
|
|
</Datagrid>
|
|
|
|
</List>
|
|
|
|
);
|
2020-04-17 00:31:41 +03:00
|
|
|
|
2020-04-21 15:03:35 +03:00
|
|
|
const validateDisplayName = fieldval =>
|
|
|
|
fieldval === undefined
|
|
|
|
? "synapseadmin.rooms.room_name_required"
|
|
|
|
: fieldval.length === 0
|
|
|
|
? "synapseadmin.rooms.room_name_required"
|
|
|
|
: undefined;
|
2020-04-17 00:31:41 +03:00
|
|
|
|
2020-04-17 01:09:56 +03:00
|
|
|
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();
|
2020-04-21 15:03:35 +03:00
|
|
|
} catch (err) {
|
2020-04-17 01:09:56 +03:00
|
|
|
if (err instanceof ReferenceError) {
|
|
|
|
te = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const aliasLength = te === undefined ? alias.length : te.encode(alias).length;
|
|
|
|
|
2020-04-21 15:03:35 +03:00
|
|
|
return "#".length + aliasLength + ":".length + homeserver.length;
|
2020-04-17 01:09:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const validateAlias = fieldval => {
|
|
|
|
if (fieldval === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-04-21 15:00:57 +03:00
|
|
|
const homeserver = localStorage.getItem("home_server_url");
|
2020-04-17 01:09:56 +03:00
|
|
|
|
|
|
|
if (approximateAliasLength(fieldval, homeserver) > 255) {
|
|
|
|
return "synapseadmin.rooms.alias_too_long";
|
|
|
|
}
|
2020-04-21 15:03:35 +03:00
|
|
|
};
|
2020-04-17 01:09:56 +03:00
|
|
|
|
2020-04-21 15:03:35 +03:00
|
|
|
const removeLeadingWhitespace = fieldVal =>
|
|
|
|
fieldVal === undefined ? undefined : fieldVal.trimStart();
|
|
|
|
const replaceAllWhitespace = fieldVal =>
|
|
|
|
fieldVal === undefined ? undefined : fieldVal.replace(/\s/, "_");
|
|
|
|
const removeLeadingSigil = fieldVal =>
|
|
|
|
fieldVal === undefined
|
|
|
|
? undefined
|
|
|
|
: fieldVal.startsWith("#")
|
|
|
|
? fieldVal.substr(1)
|
|
|
|
: fieldVal;
|
2020-04-17 00:31:41 +03:00
|
|
|
|
2020-04-17 01:09:56 +03:00
|
|
|
const validateHasAliasIfPublic = formdata => {
|
|
|
|
let errors = {};
|
|
|
|
if (formdata.public) {
|
2020-04-21 15:03:35 +03:00
|
|
|
if (
|
|
|
|
formdata.canonical_alias === undefined ||
|
|
|
|
formdata.canonical_alias.trim().length === 0
|
|
|
|
) {
|
|
|
|
errors.canonical_alias = "synapseadmin.rooms.alias_required_if_public";
|
2020-04-17 01:09:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors;
|
2020-04-21 15:03:35 +03:00
|
|
|
};
|
2020-04-17 01:09:56 +03:00
|
|
|
|
2020-04-17 00:31:41 +03:00
|
|
|
export const RoomCreate = props => (
|
2020-04-21 15:03:35 +03:00
|
|
|
<Create {...props}>
|
2020-04-17 01:09:56 +03:00
|
|
|
<SimpleForm validate={validateHasAliasIfPublic}>
|
2020-04-21 15:03:35 +03:00
|
|
|
<TextInput
|
|
|
|
source="name"
|
|
|
|
parse={removeLeadingWhitespace}
|
|
|
|
validate={validateDisplayName}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
source="canonical_alias"
|
|
|
|
parse={fv => replaceAllWhitespace(removeLeadingSigil(fv))}
|
|
|
|
validate={validateAlias}
|
|
|
|
placeholder="#"
|
|
|
|
/>
|
|
|
|
<BooleanInput source="public" label="synapseadmin.rooms.make_public" />
|
2020-04-17 00:31:41 +03:00
|
|
|
</SimpleForm>
|
|
|
|
</Create>
|
|
|
|
);
|