Implement deep-linking for threads (matrix.to) (#7003)

This commit is contained in:
Germain 2021-10-22 09:30:36 +01:00 committed by GitHub
parent bc32f05fcb
commit e20ac7bf1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 122 additions and 12 deletions

View file

@ -21,6 +21,9 @@ import shouldHideEvent from "../shouldHideEvent";
import { getHandlerTile, haveTileForEvent } from "../components/views/rooms/EventTile";
import SettingsStore from "../settings/SettingsStore";
import { EventType } from "matrix-js-sdk/src/@types/event";
import { MatrixClient } from 'matrix-js-sdk/src/client';
import { Thread } from 'matrix-js-sdk/src/models/thread';
import { logger } from 'matrix-js-sdk/src/logger';
/**
* Returns whether an event should allow actions like reply, reactions, edit, etc.
@ -158,3 +161,37 @@ export function isVoiceMessage(mxEvent: MatrixEvent): boolean {
!!content['org.matrix.msc3245.voice']
);
}
export async function fetchInitialEvent(
client: MatrixClient,
roomId: string,
eventId: string): Promise<MatrixEvent | null> {
let initialEvent: MatrixEvent;
try {
const eventData = await client.fetchRoomEvent(roomId, eventId);
initialEvent = new MatrixEvent(eventData);
} catch (e) {
logger.warn("Could not find initial event: " + initialEvent.threadRootId);
initialEvent = null;
}
if (initialEvent?.isThreadRelation) {
try {
const rootEventData = await client.fetchRoomEvent(roomId, initialEvent.threadRootId);
const rootEvent = new MatrixEvent(rootEventData);
const room = client.getRoom(roomId);
const thread = new Thread(
[rootEvent],
room,
client,
);
thread.addEvent(initialEvent);
room.threads.set(thread.id, thread);
} catch (e) {
logger.warn("Could not find root event: " + initialEvent.threadRootId);
}
}
return initialEvent;
}