Updates after review (outsource ServerNotices, update dataProvider)
This commit is contained in:
@@ -0,0 +1,106 @@
|
|||||||
|
import React, { Fragment, useState } from "react";
|
||||||
|
import MessageIcon from "@material-ui/icons/Message";
|
||||||
|
import IconCancel from "@material-ui/icons/Cancel";
|
||||||
|
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 {
|
||||||
|
TextInput,
|
||||||
|
SaveButton,
|
||||||
|
useTranslate,
|
||||||
|
Button,
|
||||||
|
useNotify,
|
||||||
|
useUnselectAll,
|
||||||
|
required,
|
||||||
|
Toolbar,
|
||||||
|
SimpleForm,
|
||||||
|
useMutation,
|
||||||
|
} from "react-admin";
|
||||||
|
|
||||||
|
const ServerNoticesButton = ({ record, selectedIds }) => {
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
const notify = useNotify();
|
||||||
|
const unselectAll = useUnselectAll();
|
||||||
|
const translate = useTranslate();
|
||||||
|
const handleDialogOpen = () => setOpen(true);
|
||||||
|
const handleDialogClose = () => setOpen(false);
|
||||||
|
const [mutate, { loading }] = useMutation();
|
||||||
|
const handleConfirm = values => {
|
||||||
|
if (Array.isArray(selectedIds)) {
|
||||||
|
mutate(
|
||||||
|
{
|
||||||
|
type: "sendMessageMany",
|
||||||
|
resource: "servernotices",
|
||||||
|
payload: { ids: selectedIds, data: values },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
onSuccess: ({ data }) => {
|
||||||
|
notify("resources.servernotices.action.send_success");
|
||||||
|
unselectAll("users");
|
||||||
|
},
|
||||||
|
onFailure: error =>
|
||||||
|
notify("resources.servernotices.action.send_failure", "error"),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
mutate(
|
||||||
|
{
|
||||||
|
type: "sendMessage",
|
||||||
|
resource: "servernotices",
|
||||||
|
payload: { id: record.id, data: values },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
onSuccess: ({ data }) =>
|
||||||
|
notify("resources.servernotices.action.send_success"),
|
||||||
|
onFailure: error =>
|
||||||
|
notify("resources.servernotices.action.send_failure", "error"),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
handleDialogClose();
|
||||||
|
};
|
||||||
|
|
||||||
|
const ServernoticesToolbar = props => (
|
||||||
|
<Toolbar {...props}>
|
||||||
|
<SaveButton label="resources.servernotices.action.send" />
|
||||||
|
<Button label="ra.action.cancel" onClick={handleDialogClose}>
|
||||||
|
<IconCancel />
|
||||||
|
</Button>
|
||||||
|
</Toolbar>
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Fragment>
|
||||||
|
<Button label="resources.servernotices.send" onClick={handleDialogOpen}>
|
||||||
|
<MessageIcon />
|
||||||
|
</Button>
|
||||||
|
<Dialog open={open} onClose={handleDialogClose} loading={loading}>
|
||||||
|
<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>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ServerNoticesButton;
|
||||||
+4
-101
@@ -1,8 +1,7 @@
|
|||||||
import React, { Fragment, useState } from "react";
|
import React, { Fragment } 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 ServerNoticesButton from "./ServerNotices";
|
||||||
import IconCancel from "@material-ui/icons/Cancel";
|
|
||||||
import {
|
import {
|
||||||
ArrayInput,
|
ArrayInput,
|
||||||
ArrayField,
|
ArrayField,
|
||||||
@@ -31,108 +30,12 @@ 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 />
|
||||||
@@ -148,7 +51,7 @@ const UserBulkActionButtons = props => {
|
|||||||
const translate = useTranslate();
|
const translate = useTranslate();
|
||||||
return (
|
return (
|
||||||
<Fragment>
|
<Fragment>
|
||||||
<ServernoticesButton {...props} method="multi" />
|
<ServerNoticesButton {...props} />
|
||||||
<BulkDeleteButton
|
<BulkDeleteButton
|
||||||
{...props}
|
{...props}
|
||||||
label="resources.users.action.erase"
|
label="resources.users.action.erase"
|
||||||
@@ -207,7 +110,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" />
|
<ServerNoticesButton {...props} />
|
||||||
</Toolbar>
|
</Toolbar>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
+1
-1
@@ -64,7 +64,7 @@ export default {
|
|||||||
name: "Server Notices",
|
name: "Server Notices",
|
||||||
send: "Send server notices",
|
send: "Send server notices",
|
||||||
fields: {
|
fields: {
|
||||||
body: "Nachricht",
|
body: "Message",
|
||||||
},
|
},
|
||||||
action: {
|
action: {
|
||||||
send: "Send note",
|
send: "Send note",
|
||||||
|
|||||||
+40
-32
@@ -58,13 +58,9 @@ const resourceMap = {
|
|||||||
data: "connections",
|
data: "connections",
|
||||||
},
|
},
|
||||||
servernotices: {
|
servernotices: {
|
||||||
map: s => ({
|
|
||||||
...s,
|
|
||||||
id: s.user_id,
|
|
||||||
}),
|
|
||||||
data: "servernotices",
|
data: "servernotices",
|
||||||
update: (id, params) => ({
|
sendMessage: (id, params) => ({
|
||||||
endpoint: `/_synapse/admin/v1/send_server_notice`,
|
endpoint: "/_synapse/admin/v1/send_server_notice",
|
||||||
body: {
|
body: {
|
||||||
user_id: `${id}`,
|
user_id: `${id}`,
|
||||||
content: {
|
content: {
|
||||||
@@ -181,16 +177,6 @@ const dataProvider = {
|
|||||||
|
|
||||||
const res = resourceMap[resource];
|
const res = resourceMap[resource];
|
||||||
|
|
||||||
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;
|
const homeserver_url = homeserver + res.path;
|
||||||
return jsonClient(`${homeserver_url}/${params.data.id}`, {
|
return jsonClient(`${homeserver_url}/${params.data.id}`, {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
@@ -198,7 +184,6 @@ const dataProvider = {
|
|||||||
}).then(({ json }) => ({
|
}).then(({ json }) => ({
|
||||||
data: res.map(json),
|
data: res.map(json),
|
||||||
}));
|
}));
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
updateMany: (resource, params) => {
|
updateMany: (resource, params) => {
|
||||||
@@ -208,20 +193,6 @@ const dataProvider = {
|
|||||||
|
|
||||||
const res = resourceMap[resource];
|
const res = resourceMap[resource];
|
||||||
|
|
||||||
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;
|
const homeserver_url = homeserver + res.path;
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
params.ids.map(id => jsonClient(`${homeserver_url}/${id}`), {
|
params.ids.map(id => jsonClient(`${homeserver_url}/${id}`), {
|
||||||
@@ -231,7 +202,6 @@ const dataProvider = {
|
|||||||
).then(responses => ({
|
).then(responses => ({
|
||||||
data: responses.map(({ json }) => json),
|
data: responses.map(({ json }) => json),
|
||||||
}));
|
}));
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
create: (resource, params) => {
|
create: (resource, params) => {
|
||||||
@@ -311,6 +281,44 @@ const dataProvider = {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
sendMessage: (resource, params) => {
|
||||||
|
console.log("sendMessage " + resource);
|
||||||
|
const homeserver = localStorage.getItem("base_url");
|
||||||
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
||||||
|
|
||||||
|
const res = resourceMap[resource];
|
||||||
|
|
||||||
|
const sendmsg = res["sendMessage"](params.id, params.data);
|
||||||
|
const homeserver_url = homeserver + sendmsg.endpoint;
|
||||||
|
return jsonClient(homeserver_url, {
|
||||||
|
method: sendmsg.method,
|
||||||
|
body: JSON.stringify(sendmsg.body),
|
||||||
|
}).then(({ json }) => ({
|
||||||
|
data: json,
|
||||||
|
}));
|
||||||
|
},
|
||||||
|
|
||||||
|
sendMessageMany: (resource, params) => {
|
||||||
|
console.log("sendMessageMany " + resource);
|
||||||
|
const homeserver = localStorage.getItem("base_url");
|
||||||
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
||||||
|
|
||||||
|
const res = resourceMap[resource];
|
||||||
|
|
||||||
|
return Promise.all(
|
||||||
|
params.ids.map(id => {
|
||||||
|
const sendmsg = res["sendMessage"](id, params.data);
|
||||||
|
const homeserver_url = homeserver + sendmsg.endpoint;
|
||||||
|
return jsonClient(homeserver_url, {
|
||||||
|
method: sendmsg.method,
|
||||||
|
body: JSON.stringify(sendmsg.body),
|
||||||
|
});
|
||||||
|
})
|
||||||
|
).then(responses => ({
|
||||||
|
data: responses.map(({ json }) => json),
|
||||||
|
}));
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default dataProvider;
|
export default dataProvider;
|
||||||
|
|||||||
Reference in New Issue
Block a user