2022-02-15 16:17:14 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const assert = require('assert');
|
|
|
|
const path = require('path');
|
2022-02-16 18:58:32 -07:00
|
|
|
const urlJoin = require('url-join');
|
2022-02-17 15:56:54 -07:00
|
|
|
const express = require('express');
|
2022-02-15 16:17:14 -07:00
|
|
|
const asyncHandler = require('../lib/express-async-handler');
|
|
|
|
const StatusError = require('../lib/status-error');
|
|
|
|
|
2022-07-14 10:08:50 -06:00
|
|
|
const { handleTracingMiddleware, getSerializableSpans } = require('../tracing/tracing-middleware');
|
|
|
|
const timeoutMiddleware = require('./timeout-middleware');
|
|
|
|
|
2022-02-15 16:17:14 -07:00
|
|
|
const fetchRoomData = require('../fetch-room-data');
|
2022-02-24 02:27:53 -07:00
|
|
|
const fetchEventsInRange = require('../fetch-events-in-range');
|
2022-07-05 16:30:52 -06:00
|
|
|
const renderHydrogenToString = require('../hydrogen-render/1-render-hydrogen-to-string');
|
2022-07-14 10:08:50 -06:00
|
|
|
const sanitizeHtml = require('../lib/sanitize-html');
|
|
|
|
const safeJson = require('../lib/safe-json');
|
2022-02-15 16:17:14 -07:00
|
|
|
|
2022-02-23 20:25:05 -07:00
|
|
|
const config = require('../lib/config');
|
|
|
|
const basePath = config.get('basePath');
|
2022-02-15 16:17:14 -07:00
|
|
|
assert(basePath);
|
2022-02-23 20:25:05 -07:00
|
|
|
const matrixAccessToken = config.get('matrixAccessToken');
|
2022-02-22 19:25:24 -07:00
|
|
|
assert(matrixAccessToken);
|
2022-02-24 02:27:53 -07:00
|
|
|
const archiveMessageLimit = config.get('archiveMessageLimit');
|
|
|
|
assert(archiveMessageLimit);
|
2022-02-22 19:25:24 -07:00
|
|
|
|
2022-02-15 20:33:31 -07:00
|
|
|
function parseArchiveRangeFromReq(req) {
|
2022-02-15 16:17:14 -07:00
|
|
|
const yyyy = parseInt(req.params.yyyy, 10);
|
|
|
|
// Month is the only zero-based index in this group
|
|
|
|
const mm = parseInt(req.params.mm, 10) - 1;
|
|
|
|
const dd = parseInt(req.params.dd, 10);
|
|
|
|
|
|
|
|
const hourRange = req.params.hourRange;
|
|
|
|
|
|
|
|
let fromHour = 0;
|
|
|
|
let toHour = 0;
|
|
|
|
if (hourRange) {
|
|
|
|
const hourMatches = hourRange.match(/^(\d\d?)-(\d\d?)$/);
|
|
|
|
|
|
|
|
if (!hourMatches) {
|
|
|
|
throw new StatusError(404, 'Hour was unable to be parsed');
|
|
|
|
}
|
|
|
|
|
|
|
|
fromHour = parseInt(hourMatches[1], 10);
|
|
|
|
toHour = parseInt(hourMatches[2], 10);
|
|
|
|
|
|
|
|
if (Number.isNaN(fromHour) || fromHour < 0 || fromHour > 23) {
|
|
|
|
throw new StatusError(404, 'From hour can only be in range 0-23');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fromTimestamp = Date.UTC(yyyy, mm, dd, fromHour);
|
|
|
|
let toTimestamp = Date.UTC(yyyy, mm, dd + 1, fromHour);
|
|
|
|
if (hourRange) {
|
|
|
|
toTimestamp = Date.UTC(yyyy, mm, dd, toHour);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
fromTimestamp,
|
|
|
|
toTimestamp,
|
|
|
|
yyyy,
|
|
|
|
mm,
|
|
|
|
dd,
|
2022-02-16 18:58:32 -07:00
|
|
|
hourRange,
|
2022-02-15 20:33:31 -07:00
|
|
|
fromHour,
|
|
|
|
toHour,
|
2022-02-15 16:17:14 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function installRoutes(app) {
|
2022-07-14 10:08:50 -06:00
|
|
|
app.use(handleTracingMiddleware);
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
'/health-check',
|
|
|
|
asyncHandler(async function (req, res) {
|
|
|
|
res.send('{ "ok": true }');
|
|
|
|
})
|
|
|
|
);
|
2022-06-15 16:12:44 -06:00
|
|
|
|
2022-02-17 15:56:54 -07:00
|
|
|
// We have to disable no-missing-require lint because it doesn't take into
|
|
|
|
// account `package.json`. `exports`, see
|
|
|
|
// https://github.com/mysticatea/eslint-plugin-node/issues/255
|
|
|
|
// eslint-disable-next-line node/no-missing-require
|
2022-06-06 17:58:45 -06:00
|
|
|
app.use(express.static(path.dirname(require.resolve('hydrogen-view-sdk/assets/main.js'))));
|
2022-02-17 15:56:54 -07:00
|
|
|
|
2022-07-14 10:08:50 -06:00
|
|
|
app.get(
|
|
|
|
'/hydrogen-styles.css',
|
|
|
|
asyncHandler(async function (req, res) {
|
|
|
|
res.set('Content-Type', 'text/css');
|
|
|
|
// We have to disable no-missing-require lint because it doesn't take into
|
|
|
|
// account `package.json`. `exports`, see
|
|
|
|
// https://github.com/mysticatea/eslint-plugin-node/issues/255
|
|
|
|
// eslint-disable-next-line node/no-missing-require
|
|
|
|
res.sendFile(require.resolve('hydrogen-view-sdk/assets/theme-element-light.css'));
|
|
|
|
})
|
|
|
|
);
|
2022-02-15 16:17:14 -07:00
|
|
|
|
2022-02-17 15:56:54 -07:00
|
|
|
// Our own archive app styles
|
2022-07-14 10:08:50 -06:00
|
|
|
app.get(
|
|
|
|
'/styles.css',
|
|
|
|
asyncHandler(async function (req, res) {
|
|
|
|
res.set('Content-Type', 'text/css');
|
|
|
|
res.sendFile(path.join(__dirname, '../../public/styles/styles.css'));
|
|
|
|
})
|
|
|
|
);
|
2022-02-15 16:17:14 -07:00
|
|
|
|
2022-07-14 10:08:50 -06:00
|
|
|
app.get(
|
|
|
|
'/matrix-public-archive.js',
|
|
|
|
asyncHandler(async function (req, res) {
|
|
|
|
res.set('Content-Type', 'text/css');
|
|
|
|
res.sendFile(path.join(__dirname, '../../dist/matrix-public-archive.es.js'));
|
|
|
|
})
|
|
|
|
);
|
2022-02-15 16:17:14 -07:00
|
|
|
|
|
|
|
app.get(
|
|
|
|
'/:roomIdOrAlias/event/:eventId',
|
|
|
|
asyncHandler(async function (req, res) {
|
2022-02-16 18:58:32 -07:00
|
|
|
// TODO: Fetch event to get `origin_server_ts` and redirect to
|
|
|
|
// /!roomId/2022/01/01?at=$eventId
|
2022-02-15 16:17:14 -07:00
|
|
|
res.send('todo');
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Based off of the Gitter archive routes,
|
|
|
|
// https://gitlab.com/gitterHQ/webapp/-/blob/14954e05c905e8c7cb675efebb89116c07cfaab5/server/handlers/app/archive.js#L190-297
|
|
|
|
app.get(
|
|
|
|
'/:roomIdOrAlias/date/:yyyy(\\d{4})/:mm(\\d{2})/:dd(\\d{2})/:hourRange(\\d\\d?-\\d\\d?)?',
|
2022-07-14 10:08:50 -06:00
|
|
|
timeoutMiddleware,
|
2022-02-15 16:17:14 -07:00
|
|
|
asyncHandler(async function (req, res) {
|
|
|
|
const roomIdOrAlias = req.params.roomIdOrAlias;
|
|
|
|
assert(roomIdOrAlias.startsWith('!') || roomIdOrAlias.startsWith('#'));
|
|
|
|
|
2022-02-24 02:27:53 -07:00
|
|
|
const { fromTimestamp, toTimestamp, hourRange, fromHour, toHour } =
|
|
|
|
parseArchiveRangeFromReq(req);
|
2022-02-15 20:33:31 -07:00
|
|
|
|
2022-02-16 18:58:32 -07:00
|
|
|
// If the hourRange is defined, we force the range to always be 1 hour. If
|
|
|
|
// the format isn't correct, redirect to the correct hour range
|
|
|
|
if (hourRange && toHour !== fromHour + 1) {
|
2022-02-15 20:33:31 -07:00
|
|
|
res.redirect(
|
|
|
|
urlJoin(
|
|
|
|
basePath,
|
|
|
|
roomIdOrAlias,
|
|
|
|
'date',
|
|
|
|
req.params.yyyy,
|
|
|
|
req.params.mm,
|
|
|
|
req.params.dd,
|
|
|
|
`${fromHour}-${fromHour + 1}`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2022-02-15 16:17:14 -07:00
|
|
|
|
2022-02-16 18:58:32 -07:00
|
|
|
// TODO: Highlight tile that matches ?at=$xxx
|
|
|
|
//const aroundId = req.query.at;
|
|
|
|
|
2022-06-29 05:57:06 -06:00
|
|
|
// Do these in parallel to avoid the extra time in sequential round-trips
|
|
|
|
// (we want to display the archive page faster)
|
2022-02-15 16:17:14 -07:00
|
|
|
const [roomData, { events, stateEventMap }] = await Promise.all([
|
2022-02-22 19:25:24 -07:00
|
|
|
fetchRoomData(matrixAccessToken, roomIdOrAlias),
|
2022-02-24 02:27:53 -07:00
|
|
|
fetchEventsInRange(
|
|
|
|
matrixAccessToken,
|
|
|
|
roomIdOrAlias,
|
|
|
|
fromTimestamp,
|
|
|
|
toTimestamp,
|
|
|
|
archiveMessageLimit
|
|
|
|
),
|
2022-02-15 16:17:14 -07:00
|
|
|
]);
|
|
|
|
|
2022-02-24 02:27:53 -07:00
|
|
|
if (events.length >= archiveMessageLimit) {
|
|
|
|
throw new Error('TODO: Redirect user to smaller hour range');
|
|
|
|
}
|
2022-02-23 20:25:05 -07:00
|
|
|
|
2022-02-16 22:08:18 -07:00
|
|
|
const hydrogenHtmlOutput = await renderHydrogenToString({
|
|
|
|
fromTimestamp,
|
|
|
|
roomData,
|
|
|
|
events,
|
|
|
|
stateEventMap,
|
|
|
|
});
|
2022-02-15 16:17:14 -07:00
|
|
|
|
2022-07-14 10:08:50 -06:00
|
|
|
const serializableSpans = getSerializableSpans();
|
|
|
|
const serializedSpans = JSON.stringify(serializableSpans);
|
|
|
|
|
2022-02-15 16:17:14 -07:00
|
|
|
const hydrogenStylesUrl = urlJoin(basePath, 'hydrogen-styles.css');
|
|
|
|
const stylesUrl = urlJoin(basePath, 'styles.css');
|
|
|
|
const jsBundleUrl = urlJoin(basePath, 'matrix-public-archive.js');
|
|
|
|
const pageHtml = `
|
|
|
|
<!doctype html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
2022-07-14 10:08:50 -06:00
|
|
|
${sanitizeHtml(`<title>${roomData.name} - Matrix Public Archive</title>`)}
|
|
|
|
<link href="${hydrogenStylesUrl}" rel="stylesheet">
|
|
|
|
<link href="${stylesUrl}" rel="stylesheet">
|
2022-02-15 16:17:14 -07:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
${hydrogenHtmlOutput}
|
|
|
|
<script type="text/javascript" src="${jsBundleUrl}"></script>
|
2022-07-14 10:08:50 -06:00
|
|
|
<script type="text/javascript">window.tracingSpansForRequest = ${safeJson(
|
|
|
|
serializedSpans
|
|
|
|
)};</script>
|
2022-02-15 16:17:14 -07:00
|
|
|
</body>
|
|
|
|
</html>
|
2022-07-14 10:08:50 -06:00
|
|
|
`;
|
2022-02-15 16:17:14 -07:00
|
|
|
|
|
|
|
res.set('Content-Type', 'text/html');
|
|
|
|
res.send(pageHtml);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = installRoutes;
|