element-web/src/utils/exportUtils/Exporter.ts

94 lines
3 KiB
TypeScript
Raw Normal View History

2021-05-24 15:18:13 +00:00
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
2021-05-24 15:50:16 +00:00
import { Room } from "matrix-js-sdk/src/models/room";
2021-06-03 07:51:56 +00:00
import { MatrixClientPeg } from "../../MatrixClientPeg";
import { TimelineWindow } from "matrix-js-sdk/src/timeline-window";
import { arrayFastClone } from "../arrays";
2021-06-04 09:38:17 +00:00
import { exportTypes } from "./exportUtils";
2021-05-24 15:18:13 +00:00
2021-06-03 08:09:14 +00:00
export default abstract class Exporter {
protected constructor(protected room: Room, protected exportType: exportTypes, protected numberOfEvents?: number) {}
2021-06-03 07:51:56 +00:00
2021-06-03 08:09:14 +00:00
protected getTimelineConversation = () : MatrixEvent[] => {
2021-06-03 07:51:56 +00:00
if (!this.room) return;
const cli = MatrixClientPeg.get();
const timelineSet = this.room.getUnfilteredTimelineSet();
const timelineWindow = new TimelineWindow(
cli, timelineSet,
{windowLimit: Number.MAX_VALUE});
timelineWindow.load(null, 30);
2021-06-04 09:38:17 +00:00
const events: MatrixEvent[] = timelineWindow.getEvents();
2021-06-03 07:51:56 +00:00
// Clone and reverse the events so that we preserve the order
arrayFastClone(events)
.reverse()
.forEach(event => {
cli.decryptEventIfNeeded(event);
});
return events;
};
protected setEventMetadata = (event: MatrixEvent) => {
const client = MatrixClientPeg.get();
const roomState = client.getRoom(this.room.roomId).currentState;
event.sender = roomState.getSentinelMember(
event.getSender(),
);
if (event.getType() === "m.room.member") {
event.target = roomState.getSentinelMember(
event.getStateKey(),
);
2021-06-04 09:38:17 +00:00
}
return event;
2021-06-04 09:38:17 +00:00
}
protected getRequiredEvents = async () : Promise<MatrixEvent[]> => {
const client = MatrixClientPeg.get();
const eventMapper = client.getEventMapper();
2021-06-04 09:38:17 +00:00
let prevToken: string|null = null;
let limit = this.numberOfEvents || Number.MAX_VALUE;
let events: MatrixEvent[] = [];
2021-06-04 09:38:17 +00:00
while (limit) {
const eventsPerCrawl = Math.min(limit, 100);
const res: any = await client.createMessagesRequest(this.room.roomId, prevToken, eventsPerCrawl, "b");
2021-06-04 09:38:17 +00:00
if (res.chunk.length === 0) break;
limit -= eventsPerCrawl;
const matrixEvents: MatrixEvent[] = res.chunk.map(eventMapper);
matrixEvents.forEach(mxEv => events.push(mxEv));
prevToken = res.end;
}
2021-06-07 09:17:27 +00:00
//Reverse the events so that we preserve the order
events = events.reverse();
2021-06-04 09:38:17 +00:00
const decryptionPromises = events
.filter(event => event.isEncrypted())
.map(event => {
return client.decryptEventIfNeeded(event, {
isRetry: true,
emit: false,
2021-06-04 09:38:17 +00:00
});
});
//Wait for all the events to get decrypted.
2021-06-04 09:38:17 +00:00
await Promise.all(decryptionPromises);
2021-06-07 09:17:27 +00:00
for (let i = 0; i < events.length; i++) this.setEventMetadata(events[i]);
2021-06-04 09:38:17 +00:00
return events;
2021-06-04 09:38:17 +00:00
}
2021-05-24 15:50:16 +00:00
abstract export(): Promise<Blob>;
2021-05-24 15:18:13 +00:00
}