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

Commit b08bdf7

Browse files
rashmitpankhaniaRashmit Pankhaniaweeman1337t3chguy
authored
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 <[email protected]> * Fix test for readability Signed-off-by: Rashmit Pankhania <[email protected]> * Fix test Signed-off-by: Rashmit Pankhania <[email protected]> * Fix test Signed-off-by: Rashmit Pankhania <[email protected]> * Fix test Signed-off-by: Rashmit Pankhania <[email protected]> Signed-off-by: Rashmit Pankhania <[email protected]> * Use dateUtils formatFullDate function Signed-off-by: Rashmit Pankhania <[email protected]> --------- Signed-off-by: Rashmit Pankhania <[email protected]> Signed-off-by: Rashmit Pankhania <[email protected]> Co-authored-by: Rashmit Pankhania <[email protected]> Co-authored-by: Michael Weimann <[email protected]> Co-authored-by: Michael Telatynski <[email protected]>
1 parent 6a14362 commit b08bdf7

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

src/utils/exportUtils/PlainTextExport.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import { _t } from "../../languageHandler";
2323
import { ExportType, IExportOptions } from "./exportUtils";
2424
import { textForEvent } from "../../TextForEvent";
2525
import { haveRendererForEvent } from "../../events/EventTileFactory";
26+
import SettingsStore from "../../settings/SettingsStore";
27+
import { formatFullDate } from "../../DateUtils";
2628

2729
export default class PlainTextExporter extends Exporter {
2830
protected totalSize: number;
@@ -121,7 +123,12 @@ export default class PlainTextExporter extends Exporter {
121123
if (this.cancelled) return this.cleanUp();
122124
if (!haveRendererForEvent(event, this.room.client, false)) continue;
123125
const textForEvent = await this.plainTextForEvent(event);
124-
content += textForEvent && `${new Date(event.getTs()).toLocaleString()} - ${textForEvent}\n`;
126+
content +=
127+
textForEvent &&
128+
`${formatFullDate(
129+
new Date(event.getTs()),
130+
SettingsStore.getValue("showTwelveHourTimestamps"),
131+
)} - ${textForEvent}\n`;
125132
}
126133
return content;
127134
}

test/utils/exportUtils/PlainTextExport-test.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,59 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
import { MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
18+
1719
import { createTestClient, mkStubRoom, REPEATABLE_DATE } from "../../test-utils";
1820
import { ExportType, IExportOptions } from "../../../src/utils/exportUtils/exportUtils";
1921
import PlainTextExporter from "../../../src/utils/exportUtils/PlainTextExport";
22+
import SettingsStore from "../../../src/settings/SettingsStore";
23+
24+
class TestablePlainTextExporter extends PlainTextExporter {
25+
public async testCreateOutput(events: MatrixEvent[]): Promise<string> {
26+
return this.createOutput(events);
27+
}
28+
}
2029

2130
describe("PlainTextExport", () => {
31+
let stubOptions: IExportOptions;
32+
let stubRoom: Room;
2233
beforeEach(() => {
2334
jest.useFakeTimers();
2435
jest.setSystemTime(REPEATABLE_DATE);
25-
});
26-
27-
it("should have a Matrix-branded destination file name", () => {
2836
const roomName = "My / Test / Room: Welcome";
2937
const client = createTestClient();
30-
const stubOptions: IExportOptions = {
38+
stubOptions = {
3139
attachmentsIncluded: false,
3240
maxSize: 50000000,
3341
};
34-
const stubRoom = mkStubRoom("!myroom:example.org", roomName, client);
42+
stubRoom = mkStubRoom("!myroom:example.org", roomName, client);
43+
});
44+
45+
it("should have a Matrix-branded destination file name", () => {
3546
const exporter = new PlainTextExporter(stubRoom, ExportType.Timeline, stubOptions, () => {});
3647

3748
expect(exporter.destinationFileName).toMatchSnapshot();
3849
});
50+
51+
it.each([
52+
[24, false, "Fri, Apr 16 2021 17:20:00 - @alice:example.com: Hello, world!\n"],
53+
[12, true, "Fri, Apr 16 2021 5:20:00PM - @alice:example.com: Hello, world!\n"],
54+
])("should return text with %i hr time format", async (hour: number, setting: boolean, expectedMessage: string) => {
55+
jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName: string) =>
56+
settingName === "showTwelveHourTimestamps" ? setting : undefined,
57+
);
58+
const events: MatrixEvent[] = [
59+
new MatrixEvent({
60+
type: "m.room.message",
61+
content: {
62+
body: "Hello, world!",
63+
},
64+
sender: "@alice:example.com",
65+
origin_server_ts: 1618593600000,
66+
}),
67+
];
68+
const exporter = new TestablePlainTextExporter(stubRoom, ExportType.Timeline, stubOptions, () => {});
69+
const output = await exporter.testCreateOutput(events);
70+
expect(output).toBe(expectedMessage);
71+
});
3972
});

0 commit comments

Comments
 (0)