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 <mail@michael-weimann.eu>

* Fix test for readability

Signed-off-by: Rashmit Pankhania <raspankh@publicisgroupe.net>

* Fix test

Signed-off-by: Rashmit Pankhania <raspankh@publicisgroupe.net>

* Fix test

Signed-off-by: Rashmit Pankhania <raspankh@publicisgroupe.net>

* Fix test

Signed-off-by: Rashmit Pankhania <rashmitpankhania@gmail.com>
Signed-off-by: Rashmit Pankhania <raspankh@publicisgroupe.net>

* Use dateUtils formatFullDate function

Signed-off-by: Rashmit Pankhania <raspankh@publicisgroupe.net>

---------

Signed-off-by: Rashmit Pankhania <raspankh@publicisgroupe.net>
Signed-off-by: Rashmit Pankhania <rashmitpankhania@gmail.com>
Co-authored-by: Rashmit Pankhania <raspankh@publicisgroupe.net>
Co-authored-by: Michael Weimann <mail@michael-weimann.eu>
Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Rashmit Pankhania 2023-08-07 19:28:45 +05:30 committed by GitHub
parent 6a14362697
commit b08bdf7e0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 6 deletions

View File

@ -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;
}

View File

@ -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);
});
});