2022-09-08 01:18:18 -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);
|
|
|
|
|
2023-05-01 23:39:01 -06:00
|
|
|
async function timestampToEvent({ accessToken, roomId, ts, direction, abortSignal }) {
|
2022-09-08 01:18:18 -06:00
|
|
|
assert(accessToken);
|
|
|
|
assert(roomId);
|
|
|
|
assert(ts);
|
|
|
|
assert(direction);
|
2023-05-01 23:39:01 -06:00
|
|
|
// TODO: Handle `fromCausalEventId` -> `org.matrix.msc3999.event_id`: See MSC3999
|
|
|
|
// (https://github.com/matrix-org/matrix-spec-proposals/pull/3999)
|
2022-09-08 01:18:18 -06:00
|
|
|
|
|
|
|
const timestampToEventEndpoint = urlJoin(
|
|
|
|
matrixServerUrl,
|
2023-02-16 19:52:28 -07:00
|
|
|
`_matrix/client/v1/rooms/${encodeURIComponent(
|
2022-10-27 00:09:13 -06:00
|
|
|
roomId
|
|
|
|
)}/timestamp_to_event?ts=${encodeURIComponent(ts)}&dir=${encodeURIComponent(direction)}`
|
2022-09-08 01:18:18 -06:00
|
|
|
);
|
2022-11-03 03:12:00 -06:00
|
|
|
const { data: timestampToEventResData } = await fetchEndpointAsJson(timestampToEventEndpoint, {
|
2022-09-08 01:18:18 -06:00
|
|
|
accessToken,
|
2023-05-01 23:39:01 -06:00
|
|
|
abortSignal,
|
2022-09-08 01:18:18 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
eventId: timestampToEventResData.event_id,
|
|
|
|
originServerTs: timestampToEventResData.origin_server_ts,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = traceFunction(timestampToEvent);
|