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"
list={RoomList}
create={RoomCreate}
icon={RoomIcon} />
icon={RoomIcon}
/>
<Resource name="connections" />
</Admin>
);

View File

@ -25,15 +25,12 @@ export const RoomList = props => (
</List>
);
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;
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 */
@ -47,8 +44,7 @@ function approximateAliasLength(alias, homeserver) {
// heuristic.
try {
te = new TextEncoder();
}
catch (err) {
} catch (err) {
if (err instanceof ReferenceError) {
te = undefined;
}
@ -56,12 +52,7 @@ function approximateAliasLength(alias, homeserver) {
const aliasLength = te === undefined ? alias.length : te.encode(alias).length;
return (
"#".length +
aliasLength +
":".length +
homeserver.length
);
return "#".length + aliasLength + ":".length + homeserver.length;
}
const validateAlias = fieldval => {
@ -73,34 +64,47 @@ const validateAlias = fieldval => {
if (approximateAliasLength(fieldval, homeserver) > 255) {
return "synapseadmin.rooms.alias_too_long";
}
}
};
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;
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;
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"
if (
formdata.canonical_alias === undefined ||
formdata.canonical_alias.trim().length === 0
) {
errors.canonical_alias = "synapseadmin.rooms.alias_required_if_public";
}
}
return errors;
}
};
export const RoomCreate = props => (
<Create record={generateRoomRecord()} {...props}>
<Create {...props}>
<SimpleForm validate={validateHasAliasIfPublic}>
<TextInput source="room_name"
parse={removeLeadingWhitespace}
validate={validateDisplayName}/>
<TextInput source="alias"
parse={fv => replaceAllWhitespace(removeLeadingSigil(fv)) }
format={fv => fv === "" ? "" : "#" + fv}
validate={validateAlias}/>
<BooleanInput source="public"
label="synapseadmin.rooms.make_public"/>
<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" />
</SimpleForm>
</Create>
);

View File

@ -19,15 +19,12 @@ export default {
rooms: {
room_name: "Raumname",
make_public: "Öffentlicher Raum",
room_name_required:
"Muss angegeben werden",
alias_required_if_public:
"Muss für öffentliche Räume angegeben werden.",
alias:
"Alias",
room_name_required: "Muss angegeben werden",
alias_required_if_public: "Muss für öffentliche Räume angegeben werden.",
alias: "Alias",
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: {
users: {

View File

@ -19,15 +19,12 @@ export default {
rooms: {
room_name: "Room Name",
make_public: "Make room public",
room_name_required:
"Must be provided",
alias_required_if_public:
"Must be provided for a public room",
alias:
"Alias",
room_name_required: "Must be provided",
alias_required_if_public: "Must be provided for a public room",
alias: "Alias",
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: {
users: {

View File

@ -45,9 +45,17 @@ const resourceMap = {
members: r.joined_members,
}),
data: "rooms",
total: json => {
return json.total_rooms;
},
total: json => 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: {
path: "/_synapse/admin/v1/whois",
@ -67,38 +75,6 @@ function filterNullValues(key, 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 = {
getList: (resource, params) => {
console.log("getList " + resource);
@ -242,23 +218,24 @@ const dataProvider = {
const res = resourceMap[resource];
const homeserver_url = homeserver + res.path;
/* Special handling for rooms, as creating a room
is a POST request rather than put, and goes through
the client-server API rather than the admin API. */
if (resource === "rooms") {
console.log("want to create a room!");
console.log(params);
return roomCreationProvider.create(resource, params);
if ("create" in res) {
const create = res["create"](params);
const homeserver_url = homeserver + create.endpoint;
return jsonClient(homeserver_url, {
method: create.method,
body: JSON.stringify(create.body, filterNullValues),
}).then(({ json }) => ({
data: create.map(json),
}));
} 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) => {