From 0823976edd9aac7ccfb9876024f87f04b99448ad Mon Sep 17 00:00:00 2001 From: Manuel Stahl Date: Tue, 21 Apr 2020 14:03:35 +0200 Subject: [PATCH] Cleanup room creation Change-Id: Ieb5189513d21606f8d0bea5692112350a68f2e14 --- src/App.js | 3 +- src/components/rooms.js | 72 +++++++++++++++++---------------- src/i18n/de.js | 13 +++--- src/i18n/en.js | 13 +++--- src/synapse/dataProvider.js | 79 +++++++++++++------------------------ 5 files changed, 78 insertions(+), 102 deletions(-) diff --git a/src/App.js b/src/App.js index 3879c14..01ea17c 100644 --- a/src/App.js +++ b/src/App.js @@ -44,7 +44,8 @@ const App = () => ( name="rooms" list={RoomList} create={RoomCreate} - icon={RoomIcon} /> + icon={RoomIcon} + /> ); diff --git a/src/components/rooms.js b/src/components/rooms.js index dc2fc30..5e5cd7c 100644 --- a/src/components/rooms.js +++ b/src/components/rooms.js @@ -25,15 +25,12 @@ export const RoomList = props => ( ); -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 => ( - + - - replaceAllWhitespace(removeLeadingSigil(fv)) } - format={fv => fv === "" ? "" : "#" + fv} - validate={validateAlias}/> - + + replaceAllWhitespace(removeLeadingSigil(fv))} + validate={validateAlias} + placeholder="#" + /> + ); diff --git a/src/i18n/de.js b/src/i18n/de.js index 452584b..0c5dd74 100644 --- a/src/i18n/de.js +++ b/src/i18n/de.js @@ -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: { diff --git a/src/i18n/en.js b/src/i18n/en.js index f6382d9..0cb5f5e 100644 --- a/src/i18n/en.js +++ b/src/i18n/en.js @@ -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: { diff --git a/src/synapse/dataProvider.js b/src/synapse/dataProvider.js index cae6e6c..24e114b 100644 --- a/src/synapse/dataProvider.js +++ b/src/synapse/dataProvider.js @@ -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) => {