2022-09-08 00:30:04 -06:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const assert = require('assert');
|
|
|
|
|
|
|
|
const urlJoin = require('url-join');
|
|
|
|
const { fetchEndpointAsJson } = require('../fetch-endpoint');
|
|
|
|
const { traceFunction } = require('../../tracing/trace-utilities');
|
|
|
|
|
|
|
|
const config = require('../config');
|
|
|
|
const matrixServerUrl = config.get('matrixServerUrl');
|
|
|
|
assert(matrixServerUrl);
|
|
|
|
|
2022-09-15 19:41:55 -06:00
|
|
|
async function fetchPublicRooms(accessToken, { server, searchTerm, paginationToken, limit } = {}) {
|
2022-09-08 00:30:04 -06:00
|
|
|
assert(accessToken);
|
|
|
|
|
|
|
|
let qs = new URLSearchParams();
|
|
|
|
if (server) {
|
|
|
|
qs.append('server', server);
|
|
|
|
}
|
|
|
|
|
|
|
|
const publicRoomsEndpoint = urlJoin(
|
|
|
|
matrixServerUrl,
|
|
|
|
`_matrix/client/v3/publicRooms?${qs.toString()}`
|
|
|
|
);
|
|
|
|
|
2022-11-03 03:12:00 -06:00
|
|
|
const { data: publicRoomsRes } = await fetchEndpointAsJson(publicRoomsEndpoint, {
|
2022-09-15 19:41:55 -06:00
|
|
|
method: 'POST',
|
|
|
|
body: {
|
2022-10-20 01:06:43 -06:00
|
|
|
include_all_networks: true,
|
2022-09-15 19:41:55 -06:00
|
|
|
filter: {
|
|
|
|
generic_search_term: searchTerm,
|
|
|
|
},
|
|
|
|
since: paginationToken,
|
|
|
|
limit,
|
|
|
|
},
|
2022-09-08 00:30:04 -06:00
|
|
|
accessToken,
|
|
|
|
});
|
|
|
|
|
|
|
|
// We only want to see public rooms in the archive
|
|
|
|
const accessibleRooms = publicRoomsRes.chunk.filter((room) => {
|
|
|
|
// `room.world_readable` is also accessible here but we only use history
|
|
|
|
// `world_readable` to determine search indexing.
|
|
|
|
return room.join_rule === 'public';
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
rooms: accessibleRooms,
|
|
|
|
nextPaginationToken: publicRoomsRes.next_batch,
|
|
|
|
prevPaginationToken: publicRoomsRes.prev_batch,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = traceFunction(fetchPublicRooms);
|