2022-02-15 20:33:31 -07:00
|
|
|
'use strict';
|
|
|
|
|
2022-02-10 00:42:02 -07:00
|
|
|
const assert = require('assert');
|
|
|
|
|
2022-02-16 18:58:32 -07:00
|
|
|
const urlJoin = require('url-join');
|
2022-09-08 00:30:04 -06:00
|
|
|
const { fetchEndpointAsJson } = require('../fetch-endpoint');
|
2023-04-19 00:26:15 -06:00
|
|
|
const parseViaServersFromUserInput = require('../parse-via-servers-from-user-input');
|
2022-09-08 00:30:04 -06:00
|
|
|
const { traceFunction } = require('../../tracing/trace-utilities');
|
2022-02-10 00:42:02 -07:00
|
|
|
|
2022-09-08 00:30:04 -06:00
|
|
|
const config = require('../config');
|
2022-02-23 20:25:05 -07:00
|
|
|
const matrixServerUrl = config.get('matrixServerUrl');
|
2022-02-10 00:42:02 -07:00
|
|
|
assert(matrixServerUrl);
|
|
|
|
|
2023-04-19 00:26:15 -06:00
|
|
|
function getStateEndpointForRoomIdAndEventType(roomId, eventType) {
|
|
|
|
return urlJoin(
|
2022-02-10 00:42:02 -07:00
|
|
|
matrixServerUrl,
|
2023-04-19 00:26:15 -06:00
|
|
|
`_matrix/client/r0/rooms/${encodeURIComponent(roomId)}/state/${encodeURIComponent(
|
|
|
|
eventType
|
|
|
|
)}?format=event`
|
2022-02-10 00:42:02 -07:00
|
|
|
);
|
2023-04-19 00:26:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unfortunately, we can't just get the event ID from the `/state?format=event`
|
|
|
|
// endpoint, so we have to do this trick. Related to
|
|
|
|
// https://github.com/matrix-org/synapse/issues/15454
|
|
|
|
//
|
|
|
|
// TODO: Remove this when we have MSC3999 (because it's the only usage)
|
2023-05-01 23:39:01 -06:00
|
|
|
const removeMe_fetchRoomCreateEventId = traceFunction(async function (
|
|
|
|
matrixAccessToken,
|
|
|
|
roomId,
|
|
|
|
{ abortSignal } = {}
|
|
|
|
) {
|
2023-04-19 00:26:15 -06:00
|
|
|
const { data } = await fetchEndpointAsJson(
|
|
|
|
urlJoin(
|
|
|
|
matrixServerUrl,
|
|
|
|
`_matrix/client/r0/rooms/${encodeURIComponent(roomId)}/messages?dir=f&limit1`
|
|
|
|
),
|
|
|
|
{
|
|
|
|
accessToken: matrixAccessToken,
|
2023-05-01 23:39:01 -06:00
|
|
|
abortSignal,
|
2023-04-19 00:26:15 -06:00
|
|
|
}
|
2022-09-08 18:15:07 -06:00
|
|
|
);
|
2022-02-10 00:42:02 -07:00
|
|
|
|
2023-04-19 00:26:15 -06:00
|
|
|
const roomCreateEventId = data?.chunk?.[0]?.event_id;
|
|
|
|
|
|
|
|
return roomCreateEventId;
|
|
|
|
});
|
|
|
|
|
2023-05-01 23:39:01 -06:00
|
|
|
const fetchRoomCreationInfo = traceFunction(async function (
|
|
|
|
matrixAccessToken,
|
|
|
|
roomId,
|
|
|
|
{ abortSignal } = {}
|
|
|
|
) {
|
2023-04-19 00:26:15 -06:00
|
|
|
const [stateCreateResDataOutcome] = await Promise.allSettled([
|
|
|
|
fetchEndpointAsJson(getStateEndpointForRoomIdAndEventType(roomId, 'm.room.create'), {
|
|
|
|
accessToken: matrixAccessToken,
|
2023-05-01 23:39:01 -06:00
|
|
|
abortSignal,
|
2023-04-19 00:26:15 -06:00
|
|
|
}),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let roomCreationTs;
|
|
|
|
let predecessorRoomId;
|
|
|
|
let predecessorLastKnownEventId;
|
|
|
|
if (stateCreateResDataOutcome.reason === undefined) {
|
|
|
|
const { data } = stateCreateResDataOutcome.value;
|
|
|
|
roomCreationTs = data?.origin_server_ts;
|
|
|
|
predecessorLastKnownEventId = data?.content?.event_id;
|
|
|
|
predecessorRoomId = data?.content?.predecessor?.room_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { roomCreationTs, predecessorRoomId, predecessorLastKnownEventId };
|
|
|
|
});
|
|
|
|
|
2023-05-01 23:39:01 -06:00
|
|
|
const fetchPredecessorInfo = traceFunction(async function (
|
|
|
|
matrixAccessToken,
|
|
|
|
roomId,
|
|
|
|
{ abortSignal } = {}
|
|
|
|
) {
|
2023-04-19 00:26:15 -06:00
|
|
|
const [roomCreationInfoOutcome, statePredecessorResDataOutcome] = await Promise.allSettled([
|
2023-05-01 23:39:01 -06:00
|
|
|
fetchRoomCreationInfo(matrixAccessToken, roomId, { abortSignal }),
|
2023-04-19 00:26:15 -06:00
|
|
|
fetchEndpointAsJson(
|
|
|
|
getStateEndpointForRoomIdAndEventType(roomId, 'org.matrix.msc3946.room_predecessor'),
|
|
|
|
{
|
|
|
|
accessToken: matrixAccessToken,
|
2023-05-01 23:39:01 -06:00
|
|
|
abortSignal,
|
2023-04-19 00:26:15 -06:00
|
|
|
}
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let predecessorRoomId;
|
|
|
|
let predecessorLastKnownEventId;
|
|
|
|
let predecessorViaServers;
|
|
|
|
// Prefer the dynamic predecessor from the dedicated state event
|
|
|
|
if (statePredecessorResDataOutcome.reason === undefined) {
|
|
|
|
const { data } = statePredecessorResDataOutcome.value;
|
|
|
|
predecessorRoomId = data?.content?.predecessor_room_id;
|
|
|
|
predecessorLastKnownEventId = data?.content?.last_known_event_id;
|
|
|
|
predecessorViaServers = parseViaServersFromUserInput(data?.content?.via_servers);
|
|
|
|
}
|
|
|
|
// Then fallback to the predecessor defined by the room creation event
|
|
|
|
else if (roomCreationInfoOutcome.reason === undefined) {
|
|
|
|
({ predecessorRoomId, predecessorLastKnownEventId } = roomCreationInfoOutcome.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { roomCreationTs: currentRoomCreationTs } = roomCreationInfoOutcome;
|
|
|
|
|
|
|
|
return {
|
|
|
|
// This is prefixed with "current" so we don't get this confused with the
|
|
|
|
// predecessor room creation timestamp.
|
|
|
|
currentRoomCreationTs,
|
|
|
|
predecessorRoomId,
|
|
|
|
predecessorLastKnownEventId,
|
|
|
|
predecessorViaServers,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2023-05-01 23:39:01 -06:00
|
|
|
const fetchSuccessorInfo = traceFunction(async function (
|
|
|
|
matrixAccessToken,
|
|
|
|
roomId,
|
|
|
|
{ abortSignal } = {}
|
|
|
|
) {
|
2023-04-19 00:26:15 -06:00
|
|
|
const [stateTombstoneResDataOutcome] = await Promise.allSettled([
|
|
|
|
fetchEndpointAsJson(getStateEndpointForRoomIdAndEventType(roomId, 'm.room.tombstone'), {
|
|
|
|
accessToken: matrixAccessToken,
|
2023-05-01 23:39:01 -06:00
|
|
|
abortSignal,
|
2023-04-19 00:26:15 -06:00
|
|
|
}),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let successorRoomId;
|
|
|
|
let successorSetTs;
|
|
|
|
if (stateTombstoneResDataOutcome.reason === undefined) {
|
|
|
|
const { data } = stateTombstoneResDataOutcome.value;
|
|
|
|
successorRoomId = data?.content?.replacement_room;
|
|
|
|
successorSetTs = data?.origin_server_ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
successorRoomId,
|
|
|
|
successorSetTs,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
// eslint-disable-next-line max-statements
|
2023-05-01 23:39:01 -06:00
|
|
|
const fetchRoomData = traceFunction(async function (
|
|
|
|
matrixAccessToken,
|
|
|
|
roomId,
|
|
|
|
{ abortSignal } = {}
|
|
|
|
) {
|
2023-04-19 00:26:15 -06:00
|
|
|
assert(matrixAccessToken);
|
|
|
|
assert(roomId);
|
|
|
|
|
2022-09-08 18:15:07 -06:00
|
|
|
const [
|
|
|
|
stateNameResDataOutcome,
|
2023-05-05 14:36:26 -06:00
|
|
|
stateTopicResDataOutcome,
|
2022-10-27 00:09:13 -06:00
|
|
|
stateCanonicalAliasResDataOutcome,
|
2022-09-08 18:15:07 -06:00
|
|
|
stateAvatarResDataOutcome,
|
|
|
|
stateHistoryVisibilityResDataOutcome,
|
|
|
|
stateJoinRulesResDataOutcome,
|
2023-04-19 00:26:15 -06:00
|
|
|
predecessorInfoOutcome,
|
|
|
|
successorInfoOutcome,
|
2022-09-08 18:15:07 -06:00
|
|
|
] = await Promise.allSettled([
|
2023-04-19 00:26:15 -06:00
|
|
|
fetchEndpointAsJson(getStateEndpointForRoomIdAndEventType(roomId, 'm.room.name'), {
|
|
|
|
accessToken: matrixAccessToken,
|
2023-05-01 23:39:01 -06:00
|
|
|
abortSignal,
|
2022-02-22 19:25:24 -07:00
|
|
|
}),
|
2023-05-05 14:36:26 -06:00
|
|
|
fetchEndpointAsJson(getStateEndpointForRoomIdAndEventType(roomId, 'm.room.topic'), {
|
|
|
|
accessToken: matrixAccessToken,
|
|
|
|
abortSignal,
|
|
|
|
}),
|
2023-04-19 00:26:15 -06:00
|
|
|
fetchEndpointAsJson(getStateEndpointForRoomIdAndEventType(roomId, 'm.room.canonical_alias'), {
|
|
|
|
accessToken: matrixAccessToken,
|
2023-05-01 23:39:01 -06:00
|
|
|
abortSignal,
|
2022-10-27 00:09:13 -06:00
|
|
|
}),
|
2023-04-19 00:26:15 -06:00
|
|
|
fetchEndpointAsJson(getStateEndpointForRoomIdAndEventType(roomId, 'm.room.avatar'), {
|
|
|
|
accessToken: matrixAccessToken,
|
2023-05-01 23:39:01 -06:00
|
|
|
abortSignal,
|
2022-02-22 19:25:24 -07:00
|
|
|
}),
|
2023-04-19 00:26:15 -06:00
|
|
|
fetchEndpointAsJson(
|
|
|
|
getStateEndpointForRoomIdAndEventType(roomId, 'm.room.history_visibility'),
|
|
|
|
{
|
|
|
|
accessToken: matrixAccessToken,
|
2023-05-01 23:39:01 -06:00
|
|
|
abortSignal,
|
2023-04-19 00:26:15 -06:00
|
|
|
}
|
|
|
|
),
|
|
|
|
fetchEndpointAsJson(getStateEndpointForRoomIdAndEventType(roomId, 'm.room.join_rules'), {
|
|
|
|
accessToken: matrixAccessToken,
|
2023-05-01 23:39:01 -06:00
|
|
|
abortSignal,
|
2022-09-08 18:15:07 -06:00
|
|
|
}),
|
2023-05-01 23:39:01 -06:00
|
|
|
fetchPredecessorInfo(matrixAccessToken, roomId, { abortSignal }),
|
|
|
|
fetchSuccessorInfo(matrixAccessToken, roomId, { abortSignal }),
|
2022-02-10 00:42:02 -07:00
|
|
|
]);
|
|
|
|
|
|
|
|
let name;
|
|
|
|
if (stateNameResDataOutcome.reason === undefined) {
|
2022-11-03 03:12:00 -06:00
|
|
|
const { data } = stateNameResDataOutcome.value;
|
2023-04-19 00:26:15 -06:00
|
|
|
name = data?.content?.name;
|
2022-02-10 00:42:02 -07:00
|
|
|
}
|
|
|
|
|
2022-10-27 00:09:13 -06:00
|
|
|
let canonicalAlias;
|
|
|
|
if (stateCanonicalAliasResDataOutcome.reason === undefined) {
|
2022-11-03 03:12:00 -06:00
|
|
|
const { data } = stateCanonicalAliasResDataOutcome.value;
|
2023-04-19 00:26:15 -06:00
|
|
|
canonicalAlias = data?.content?.alias;
|
2022-10-27 00:09:13 -06:00
|
|
|
}
|
|
|
|
|
2023-05-05 14:36:26 -06:00
|
|
|
let topic;
|
|
|
|
if (stateTopicResDataOutcome.reason === undefined) {
|
|
|
|
const { data } = stateTopicResDataOutcome.value;
|
|
|
|
topic = data?.content?.topic;
|
|
|
|
}
|
|
|
|
|
2022-02-10 00:42:02 -07:00
|
|
|
let avatarUrl;
|
|
|
|
if (stateAvatarResDataOutcome.reason === undefined) {
|
2022-11-03 03:12:00 -06:00
|
|
|
const { data } = stateAvatarResDataOutcome.value;
|
2023-04-19 00:26:15 -06:00
|
|
|
avatarUrl = data?.content?.url;
|
2022-02-10 00:42:02 -07:00
|
|
|
}
|
|
|
|
|
2022-09-08 18:15:07 -06:00
|
|
|
let historyVisibility;
|
|
|
|
if (stateHistoryVisibilityResDataOutcome.reason === undefined) {
|
2022-11-03 03:12:00 -06:00
|
|
|
const { data } = stateHistoryVisibilityResDataOutcome.value;
|
2023-04-19 00:26:15 -06:00
|
|
|
historyVisibility = data?.content?.history_visibility;
|
2022-09-08 18:15:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let joinRule;
|
|
|
|
if (stateJoinRulesResDataOutcome.reason === undefined) {
|
2022-11-03 03:12:00 -06:00
|
|
|
const { data } = stateJoinRulesResDataOutcome.value;
|
2023-04-19 00:26:15 -06:00
|
|
|
joinRule = data?.content?.join_rule;
|
|
|
|
}
|
|
|
|
|
|
|
|
let roomCreationTs;
|
|
|
|
let predecessorRoomId;
|
|
|
|
let predecessorLastKnownEventId;
|
|
|
|
let predecessorViaServers;
|
|
|
|
if (predecessorInfoOutcome.reason === undefined) {
|
|
|
|
({
|
|
|
|
currentRoomCreationTs: roomCreationTs,
|
|
|
|
predecessorRoomId,
|
|
|
|
predecessorLastKnownEventId,
|
|
|
|
predecessorViaServers,
|
|
|
|
} = predecessorInfoOutcome.value);
|
|
|
|
}
|
|
|
|
let successorRoomId;
|
|
|
|
let successorSetTs;
|
|
|
|
if (successorInfoOutcome.reason === undefined) {
|
|
|
|
({ successorRoomId, successorSetTs } = successorInfoOutcome.value);
|
2022-09-08 18:15:07 -06:00
|
|
|
}
|
|
|
|
|
2022-02-10 00:42:02 -07:00
|
|
|
return {
|
|
|
|
id: roomId,
|
|
|
|
name,
|
2023-05-05 14:36:26 -06:00
|
|
|
topic,
|
2022-10-27 00:09:13 -06:00
|
|
|
canonicalAlias,
|
2022-02-10 00:42:02 -07:00
|
|
|
avatarUrl,
|
2022-09-08 18:15:07 -06:00
|
|
|
historyVisibility,
|
|
|
|
joinRule,
|
2023-04-19 00:26:15 -06:00
|
|
|
roomCreationTs,
|
|
|
|
predecessorRoomId,
|
|
|
|
predecessorLastKnownEventId,
|
|
|
|
predecessorViaServers,
|
|
|
|
successorRoomId,
|
|
|
|
successorSetTs,
|
2022-02-10 00:42:02 -07:00
|
|
|
};
|
2023-04-19 00:26:15 -06:00
|
|
|
});
|
2022-02-10 00:42:02 -07:00
|
|
|
|
2023-04-19 00:26:15 -06:00
|
|
|
module.exports = {
|
|
|
|
fetchRoomData,
|
|
|
|
fetchRoomCreationInfo,
|
|
|
|
fetchPredecessorInfo,
|
|
|
|
fetchSuccessorInfo,
|
|
|
|
removeMe_fetchRoomCreateEventId,
|
|
|
|
};
|