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

Commit

Permalink
Fix export type "Current timeline" to match its behaviour to its name (
Browse files Browse the repository at this point in the history
…#11426)

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

* Iterate tests
  • Loading branch information
t3chguy authored Aug 18, 2023
1 parent 4a5b686 commit ff9d490
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 62 deletions.
99 changes: 49 additions & 50 deletions src/utils/exportUtils/Exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
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 { logger } from "matrix-js-sdk/src/logger";
import sanitizeFilename from "sanitize-filename";
Expand Down Expand Up @@ -135,9 +135,6 @@ export default abstract class Exporter {
// when export type is LastNMessages
limit = this.exportOptions.numberOfMessages!;
break;
case ExportType.Timeline:
limit = 40;
break;
default:
limit = 10 ** 8;
}
Expand All @@ -148,58 +145,60 @@ export default abstract class Exporter {
const eventMapper = this.room.client.getEventMapper();

let prevToken: string | null = null;
let limit = this.getLimit();
const events: MatrixEvent[] = [];

while (limit) {
const eventsPerCrawl = Math.min(limit, 1000);
const res = await this.room.client.createMessagesRequest(
this.room.roomId,
prevToken,
eventsPerCrawl,
Direction.Backward,
);

if (this.cancelled) {
this.cleanUp();
return [];
}

if (res.chunk.length === 0) break;
let events: MatrixEvent[] = [];
if (this.exportType === ExportType.Timeline) {
events = this.room.getLiveTimeline().getEvents();
} else {
let limit = this.getLimit();
while (limit) {
const eventsPerCrawl = Math.min(limit, 1000);
const res = await this.room.client.createMessagesRequest(
this.room.roomId,
prevToken,
eventsPerCrawl,
Direction.Backward,
);

limit -= res.chunk.length;
if (this.cancelled) {
this.cleanUp();
return [];
}

const matrixEvents: MatrixEvent[] = res.chunk.map(eventMapper);
if (res.chunk.length === 0) break;

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);
}
limit -= res.chunk.length;

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,
}),
);
}
const matrixEvents: MatrixEvent[] = res.chunk.map(eventMapper);

prevToken = res.end ?? null;
}
// Reverse the events so that we preserve the order
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]];
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
Expand Down
68 changes: 56 additions & 12 deletions test/utils/exportUtils/HTMLExport-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,10 @@ describe("HTMLExport", () => {
const stubOptions: IExportOptions = {
attachmentsIncluded: false,
maxSize: 50000000,
numberOfMessages: 40,
};
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();

Expand Down Expand Up @@ -203,10 +204,11 @@ describe("HTMLExport", () => {

const exporter = new HTMLExporter(
room,
ExportType.Timeline,
ExportType.LastNMessages,
{
attachmentsIncluded: false,
maxSize: 1_024 * 1_024,
numberOfMessages: 40,
},
() => {},
);
Expand Down Expand Up @@ -234,10 +236,11 @@ describe("HTMLExport", () => {

const exporter = new HTMLExporter(
room,
ExportType.Timeline,
ExportType.LastNMessages,
{
attachmentsIncluded: false,
maxSize: 1_024 * 1_024,
numberOfMessages: 40,
},
() => {},
);
Expand All @@ -264,10 +267,11 @@ describe("HTMLExport", () => {

const exporter = new HTMLExporter(
room,
ExportType.Timeline,
ExportType.LastNMessages,
{
attachmentsIncluded: false,
maxSize: 1_024 * 1_024,
numberOfMessages: 40,
},
() => {},
);
Expand All @@ -287,10 +291,11 @@ describe("HTMLExport", () => {

const exporter = new HTMLExporter(
room,
ExportType.Timeline,
ExportType.LastNMessages,
{
attachmentsIncluded: false,
maxSize: 1_024 * 1_024,
numberOfMessages: 40,
},
() => {},
);
Expand Down Expand Up @@ -321,10 +326,11 @@ describe("HTMLExport", () => {

const exporter = new HTMLExporter(
room,
ExportType.Timeline,
ExportType.LastNMessages,
{
attachmentsIncluded: false,
maxSize: 1_024 * 1_024,
numberOfMessages: 40,
},
() => {},
);
Expand All @@ -342,10 +348,11 @@ describe("HTMLExport", () => {

const exporter = new HTMLExporter(
room,
ExportType.Timeline,
ExportType.LastNMessages,
{
attachmentsIncluded: false,
maxSize: 1_024 * 1_024,
numberOfMessages: 40,
},
() => {},
);
Expand All @@ -364,10 +371,11 @@ describe("HTMLExport", () => {

const exporter = new HTMLExporter(
room,
ExportType.Timeline,
ExportType.LastNMessages,
{
attachmentsIncluded: true,
maxSize: 1_024 * 1_024,
numberOfMessages: 40,
},
() => {},
);
Expand All @@ -392,10 +400,11 @@ describe("HTMLExport", () => {

const exporter = new HTMLExporter(
room,
ExportType.Timeline,
ExportType.LastNMessages,
{
attachmentsIncluded: true,
maxSize: 1_024 * 1_024,
numberOfMessages: 40,
},
() => {},
);
Expand Down Expand Up @@ -426,10 +435,11 @@ describe("HTMLExport", () => {

const exporter = new HTMLExporter(
room,
ExportType.Timeline,
ExportType.LastNMessages,
{
attachmentsIncluded: true,
maxSize: 1_024 * 1_024,
numberOfMessages: 40,
},
() => {},
);
Expand All @@ -451,10 +461,11 @@ describe("HTMLExport", () => {

const exporter = new HTMLExporter(
room,
ExportType.Timeline,
ExportType.LastNMessages,
{
attachmentsIncluded: false,
maxSize: 1_024 * 1_024,
numberOfMessages: 40,
},
() => {},
);
Expand Down Expand Up @@ -535,10 +546,11 @@ describe("HTMLExport", () => {

const exporter = new HTMLExporter(
room,
ExportType.Timeline,
ExportType.LastNMessages,
{
attachmentsIncluded: false,
maxSize: 1_024 * 1_024,
numberOfMessages: 40,
},
() => {},
);
Expand All @@ -551,4 +563,36 @@ describe("HTMLExport", () => {
expect(html).not.toContain(`${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();
});
});

0 comments on commit ff9d490

Please sign in to comment.