Add feature send server notices

This commit is contained in:
dklimpel 2020-04-20 22:40:05 +02:00
parent b7f009e559
commit 136487bbe1
5 changed files with 201 additions and 18 deletions

View File

@ -37,6 +37,7 @@ const App = () => (
/> />
<Resource name="rooms" list={RoomList} icon={RoomIcon} /> <Resource name="rooms" list={RoomList} icon={RoomIcon} />
<Resource name="connections" /> <Resource name="connections" />
<Resource name="servernotices" />
</Admin> </Admin>
); );

View File

@ -1,6 +1,8 @@
import React, { Fragment } from "react"; import React, { Fragment, useState } from "react";
import PersonPinIcon from "@material-ui/icons/PersonPin"; import PersonPinIcon from "@material-ui/icons/PersonPin";
import SettingsInputComponentIcon from "@material-ui/icons/SettingsInputComponent"; import SettingsInputComponentIcon from "@material-ui/icons/SettingsInputComponent";
import MessageIcon from "@material-ui/icons/Message";
import IconCancel from "@material-ui/icons/Cancel";
import { import {
ArrayInput, ArrayInput,
ArrayField, ArrayField,
@ -29,12 +31,108 @@ import {
regex, regex,
useTranslate, useTranslate,
Pagination, Pagination,
Button,
useNotify,
useUnselectAll,
required,
fetchStart,
fetchEnd,
} from "react-admin"; } from "react-admin";
import Dialog from "@material-ui/core/Dialog";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";
import DialogTitle from "@material-ui/core/DialogTitle";
import dataProvider from "../synapse/dataProvider.js";
const UserPagination = props => ( const UserPagination = props => (
<Pagination {...props} rowsPerPageOptions={[10, 25, 50, 100, 500, 1000]} /> <Pagination {...props} rowsPerPageOptions={[10, 25, 50, 100, 500, 1000]} />
); );
const ServernoticesButton = ({ record, selectedIds, method = "single" }) => {
const [open, setOpen] = useState(false);
const notify = useNotify();
const unselectAll = useUnselectAll();
const translate = useTranslate();
const handleDialogOpen = () => setOpen(true);
const handleDialogClose = () => setOpen(false);
const handleConfirm = values => {
fetchStart();
if (method === "multi") {
console.log(method);
dataProvider
.updateMany("servernotices", { ids: selectedIds, data: values })
.then(({ response }) => {
notify("resources.servernotices.action.send_success");
unselectAll("users");
})
.catch(error => {
notify("resources.servernotices.action.send_failure", "error");
})
.finally(() => {
fetchEnd();
});
} else {
dataProvider
.update("servernotices", { data: { ...values, ...{ id: record.id } } })
.then(({ response }) => {
notify("resources.servernotices.action.send_success");
})
.catch(error => {
notify("resources.servernotices.action.send_failure", "error");
})
.finally(() => {
fetchEnd();
});
}
handleDialogClose();
};
const ServernoticesToolbar = props => (
<Toolbar {...props}>
<SaveButton
label="resources.servernotices.action.send"
submitOnEnter={false}
/>
<Button label="ra.action.cancel" onClick={handleDialogClose}>
<IconCancel />
</Button>
</Toolbar>
);
return (
<Fragment>
<Button label="resources.servernotices.send" onClick={handleDialogOpen}>
<MessageIcon />
</Button>
<Dialog fullWidth open={open} onClose={handleDialogClose}>
<DialogTitle>
{translate("resources.servernotices.action.send")}
</DialogTitle>
<DialogContent>
<DialogContentText>
{translate("resources.servernotices.helper.send")}
</DialogContentText>
<SimpleForm
toolbar={<ServernoticesToolbar />}
submitOnEnter={false}
redirect={false}
save={handleConfirm}
>
<TextInput
source="body"
label="resources.servernotices.fields.body"
multiline
resettable
validate={required()}
/>
</SimpleForm>
</DialogContent>
</Dialog>
</Fragment>
);
};
const UserFilter = props => ( const UserFilter = props => (
<Filter {...props}> <Filter {...props}>
<BooleanInput source="guests" alwaysOn /> <BooleanInput source="guests" alwaysOn />
@ -50,6 +148,7 @@ const UserBulkActionButtons = props => {
const translate = useTranslate(); const translate = useTranslate();
return ( return (
<Fragment> <Fragment>
<ServernoticesButton {...props} method="multi" />
<BulkDeleteButton <BulkDeleteButton
{...props} {...props}
label="resources.users.action.erase" label="resources.users.action.erase"
@ -108,6 +207,7 @@ const UserEditToolbar = props => {
label="resources.users.action.erase" label="resources.users.action.erase"
title={translate("resources.users.helper.erase")} title={translate("resources.users.helper.erase")}
/> />
<ServernoticesButton {...props} method="single" />
</Toolbar> </Toolbar>
); );
}; };

View File

@ -60,5 +60,21 @@ export default {
user_agent: "User Agent", user_agent: "User Agent",
}, },
}, },
servernotices: {
name: "Serverbenachrichtigungen",
send: "Servernachricht versenden",
fields: {
body: "Nachricht",
},
action: {
send: "Sende Nachricht",
send_success: "Nachricht erfolgreich versendet.",
send_failure: "Beim Versenden ist ein Fehler aufgetreten.",
},
helper: {
send:
'Sendet eine Serverbenachrichtigung an die ausgewählten Nutzer. Hierfür muss das Feature "Server Notices" auf dem Server aktiviert sein.',
},
},
}, },
}; };

View File

@ -60,5 +60,21 @@ export default {
user_agent: "User agent", user_agent: "User agent",
}, },
}, },
servernotices: {
name: "Server Notices",
send: "Send server notices",
fields: {
body: "Nachricht",
},
action: {
send: "Send note",
send_success: "Note sent successfully.",
send_failure: "An error has occurred.",
},
helper: {
send:
'Sends a server notices to the selected users. For this, the feature "Server Notices" must be activated on the server.',
},
},
}, },
}; };

View File

@ -57,6 +57,24 @@ const resourceMap = {
}), }),
data: "connections", data: "connections",
}, },
servernotices: {
map: s => ({
...s,
id: s.user_id,
}),
data: "servernotices",
update: (id, params) => ({
endpoint: `/_synapse/admin/v1/send_server_notice`,
body: {
user_id: `${id}`,
content: {
msgtype: "m.text",
body: `${params.body}`,
},
},
method: "POST",
}),
},
}; };
function filterNullValues(key, value) { function filterNullValues(key, value) {
@ -146,7 +164,13 @@ const dataProvider = {
return jsonClient(url).then(({ headers, json }) => ({ return jsonClient(url).then(({ headers, json }) => ({
data: json, data: json,
total: parseInt(headers.get("content-range").split("/").pop(), 10), total: parseInt(
headers
.get("content-range")
.split("/")
.pop(),
10
),
})); }));
}, },
@ -157,13 +181,24 @@ const dataProvider = {
const res = resourceMap[resource]; const res = resourceMap[resource];
const homeserver_url = homeserver + res.path; if ("update" in res) {
return jsonClient(`${homeserver_url}/${params.data.id}`, { const upd = res["update"](params.data.id, params.data);
method: "PUT", const homeserver_url = homeserver + upd.endpoint;
body: JSON.stringify(params.data, filterNullValues), return jsonClient(homeserver_url, {
}).then(({ json }) => ({ method: upd.method,
data: res.map(json), body: JSON.stringify(upd.body),
})); }).then(({ json }) => ({
data: 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),
}));
}
}, },
updateMany: (resource, params) => { updateMany: (resource, params) => {
@ -173,15 +208,30 @@ const dataProvider = {
const res = resourceMap[resource]; const res = resourceMap[resource];
const homeserver_url = homeserver + res.path; if ("update" in res) {
return Promise.all( return Promise.all(
params.ids.map(id => jsonClient(`${homeserver_url}/${id}`), { params.ids.map(id => {
method: "PUT", const upd = res["update"](id, params.data);
body: JSON.stringify(params.data, filterNullValues), const homeserver_url = homeserver + upd.endpoint;
}) return jsonClient(homeserver_url, {
).then(responses => ({ method: upd.method,
data: responses.map(({ json }) => json), body: JSON.stringify(upd.body),
})); });
})
).then(responses => ({
data: responses.map(({ json }) => json),
}));
} else {
const homeserver_url = 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) => { create: (resource, params) => {