2022-02-15 20:33:31 -07:00
|
|
|
'use strict';
|
|
|
|
|
2022-02-10 00:42:02 -07:00
|
|
|
const assert = require('assert');
|
|
|
|
|
2022-02-16 18:58:32 -07:00
|
|
|
const urlJoin = require('url-join');
|
2022-02-23 20:25:05 -07:00
|
|
|
const { fetchEndpointAsJson } = require('./lib/fetch-endpoint');
|
2022-02-10 00:42:02 -07:00
|
|
|
|
2022-02-23 20:25:05 -07:00
|
|
|
const config = require('./lib/config');
|
|
|
|
const matrixServerUrl = config.get('matrixServerUrl');
|
2022-02-10 00:42:02 -07:00
|
|
|
assert(matrixServerUrl);
|
|
|
|
|
2022-02-22 19:25:24 -07:00
|
|
|
async function fetchRoomData(accessToken, roomId) {
|
|
|
|
assert(accessToken);
|
|
|
|
assert(roomId);
|
|
|
|
|
2022-02-10 00:42:02 -07:00
|
|
|
const stateNameEndpoint = urlJoin(
|
|
|
|
matrixServerUrl,
|
|
|
|
`_matrix/client/r0/rooms/${roomId}/state/m.room.name`
|
|
|
|
);
|
|
|
|
const stateAvatarEndpoint = urlJoin(
|
|
|
|
matrixServerUrl,
|
|
|
|
`_matrix/client/r0/rooms/${roomId}/state/m.room.avatar`
|
|
|
|
);
|
|
|
|
|
|
|
|
const [stateNameResDataOutcome, stateAvatarResDataOutcome] = await Promise.allSettled([
|
2022-02-23 20:25:05 -07:00
|
|
|
fetchEndpointAsJson(stateNameEndpoint, {
|
2022-02-22 19:25:24 -07:00
|
|
|
accessToken,
|
|
|
|
}),
|
2022-02-23 20:25:05 -07:00
|
|
|
fetchEndpointAsJson(stateAvatarEndpoint, {
|
2022-02-22 19:25:24 -07:00
|
|
|
accessToken,
|
|
|
|
}),
|
2022-02-10 00:42:02 -07:00
|
|
|
]);
|
|
|
|
|
|
|
|
let name;
|
|
|
|
if (stateNameResDataOutcome.reason === undefined) {
|
|
|
|
name = stateNameResDataOutcome.value.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
let avatarUrl;
|
|
|
|
if (stateAvatarResDataOutcome.reason === undefined) {
|
|
|
|
avatarUrl = stateAvatarResDataOutcome.value.url;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: roomId,
|
|
|
|
name,
|
|
|
|
avatarUrl,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = fetchRoomData;
|