From b08bdf7e0d4580dc62f51afa9de1e32082963fe7 Mon Sep 17 00:00:00 2001 From: Rashmit Pankhania Date: Mon, 7 Aug 2023 19:28:45 +0530 Subject: [PATCH] Fix "Export chat" not respecting configured time format in plain text mode (#10696) * #23838 Fix "Export chat" time format in plain text mode * #23838 Fix import of matrix-js-sdk * Remove hardcoded locale Co-authored-by: Michael Weimann * Fix test for readability Signed-off-by: Rashmit Pankhania * Fix test Signed-off-by: Rashmit Pankhania * Fix test Signed-off-by: Rashmit Pankhania * Fix test Signed-off-by: Rashmit Pankhania Signed-off-by: Rashmit Pankhania * Use dateUtils formatFullDate function Signed-off-by: Rashmit Pankhania --------- Signed-off-by: Rashmit Pankhania Signed-off-by: Rashmit Pankhania Co-authored-by: Rashmit Pankhania Co-authored-by: Michael Weimann Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> --- src/utils/exportUtils/PlainTextExport.ts | 9 +++- .../utils/exportUtils/PlainTextExport-test.ts | 43 ++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/utils/exportUtils/PlainTextExport.ts b/src/utils/exportUtils/PlainTextExport.ts index 45302a5b68..edf2a6e1a0 100644 --- a/src/utils/exportUtils/PlainTextExport.ts +++ b/src/utils/exportUtils/PlainTextExport.ts @@ -23,6 +23,8 @@ import { _t } from "../../languageHandler"; import { ExportType, IExportOptions } from "./exportUtils"; import { textForEvent } from "../../TextForEvent"; import { haveRendererForEvent } from "../../events/EventTileFactory"; +import SettingsStore from "../../settings/SettingsStore"; +import { formatFullDate } from "../../DateUtils"; export default class PlainTextExporter extends Exporter { protected totalSize: number; @@ -121,7 +123,12 @@ export default class PlainTextExporter extends Exporter { if (this.cancelled) return this.cleanUp(); if (!haveRendererForEvent(event, this.room.client, false)) continue; const textForEvent = await this.plainTextForEvent(event); - content += textForEvent && `${new Date(event.getTs()).toLocaleString()} - ${textForEvent}\n`; + content += + textForEvent && + `${formatFullDate( + new Date(event.getTs()), + SettingsStore.getValue("showTwelveHourTimestamps"), + )} - ${textForEvent}\n`; } return content; } diff --git a/test/utils/exportUtils/PlainTextExport-test.ts b/test/utils/exportUtils/PlainTextExport-test.ts index 400ca62c7d..2930900cdd 100644 --- a/test/utils/exportUtils/PlainTextExport-test.ts +++ b/test/utils/exportUtils/PlainTextExport-test.ts @@ -14,26 +14,59 @@ See the License for the specific language governing permissions and limitations under the License. */ +import { MatrixEvent, Room } from "matrix-js-sdk/src/matrix"; + import { createTestClient, mkStubRoom, REPEATABLE_DATE } from "../../test-utils"; import { ExportType, IExportOptions } from "../../../src/utils/exportUtils/exportUtils"; import PlainTextExporter from "../../../src/utils/exportUtils/PlainTextExport"; +import SettingsStore from "../../../src/settings/SettingsStore"; + +class TestablePlainTextExporter extends PlainTextExporter { + public async testCreateOutput(events: MatrixEvent[]): Promise { + return this.createOutput(events); + } +} describe("PlainTextExport", () => { + let stubOptions: IExportOptions; + let stubRoom: Room; beforeEach(() => { jest.useFakeTimers(); jest.setSystemTime(REPEATABLE_DATE); - }); - - it("should have a Matrix-branded destination file name", () => { const roomName = "My / Test / Room: Welcome"; const client = createTestClient(); - const stubOptions: IExportOptions = { + stubOptions = { attachmentsIncluded: false, maxSize: 50000000, }; - const stubRoom = mkStubRoom("!myroom:example.org", roomName, client); + stubRoom = mkStubRoom("!myroom:example.org", roomName, client); + }); + + it("should have a Matrix-branded destination file name", () => { const exporter = new PlainTextExporter(stubRoom, ExportType.Timeline, stubOptions, () => {}); expect(exporter.destinationFileName).toMatchSnapshot(); }); + + it.each([ + [24, false, "Fri, Apr 16 2021 17:20:00 - @alice:example.com: Hello, world!\n"], + [12, true, "Fri, Apr 16 2021 5:20:00PM - @alice:example.com: Hello, world!\n"], + ])("should return text with %i hr time format", async (hour: number, setting: boolean, expectedMessage: string) => { + jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName: string) => + settingName === "showTwelveHourTimestamps" ? setting : undefined, + ); + const events: MatrixEvent[] = [ + new MatrixEvent({ + type: "m.room.message", + content: { + body: "Hello, world!", + }, + sender: "@alice:example.com", + origin_server_ts: 1618593600000, + }), + ]; + const exporter = new TestablePlainTextExporter(stubRoom, ExportType.Timeline, stubOptions, () => {}); + const output = await exporter.testCreateOutput(events); + expect(output).toBe(expectedMessage); + }); });