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="connections" />
<Resource name="servernotices" />
</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 SettingsInputComponentIcon from "@material-ui/icons/SettingsInputComponent";
import MessageIcon from "@material-ui/icons/Message";
import IconCancel from "@material-ui/icons/Cancel";
import {
ArrayInput,
ArrayField,
@ -29,12 +31,108 @@ import {
regex,
useTranslate,
Pagination,
Button,
useNotify,
useUnselectAll,
required,
fetchStart,
fetchEnd,
} 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 => (
<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 => (
<Filter {...props}>
<BooleanInput source="guests" alwaysOn />
@ -50,6 +148,7 @@ const UserBulkActionButtons = props => {
const translate = useTranslate();
return (
<Fragment>
<ServernoticesButton {...props} method="multi" />
<BulkDeleteButton
{...props}
label="resources.users.action.erase"
@ -108,6 +207,7 @@ const UserEditToolbar = props => {
label="resources.users.action.erase"
title={translate("resources.users.helper.erase")}
/>
<ServernoticesButton {...props} method="single" />
</Toolbar>
);
};

View File

@ -60,5 +60,21 @@ export default {
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",
},
},
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",
},
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) {
@ -146,7 +164,13 @@ const dataProvider = {
return jsonClient(url).then(({ headers, 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 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),
}));
if ("update" in res) {
const upd = res["update"](params.data.id, params.data);
const homeserver_url = homeserver + upd.endpoint;
return jsonClient(homeserver_url, {
method: upd.method,
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) => {
@ -173,15 +208,30 @@ const dataProvider = {
const res = resourceMap[resource];
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),
}));
if ("update" in res) {
return Promise.all(
params.ids.map(id => {
const upd = res["update"](id, params.data);
const homeserver_url = homeserver + upd.endpoint;
return jsonClient(homeserver_url, {
method: upd.method,
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) => {