Fix export type "Current timeline" to match its behaviour to its name (#11426)

* Fix export type "Current timeline" to match its behaviour to its name

* Iterate tests
This commit is contained in:
Michael Telatynski 2023-08-18 12:58:58 +01:00 committed by GitHub
parent 4a5b686aaa
commit ff9d4905d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 108 additions and 65 deletions

View file

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import { MatrixEvent, Room, Direction } from "matrix-js-sdk/src/matrix"; import { Direction, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import { saveAs } from "file-saver"; import { saveAs } from "file-saver";
import { logger } from "matrix-js-sdk/src/logger"; import { logger } from "matrix-js-sdk/src/logger";
import sanitizeFilename from "sanitize-filename"; import sanitizeFilename from "sanitize-filename";
@ -135,9 +135,6 @@ export default abstract class Exporter {
// when export type is LastNMessages // when export type is LastNMessages
limit = this.exportOptions.numberOfMessages!; limit = this.exportOptions.numberOfMessages!;
break; break;
case ExportType.Timeline:
limit = 40;
break;
default: default:
limit = 10 ** 8; limit = 10 ** 8;
} }
@ -148,58 +145,60 @@ export default abstract class Exporter {
const eventMapper = this.room.client.getEventMapper(); const eventMapper = this.room.client.getEventMapper();
let prevToken: string | null = null; let prevToken: string | null = null;
let limit = this.getLimit();
const events: MatrixEvent[] = [];
while (limit) { let events: MatrixEvent[] = [];
const eventsPerCrawl = Math.min(limit, 1000); if (this.exportType === ExportType.Timeline) {
const res = await this.room.client.createMessagesRequest( events = this.room.getLiveTimeline().getEvents();
this.room.roomId, } else {
prevToken, let limit = this.getLimit();
eventsPerCrawl, while (limit) {
Direction.Backward, const eventsPerCrawl = Math.min(limit, 1000);
); const res = await this.room.client.createMessagesRequest(
this.room.roomId,
if (this.cancelled) { prevToken,
this.cleanUp(); eventsPerCrawl,
return []; Direction.Backward,
}
if (res.chunk.length === 0) break;
limit -= res.chunk.length;
const matrixEvents: MatrixEvent[] = res.chunk.map(eventMapper);
for (const mxEv of matrixEvents) {
// if (this.exportOptions.startDate && mxEv.getTs() < this.exportOptions.startDate) {
// // Once the last message received is older than the start date, we break out of both the loops
// limit = 0;
// break;
// }
events.push(mxEv);
}
if (this.exportType === ExportType.LastNMessages) {
this.updateProgress(
_t("Fetched %(count)s events out of %(total)s", {
count: events.length,
total: this.exportOptions.numberOfMessages,
}),
); );
} else {
this.updateProgress(
_t("Fetched %(count)s events so far", {
count: events.length,
}),
);
}
prevToken = res.end ?? null; if (this.cancelled) {
} this.cleanUp();
// Reverse the events so that we preserve the order return [];
for (let i = 0; i < Math.floor(events.length / 2); i++) { }
[events[i], events[events.length - i - 1]] = [events[events.length - i - 1], events[i]];
if (res.chunk.length === 0) break;
limit -= res.chunk.length;
const matrixEvents: MatrixEvent[] = res.chunk.map(eventMapper);
for (const mxEv of matrixEvents) {
// if (this.exportOptions.startDate && mxEv.getTs() < this.exportOptions.startDate) {
// // Once the last message received is older than the start date, we break out of both the loops
// limit = 0;
// break;
// }
events.push(mxEv);
}
if (this.exportType === ExportType.LastNMessages) {
this.updateProgress(
_t("Fetched %(count)s events out of %(total)s", {
count: events.length,
total: this.exportOptions.numberOfMessages,
}),
);
} else {
this.updateProgress(
_t("Fetched %(count)s events so far", {
count: events.length,
}),
);
}
prevToken = res.end ?? null;
}
// Reverse the events so that we preserve the order
events.reverse();
} }
const decryptionPromises = events const decryptionPromises = events

View file

@ -152,9 +152,10 @@ describe("HTMLExport", () => {
const stubOptions: IExportOptions = { const stubOptions: IExportOptions = {
attachmentsIncluded: false, attachmentsIncluded: false,
maxSize: 50000000, maxSize: 50000000,
numberOfMessages: 40,
}; };
const stubRoom = mkStubRoom("!myroom:example.org", roomName, client); const stubRoom = mkStubRoom("!myroom:example.org", roomName, client);
const exporter = new HTMLExporter(stubRoom, ExportType.Timeline, stubOptions, () => {}); const exporter = new HTMLExporter(stubRoom, ExportType.LastNMessages, stubOptions, () => {});
expect(exporter.destinationFileName).toMatchSnapshot(); expect(exporter.destinationFileName).toMatchSnapshot();
@ -203,10 +204,11 @@ describe("HTMLExport", () => {
const exporter = new HTMLExporter( const exporter = new HTMLExporter(
room, room,
ExportType.Timeline, ExportType.LastNMessages,
{ {
attachmentsIncluded: false, attachmentsIncluded: false,
maxSize: 1_024 * 1_024, maxSize: 1_024 * 1_024,
numberOfMessages: 40,
}, },
() => {}, () => {},
); );
@ -234,10 +236,11 @@ describe("HTMLExport", () => {
const exporter = new HTMLExporter( const exporter = new HTMLExporter(
room, room,
ExportType.Timeline, ExportType.LastNMessages,
{ {
attachmentsIncluded: false, attachmentsIncluded: false,
maxSize: 1_024 * 1_024, maxSize: 1_024 * 1_024,
numberOfMessages: 40,
}, },
() => {}, () => {},
); );
@ -264,10 +267,11 @@ describe("HTMLExport", () => {
const exporter = new HTMLExporter( const exporter = new HTMLExporter(
room, room,
ExportType.Timeline, ExportType.LastNMessages,
{ {
attachmentsIncluded: false, attachmentsIncluded: false,
maxSize: 1_024 * 1_024, maxSize: 1_024 * 1_024,
numberOfMessages: 40,
}, },
() => {}, () => {},
); );
@ -287,10 +291,11 @@ describe("HTMLExport", () => {
const exporter = new HTMLExporter( const exporter = new HTMLExporter(
room, room,
ExportType.Timeline, ExportType.LastNMessages,
{ {
attachmentsIncluded: false, attachmentsIncluded: false,
maxSize: 1_024 * 1_024, maxSize: 1_024 * 1_024,
numberOfMessages: 40,
}, },
() => {}, () => {},
); );
@ -321,10 +326,11 @@ describe("HTMLExport", () => {
const exporter = new HTMLExporter( const exporter = new HTMLExporter(
room, room,
ExportType.Timeline, ExportType.LastNMessages,
{ {
attachmentsIncluded: false, attachmentsIncluded: false,
maxSize: 1_024 * 1_024, maxSize: 1_024 * 1_024,
numberOfMessages: 40,
}, },
() => {}, () => {},
); );
@ -342,10 +348,11 @@ describe("HTMLExport", () => {
const exporter = new HTMLExporter( const exporter = new HTMLExporter(
room, room,
ExportType.Timeline, ExportType.LastNMessages,
{ {
attachmentsIncluded: false, attachmentsIncluded: false,
maxSize: 1_024 * 1_024, maxSize: 1_024 * 1_024,
numberOfMessages: 40,
}, },
() => {}, () => {},
); );
@ -364,10 +371,11 @@ describe("HTMLExport", () => {
const exporter = new HTMLExporter( const exporter = new HTMLExporter(
room, room,
ExportType.Timeline, ExportType.LastNMessages,
{ {
attachmentsIncluded: true, attachmentsIncluded: true,
maxSize: 1_024 * 1_024, maxSize: 1_024 * 1_024,
numberOfMessages: 40,
}, },
() => {}, () => {},
); );
@ -392,10 +400,11 @@ describe("HTMLExport", () => {
const exporter = new HTMLExporter( const exporter = new HTMLExporter(
room, room,
ExportType.Timeline, ExportType.LastNMessages,
{ {
attachmentsIncluded: true, attachmentsIncluded: true,
maxSize: 1_024 * 1_024, maxSize: 1_024 * 1_024,
numberOfMessages: 40,
}, },
() => {}, () => {},
); );
@ -426,10 +435,11 @@ describe("HTMLExport", () => {
const exporter = new HTMLExporter( const exporter = new HTMLExporter(
room, room,
ExportType.Timeline, ExportType.LastNMessages,
{ {
attachmentsIncluded: true, attachmentsIncluded: true,
maxSize: 1_024 * 1_024, maxSize: 1_024 * 1_024,
numberOfMessages: 40,
}, },
() => {}, () => {},
); );
@ -451,10 +461,11 @@ describe("HTMLExport", () => {
const exporter = new HTMLExporter( const exporter = new HTMLExporter(
room, room,
ExportType.Timeline, ExportType.LastNMessages,
{ {
attachmentsIncluded: false, attachmentsIncluded: false,
maxSize: 1_024 * 1_024, maxSize: 1_024 * 1_024,
numberOfMessages: 40,
}, },
() => {}, () => {},
); );
@ -535,10 +546,11 @@ describe("HTMLExport", () => {
const exporter = new HTMLExporter( const exporter = new HTMLExporter(
room, room,
ExportType.Timeline, ExportType.LastNMessages,
{ {
attachmentsIncluded: false, attachmentsIncluded: false,
maxSize: 1_024 * 1_024, maxSize: 1_024 * 1_024,
numberOfMessages: 40,
}, },
() => {}, () => {},
); );
@ -551,4 +563,36 @@ describe("HTMLExport", () => {
expect(html).not.toContain(`${topic}`); expect(html).not.toContain(`${topic}`);
expect(html).toContain(`Topic: ${escapeHtml(topic)}`); expect(html).toContain(`Topic: ${escapeHtml(topic)}`);
}); });
it("should not make /messages requests when exporting 'Current Timeline'", async () => {
client.createMessagesRequest.mockRejectedValue(new Error("Should never be called"));
room.addLiveEvents([
new MatrixEvent({
event_id: `$eventId`,
type: EventType.RoomMessage,
sender: client.getSafeUserId(),
origin_server_ts: 123456789,
content: {
msgtype: "m.text",
body: `testing testing`,
},
}),
]);
const exporter = new HTMLExporter(
room,
ExportType.Timeline,
{
attachmentsIncluded: false,
maxSize: 1_024 * 1_024,
},
() => {},
);
await exporter.export();
const file = getMessageFile(exporter);
expect(await file.text()).toContain("testing testing");
expect(client.createMessagesRequest).not.toHaveBeenCalled();
});
}); });