2020-02-07 15:10:43 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2020-06-10 12:59:55 +00:00
|
|
|
const mxcUrlToHttp = mxcUrl => {
|
|
|
|
const homeserver = localStorage.getItem("base_url");
|
|
|
|
const re = /^mxc:\/\/([^/]+)\/(\w+)/;
|
|
|
|
var ret = re.exec(mxcUrl);
|
|
|
|
console.log("mxcClient " + ret);
|
|
|
|
if (ret == null) return null;
|
|
|
|
const serverName = ret[1];
|
|
|
|
const mediaId = ret[2];
|
|
|
|
return `${homeserver}/_matrix/media/r0/thumbnail/${serverName}/${mediaId}?width=24&height=24&method=scale`;
|
|
|
|
};
|
|
|
|
|
2020-02-07 15:10:43 +00:00
|
|
|
const resourceMap = {
|
|
|
|
users: {
|
|
|
|
path: "/_synapse/admin/v2/users",
|
|
|
|
map: u => ({
|
|
|
|
...u,
|
|
|
|
id: u.name,
|
2020-06-10 12:59:55 +00:00
|
|
|
avatar_src: mxcUrlToHttp(u.avatar_url),
|
2020-02-07 15:10:43 +00:00
|
|
|
is_guest: !!u.is_guest,
|
|
|
|
admin: !!u.admin,
|
|
|
|
deactivated: !!u.deactivated,
|
2020-06-16 07:24:49 +00:00
|
|
|
// need timestamp in milliseconds
|
2020-06-16 08:05:38 +00:00
|
|
|
creation_ts_ms: u.creation_ts * 1000,
|
2020-02-07 15:10:43 +00:00
|
|
|
}),
|
|
|
|
data: "users",
|
2020-06-10 12:59:55 +00:00
|
|
|
total: json => json.total,
|
2020-04-23 08:44:52 +00:00
|
|
|
create: data => ({
|
|
|
|
endpoint: `/_synapse/admin/v2/users/${data.id}`,
|
|
|
|
body: data,
|
|
|
|
method: "PUT",
|
|
|
|
}),
|
2020-07-08 09:11:24 +00:00
|
|
|
delete: params => ({
|
|
|
|
endpoint: `/_synapse/admin/v1/deactivate/${params.id}`,
|
2020-04-09 08:32:06 +00:00
|
|
|
body: { erase: true },
|
|
|
|
method: "POST",
|
|
|
|
}),
|
2020-02-07 15:10:43 +00:00
|
|
|
},
|
|
|
|
rooms: {
|
|
|
|
path: "/_synapse/admin/v1/rooms",
|
|
|
|
map: r => ({
|
|
|
|
...r,
|
|
|
|
id: r.room_id,
|
|
|
|
alias: r.canonical_alias,
|
|
|
|
members: r.joined_members,
|
2020-05-23 15:43:33 +00:00
|
|
|
is_encrypted: !!r.encryption,
|
|
|
|
federatable: !!r.federatable,
|
|
|
|
public: !!r.public,
|
2020-02-07 15:10:43 +00:00
|
|
|
}),
|
|
|
|
data: "rooms",
|
|
|
|
total: json => {
|
|
|
|
return json.total_rooms;
|
|
|
|
},
|
2020-10-26 09:09:09 +00:00
|
|
|
delete: params => ({
|
|
|
|
endpoint: `/_synapse/admin/v1/rooms/${params.id}/delete`,
|
2020-10-07 18:50:10 +00:00
|
|
|
body: { block: false },
|
|
|
|
method: "POST",
|
|
|
|
}),
|
2020-02-07 15:10:43 +00:00
|
|
|
},
|
2021-01-04 08:14:33 +00:00
|
|
|
reports: {
|
|
|
|
path: "/_synapse/admin/v1/event_reports",
|
|
|
|
map: er => ({
|
|
|
|
...er,
|
|
|
|
id: er.id,
|
|
|
|
}),
|
|
|
|
data: "event_reports",
|
|
|
|
total: json => json.total,
|
|
|
|
},
|
2020-07-08 08:58:57 +00:00
|
|
|
devices: {
|
|
|
|
map: d => ({
|
|
|
|
...d,
|
|
|
|
id: d.device_id,
|
|
|
|
}),
|
|
|
|
data: "devices",
|
2021-02-11 19:20:42 +00:00
|
|
|
total: json => {
|
|
|
|
return json.total;
|
|
|
|
},
|
2020-07-08 08:58:57 +00:00
|
|
|
reference: id => ({
|
|
|
|
endpoint: `/_synapse/admin/v2/users/${id}/devices`,
|
|
|
|
}),
|
2020-07-08 09:11:24 +00:00
|
|
|
delete: params => ({
|
|
|
|
endpoint: `/_synapse/admin/v2/users/${params.user_id}/devices/${params.id}`,
|
|
|
|
}),
|
2020-07-08 08:58:57 +00:00
|
|
|
},
|
2020-03-28 20:25:34 +00:00
|
|
|
connections: {
|
|
|
|
path: "/_synapse/admin/v1/whois",
|
|
|
|
map: c => ({
|
|
|
|
...c,
|
|
|
|
id: c.user_id,
|
|
|
|
}),
|
|
|
|
data: "connections",
|
|
|
|
},
|
2020-08-06 19:43:19 +00:00
|
|
|
room_members: {
|
|
|
|
map: m => ({
|
|
|
|
id: m,
|
|
|
|
}),
|
|
|
|
reference: id => ({
|
|
|
|
endpoint: `/_synapse/admin/v1/rooms/${id}/members`,
|
|
|
|
}),
|
|
|
|
data: "members",
|
2021-02-11 19:20:42 +00:00
|
|
|
total: json => {
|
|
|
|
return json.total;
|
|
|
|
},
|
2020-08-06 19:43:19 +00:00
|
|
|
},
|
2021-05-04 11:57:41 +00:00
|
|
|
room_state: {
|
|
|
|
map: rs => ({
|
|
|
|
...rs,
|
|
|
|
id: rs.event_id,
|
|
|
|
}),
|
|
|
|
reference: id => ({
|
|
|
|
endpoint: `/_synapse/admin/v1/rooms/${id}/state`,
|
|
|
|
}),
|
|
|
|
data: "state",
|
|
|
|
total: json => {
|
|
|
|
return json.state.length;
|
|
|
|
},
|
|
|
|
},
|
2021-02-11 19:37:20 +00:00
|
|
|
pushers: {
|
|
|
|
map: p => ({
|
|
|
|
...p,
|
|
|
|
id: p.pushkey,
|
|
|
|
}),
|
|
|
|
reference: id => ({
|
|
|
|
endpoint: `/_synapse/admin/v1/users/${id}/pushers`,
|
|
|
|
}),
|
|
|
|
data: "pushers",
|
|
|
|
total: json => {
|
|
|
|
return json.total;
|
|
|
|
},
|
|
|
|
},
|
2021-02-11 19:45:02 +00:00
|
|
|
joined_rooms: {
|
|
|
|
map: jr => ({
|
|
|
|
id: jr,
|
|
|
|
}),
|
|
|
|
reference: id => ({
|
|
|
|
endpoint: `/_synapse/admin/v1/users/${id}/joined_rooms`,
|
|
|
|
}),
|
|
|
|
data: "joined_rooms",
|
|
|
|
total: json => {
|
|
|
|
return json.total;
|
|
|
|
},
|
|
|
|
},
|
2021-02-11 20:11:34 +00:00
|
|
|
users_media: {
|
|
|
|
map: um => ({
|
|
|
|
...um,
|
|
|
|
id: um.media_id,
|
|
|
|
}),
|
|
|
|
reference: id => ({
|
|
|
|
endpoint: `/_synapse/admin/v1/users/${id}/media`,
|
|
|
|
}),
|
|
|
|
data: "media",
|
|
|
|
total: json => {
|
|
|
|
return json.total;
|
|
|
|
},
|
|
|
|
delete: params => ({
|
|
|
|
endpoint: `/_synapse/admin/v1/media/${localStorage.getItem(
|
|
|
|
"home_server"
|
|
|
|
)}/${params.id}`,
|
|
|
|
}),
|
|
|
|
},
|
2021-02-23 12:37:54 +00:00
|
|
|
delete_media: {
|
|
|
|
delete: params => ({
|
|
|
|
endpoint: `/_synapse/admin/v1/media/${localStorage.getItem(
|
|
|
|
"home_server"
|
|
|
|
)}/delete?before_ts=${params.before_ts}&size_gt=${
|
|
|
|
params.size_gt
|
|
|
|
}&keep_profiles=${params.keep_profiles}`,
|
|
|
|
method: "POST",
|
|
|
|
}),
|
|
|
|
},
|
2020-04-23 08:00:46 +00:00
|
|
|
servernotices: {
|
|
|
|
map: n => ({ id: n.event_id }),
|
|
|
|
create: data => ({
|
|
|
|
endpoint: "/_synapse/admin/v1/send_server_notice",
|
|
|
|
body: {
|
|
|
|
user_id: data.id,
|
|
|
|
content: {
|
|
|
|
msgtype: "m.text",
|
|
|
|
body: data.body,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
method: "POST",
|
|
|
|
}),
|
|
|
|
},
|
2021-02-11 20:14:02 +00:00
|
|
|
user_media_statistics: {
|
|
|
|
path: "/_synapse/admin/v1/statistics/users/media",
|
|
|
|
map: usms => ({
|
|
|
|
...usms,
|
|
|
|
id: usms.user_id,
|
|
|
|
}),
|
|
|
|
data: "users",
|
|
|
|
total: json => {
|
|
|
|
return json.total;
|
|
|
|
},
|
|
|
|
},
|
2021-05-04 11:52:43 +00:00
|
|
|
room_directory: {
|
|
|
|
path: "/_matrix/client/r0/publicRooms",
|
|
|
|
map: rd => ({
|
|
|
|
...rd,
|
|
|
|
id: rd.room_id,
|
|
|
|
public: !!rd.public,
|
|
|
|
guest_access: !!rd.guest_access,
|
|
|
|
avatar_src: mxcUrlToHttp(rd.avatar_url),
|
|
|
|
}),
|
|
|
|
data: "chunk",
|
|
|
|
total: json => {
|
|
|
|
return json.total_room_count_estimate;
|
|
|
|
},
|
|
|
|
create: params => ({
|
|
|
|
endpoint: `/_matrix/client/r0/directory/list/room/${params.id}`,
|
|
|
|
body: { visibility: "public" },
|
|
|
|
method: "PUT",
|
|
|
|
}),
|
|
|
|
delete: params => ({
|
|
|
|
endpoint: `/_matrix/client/r0/directory/list/room/${params.id}`,
|
|
|
|
body: { visibility: "private" },
|
|
|
|
method: "PUT",
|
|
|
|
}),
|
|
|
|
},
|
2020-02-07 15:10:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function filterNullValues(key, value) {
|
|
|
|
// Filtering out null properties
|
|
|
|
if (value === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-05-23 15:43:33 +00:00
|
|
|
function getSearchOrder(order) {
|
|
|
|
if (order === "DESC") {
|
|
|
|
return "b";
|
|
|
|
} else {
|
|
|
|
return "f";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 15:10:43 +00:00
|
|
|
const dataProvider = {
|
|
|
|
getList: (resource, params) => {
|
|
|
|
console.log("getList " + resource);
|
2020-11-12 13:56:21 +00:00
|
|
|
const { user_id, name, guests, deactivated, search_term } = params.filter;
|
2020-02-07 15:10:43 +00:00
|
|
|
const { page, perPage } = params.pagination;
|
2020-05-23 15:43:33 +00:00
|
|
|
const { field, order } = params.sort;
|
2020-03-24 15:53:09 +00:00
|
|
|
const from = (page - 1) * perPage;
|
2020-02-07 15:10:43 +00:00
|
|
|
const query = {
|
2020-03-24 15:53:09 +00:00
|
|
|
from: from,
|
2020-02-07 15:10:43 +00:00
|
|
|
limit: perPage,
|
|
|
|
user_id: user_id,
|
2020-11-12 13:56:21 +00:00
|
|
|
search_term: search_term,
|
2020-10-08 06:07:55 +00:00
|
|
|
name: name,
|
2020-02-07 15:10:43 +00:00
|
|
|
guests: guests,
|
2020-03-24 15:53:09 +00:00
|
|
|
deactivated: deactivated,
|
2020-05-23 15:43:33 +00:00
|
|
|
order_by: field,
|
|
|
|
dir: getSearchOrder(order),
|
2020-02-07 15:10:43 +00:00
|
|
|
};
|
2020-03-04 22:21:36 +00:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 15:10:43 +00:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-04 22:21:36 +00:00
|
|
|
|
2020-04-23 08:25:59 +00:00
|
|
|
const endpoint_url = homeserver + res.path;
|
|
|
|
const url = `${endpoint_url}?${stringify(query)}`;
|
2020-02-07 15:10:43 +00:00
|
|
|
|
|
|
|
return jsonClient(url).then(({ json }) => ({
|
|
|
|
data: json[res.data].map(res.map),
|
2020-03-24 15:53:09 +00:00
|
|
|
total: res.total(json, from, perPage),
|
2020-02-07 15:10:43 +00:00
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
getOne: (resource, params) => {
|
|
|
|
console.log("getOne " + resource);
|
2020-03-04 22:21:36 +00:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 15:10:43 +00:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-04 22:21:36 +00:00
|
|
|
|
2020-04-23 08:25:59 +00:00
|
|
|
const endpoint_url = homeserver + res.path;
|
|
|
|
return jsonClient(`${endpoint_url}/${params.id}`).then(({ json }) => ({
|
2020-02-07 15:10:43 +00:00
|
|
|
data: res.map(json),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
getMany: (resource, params) => {
|
|
|
|
console.log("getMany " + resource);
|
2020-03-04 22:21:36 +00:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 15:10:43 +00:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-04 22:21:36 +00:00
|
|
|
|
2020-04-23 08:25:59 +00:00
|
|
|
const endpoint_url = homeserver + res.path;
|
2020-02-07 15:10:43 +00:00
|
|
|
return Promise.all(
|
2020-04-23 08:25:59 +00:00
|
|
|
params.ids.map(id => jsonClient(`${endpoint_url}/${id}`))
|
2020-02-07 15:10:43 +00:00
|
|
|
).then(responses => ({
|
|
|
|
data: responses.map(({ json }) => res.map(json)),
|
2020-11-12 15:14:30 +00:00
|
|
|
total: responses.length,
|
2020-02-07 15:10:43 +00:00
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
getManyReference: (resource, params) => {
|
|
|
|
console.log("getManyReference " + resource);
|
2020-11-12 15:14:30 +00:00
|
|
|
const { page, perPage } = params.pagination;
|
2021-05-04 14:18:12 +00:00
|
|
|
const { field, order } = params.sort;
|
2020-11-12 15:14:30 +00:00
|
|
|
const from = (page - 1) * perPage;
|
2021-02-11 20:11:34 +00:00
|
|
|
const query = {
|
|
|
|
from: from,
|
|
|
|
limit: perPage,
|
2021-05-04 14:18:12 +00:00
|
|
|
order_by: field,
|
|
|
|
dir: getSearchOrder(order),
|
2021-02-11 20:11:34 +00:00
|
|
|
};
|
2020-02-07 15:10:43 +00:00
|
|
|
|
2020-03-04 22:21:36 +00:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 15:10:43 +00:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-04 22:21:36 +00:00
|
|
|
|
2020-07-08 08:58:57 +00:00
|
|
|
const ref = res["reference"](params.id);
|
2021-02-11 20:11:34 +00:00
|
|
|
const endpoint_url = `${homeserver}${ref.endpoint}?${stringify(query)}`;
|
2020-02-07 15:10:43 +00:00
|
|
|
|
2020-07-08 08:58:57 +00:00
|
|
|
return jsonClient(endpoint_url).then(({ headers, json }) => ({
|
|
|
|
data: json[res.data].map(res.map),
|
2020-11-12 15:14:30 +00:00
|
|
|
total: res.total(json, from, perPage),
|
2020-02-07 15:10:43 +00:00
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
update: (resource, params) => {
|
|
|
|
console.log("update " + resource);
|
2020-03-04 22:21:36 +00:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 15:10:43 +00:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-04 22:21:36 +00:00
|
|
|
|
2020-04-23 08:25:59 +00:00
|
|
|
const endpoint_url = homeserver + res.path;
|
|
|
|
return jsonClient(`${endpoint_url}/${params.data.id}`, {
|
2020-02-07 15:10:43 +00:00
|
|
|
method: "PUT",
|
|
|
|
body: JSON.stringify(params.data, filterNullValues),
|
|
|
|
}).then(({ json }) => ({
|
2020-02-10 17:06:05 +00:00
|
|
|
data: res.map(json),
|
2020-02-07 15:10:43 +00:00
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
updateMany: (resource, params) => {
|
|
|
|
console.log("updateMany " + resource);
|
2020-03-04 22:21:36 +00:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 15:10:43 +00:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-04 22:21:36 +00:00
|
|
|
|
2020-04-23 08:25:59 +00:00
|
|
|
const endpoint_url = homeserver + res.path;
|
2020-02-07 15:10:43 +00:00
|
|
|
return Promise.all(
|
2020-04-23 08:25:59 +00:00
|
|
|
params.ids.map(id => jsonClient(`${endpoint_url}/${id}`), {
|
2020-02-07 15:10:43 +00:00
|
|
|
method: "PUT",
|
|
|
|
body: JSON.stringify(params.data, filterNullValues),
|
|
|
|
})
|
|
|
|
).then(responses => ({
|
|
|
|
data: responses.map(({ json }) => json),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
create: (resource, params) => {
|
|
|
|
console.log("create " + resource);
|
2020-03-04 22:21:36 +00:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 15:10:43 +00:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-04-23 08:44:52 +00:00
|
|
|
if (!("create" in res)) return Promise.reject();
|
2020-03-04 22:21:36 +00:00
|
|
|
|
2020-04-23 08:44:52 +00:00
|
|
|
const create = res["create"](params.data);
|
|
|
|
const endpoint_url = homeserver + create.endpoint;
|
|
|
|
return jsonClient(endpoint_url, {
|
|
|
|
method: create.method,
|
|
|
|
body: JSON.stringify(create.body, filterNullValues),
|
2020-02-07 15:10:43 +00:00
|
|
|
}).then(({ json }) => ({
|
2020-02-10 17:06:05 +00:00
|
|
|
data: res.map(json),
|
2020-02-07 15:10:43 +00:00
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
2020-05-06 07:03:33 +00:00
|
|
|
createMany: (resource, params) => {
|
|
|
|
console.log("createMany " + resource);
|
|
|
|
const homeserver = localStorage.getItem("base_url");
|
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
|
|
|
if (!("create" in res)) return Promise.reject();
|
|
|
|
|
|
|
|
return Promise.all(
|
|
|
|
params.ids.map(id => {
|
|
|
|
params.data.id = id;
|
|
|
|
const cre = res["create"](params.data);
|
|
|
|
const endpoint_url = homeserver + cre.endpoint;
|
|
|
|
return jsonClient(endpoint_url, {
|
|
|
|
method: cre.method,
|
|
|
|
body: JSON.stringify(cre.body, filterNullValues),
|
|
|
|
});
|
|
|
|
})
|
|
|
|
).then(responses => ({
|
|
|
|
data: responses.map(({ json }) => json),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
2020-02-07 15:10:43 +00:00
|
|
|
delete: (resource, params) => {
|
|
|
|
console.log("delete " + resource);
|
2020-03-04 22:21:36 +00:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 15:10:43 +00:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-04 22:21:36 +00:00
|
|
|
|
2020-04-09 08:32:06 +00:00
|
|
|
if ("delete" in res) {
|
2020-07-08 09:11:24 +00:00
|
|
|
const del = res["delete"](params);
|
2020-04-23 08:25:59 +00:00
|
|
|
const endpoint_url = homeserver + del.endpoint;
|
|
|
|
return jsonClient(endpoint_url, {
|
2020-07-08 09:11:24 +00:00
|
|
|
method: "method" in del ? del.method : "DELETE",
|
|
|
|
body: "body" in del ? JSON.stringify(del.body) : null,
|
2020-04-09 08:32:06 +00:00
|
|
|
}).then(({ json }) => ({
|
|
|
|
data: json,
|
|
|
|
}));
|
|
|
|
} else {
|
2020-04-23 08:25:59 +00:00
|
|
|
const endpoint_url = homeserver + res.path;
|
|
|
|
return jsonClient(`${endpoint_url}/${params.id}`, {
|
2020-04-09 08:32:06 +00:00
|
|
|
method: "DELETE",
|
|
|
|
body: JSON.stringify(params.data, filterNullValues),
|
|
|
|
}).then(({ json }) => ({
|
|
|
|
data: json,
|
|
|
|
}));
|
|
|
|
}
|
2020-02-07 15:10:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
deleteMany: (resource, params) => {
|
|
|
|
console.log("deleteMany " + resource);
|
2020-03-04 22:21:36 +00:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 15:10:43 +00:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-04 22:21:36 +00:00
|
|
|
|
2020-04-09 08:32:06 +00:00
|
|
|
if ("delete" in res) {
|
|
|
|
return Promise.all(
|
|
|
|
params.ids.map(id => {
|
2020-07-08 09:11:24 +00:00
|
|
|
const del = res["delete"]({ ...params, id: id });
|
2020-04-23 08:25:59 +00:00
|
|
|
const endpoint_url = homeserver + del.endpoint;
|
|
|
|
return jsonClient(endpoint_url, {
|
2020-07-08 09:11:24 +00:00
|
|
|
method: "method" in del ? del.method : "DELETE",
|
|
|
|
body: "body" in del ? JSON.stringify(del.body) : null,
|
2020-04-09 08:32:06 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
).then(responses => ({
|
|
|
|
data: responses.map(({ json }) => json),
|
|
|
|
}));
|
|
|
|
} else {
|
2020-04-23 08:25:59 +00:00
|
|
|
const endpoint_url = homeserver + res.path;
|
2020-04-09 08:32:06 +00:00
|
|
|
return Promise.all(
|
|
|
|
params.ids.map(id =>
|
2020-04-23 08:25:59 +00:00
|
|
|
jsonClient(`${endpoint_url}/${id}`, {
|
2020-04-09 08:32:06 +00:00
|
|
|
method: "DELETE",
|
|
|
|
body: JSON.stringify(params.data, filterNullValues),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).then(responses => ({
|
|
|
|
data: responses.map(({ json }) => json),
|
|
|
|
}));
|
|
|
|
}
|
2020-02-07 15:10:43 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default dataProvider;
|