2021-07-26 18:10:27 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-06-17 05:16:08 +00:00
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
2021-08-09 07:06:06 +00:00
|
|
|
import { IContent, MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2021-10-22 22:23:32 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
|
|
|
import Exporter from "./Exporter";
|
2021-06-17 05:16:08 +00:00
|
|
|
import { formatFullDateNoDay } from "../../DateUtils";
|
|
|
|
import { _t } from "../../languageHandler";
|
2021-10-22 22:23:32 +00:00
|
|
|
import { ExportType, IExportOptions } from "./exportUtils";
|
2021-06-17 05:16:08 +00:00
|
|
|
import { textForEvent } from "../../TextForEvent";
|
2022-03-23 05:16:25 +00:00
|
|
|
import { haveRendererForEvent } from "../../events/EventTileFactory";
|
2021-06-23 06:28:50 +00:00
|
|
|
|
2021-06-17 05:16:08 +00:00
|
|
|
export default class PlainTextExporter extends Exporter {
|
|
|
|
protected totalSize: number;
|
|
|
|
protected mediaOmitText: string;
|
|
|
|
|
2021-07-02 04:53:25 +00:00
|
|
|
constructor(
|
|
|
|
room: Room,
|
2021-08-13 03:00:50 +00:00
|
|
|
exportType: ExportType,
|
2021-07-26 18:10:27 +00:00
|
|
|
exportOptions: IExportOptions,
|
2021-08-13 18:33:02 +00:00
|
|
|
setProgressText: React.Dispatch<React.SetStateAction<string>>,
|
2021-07-02 04:53:25 +00:00
|
|
|
) {
|
2021-08-13 18:33:02 +00:00
|
|
|
super(room, exportType, exportOptions, setProgressText);
|
2021-06-17 05:16:08 +00:00
|
|
|
this.totalSize = 0;
|
|
|
|
this.mediaOmitText = !this.exportOptions.attachmentsIncluded
|
|
|
|
? _t("Media omitted")
|
|
|
|
: _t("Media omitted - file size limit exceeded");
|
|
|
|
}
|
|
|
|
|
2021-08-09 07:06:06 +00:00
|
|
|
public textForReplyEvent = (content: IContent) => {
|
|
|
|
const REPLY_REGEX = /> <(.*?)>(.*?)\n\n(.*)/s;
|
2021-06-22 05:19:14 +00:00
|
|
|
const REPLY_SOURCE_MAX_LENGTH = 32;
|
|
|
|
|
|
|
|
const match = REPLY_REGEX.exec(content.body);
|
|
|
|
|
2021-08-09 07:54:54 +00:00
|
|
|
// if the reply format is invalid, then return the body
|
2021-06-22 06:42:37 +00:00
|
|
|
if (!match) return content.body;
|
|
|
|
|
2021-06-22 05:19:14 +00:00
|
|
|
let rplSource: string;
|
|
|
|
const rplName = match[1];
|
|
|
|
const rplText = match[3];
|
2021-06-22 06:42:37 +00:00
|
|
|
|
2021-08-09 07:06:06 +00:00
|
|
|
rplSource = match[2].substring(1);
|
2021-06-22 05:19:14 +00:00
|
|
|
// Get the first non-blank line from the source.
|
2021-06-30 08:38:22 +00:00
|
|
|
const lines = rplSource.split('\n').filter((line) => !/^\s*$/.test(line));
|
2021-06-22 05:19:14 +00:00
|
|
|
if (lines.length > 0) {
|
|
|
|
// Cut to a maximum length.
|
|
|
|
rplSource = lines[0].substring(0, REPLY_SOURCE_MAX_LENGTH);
|
|
|
|
// Ellipsis if needed.
|
|
|
|
if (lines[0].length > REPLY_SOURCE_MAX_LENGTH) {
|
|
|
|
rplSource = rplSource + "...";
|
|
|
|
}
|
|
|
|
// Wrap in formatting
|
|
|
|
rplSource = ` "${rplSource}"`;
|
|
|
|
} else {
|
|
|
|
// Don't show a source because we couldn't format one.
|
|
|
|
rplSource = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return `<${rplName}${rplSource}> ${rplText}`;
|
2021-06-30 08:38:22 +00:00
|
|
|
};
|
2021-06-22 05:19:14 +00:00
|
|
|
|
2021-07-21 06:18:37 +00:00
|
|
|
protected plainTextForEvent = async (mxEv: MatrixEvent) => {
|
2021-06-22 05:19:14 +00:00
|
|
|
const senderDisplayName = mxEv.sender && mxEv.sender.name ? mxEv.sender.name : mxEv.getSender();
|
2021-06-25 09:19:01 +00:00
|
|
|
let mediaText = "";
|
|
|
|
if (this.isAttachment(mxEv)) {
|
|
|
|
if (this.exportOptions.attachmentsIncluded) {
|
|
|
|
try {
|
|
|
|
const blob = await this.getMediaBlob(mxEv);
|
2021-07-19 07:47:19 +00:00
|
|
|
if (this.totalSize + blob.size > this.exportOptions.maxSize) {
|
|
|
|
mediaText = ` (${this.mediaOmitText})`;
|
|
|
|
} else {
|
|
|
|
this.totalSize += blob.size;
|
|
|
|
const filePath = this.getFilePath(mxEv);
|
|
|
|
mediaText = " (" + _t("File Attached") + ")";
|
|
|
|
this.addFile(filePath, blob);
|
|
|
|
if (this.totalSize == this.exportOptions.maxSize) {
|
|
|
|
this.exportOptions.attachmentsIncluded = false;
|
|
|
|
}
|
2021-06-25 09:19:01 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
mediaText = " (" + _t("Error fetching file") + ")";
|
2021-10-15 14:26:54 +00:00
|
|
|
logger.log("Error fetching file " + error);
|
2021-06-25 09:19:01 +00:00
|
|
|
}
|
|
|
|
} else mediaText = ` (${this.mediaOmitText})`;
|
2021-06-23 06:28:50 +00:00
|
|
|
}
|
2021-08-09 07:06:06 +00:00
|
|
|
if (this.isReply(mxEv)) return senderDisplayName + ": " + this.textForReplyEvent(mxEv.getContent()) + mediaText;
|
2021-06-25 09:31:14 +00:00
|
|
|
else return textForEvent(mxEv) + mediaText;
|
2021-06-30 08:38:22 +00:00
|
|
|
};
|
2021-06-22 05:19:14 +00:00
|
|
|
|
2021-06-17 05:16:08 +00:00
|
|
|
protected async createOutput(events: MatrixEvent[]) {
|
|
|
|
let content = "";
|
2021-07-02 04:53:25 +00:00
|
|
|
for (let i = 0; i < events.length; i++) {
|
|
|
|
const event = events[i];
|
2021-12-15 16:21:51 +00:00
|
|
|
this.updateProgress(_t("Processing event %(number)s out of %(total)s", {
|
|
|
|
number: i + 1,
|
|
|
|
total: events.length,
|
|
|
|
}), false, true);
|
2021-06-27 15:25:54 +00:00
|
|
|
if (this.cancelled) return this.cleanUp();
|
2022-04-14 23:23:22 +00:00
|
|
|
if (!haveRendererForEvent(event, false)) continue;
|
2021-07-21 06:18:37 +00:00
|
|
|
const textForEvent = await this.plainTextForEvent(event);
|
2021-06-22 07:20:15 +00:00
|
|
|
content += textForEvent && `${new Date(event.getTs()).toLocaleString()} - ${textForEvent}\n`;
|
2021-06-17 05:16:08 +00:00
|
|
|
}
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async export() {
|
2021-12-15 16:21:51 +00:00
|
|
|
this.updateProgress(_t("Starting export process..."));
|
|
|
|
this.updateProgress(_t("Fetching events..."));
|
2021-06-25 10:19:39 +00:00
|
|
|
|
2021-06-17 05:16:08 +00:00
|
|
|
const fetchStart = performance.now();
|
|
|
|
const res = await this.getRequiredEvents();
|
|
|
|
const fetchEnd = performance.now();
|
|
|
|
|
2021-10-15 14:26:54 +00:00
|
|
|
logger.log(`Fetched ${res.length} events in ${(fetchEnd - fetchStart)/1000}s`);
|
2021-06-17 05:16:08 +00:00
|
|
|
|
2021-12-15 16:21:51 +00:00
|
|
|
this.updateProgress(_t("Creating output..."));
|
2021-06-17 05:16:08 +00:00
|
|
|
const text = await this.createOutput(res);
|
|
|
|
|
2021-06-25 05:46:59 +00:00
|
|
|
if (this.files.length) {
|
|
|
|
this.addFile("export.txt", new Blob([text]));
|
|
|
|
await this.downloadZIP();
|
2021-06-23 06:28:50 +00:00
|
|
|
} else {
|
2021-06-25 05:46:59 +00:00
|
|
|
const fileName = `matrix-export-${formatFullDateNoDay(new Date())}.txt`;
|
2021-07-30 06:16:55 +00:00
|
|
|
this.downloadPlainText(fileName, text);
|
2021-06-23 06:28:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 05:16:08 +00:00
|
|
|
const exportEnd = performance.now();
|
2021-06-25 10:19:39 +00:00
|
|
|
|
2021-06-27 15:25:54 +00:00
|
|
|
if (this.cancelled) {
|
2021-10-15 14:32:15 +00:00
|
|
|
logger.info("Export cancelled successfully");
|
2021-06-27 15:25:54 +00:00
|
|
|
} else {
|
2021-10-15 14:32:15 +00:00
|
|
|
logger.info("Export successful!");
|
2021-10-15 14:26:54 +00:00
|
|
|
logger.log(`Exported ${res.length} events in ${(exportEnd - fetchStart)/1000} seconds`);
|
2021-06-27 15:25:54 +00:00
|
|
|
}
|
2021-06-25 10:19:39 +00:00
|
|
|
|
2021-06-27 15:25:54 +00:00
|
|
|
this.cleanUp();
|
2021-06-17 05:16:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|