Move differences in dataProvider into the resourceMap

This commit is contained in:
dklimpel 2020-04-10 20:28:17 +02:00
parent 2bbc0c6ae6
commit 22ddf87f00

View File

@ -30,7 +30,12 @@ const resourceMap = {
? parseInt(json.next_token, 10) + perPage ? parseInt(json.next_token, 10) + perPage
: from + json.users.length; : from + json.users.length;
}, },
delete_path: "/_synapse/admin/v1/deactivate", delete_path: id => {
return "/_synapse/admin/v1/deactivate/" + id;
},
delete_body: () => {
return JSON.stringify({ erase: true });
},
}, },
rooms: { rooms: {
path: "/_synapse/admin/v1/rooms", path: "/_synapse/admin/v1/rooms",
@ -44,7 +49,12 @@ const resourceMap = {
total: json => { total: json => {
return json.total_rooms; return json.total_rooms;
}, },
delete_path: "/_synapse/admin/v1/purge_room", delete_path: () => {
return "/_synapse/admin/v1/purge_room";
},
delete_body: id => {
return JSON.stringify({ room_id: `${id}` });
},
}, },
connections: { connections: {
path: "/_synapse/admin/v1/whois", path: "/_synapse/admin/v1/whois",
@ -203,22 +213,17 @@ const dataProvider = {
if (!homeserver || !(resource in resourceMap)) return Promise.reject(); if (!homeserver || !(resource in resourceMap)) return Promise.reject();
const res = resourceMap[resource]; const res = resourceMap[resource];
const homeserver_url = homeserver + res.delete_path; const homeserver_url = homeserver + res.path;
var request_url; return jsonClient(
var request_body; res.delete_path(params.id)
if (res.data === "users") { ? `${homeserver}` + res.delete_path(params.id)
request_url = `${homeserver_url}/${params.id}`; : `${homeserver_url}/${params.id}`,
request_body = { erase: true }; {
} else if (res.data === "rooms") { method: res.delete_path(params.id) ? "POST" : "DELETE",
request_url = `${homeserver_url}`; body: res.delete_body(params.id),
request_body = { room_id: `${params.id}` }; }
} ).then(({ json }) => ({
return jsonClient(request_url, {
method: "POST",
body: JSON.stringify(request_body),
}).then(({ json }) => ({
data: json, data: json,
})); }));
}, },
@ -229,31 +234,23 @@ const dataProvider = {
if (!homeserver || !(resource in resourceMap)) return Promise.reject(); if (!homeserver || !(resource in resourceMap)) return Promise.reject();
const res = resourceMap[resource]; const res = resourceMap[resource];
const homeserver_url = homeserver + res.delete_path; const homeserver_url = homeserver + res.path;
if (res.data === "users") { return Promise.all(
return Promise.all( params.ids.map(id =>
params.ids.map(id => jsonClient(
jsonClient(`${homeserver_url}/${id}`, { res.delete_path(id)
method: "POST", ? `${homeserver}` + res.delete_path(id)
body: JSON.stringify({ erase: true }), : `${homeserver_url}/${id}`,
}).then(json => ({ {
data: json, method: res.delete_path(id) ? "POST" : "DELETE",
})) body: res.delete_body(id),
) }
); ).then(json => ({
} else if (res.data === "rooms") { data: json,
return Promise.all( }))
params.ids.map(id => )
jsonClient(`${homeserver_url}`, { );
method: "POST",
body: JSON.stringify({ room_id: `${id}` }),
}).then(json => ({
data: json,
}))
)
);
}
}, },
}; };