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

Commit 4776f87

Browse files
authored
Ignore chat effect when older than 48h (#48)
* Ignore effect later than 48h * Add tests for `EffectsOverlay-test.tsx`
1 parent 0cc0ebe commit 4776f87

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

src/components/structures/RoomView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
13641364
if (containsEmoji(ev.getContent(), effect.emojis) || ev.getContent().msgtype === effect.msgType) {
13651365
// For initial threads launch, chat effects are disabled see #19731
13661366
if (!ev.isRelation(THREAD_RELATION_TYPE.name)) {
1367-
dis.dispatch({ action: `effects.${effect.command}` });
1367+
dis.dispatch({ action: `effects.${effect.command}`, event: ev });
13681368
}
13691369
}
13701370
});

src/components/views/elements/EffectsOverlay.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Please see LICENSE files in the repository root for full details.
88
*/
99
import React, { FunctionComponent, useEffect, useRef } from "react";
1010
import { logger } from "matrix-js-sdk/src/logger";
11+
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
1112

1213
import dis from "../../../dispatcher/dispatcher";
1314
import ICanvasEffect from "../../../effects/ICanvasEffect";
@@ -44,9 +45,10 @@ const EffectsOverlay: FunctionComponent<IProps> = ({ roomWidth }) => {
4445
canvasRef.current.height = UIStore.instance.windowHeight;
4546
}
4647
};
47-
const onAction = (payload: { action: string }): void => {
48+
const onAction = (payload: { action: string; event?: MatrixEvent }): void => {
4849
const actionPrefix = "effects.";
49-
if (canvasRef.current && payload.action.startsWith(actionPrefix)) {
50+
const isOutdated = isEventOutdated(payload.event);
51+
if (canvasRef.current && payload.action.startsWith(actionPrefix) && !isOutdated) {
5052
const effect = payload.action.slice(actionPrefix.length);
5153
lazyLoadEffectModule(effect).then((module) => module?.start(canvasRef.current!));
5254
}
@@ -88,3 +90,19 @@ const EffectsOverlay: FunctionComponent<IProps> = ({ roomWidth }) => {
8890
};
8991

9092
export default EffectsOverlay;
93+
94+
// 48 hours
95+
// 48h * 60m * 60s * 1000ms
96+
const OUTDATED_EVENT_THRESHOLD = 48 * 60 * 60 * 1000;
97+
98+
/**
99+
* Return true if the event is older than 48h.
100+
* @param event
101+
*/
102+
function isEventOutdated(event?: MatrixEvent): boolean {
103+
if (!event) return false;
104+
105+
const nowTs = Date.now();
106+
const eventTs = event.getTs();
107+
return nowTs - eventTs > OUTDATED_EVENT_THRESHOLD;
108+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2024 New Vector Ltd.
3+
*
4+
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
5+
* Please see LICENSE files in the repository root for full details.
6+
*
7+
*/
8+
9+
import React from "react";
10+
import { render, waitFor } from "@testing-library/react";
11+
12+
import dis from "../../../../src/dispatcher/dispatcher";
13+
import EffectsOverlay from "../../../../src/components/views/elements/EffectsOverlay.tsx";
14+
15+
describe("<EffectsOverlay/>", () => {
16+
let isStarted: boolean;
17+
beforeEach(() => {
18+
isStarted = false;
19+
jest.mock("../../../../src/effects/confetti/index.ts", () => {
20+
return class Confetti {
21+
start = () => {
22+
isStarted = true;
23+
};
24+
stop = jest.fn();
25+
};
26+
});
27+
});
28+
29+
afterEach(() => jest.useRealTimers());
30+
31+
it("should render", () => {
32+
const { asFragment } = render(<EffectsOverlay roomWidth={100} />);
33+
expect(asFragment()).toMatchSnapshot();
34+
});
35+
36+
it("should start the confetti effect", async () => {
37+
render(<EffectsOverlay roomWidth={100} />);
38+
dis.dispatch({ action: "effects.confetti" });
39+
await waitFor(() => expect(isStarted).toBe(true));
40+
});
41+
42+
it("should start the confetti effect when the event is not outdated", async () => {
43+
const eventDate = new Date("2024-09-01");
44+
const date = new Date("2024-09-02");
45+
jest.useFakeTimers().setSystemTime(date);
46+
47+
render(<EffectsOverlay roomWidth={100} />);
48+
dis.dispatch({ action: "effects.confetti", event: { getTs: () => eventDate.getTime() } });
49+
await waitFor(() => expect(isStarted).toBe(true));
50+
});
51+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`<EffectsOverlay/> should render 1`] = `
4+
<DocumentFragment>
5+
<canvas
6+
aria-hidden="true"
7+
height="768"
8+
style="display: block; z-index: 999999; pointer-events: none; position: fixed; top: 0px; right: 0px;"
9+
width="100"
10+
/>
11+
</DocumentFragment>
12+
`;

0 commit comments

Comments
 (0)