2020-02-07 18:10:43 +03: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);
|
|
|
|
};
|
|
|
|
|
|
|
|
const resourceMap = {
|
|
|
|
users: {
|
|
|
|
path: "/_synapse/admin/v2/users",
|
|
|
|
map: u => ({
|
|
|
|
...u,
|
|
|
|
id: u.name,
|
|
|
|
is_guest: !!u.is_guest,
|
|
|
|
admin: !!u.admin,
|
|
|
|
deactivated: !!u.deactivated,
|
|
|
|
}),
|
|
|
|
data: "users",
|
2020-03-24 18:53:09 +03:00
|
|
|
total: (json, from, perPage) => {
|
2020-03-27 23:22:16 +03:00
|
|
|
return json.next_token
|
|
|
|
? parseInt(json.next_token, 10) + perPage
|
|
|
|
: from + json.users.length;
|
2020-02-07 18:10:43 +03:00
|
|
|
},
|
2020-04-09 11:32:06 +03:00
|
|
|
delete: id => ({
|
|
|
|
endpoint: `/_synapse/admin/v1/deactivate/${id}`,
|
|
|
|
body: { erase: true },
|
|
|
|
method: "POST",
|
|
|
|
}),
|
2020-02-07 18:10:43 +03:00
|
|
|
},
|
|
|
|
rooms: {
|
|
|
|
path: "/_synapse/admin/v1/rooms",
|
|
|
|
map: r => ({
|
|
|
|
...r,
|
|
|
|
id: r.room_id,
|
|
|
|
alias: r.canonical_alias,
|
|
|
|
members: r.joined_members,
|
|
|
|
}),
|
|
|
|
data: "rooms",
|
|
|
|
total: json => {
|
|
|
|
return json.total_rooms;
|
|
|
|
},
|
|
|
|
},
|
2020-03-28 23:25:34 +03:00
|
|
|
connections: {
|
|
|
|
path: "/_synapse/admin/v1/whois",
|
|
|
|
map: c => ({
|
|
|
|
...c,
|
|
|
|
id: c.user_id,
|
|
|
|
}),
|
|
|
|
data: "connections",
|
|
|
|
},
|
2020-02-07 18:10:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
function filterNullValues(key, value) {
|
|
|
|
// Filtering out null properties
|
|
|
|
if (value === null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-04-17 00:31:41 +03:00
|
|
|
const roomCreationMap = {
|
|
|
|
path: "/_matrix/client/r0/createRoom",
|
|
|
|
map: r => ({
|
|
|
|
room_id: r.room_id
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
const roomCreationProvider = {
|
|
|
|
create: (resource, params) => {
|
2020-04-21 15:00:57 +03:00
|
|
|
const homeserver = localStorage.getItem("home_server_url");
|
2020-04-17 00:31:41 +03:00
|
|
|
|
|
|
|
const homeserver_url = "https://" + homeserver + roomCreationMap.path;
|
|
|
|
|
|
|
|
const newParams = { ...params.data,
|
|
|
|
public: undefined,
|
|
|
|
room_name: undefined,
|
2020-04-17 01:09:56 +03:00
|
|
|
alias: undefined,
|
2020-04-17 00:31:41 +03:00
|
|
|
|
|
|
|
name: params.data.room_name,
|
2020-04-17 01:09:56 +03:00
|
|
|
room_alias_name: params.data.alias,
|
2020-04-17 00:31:41 +03:00
|
|
|
visibility: params.data.public ? "public" : "private",
|
|
|
|
}
|
|
|
|
|
|
|
|
return jsonClient(homeserver_url, {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify(newParams, filterNullValues),
|
|
|
|
}).then(({ json }) => ({
|
|
|
|
data: roomCreationMap.map(json),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-07 18:10:43 +03:00
|
|
|
const dataProvider = {
|
|
|
|
getList: (resource, params) => {
|
|
|
|
console.log("getList " + resource);
|
2020-03-24 18:53:09 +03:00
|
|
|
const { user_id, guests, deactivated } = params.filter;
|
2020-02-07 18:10:43 +03:00
|
|
|
const { page, perPage } = params.pagination;
|
2020-03-24 18:53:09 +03:00
|
|
|
const from = (page - 1) * perPage;
|
2020-02-07 18:10:43 +03:00
|
|
|
const query = {
|
2020-03-24 18:53:09 +03:00
|
|
|
from: from,
|
2020-02-07 18:10:43 +03:00
|
|
|
limit: perPage,
|
|
|
|
user_id: user_id,
|
|
|
|
guests: guests,
|
2020-03-24 18:53:09 +03:00
|
|
|
deactivated: deactivated,
|
2020-02-07 18:10:43 +03:00
|
|
|
};
|
2020-03-05 01:21:36 +03:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 18:10:43 +03:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-05 01:21:36 +03:00
|
|
|
|
|
|
|
const homeserver_url = homeserver + res.path;
|
2020-02-07 18:10:43 +03:00
|
|
|
const url = `${homeserver_url}?${stringify(query)}`;
|
|
|
|
|
2020-04-15 16:31:10 +03:00
|
|
|
// searching for users is not implemented in admin api
|
|
|
|
if (params.filter.q) {
|
|
|
|
console.log("searching");
|
|
|
|
const search_query = { limit: perPage, search_term: params.filter.q };
|
|
|
|
const search_url =
|
|
|
|
homeserver + `/_matrix/client/r0/user_directory/search`;
|
|
|
|
return jsonClient(search_url, {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify(search_query),
|
|
|
|
}).then(({ json }) => ({
|
|
|
|
data: json["results"].map(u => ({
|
|
|
|
...u,
|
|
|
|
id: u.user_id,
|
|
|
|
name: u.user_id,
|
|
|
|
})),
|
|
|
|
total: json.limited ? perPage * 2 : json.results.length,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-02-07 18:10:43 +03:00
|
|
|
return jsonClient(url).then(({ json }) => ({
|
|
|
|
data: json[res.data].map(res.map),
|
2020-03-24 18:53:09 +03:00
|
|
|
total: res.total(json, from, perPage),
|
2020-02-07 18:10:43 +03:00
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
getOne: (resource, params) => {
|
|
|
|
console.log("getOne " + resource);
|
2020-03-05 01:21:36 +03:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 18:10:43 +03:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-05 01:21:36 +03:00
|
|
|
|
|
|
|
const homeserver_url = homeserver + res.path;
|
2020-02-07 18:10:43 +03:00
|
|
|
return jsonClient(`${homeserver_url}/${params.id}`).then(({ json }) => ({
|
|
|
|
data: res.map(json),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
getMany: (resource, params) => {
|
|
|
|
console.log("getMany " + resource);
|
2020-03-05 01:21:36 +03:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 18:10:43 +03:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-05 01:21:36 +03:00
|
|
|
|
|
|
|
const homeserver_url = homeserver + res.path;
|
2020-02-07 18:10:43 +03:00
|
|
|
return Promise.all(
|
|
|
|
params.ids.map(id => jsonClient(`${homeserver_url}/${id}`))
|
|
|
|
).then(responses => ({
|
|
|
|
data: responses.map(({ json }) => res.map(json)),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
getManyReference: (resource, params) => {
|
|
|
|
// FIXME
|
|
|
|
console.log("getManyReference " + resource);
|
|
|
|
const { page, perPage } = params.pagination;
|
|
|
|
const { field, order } = params.sort;
|
|
|
|
const query = {
|
|
|
|
sort: JSON.stringify([field, order]),
|
|
|
|
range: JSON.stringify([(page - 1) * perPage, page * perPage - 1]),
|
|
|
|
filter: JSON.stringify({
|
|
|
|
...params.filter,
|
|
|
|
[params.target]: params.id,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2020-03-05 01:21:36 +03:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 18:10:43 +03:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-05 01:21:36 +03:00
|
|
|
|
|
|
|
const homeserver_url = homeserver + res.path;
|
2020-02-07 18:10:43 +03:00
|
|
|
const url = `${homeserver_url}?${stringify(query)}`;
|
|
|
|
|
|
|
|
return jsonClient(url).then(({ headers, json }) => ({
|
|
|
|
data: json,
|
2020-04-06 12:42:49 +03:00
|
|
|
total: parseInt(headers.get("content-range").split("/").pop(), 10),
|
2020-02-07 18:10:43 +03:00
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
update: (resource, params) => {
|
|
|
|
console.log("update " + resource);
|
2020-03-05 01:21:36 +03:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 18:10:43 +03:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-05 01:21:36 +03:00
|
|
|
|
|
|
|
const homeserver_url = homeserver + res.path;
|
2020-02-10 20:06:05 +03:00
|
|
|
return jsonClient(`${homeserver_url}/${params.data.id}`, {
|
2020-02-07 18:10:43 +03:00
|
|
|
method: "PUT",
|
|
|
|
body: JSON.stringify(params.data, filterNullValues),
|
|
|
|
}).then(({ json }) => ({
|
2020-02-10 20:06:05 +03:00
|
|
|
data: res.map(json),
|
2020-02-07 18:10:43 +03:00
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
updateMany: (resource, params) => {
|
|
|
|
console.log("updateMany " + resource);
|
2020-03-05 01:21:36 +03:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 18:10:43 +03:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-05 01:21:36 +03:00
|
|
|
|
|
|
|
const homeserver_url = homeserver + res.path;
|
2020-02-07 18:10:43 +03:00
|
|
|
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) => {
|
|
|
|
console.log("create " + resource);
|
2020-03-05 01:21:36 +03:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 18:10:43 +03:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-05 01:21:36 +03:00
|
|
|
|
|
|
|
const homeserver_url = homeserver + res.path;
|
2020-04-17 00:31:41 +03:00
|
|
|
|
|
|
|
/* Special handling for rooms, as creating a room
|
|
|
|
is a POST request rather than put, and goes through
|
|
|
|
the client-server API rather than the admin API. */
|
|
|
|
if (resource === "rooms") {
|
|
|
|
console.log("want to create a room!");
|
|
|
|
console.log(params);
|
|
|
|
return roomCreationProvider.create(resource, params);
|
|
|
|
}
|
|
|
|
|
2020-02-10 20:06:05 +03:00
|
|
|
return jsonClient(`${homeserver_url}/${params.data.id}`, {
|
2020-02-07 18:10:43 +03:00
|
|
|
method: "PUT",
|
|
|
|
body: JSON.stringify(params.data, filterNullValues),
|
|
|
|
}).then(({ json }) => ({
|
2020-02-10 20:06:05 +03:00
|
|
|
data: res.map(json),
|
2020-02-07 18:10:43 +03:00
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
delete: (resource, params) => {
|
|
|
|
console.log("delete " + resource);
|
2020-03-05 01:21:36 +03:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 18:10:43 +03:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-05 01:21:36 +03:00
|
|
|
|
2020-04-09 11:32:06 +03:00
|
|
|
if ("delete" in res) {
|
|
|
|
const del = res["delete"](params.id);
|
|
|
|
const homeserver_url = homeserver + del.endpoint;
|
|
|
|
return jsonClient(homeserver_url, {
|
|
|
|
method: del.method,
|
|
|
|
body: JSON.stringify(del.body),
|
|
|
|
}).then(({ json }) => ({
|
|
|
|
data: json,
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
const homeserver_url = homeserver + res.path;
|
|
|
|
return jsonClient(`${homeserver_url}/${params.id}`, {
|
|
|
|
method: "DELETE",
|
|
|
|
body: JSON.stringify(params.data, filterNullValues),
|
|
|
|
}).then(({ json }) => ({
|
|
|
|
data: json,
|
|
|
|
}));
|
|
|
|
}
|
2020-02-07 18:10:43 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
deleteMany: (resource, params) => {
|
|
|
|
console.log("deleteMany " + resource);
|
2020-03-05 01:21:36 +03:00
|
|
|
const homeserver = localStorage.getItem("base_url");
|
2020-02-07 18:10:43 +03:00
|
|
|
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
|
|
|
|
|
|
|
const res = resourceMap[resource];
|
2020-03-05 01:21:36 +03:00
|
|
|
|
2020-04-09 11:32:06 +03:00
|
|
|
if ("delete" in res) {
|
|
|
|
return Promise.all(
|
|
|
|
params.ids.map(id => {
|
|
|
|
const del = res["delete"](id);
|
|
|
|
const homeserver_url = homeserver + del.endpoint;
|
|
|
|
return jsonClient(homeserver_url, {
|
|
|
|
method: del.method,
|
|
|
|
body: JSON.stringify(del.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: "DELETE",
|
|
|
|
body: JSON.stringify(params.data, filterNullValues),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).then(responses => ({
|
|
|
|
data: responses.map(({ json }) => json),
|
|
|
|
}));
|
|
|
|
}
|
2020-02-07 18:10:43 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default dataProvider;
|