From a151a18b6ee936f6daceca6d6818622f83499951 Mon Sep 17 00:00:00 2001 From: Manuel Stahl Date: Fri, 7 Feb 2020 16:10:43 +0100 Subject: [PATCH] Add dataProvider for synapse Change-Id: Id1bd6e840a1af8c647c316e29a39e988ac224dba --- src/App.js | 4 +- src/dataProvider.js | 13 --- src/synapse/dataProvider.js | 220 ++++++++++++++++++++++++++++++++++++ 3 files changed, 222 insertions(+), 15 deletions(-) delete mode 100644 src/dataProvider.js create mode 100644 src/synapse/dataProvider.js diff --git a/src/App.js b/src/App.js index 7b71d24..68b8f2d 100644 --- a/src/App.js +++ b/src/App.js @@ -2,7 +2,7 @@ import React from "react"; import { Admin, Resource, resolveBrowserLocale } from "react-admin"; import polyglotI18nProvider from "ra-i18n-polyglot"; import authProvider from "./synapse/authProvider"; -import dataProvider from "./dataProvider"; +import dataProvider from "./synapse/dataProvider"; import LoginPage from "./components/LoginPage"; import germanMessages from "./i18n/de"; import englishMessages from "./i18n/en"; @@ -24,7 +24,7 @@ const App = () => ( dataProvider={dataProvider} i18nProvider={i18nProvider} > - + ); diff --git a/src/dataProvider.js b/src/dataProvider.js deleted file mode 100644 index fb57c93..0000000 --- a/src/dataProvider.js +++ /dev/null @@ -1,13 +0,0 @@ -const dataProvider = { - getList: (resource, params) => Promise, - getOne: (resource, params) => Promise, - getMany: (resource, params) => Promise, - getManyReference: (resource, params) => Promise, - create: (resource, params) => Promise, - update: (resource, params) => Promise, - updateMany: (resource, params) => Promise, - delete: (resource, params) => Promise, - deleteMany: (resource, params) => Promise, -}; - -export default dataProvider; diff --git a/src/synapse/dataProvider.js b/src/synapse/dataProvider.js new file mode 100644 index 0000000..441f813 --- /dev/null +++ b/src/synapse/dataProvider.js @@ -0,0 +1,220 @@ +import { fetchUtils } from "react-admin"; +import { stringify } from "query-string"; + +// Adds the access token to all requests +const jsonClient = (url, options = {}) => { + const token = localStorage.getItem("access_token"); + console.log("httpClient " + url); + if (token != null) { + options.user = { + authenticated: true, + token: `Bearer ${token}`, + }; + } + return fetchUtils.fetchJson(url, options); +}; + +const resourceMap = { + users: { + path: "/_synapse/admin/v2/users", + map: u => ({ + ...u, + id: u.name, + is_guest: !!u.is_guest, + admin: !!u.admin, + deactivated: !!u.deactivated, + }), + data: "users", + total: (json, perPage) => { + return parseInt(json.next_token, 10) + perPage; + }, + }, + rooms: { + path: "/_synapse/admin/v1/rooms", + map: r => ({ + ...r, + id: r.room_id, + alias: r.canonical_alias, + members: r.joined_members, + }), + data: "rooms", + total: json => { + return json.total_rooms; + }, + }, +}; + +function filterNullValues(key, value) { + // Filtering out null properties + if (value === null) { + return undefined; + } + return value; +} + +const dataProvider = { + getList: (resource, params) => { + console.log("getList " + resource); + const { user_id, guests } = params.filter; + const { page, perPage } = params.pagination; + const query = { + from: (page - 1) * perPage, + limit: perPage, + user_id: user_id, + guests: guests, + }; + const homeserver = localStorage.getItem("home_server"); + if (!homeserver || !(resource in resourceMap)) return Promise.reject(); + + const res = resourceMap[resource]; + const homeserver_url = "https://" + homeserver + res.path; + const url = `${homeserver_url}?${stringify(query)}`; + + return jsonClient(url).then(({ json }) => ({ + data: json[res.data].map(res.map), + total: res.total(json, perPage), + })); + }, + + getOne: (resource, params) => { + console.log("getOne " + resource); + const homeserver = localStorage.getItem("home_server"); + if (!homeserver || !(resource in resourceMap)) return Promise.reject(); + + const res = resourceMap[resource]; + const homeserver_url = "https://" + homeserver + res.path; + return jsonClient(`${homeserver_url}/${params.id}`).then(({ json }) => ({ + data: res.map(json), + })); + }, + + getMany: (resource, params) => { + console.log("getMany " + resource); + const homeserver = localStorage.getItem("home_server"); + if (!homeserver || !(resource in resourceMap)) return Promise.reject(); + + const res = resourceMap[resource]; + const homeserver_url = "https://" + homeserver + res.path; + return Promise.all( + params.ids.map(id => jsonClient(`${homeserver_url}/${id}`)) + ).then(responses => ({ + data: responses.map(({ json }) => res.map(json)), + })); + }, + + getManyReference: (resource, params) => { + // FIXME + console.log("getManyReference " + resource); + const { page, perPage } = params.pagination; + const { field, order } = params.sort; + const query = { + sort: JSON.stringify([field, order]), + range: JSON.stringify([(page - 1) * perPage, page * perPage - 1]), + filter: JSON.stringify({ + ...params.filter, + [params.target]: params.id, + }), + }; + + const homeserver = localStorage.getItem("home_server"); + if (!homeserver || !(resource in resourceMap)) return Promise.reject(); + + const res = resourceMap[resource]; + const homeserver_url = "https://" + homeserver + res.path; + const url = `${homeserver_url}?${stringify(query)}`; + + return jsonClient(url).then(({ headers, json }) => ({ + data: json, + total: parseInt( + headers + .get("content-range") + .split("/") + .pop(), + 10 + ), + })); + }, + + update: (resource, params) => { + console.log("update " + resource); + const homeserver = localStorage.getItem("home_server"); + if (!homeserver || !(resource in resourceMap)) return Promise.reject(); + + const res = resourceMap[resource]; + const homeserver_url = "https://" + homeserver + res.path; + return jsonClient(`${homeserver_url}/${params.id}`, { + method: "PUT", + body: JSON.stringify(params.data, filterNullValues), + }).then(({ json }) => ({ + data: json, + })); + }, + + updateMany: (resource, params) => { + console.log("updateMany " + resource); + const homeserver = localStorage.getItem("home_server"); + if (!homeserver || !(resource in resourceMap)) return Promise.reject(); + + const res = resourceMap[resource]; + const homeserver_url = "https://" + homeserver + res.path; + return Promise.all( + params.ids.map(id => jsonClient(`${homeserver_url}/${id}`), { + method: "PUT", + body: JSON.stringify(params.data, filterNullValues), + }) + ).then(responses => ({ + data: responses.map(({ json }) => json), + })); + }, + + create: (resource, params) => { + console.log("create " + resource); + const homeserver = localStorage.getItem("home_server"); + if (!homeserver || !(resource in resourceMap)) return Promise.reject(); + + const res = resourceMap[resource]; + const homeserver_url = "https://" + homeserver + res.path; + return jsonClient(`${homeserver_url}/${params.id}`, { + method: "PUT", + body: JSON.stringify(params.data, filterNullValues), + }).then(({ json }) => ({ + data: { + ...params.data, + id: json.id, + }, + })); + }, + + delete: (resource, params) => { + console.log("delete " + resource); + const homeserver = localStorage.getItem("home_server"); + if (!homeserver || !(resource in resourceMap)) return Promise.reject(); + + const res = resourceMap[resource]; + const homeserver_url = "https://" + homeserver + res.path; + return jsonClient(`${homeserver_url}/${params.id}`, { + method: "DELETE", + }).then(({ json }) => ({ + data: json, + })); + }, + + deleteMany: (resource, params) => { + console.log("deleteMany " + resource); + const homeserver = localStorage.getItem("home_server"); + if (!homeserver || !(resource in resourceMap)) return Promise.reject(); + + const res = resourceMap[resource]; + const homeserver_url = "https://" + homeserver + res.path; + return Promise.all( + params.ids.map(id => jsonClient(`${homeserver_url}/${id}`), { + method: "DELETE", + body: JSON.stringify(params.data, filterNullValues), + }) + ).then(responses => ({ + data: responses.map(({ json }) => json), + })); + }, +}; + +export default dataProvider;