matrix-public-archive/server/fetch-room-data.js

51 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-02-15 20:33:31 -07:00
'use strict';
const assert = require('assert');
2022-02-16 18:58:32 -07:00
const urlJoin = require('url-join');
const fetchEndpoint = require('./lib/fetch-endpoint');
const { matrixServerUrl } = require('../config.json');
assert(matrixServerUrl);
async function fetchRoomData(accessToken, roomId) {
assert(accessToken);
assert(roomId);
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([
fetchEndpoint(stateNameEndpoint, {
accessToken,
}),
fetchEndpoint(stateAvatarEndpoint, {
accessToken,
}),
]);
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;