Files
ThreadNet-Web/src/utils/exportUtils/JSONExport.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

123 lines
4.5 KiB
TypeScript
Raw Normal View History

2021-07-26 23:40:27 +05:30
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
Copyright 2021, 2022 The Matrix.org Foundation C.I.C.
2021-07-26 23:40:27 +05:30
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2021-07-26 23:40:27 +05:30
*/
2025-02-05 13:25:06 +00:00
import { type Room, type IEvent, type MatrixEvent, EventType } from "matrix-js-sdk/src/matrix";
2021-10-22 17:23:32 -05:00
import { logger } from "matrix-js-sdk/src/logger";
import Exporter from "./Exporter";
import { formatFullDateNoDayNoTime } from "../../DateUtils";
2025-02-05 13:25:06 +00:00
import { type ExportType, type IExportOptions } from "./exportUtils";
import { _t } from "../../languageHandler";
import { haveRendererForEvent } from "../../events/EventTileFactory";
2021-10-15 16:26:54 +02:00
2021-06-24 18:19:12 +05:30
export default class JSONExporter extends Exporter {
2021-08-13 08:30:50 +05:30
protected totalSize = 0;
2021-08-14 11:50:13 +05:30
protected messages: Record<string, any>[] = [];
2021-06-24 18:19:12 +05:30
public constructor(
2021-07-02 10:23:25 +05:30
room: Room,
2021-08-13 08:30:50 +05:30
exportType: ExportType,
2021-07-26 23:40:27 +05:30
exportOptions: IExportOptions,
2021-08-14 00:03:02 +05:30
setProgressText: React.Dispatch<React.SetStateAction<string>>,
2021-07-02 10:23:25 +05:30
) {
2021-08-14 00:03:02 +05:30
super(room, exportType, exportOptions, setProgressText);
2021-06-24 18:19:12 +05:30
}
public get destinationFileName(): string {
return this.makeFileNameNoExtension() + ".json";
}
2021-06-29 10:57:02 +05:30
protected createJSONString(): string {
2021-06-24 18:19:12 +05:30
const exportDate = formatFullDateNoDayNoTime(new Date());
const creator = this.room.currentState.getStateEvents(EventType.RoomCreate, "")?.getSender();
const creatorName = (creator && this.room?.getMember(creator)?.rawDisplayName) || creator;
2021-06-24 18:19:12 +05:30
const topic = this.room.currentState.getStateEvents(EventType.RoomTopic, "")?.getContent()?.topic || "";
const exporter = this.room.client.getUserId()!;
2021-06-24 18:19:12 +05:30
const exporterName = this.room?.getMember(exporter)?.rawDisplayName || exporter;
2021-06-29 10:57:02 +05:30
const jsonObject = {
room_name: this.room.name,
room_creator: creatorName,
topic,
export_date: exportDate,
exported_by: exporterName,
messages: this.messages,
2021-06-30 14:08:22 +05:30
};
2021-06-29 10:57:02 +05:30
return JSON.stringify(jsonObject, null, 2);
2021-06-24 18:19:12 +05:30
}
protected async getJSONString(mxEv: MatrixEvent): Promise<IEvent> {
2021-06-24 18:19:12 +05:30
if (this.exportOptions.attachmentsIncluded && this.isAttachment(mxEv)) {
try {
const blob = await this.getMediaBlob(mxEv);
if (this.totalSize + blob.size < this.exportOptions.maxSize) {
this.totalSize += blob.size;
const filePath = this.getFilePath(mxEv);
if (this.totalSize == this.exportOptions.maxSize) {
this.exportOptions.attachmentsIncluded = false;
}
this.addFile(filePath, blob);
2021-06-24 18:19:12 +05:30
}
} catch (err) {
2021-10-15 16:26:54 +02:00
logger.log("Error fetching file: " + err);
2021-06-24 18:19:12 +05:30
}
}
return mxEv.getEffectiveEvent();
2021-06-24 18:19:12 +05:30
}
protected async createOutput(events: MatrixEvent[]): Promise<string> {
2021-07-02 10:23:25 +05:30
for (let i = 0; i < events.length; i++) {
const event = events[i];
this.updateProgress(
_t("export_chat|processing_event_n", {
number: i + 1,
total: events.length,
}),
false,
true,
);
2021-06-27 20:55:54 +05:30
if (this.cancelled) return this.cleanUp();
if (!haveRendererForEvent(event, this.room.client, false)) continue;
2021-06-29 10:57:02 +05:30
this.messages.push(await this.getJSONString(event));
2021-06-24 18:19:12 +05:30
}
2021-06-29 10:57:02 +05:30
return this.createJSONString();
2021-06-24 18:19:12 +05:30
}
public async export(): Promise<void> {
2021-10-15 16:32:15 +02:00
logger.info("Starting export process...");
logger.info("Fetching events...");
2021-06-25 15:49:39 +05:30
2021-06-24 18:19:12 +05:30
const fetchStart = performance.now();
const res = await this.getRequiredEvents();
const fetchEnd = performance.now();
2021-10-15 16:26:54 +02:00
logger.log(`Fetched ${res.length} events in ${(fetchEnd - fetchStart) / 1000}s`);
2021-06-24 18:19:12 +05:30
2021-10-15 16:32:15 +02:00
logger.info("Creating output...");
2021-06-24 18:19:12 +05:30
const text = await this.createOutput(res);
if (this.files.length) {
this.addFile("export.json", new Blob([text]));
await this.downloadZIP();
2021-06-24 18:19:12 +05:30
} else {
const fileName = this.destinationFileName;
2021-07-30 11:46:55 +05:30
this.downloadPlainText(fileName, text);
2021-06-24 18:19:12 +05:30
}
const exportEnd = performance.now();
2021-06-25 15:49:39 +05:30
2021-06-27 20:55:54 +05:30
if (this.cancelled) {
2021-10-15 16:32:15 +02:00
logger.info("Export cancelled successfully");
2021-06-27 20:55:54 +05:30
} else {
2021-10-15 16:32:15 +02:00
logger.info("Export successful!");
2021-10-15 16:26:54 +02:00
logger.log(`Exported ${res.length} events in ${(exportEnd - fetchStart) / 1000} seconds`);
2021-06-27 20:55:54 +05:30
}
2021-06-25 15:49:39 +05:30
2021-06-30 14:08:22 +05:30
this.cleanUp();
2021-06-24 18:19:12 +05:30
}
}