From 7056dc0fdbc6d852fe53a60caff2fd7ef2195910 Mon Sep 17 00:00:00 2001 From: Jeena Date: Tue, 4 Aug 2020 17:38:42 +0200 Subject: [PATCH] Expose user avatar URL field in the UI To be able to change a avatar the URL field needs to be exposed but changing the URL alone is difficult. This patch adds a image upload field to the edit view of a user. If there is a file detected it uploads it to the servers media repository and sets that new URL as the avatar URL one. This way a admin can change any users avatar which is especially nice for bridged users like in the IRC bridge and bots. --- src/components/users.js | 5 +++++ src/synapse/dataProvider.js | 39 +++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/components/users.js b/src/components/users.js index a6889b2..1cd1ef8 100644 --- a/src/components/users.js +++ b/src/components/users.js @@ -36,6 +36,8 @@ import { ExportButton, TopToolbar, sanitizeListRestProps, + ImageInput, + ImageField, } from "react-admin"; import { ServerNoticeButton, ServerNoticeBulkButton } from "./ServerNotices"; import { DeviceRemoveButton } from "./devices"; @@ -221,6 +223,9 @@ export const UserEdit = props => { + + + ({ - data: res.map(json), - })); + + // In case there is a avatar_file object, save it in the media repository + // and update the avatar_url. + const f = params?.data?.avatar_file?.rawFile; + if (f instanceof File) { + const file_endpoint = + homeserver + "/_matrix/media/r0/upload?filename=" + f.name; + const headers = new Headers(); + headers.append("Content-Type", f.type); + const options = { + method: "POST", + body: f, + headers: headers, + }; + return jsonClient(file_endpoint, options).then(r => { + params.data.avatar_url = r.json.content_uri; + + return jsonClient(`${endpoint_url}/${params.data.id}`, { + method: "PUT", + body: JSON.stringify(params.data, filterNullValues), + }).then(({ json }) => ({ + data: res.map(json), + })); + }); + } else { + return jsonClient(`${endpoint_url}/${params.data.id}`, { + method: "PUT", + body: JSON.stringify(params.data, filterNullValues), + }).then(({ json }) => ({ + data: res.map(json), + })); + } }, updateMany: (resource, params) => {