2022-08-29 17:56:31 -06:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const assert = require('assert');
|
|
|
|
const urlJoin = require('url-join');
|
|
|
|
|
2022-09-08 00:30:04 -06:00
|
|
|
const { fetchEndpointAsJson } = require('../fetch-endpoint');
|
2022-08-29 17:56:31 -06:00
|
|
|
|
2022-09-08 00:30:04 -06:00
|
|
|
const config = require('../config');
|
2022-09-08 18:15:07 -06:00
|
|
|
const StatusError = require('../status-error');
|
2022-08-29 17:56:31 -06:00
|
|
|
const matrixServerUrl = config.get('matrixServerUrl');
|
|
|
|
assert(matrixServerUrl);
|
|
|
|
|
|
|
|
async function ensureRoomJoined(accessToken, roomId, viaServers = []) {
|
|
|
|
let qs = new URLSearchParams();
|
|
|
|
[].concat(viaServers).forEach((viaServer) => {
|
|
|
|
qs.append('server_name', viaServer);
|
|
|
|
});
|
|
|
|
|
|
|
|
const joinEndpoint = urlJoin(
|
|
|
|
matrixServerUrl,
|
|
|
|
`_matrix/client/r0/join/${roomId}?${qs.toString()}`
|
|
|
|
);
|
2022-09-08 18:15:07 -06:00
|
|
|
try {
|
|
|
|
await fetchEndpointAsJson(joinEndpoint, {
|
|
|
|
method: 'POST',
|
|
|
|
accessToken,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
throw new StatusError(403, `Archiver is unable to join room: ${err.message}`);
|
|
|
|
}
|
2022-08-29 17:56:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ensureRoomJoined;
|