Skip to content

Commit

Permalink
Add reactions to html export and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
langleyd committed Oct 16, 2024
1 parent 705625d commit a19b9d3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/utils/exportUtils/Exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/

import { Direction, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import { MediaEventContent } from "matrix-js-sdk/src/types";
import { Direction, MatrixEvent, Relations, Room } from "matrix-js-sdk/src/matrix";
import { EventType, MediaEventContent, RelationType } from "matrix-js-sdk/src/types";
import { saveAs } from "file-saver";
import { logger } from "matrix-js-sdk/src/logger";
import sanitizeFilename from "sanitize-filename";
Expand Down Expand Up @@ -284,5 +284,13 @@ export default abstract class Exporter {
return mxEv.getType() === attachmentTypes[0] || attachmentTypes.includes(mxEv.getContent().msgtype!);
}

protected getRelationsForEvent = (
eventId: string,
relationType: RelationType | string,
eventType: EventType | string,
): Relations | undefined => {
return this.room.getUnfilteredTimelineSet().relations.getChildEventsForEvent(eventId, relationType, eventType);
};

public abstract export(): Promise<void>;
}
3 changes: 2 additions & 1 deletion src/utils/exportUtils/HtmlExport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,10 @@ export default class HTMLExporter extends Exporter {
permalinkCreator={this.permalinkCreator}
lastSuccessful={false}
isSelectedEvent={false}
showReactions={false}
showReactions={true}
layout={Layout.Group}
showReadReceipts={false}
getRelationsForEvent={this.getRelationsForEvent}
/>
</TooltipProvider>
</MatrixClientContext.Provider>
Expand Down
44 changes: 43 additions & 1 deletion test/unit-tests/utils/exportUtils/HTMLExport-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@ Please see LICENSE files in the repository root for full details.
*/

import {
EventTimeline,
EventTimelineSet,
EventType,
IRoomEvent,
MatrixClient,
MatrixEvent,
MsgType,
Relations,
RelationType,
Room,
RoomMember,
RoomState,
} from "matrix-js-sdk/src/matrix";
import fetchMock from "fetch-mock-jest";
import escapeHtml from "escape-html";
import { RelationsContainer } from "matrix-js-sdk/src/models/relations-container";

import { filterConsole, mkStubRoom, REPEATABLE_DATE, stubClient } from "../../../test-utils";
import { filterConsole, mkReaction, mkStubRoom, REPEATABLE_DATE, stubClient } from "../../../test-utils";
import { ExportType, IExportOptions } from "../../../../src/utils/exportUtils/exportUtils";
import SdkConfig from "../../../../src/SdkConfig";
import HTMLExporter from "../../../../src/utils/exportUtils/HtmlExport";
Expand Down Expand Up @@ -587,4 +592,41 @@ describe("HTMLExport", () => {
expect(await file.text()).toContain("testing testing");
expect(client.createMessagesRequest).not.toHaveBeenCalled();
});

it("should include reactions", async () => {
const firstMessage = new MatrixEvent(EVENT_MESSAGE);
const reaction = mkReaction(firstMessage);

const relationsContainer = {
getRelations: jest.fn(),
getChildEventsForEvent: jest.fn(),
} as unknown as RelationsContainer;
const relations = new Relations(RelationType.Annotation, EventType.Reaction, client);
relations.addEvent(reaction);
relationsContainer.getChildEventsForEvent = jest.fn().mockReturnValue(relations);

const timelineSet = {
relations: relationsContainer,
getLiveTimeline: () => timeline,
} as unknown as EventTimelineSet;
const timeline = new EventTimeline(timelineSet);
room.getUnfilteredTimelineSet = jest.fn().mockReturnValue(timelineSet);
mockMessages(EVENT_MESSAGE);

const exporter = new HTMLExporter(
room,
ExportType.LastNMessages,
{
attachmentsIncluded: false,
maxSize: 1_024 * 1_024,
numberOfMessages: 40,
},
() => {},
);

await exporter.export();

const file = getMessageFile(exporter);
expect(await file.text()).toContain(reaction.getContent()["m.relates_to"]?.key);
});
});

0 comments on commit a19b9d3

Please sign in to comment.