Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Make chat exports use ISO 8601/RFC 3339 dates and be more informative #8558

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ export function formatFullDateNoDay(date: Date) {
});
}

export function formatFullDateNoDayISO(date: Date) {
return date.toISOString();
}

export function formatFullDateNoDayNoTime(date: Date) {
return (
date.getFullYear() +
Expand Down
6 changes: 2 additions & 4 deletions src/utils/exportUtils/Exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ import { saveAs } from "file-saver";
import { logger } from "matrix-js-sdk/src/logger";

import { MatrixClientPeg } from "../../MatrixClientPeg";
import { ExportType, IExportOptions } from "./exportUtils";
import { ExportType, IExportOptions, formatFilename } from "./exportUtils";
import { decryptFile } from "../DecryptFile";
import { mediaFromContent } from "../../customisations/Media";
import { formatFullDateNoDay } from "../../DateUtils";
import { isVoiceMessage } from "../EventUtils";
import { IMediaEventContent } from "../../customisations/models/IMediaEventContent";
import { _t } from "../../languageHandler";
import SdkConfig from "../../SdkConfig";

type BlobFile = {
name: string;
Expand Down Expand Up @@ -76,8 +75,7 @@ export default abstract class Exporter {
}

protected async downloadZIP(): Promise<string | void> {
const brand = SdkConfig.get().brand;
const filenameWithoutExt = `${brand} - Chat Export - ${formatFullDateNoDay(new Date())}`;
const filenameWithoutExt = formatFilename(this.room.name);
const filename = `${filenameWithoutExt}.zip`;
const { default: JSZip } = await import('jszip');

Expand Down
6 changes: 3 additions & 3 deletions src/utils/exportUtils/JSONExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { EventType } from "matrix-js-sdk/src/@types/event";
import { logger } from "matrix-js-sdk/src/logger";

import Exporter from "./Exporter";
import { formatFullDateNoDay, formatFullDateNoDayNoTime } from "../../DateUtils";
import { ExportType, IExportOptions } from "./exportUtils";
import { formatFullDateNoDayNoTime } from "../../DateUtils";
import { ExportType, formatFilename, IExportOptions } from "./exportUtils";
import { _t } from "../../languageHandler";
import { haveRendererForEvent } from "../../events/EventTileFactory";

Expand Down Expand Up @@ -108,7 +108,7 @@ export default class JSONExporter extends Exporter {
this.addFile("export.json", new Blob([text]));
await this.downloadZIP();
} else {
const fileName = `matrix-export-${formatFullDateNoDay(new Date())}.json`;
const fileName = `${formatFilename(this.room.name)}.json`;
this.downloadPlainText(fileName, text);
}

Expand Down
5 changes: 2 additions & 3 deletions src/utils/exportUtils/PlainTextExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ import { IContent, MatrixEvent } from "matrix-js-sdk/src/models/event";
import { logger } from "matrix-js-sdk/src/logger";

import Exporter from "./Exporter";
import { formatFullDateNoDay } from "../../DateUtils";
import { _t } from "../../languageHandler";
import { ExportType, IExportOptions } from "./exportUtils";
import { ExportType, formatFilename, IExportOptions } from "./exportUtils";
import { textForEvent } from "../../TextForEvent";
import { haveRendererForEvent } from "../../events/EventTileFactory";

Expand Down Expand Up @@ -136,7 +135,7 @@ export default class PlainTextExporter extends Exporter {
this.addFile("export.txt", new Blob([text]));
await this.downloadZIP();
} else {
const fileName = `matrix-export-${formatFullDateNoDay(new Date())}.txt`;
const fileName = `${formatFilename(this.room.name)}.txt`;
this.downloadPlainText(fileName, text);
}

Expand Down
19 changes: 19 additions & 0 deletions src/utils/exportUtils/exportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ limitations under the License.
*/

import { _t } from "../../languageHandler";
import { formatFullDateNoDayISO } from "../../DateUtils";
import SdkConfig from "../../SdkConfig";

export enum ExportFormat {
Html = "Html",
Expand Down Expand Up @@ -63,3 +65,20 @@ export interface IExportOptions {
attachmentsIncluded: boolean;
maxSize: number;
}

export const formatFilename = (roomName: string): string => {
const brand = SdkConfig.get().brand;
const regex = /([^\x20-~.]+)|([\\/.:?"<>|]+)/g;
let filename = "";

if (roomName.match(regex)?.length > 0) {
const roomnameStripped = roomName.replace(regex, "");
filename = `${brand}-${roomnameStripped}-${formatFullDateNoDayISO(new Date())}`;
//after stripping off invalid characters but left with empty string
if (roomnameStripped === "") filename = `${brand}-Chat-Export-${(new Date()).toISOString()}`;
} else {
filename = `${brand}-${roomName}-${(new Date()).toISOString()}`;
}

return filename;
};