Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d5272e3
Draft: mostly test setup
MadLittleMods Apr 10, 2023
359f95d
Draft: jumping to predecessor/successor
MadLittleMods Apr 11, 2023
536512b
Merge branch 'main' into madlittlemods/59-follow-tombstone-and-predec…
MadLittleMods Apr 11, 2023
7f98efa
Better log when event is missing from expected
MadLittleMods Apr 11, 2023
63fac48
Add note on alternative place to start from
MadLittleMods Apr 12, 2023
d27b6c2
Add comment explaining what we're parsing here
MadLittleMods Apr 12, 2023
0e21449
Crude working to jump backwards to predecessor in first test case
MadLittleMods Apr 13, 2023
14f6e72
Fix m.room.create predecessor format
MadLittleMods Apr 13, 2023
4549cc9
Slight refactor
MadLittleMods Apr 13, 2023
ed66667
Add note about assuming we are already joined
MadLittleMods Apr 13, 2023
60b11dd
Re-usable URL converting utilities
MadLittleMods Apr 13, 2023
bd94901
Add test for same day
MadLittleMods Apr 13, 2023
078fa0a
Change up tactic to display the given day of the predecessor room
MadLittleMods Apr 13, 2023
54d7feb
Bail earlier
MadLittleMods Apr 13, 2023
f0bbc2b
Cleaner fetching
MadLittleMods Apr 13, 2023
b8a80fd
Working predecessor tests
MadLittleMods Apr 13, 2023
17a045a
Fix jumping forward when there is a multiple day gap
MadLittleMods Apr 18, 2023
0834f3f
Fix assertions in jump forward successor test
MadLittleMods Apr 18, 2023
c91a326
Fix predecessor not being picked up
MadLittleMods Apr 18, 2023
4fd584d
Add more unconfirmed tests
MadLittleMods Apr 18, 2023
e9704a6
Revert back gap time so paginate out further as expected
MadLittleMods Apr 18, 2023
decb777
Fix jumping backwards when messages from the same hour
MadLittleMods Apr 19, 2023
4d2a130
Add some tests that fail starting from larger precision
MadLittleMods Apr 19, 2023
4a2bbcb
Try `ts` and doTimestampsStartFromSameUtcX(ts, tsForClosestEvent)
MadLittleMods Apr 19, 2023
43bdc7d
Passing tests and maybe good logic
MadLittleMods Apr 19, 2023
a969b4d
Simplify and tests still pass
MadLittleMods Apr 19, 2023
198a4b4
Add missing tests (all tests passing)
MadLittleMods Apr 19, 2023
d00cbfe
Remove `doTimestampsShareRoundedUpUtcX` utilities and rename `doTimes…
MadLittleMods Apr 19, 2023
1d89487
Remove debug logging
MadLittleMods Apr 19, 2023
81bc77a
Prefer tracking just by searching MSC3999 everywhere
MadLittleMods Apr 19, 2023
9389bb2
Add comment thread reference for future reading
MadLittleMods Apr 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions server/lib/matrix-utils/ensure-room-joined.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,29 @@ const assert = require('assert');
const urlJoin = require('url-join');

const { fetchEndpointAsJson } = require('../fetch-endpoint');
const getServerNameFromMatrixRoomIdOrAlias = require('./get-server-name-from-matrix-room-id-or-alias');

const config = require('../config');
const StatusError = require('../status-error');
const matrixServerUrl = config.get('matrixServerUrl');
assert(matrixServerUrl);

async function ensureRoomJoined(accessToken, roomIdOrAlias, viaServers = []) {
async function ensureRoomJoined(accessToken, roomIdOrAlias, viaServers = new Set()) {
// We use a `Set` to ensure that we don't have duplicate servers in the list
assert(viaServers instanceof Set);

// Let's do our best for the user to join the room. Since room ID's are
// unroutable on their own and won't be found if the server doesn't already
// know about the room, we'll try to join the room via the server name that
// we derived from the room ID or alias.
const viaServersWithAssumptions = new Set(viaServers);
const derivedServerName = getServerNameFromMatrixRoomIdOrAlias(roomIdOrAlias);
if (derivedServerName) {
viaServersWithAssumptions.add(derivedServerName);
}

let qs = new URLSearchParams();
[].concat(viaServers).forEach((viaServer) => {
Array.from(viaServersWithAssumptions).forEach((viaServer) => {
qs.append('server_name', viaServer);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const assert = require('assert');

function getServerNameFromMatrixRoomIdOrAlias(roomIdOrAlias) {
assert(roomIdOrAlias);

const pieces = roomIdOrAlias.split(':');
// We can only derive the server name if there is a colon in the string. Since room
// IDs are supposed to be treated as opaque strings, there is a future possibility
// that they will not contain a colon.
if (pieces.length < 2) {
return null;
}

const servername = pieces.slice(1).join(':');

return servername;
}

module.exports = getServerNameFromMatrixRoomIdOrAlias;
49 changes: 41 additions & 8 deletions server/routes/room-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const {
MS_LOOKUP,
TIME_PRECISION_VALUES,
DIRECTION,
VALID_ENTITY_DESCRIPTOR_TO_SIGIL_MAP,
} = require('matrix-public-archive-shared/lib/reference-values');
const { ONE_DAY_IN_MS, ONE_HOUR_IN_MS, ONE_MINUTE_IN_MS, ONE_SECOND_IN_MS } = MS_LOOKUP;
const {
Expand Down Expand Up @@ -56,10 +57,6 @@ const router = express.Router({
mergeParams: true,
});

const VALID_ENTITY_DESCRIPTOR_TO_SIGIL_MAP = {
r: '#',
roomid: '!',
};
const validSigilList = Object.values(VALID_ENTITY_DESCRIPTOR_TO_SIGIL_MAP);
const sigilRe = new RegExp(`^(${validSigilList.join('|')})`);

Expand Down Expand Up @@ -159,6 +156,29 @@ function parseArchiveRangeFromReq(req) {
};
}

function parseViaServersFromReq(req) {
const rawViaServers = [].concat(req.query.via || []);
if (rawViaServers.length === 0) {
return new Set();
}

const viaServerList = rawViaServers.map((viaServer) => {
// Sanity check to ensure that the via servers are strings (valid enough looking
// host names)
if (typeof viaServer !== 'string') {
throw new StatusError(
400,
`?via server must be a string, got ${viaServer} (${typeof viaServer})`
);
}

return viaServer;
});

// We use a `Set` to ensure that we don't have duplicate servers in the list
return new Set(viaServerList);
}

router.use(redirectToCorrectArchiveUrlIfBadSigil);

router.get(
Expand All @@ -173,7 +193,11 @@ router.get(

// We have to wait for the room join to happen first before we can fetch
// any of the additional room info or messages.
const roomId = await ensureRoomJoined(matrixAccessToken, roomIdOrAlias, req.query.via);
const roomId = await ensureRoomJoined(
matrixAccessToken,
roomIdOrAlias,
parseViaServersFromReq(req)
);

// Find the closest day to the current time with messages
const { originServerTs } = await timestampToEvent({
Expand All @@ -192,7 +216,7 @@ router.get(
// We can avoid passing along the `via` query parameter because we already
// joined the room above (see `ensureRoomJoined`).
//
//viaServers: req.query.via,
//viaServers: parseViaServersFromReq(req),
})
);
})
Expand Down Expand Up @@ -245,7 +269,11 @@ router.get(

// We have to wait for the room join to happen first before we can use the jump to
// date endpoint
const roomId = await ensureRoomJoined(matrixAccessToken, roomIdOrAlias, req.query.via);
const roomId = await ensureRoomJoined(
matrixAccessToken,
roomIdOrAlias,
parseViaServersFromReq(req)
);

let eventIdForClosestEvent;
let tsForClosestEvent;
Expand Down Expand Up @@ -536,7 +564,11 @@ router.get(

// We have to wait for the room join to happen first before we can fetch
// any of the additional room info or messages.
const roomId = await ensureRoomJoined(matrixAccessToken, roomIdOrAlias, req.query.via);
const roomId = await ensureRoomJoined(
matrixAccessToken,
roomIdOrAlias,
parseViaServersFromReq(req)
);

// Do these in parallel to avoid the extra time in sequential round-trips
// (we want to display the archive page faster)
Expand Down Expand Up @@ -587,6 +619,7 @@ router.get(
const stylesUrl = urlJoin(basePath, '/css/styles.css');
const jsBundleUrl = urlJoin(basePath, '/js/entry-client-hydrogen.es.js');

// XXX: The `renderHydrogenVmRenderScriptToPageHtml` API surface is pretty awkward
const pageHtml = await renderHydrogenVmRenderScriptToPageHtml(
path.resolve(__dirname, '../../shared/hydrogen-vm-render-script.js'),
{
Expand Down
6 changes: 6 additions & 0 deletions shared/lib/reference-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ const VALID_SIGIL_TO_ENTITY_DESCRIPTOR_MAP = {
'!': 'roomid',
};

const VALID_ENTITY_DESCRIPTOR_TO_SIGIL_MAP = {
r: '#',
roomid: '!',
};

module.exports = {
MS_LOOKUP,
TIME_PRECISION_VALUES,
DIRECTION,
VALID_SIGIL_TO_ENTITY_DESCRIPTOR_MAP,
VALID_ENTITY_DESCRIPTOR_TO_SIGIL_MAP,
};
1 change: 1 addition & 0 deletions shared/lib/timestamp-utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function roundUpTimestampToUtcSecond(ts) {
return dateRountedUp.getTime();
}

// XXX: Should these just be renamed to `roundDownTimestampToUtcDay`?
function getUtcStartOfDayTs(ts) {
assert(typeof ts === 'number' || ts instanceof Date);
const date = new Date(ts);
Expand Down
Loading