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');
|
|
|
|
|
|
|
|
const fetchRoomData = require('../fetch-room-data');
|
|
|
|
const fetchEventsForTimestamp = require('../fetch-events-for-timestamp');
|
|
|
|
const renderHydrogenToString = require('../render-hydrogen-to-string');
|
|
|
|
|
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-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-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
|
|
|
|
app.use(express.static(path.dirname(require.resolve('hydrogen-view-sdk/assets/'))));
|
|
|
|
|
2022-02-15 16:17:14 -07:00
|
|
|
app.get('/hydrogen-styles.css', async function (req, res) {
|
|
|
|
res.set('Content-Type', 'text/css');
|
|
|
|
res.sendFile(require.resolve('hydrogen-view-sdk/style.css'));
|
|
|
|
});
|
|
|
|
|
2022-02-17 15:56:54 -07:00
|
|
|
// Our own archive app styles
|
2022-02-15 16:17:14 -07:00
|
|
|
app.get('/styles.css', async function (req, res) {
|
|
|
|
res.set('Content-Type', 'text/css');
|
2022-02-16 13:13:30 -07:00
|
|
|
res.sendFile(path.join(__dirname, '../../public/styles/styles.css'));
|
2022-02-15 16:17:14 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/matrix-public-archive.js', async function (req, res) {
|
|
|
|
res.set('Content-Type', 'text/css');
|
2022-02-16 13:13:30 -07:00
|
|
|
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?)?',
|
|
|
|
asyncHandler(async function (req, res) {
|
|
|
|
const roomIdOrAlias = req.params.roomIdOrAlias;
|
|
|
|
assert(roomIdOrAlias.startsWith('!') || roomIdOrAlias.startsWith('#'));
|
|
|
|
|
2022-02-16 18:58:32 -07:00
|
|
|
const { fromTimestamp, 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-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),
|
|
|
|
fetchEventsForTimestamp(matrixAccessToken, roomIdOrAlias, fromTimestamp),
|
2022-02-15 16:17:14 -07:00
|
|
|
]);
|
|
|
|
|
2022-02-23 20:25:05 -07:00
|
|
|
console.log('events', JSON.stringify(events, null, 2));
|
|
|
|
|
2022-02-16 22:08:18 -07:00
|
|
|
const hydrogenHtmlOutput = await renderHydrogenToString({
|
|
|
|
fromTimestamp,
|
|
|
|
roomData,
|
|
|
|
events,
|
|
|
|
stateEventMap,
|
|
|
|
});
|
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>
|
|
|
|
<link href="${hydrogenStylesUrl}" rel="stylesheet">
|
|
|
|
<link href="${stylesUrl}" rel="stylesheet">
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
${hydrogenHtmlOutput}
|
|
|
|
<script type="text/javascript" src="${jsBundleUrl}"></script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`;
|
|
|
|
|
|
|
|
res.set('Content-Type', 'text/html');
|
|
|
|
res.send(pageHtml);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = installRoutes;
|