2022-02-23 20:25:05 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const urlJoin = require('url-join');
|
|
|
|
|
2022-09-08 00:30:04 -06:00
|
|
|
const assert = require('matrix-public-archive-shared/lib/assert');
|
|
|
|
|
|
|
|
function qsToUrlPiece(qs) {
|
|
|
|
if (qs.toString()) {
|
|
|
|
return `?${qs.toString()}`;
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 20:25:05 -07:00
|
|
|
class URLCreator {
|
|
|
|
constructor(basePath) {
|
|
|
|
this._basePath = basePath;
|
|
|
|
}
|
|
|
|
|
2022-09-20 15:02:09 -06:00
|
|
|
permalinkForRoomId(roomId) {
|
|
|
|
return `https://matrix.to/#/${roomId}`;
|
|
|
|
}
|
|
|
|
|
2022-09-15 19:41:55 -06:00
|
|
|
roomDirectoryUrl({ searchTerm, paginationToken } = {}) {
|
2022-09-08 00:30:04 -06:00
|
|
|
let qs = new URLSearchParams();
|
2022-09-15 19:41:55 -06:00
|
|
|
if (searchTerm) {
|
|
|
|
qs.append('search', searchTerm);
|
|
|
|
}
|
2022-09-08 00:30:04 -06:00
|
|
|
if (paginationToken) {
|
|
|
|
qs.append('page', paginationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${this._basePath}${qsToUrlPiece(qs)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
archiveUrlForRoom(roomId, { viaServers = [] } = {}) {
|
|
|
|
assert(roomId);
|
|
|
|
let qs = new URLSearchParams();
|
|
|
|
[].concat(viaServers).forEach((viaServer) => {
|
|
|
|
qs.append('via', viaServer);
|
|
|
|
});
|
|
|
|
|
|
|
|
return `${urlJoin(this._basePath, `${roomId}`)}${qsToUrlPiece(qs)}`;
|
|
|
|
}
|
|
|
|
|
2022-08-29 17:56:31 -06:00
|
|
|
archiveUrlForDate(roomId, date, { viaServers = [] } = {}) {
|
2022-09-08 00:30:04 -06:00
|
|
|
assert(roomId);
|
|
|
|
assert(date);
|
|
|
|
|
2022-08-29 17:56:31 -06:00
|
|
|
let qs = new URLSearchParams();
|
|
|
|
[].concat(viaServers).forEach((viaServer) => {
|
|
|
|
qs.append('via', viaServer);
|
|
|
|
});
|
|
|
|
|
2022-02-23 20:25:05 -07:00
|
|
|
// Gives the date in YYYY/mm/dd format.
|
|
|
|
// date.toISOString() -> 2022-02-16T23:20:04.709Z
|
|
|
|
const urlDate = date.toISOString().split('T')[0].replaceAll('-', '/');
|
|
|
|
|
2022-09-08 00:30:04 -06:00
|
|
|
return `${urlJoin(this._basePath, `${roomId}/date/${urlDate}`)}${qsToUrlPiece(qs)}`;
|
2022-02-23 20:25:05 -07:00
|
|
|
}
|
2022-09-20 15:02:09 -06:00
|
|
|
|
|
|
|
archiveJumpUrlForRoom(roomId, { ts, dir }) {
|
|
|
|
assert(roomId);
|
|
|
|
assert(ts);
|
|
|
|
assert(dir);
|
|
|
|
|
|
|
|
let qs = new URLSearchParams();
|
|
|
|
qs.append('ts', ts);
|
|
|
|
qs.append('dir', dir);
|
|
|
|
|
|
|
|
return `${urlJoin(this._basePath, `${roomId}/jump`)}${qsToUrlPiece(qs)}`;
|
|
|
|
}
|
2022-02-23 20:25:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = URLCreator;
|