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

Fix "Export chat" not respecting configured time format in plain text mode #10696

Merged
merged 15 commits into from
Aug 7, 2023
Merged
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
9 changes: 8 additions & 1 deletion src/utils/exportUtils/PlainTextExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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;
Expand Down Expand Up @@ -122,7 +124,12 @@ export default class PlainTextExporter extends Exporter {
if (this.cancelled) return this.cleanUp();
if (!haveRendererForEvent(event, 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;
}
Expand Down
43 changes: 38 additions & 5 deletions test/utils/exportUtils/PlainTextExport-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
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);
});
});
Loading