Cleanup room creation

Change-Id: Ieb5189513d21606f8d0bea5692112350a68f2e14
This commit is contained in:
Manuel Stahl 2020-04-21 14:03:35 +02:00
parent d3cd2e9e33
commit 0823976edd
5 changed files with 78 additions and 102 deletions

View File

@ -44,7 +44,8 @@ const App = () => (
name="rooms" name="rooms"
list={RoomList} list={RoomList}
create={RoomCreate} create={RoomCreate}
icon={RoomIcon} /> icon={RoomIcon}
/>
<Resource name="connections" /> <Resource name="connections" />
</Admin> </Admin>
); );

View File

@ -25,15 +25,12 @@ export const RoomList = props => (
</List> </List>
); );
function generateRoomRecord() { const validateDisplayName = fieldval =>
return { fieldval === undefined
room_name: "", ? "synapseadmin.rooms.room_name_required"
public: true, : fieldval.length === 0
alias: "", ? "synapseadmin.rooms.room_name_required"
} : undefined;
}
const validateDisplayName = fieldval => fieldval === undefined ? "synapseadmin.rooms.room_name_required" : fieldval.length === 0 ? "synapseadmin.rooms.room_name_required" : undefined;
function approximateAliasLength(alias, homeserver) { function approximateAliasLength(alias, homeserver) {
/* TODO maybe handle punycode in homeserver URL */ /* TODO maybe handle punycode in homeserver URL */
@ -47,8 +44,7 @@ function approximateAliasLength(alias, homeserver) {
// heuristic. // heuristic.
try { try {
te = new TextEncoder(); te = new TextEncoder();
} } catch (err) {
catch (err) {
if (err instanceof ReferenceError) { if (err instanceof ReferenceError) {
te = undefined; te = undefined;
} }
@ -56,12 +52,7 @@ function approximateAliasLength(alias, homeserver) {
const aliasLength = te === undefined ? alias.length : te.encode(alias).length; const aliasLength = te === undefined ? alias.length : te.encode(alias).length;
return ( return "#".length + aliasLength + ":".length + homeserver.length;
"#".length +
aliasLength +
":".length +
homeserver.length
);
} }
const validateAlias = fieldval => { const validateAlias = fieldval => {
@ -73,34 +64,47 @@ const validateAlias = fieldval => {
if (approximateAliasLength(fieldval, homeserver) > 255) { if (approximateAliasLength(fieldval, homeserver) > 255) {
return "synapseadmin.rooms.alias_too_long"; return "synapseadmin.rooms.alias_too_long";
} }
} };
const removeLeadingWhitespace = fieldVal => fieldVal === undefined ? undefined : fieldVal.trimStart(); const removeLeadingWhitespace = fieldVal =>
const replaceAllWhitespace = fieldVal => fieldVal === undefined ? undefined : fieldVal.replace(/\s/, "_"); fieldVal === undefined ? undefined : fieldVal.trimStart();
const removeLeadingSigil = fieldVal => fieldVal === undefined ? undefined : fieldVal.startsWith("#") ? fieldVal.substr(1) : fieldVal; const replaceAllWhitespace = fieldVal =>
fieldVal === undefined ? undefined : fieldVal.replace(/\s/, "_");
const removeLeadingSigil = fieldVal =>
fieldVal === undefined
? undefined
: fieldVal.startsWith("#")
? fieldVal.substr(1)
: fieldVal;
const validateHasAliasIfPublic = formdata => { const validateHasAliasIfPublic = formdata => {
let errors = {}; let errors = {};
if (formdata.public) { if (formdata.public) {
if (formdata.alias === undefined || formdata.alias.trim().length === 0) { if (
errors.alias = "synapseadmin.rooms.alias_required_if_public" formdata.canonical_alias === undefined ||
formdata.canonical_alias.trim().length === 0
) {
errors.canonical_alias = "synapseadmin.rooms.alias_required_if_public";
} }
} }
return errors; return errors;
} };
export const RoomCreate = props => ( export const RoomCreate = props => (
<Create record={generateRoomRecord()} {...props}> <Create {...props}>
<SimpleForm validate={validateHasAliasIfPublic}> <SimpleForm validate={validateHasAliasIfPublic}>
<TextInput source="room_name" <TextInput
parse={removeLeadingWhitespace} source="name"
validate={validateDisplayName}/> parse={removeLeadingWhitespace}
<TextInput source="alias" validate={validateDisplayName}
parse={fv => replaceAllWhitespace(removeLeadingSigil(fv)) } />
format={fv => fv === "" ? "" : "#" + fv} <TextInput
validate={validateAlias}/> source="canonical_alias"
<BooleanInput source="public" parse={fv => replaceAllWhitespace(removeLeadingSigil(fv))}
label="synapseadmin.rooms.make_public"/> validate={validateAlias}
placeholder="#"
/>
<BooleanInput source="public" label="synapseadmin.rooms.make_public" />
</SimpleForm> </SimpleForm>
</Create> </Create>
); );

View File

@ -19,15 +19,12 @@ export default {
rooms: { rooms: {
room_name: "Raumname", room_name: "Raumname",
make_public: "Öffentlicher Raum", make_public: "Öffentlicher Raum",
room_name_required: room_name_required: "Muss angegeben werden",
"Muss angegeben werden", alias_required_if_public: "Muss für öffentliche Räume angegeben werden.",
alias_required_if_public: alias: "Alias",
"Muss für öffentliche Räume angegeben werden.",
alias:
"Alias",
alias_too_long: alias_too_long:
"Darf zusammen mit der Domain des Homeservers 255 bytes nicht überschreiten" "Darf zusammen mit der Domain des Homeservers 255 bytes nicht überschreiten",
} },
}, },
resources: { resources: {
users: { users: {

View File

@ -19,15 +19,12 @@ export default {
rooms: { rooms: {
room_name: "Room Name", room_name: "Room Name",
make_public: "Make room public", make_public: "Make room public",
room_name_required: room_name_required: "Must be provided",
"Must be provided", alias_required_if_public: "Must be provided for a public room",
alias_required_if_public: alias: "Alias",
"Must be provided for a public room",
alias:
"Alias",
alias_too_long: alias_too_long:
"Must not exceed 255 bytes including the domain of the homeserver." "Must not exceed 255 bytes including the domain of the homeserver.",
} },
}, },
resources: { resources: {
users: { users: {

View File

@ -45,9 +45,17 @@ const resourceMap = {
members: r.joined_members, members: r.joined_members,
}), }),
data: "rooms", data: "rooms",
total: json => { total: json => json.total_rooms,
return json.total_rooms; create: params => ({
}, method: "POST",
endpoint: "/_matrix/client/r0/createRoom",
body: {
name: params.data.name,
room_alias_name: params.data.canonical_alias,
visibility: params.data.public ? "public" : "private",
},
map: r => ({ id: r.room_id }),
}),
}, },
connections: { connections: {
path: "/_synapse/admin/v1/whois", path: "/_synapse/admin/v1/whois",
@ -67,38 +75,6 @@ function filterNullValues(key, value) {
return value; return value;
} }
const roomCreationMap = {
path: "/_matrix/client/r0/createRoom",
map: r => ({
room_id: r.room_id
})
};
const roomCreationProvider = {
create: (resource, params) => {
const homeserver = localStorage.getItem("home_server_url");
const homeserver_url = "https://" + homeserver + roomCreationMap.path;
const newParams = { ...params.data,
public: undefined,
room_name: undefined,
alias: undefined,
name: params.data.room_name,
room_alias_name: params.data.alias,
visibility: params.data.public ? "public" : "private",
}
return jsonClient(homeserver_url, {
method: "POST",
body: JSON.stringify(newParams, filterNullValues),
}).then(({ json }) => ({
data: roomCreationMap.map(json),
}));
}
};
const dataProvider = { const dataProvider = {
getList: (resource, params) => { getList: (resource, params) => {
console.log("getList " + resource); console.log("getList " + resource);
@ -242,23 +218,24 @@ const dataProvider = {
const res = resourceMap[resource]; const res = resourceMap[resource];
const homeserver_url = homeserver + res.path; if ("create" in res) {
const create = res["create"](params);
/* Special handling for rooms, as creating a room const homeserver_url = homeserver + create.endpoint;
is a POST request rather than put, and goes through return jsonClient(homeserver_url, {
the client-server API rather than the admin API. */ method: create.method,
if (resource === "rooms") { body: JSON.stringify(create.body, filterNullValues),
console.log("want to create a room!"); }).then(({ json }) => ({
console.log(params); data: create.map(json),
return roomCreationProvider.create(resource, params); }));
} else {
const homeserver_url = homeserver + res.path;
return jsonClient(`${homeserver_url}/${params.data.id}`, {
method: "PUT",
body: JSON.stringify(params.data, filterNullValues),
}).then(({ json }) => ({
data: res.map(json),
}));
} }
return jsonClient(`${homeserver_url}/${params.data.id}`, {
method: "PUT",
body: JSON.stringify(params.data, filterNullValues),
}).then(({ json }) => ({
data: res.map(json),
}));
}, },
delete: (resource, params) => { delete: (resource, params) => {