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

Commit e807457

Browse files
authored
Use correct push rule to evaluate room-wide mentions (#12318)
Signed-off-by: Michael Telatynski <[email protected]>
1 parent 42ac873 commit e807457

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

src/utils/pillify.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ limitations under the License.
1717
import React from "react";
1818
import ReactDOM from "react-dom";
1919
import { PushProcessor } from "matrix-js-sdk/src/pushprocessor";
20-
import { MatrixEvent, MatrixClient } from "matrix-js-sdk/src/matrix";
20+
import { MatrixClient, MatrixEvent, RuleId } from "matrix-js-sdk/src/matrix";
2121
import { TooltipProvider } from "@vector-im/compound-web";
2222

2323
import SettingsStore from "../settings/SettingsStore";
24-
import { Pill, PillType, pillRoomNotifLen, pillRoomNotifPos } from "../components/views/elements/Pill";
24+
import { Pill, pillRoomNotifLen, pillRoomNotifPos, PillType } from "../components/views/elements/Pill";
2525
import { parsePermalink } from "./permalinks/Permalinks";
2626
import { PermalinkParts } from "./permalinks/PermalinkConstructor";
2727

@@ -127,7 +127,9 @@ export function pillifyLinks(
127127

128128
if (roomNotifTextNodes.length > 0) {
129129
const pushProcessor = new PushProcessor(matrixClient);
130-
const atRoomRule = pushProcessor.getPushRuleById(".m.rule.roomnotif");
130+
const atRoomRule = pushProcessor.getPushRuleById(
131+
mxEvent.getContent()["m.mentions"] !== undefined ? RuleId.IsRoomMention : RuleId.AtRoomNotification,
132+
);
131133
if (atRoomRule && pushProcessor.ruleMatchesEvent(atRoomRule, mxEvent)) {
132134
// Now replace all those nodes with Pills
133135
for (const roomNotifTextNode of roomNotifTextNodes) {

test/test-utils/test-utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export function createTestClient(): MatrixClient {
195195
isUserIgnored: jest.fn().mockReturnValue(false),
196196
getCapabilities: jest.fn().mockResolvedValue({}),
197197
supportsThreads: jest.fn().mockReturnValue(false),
198-
supportsIntentionalMentions: () => false,
198+
supportsIntentionalMentions: jest.fn().mockReturnValue(false),
199199
getRoomUpgradeHistory: jest.fn().mockReturnValue([]),
200200
getOpenIdToken: jest.fn().mockResolvedValue(undefined),
201201
registerWithIdentityServer: jest.fn().mockResolvedValue({}),

test/utils/pillify-test.tsx

+49-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
import React from "react";
1818
import { render } from "@testing-library/react";
1919
import { MatrixEvent, ConditionKind, EventType, PushRuleActionName, Room, TweakName } from "matrix-js-sdk/src/matrix";
20+
import { mocked } from "jest-mock";
2021

2122
import { pillifyLinks } from "../../src/utils/pillify";
2223
import { stubClient } from "../test-utils";
@@ -36,7 +37,9 @@ describe("pillify", () => {
3637
beforeEach(() => {
3738
stubClient();
3839
const cli = MatrixClientPeg.safeGet();
39-
(cli.getRoom as jest.Mock).mockReturnValue(new Room(roomId, cli, cli.getUserId()!));
40+
const room = new Room(roomId, cli, cli.getUserId()!);
41+
room.currentState.mayTriggerNotifOfType = jest.fn().mockReturnValue(true);
42+
(cli.getRoom as jest.Mock).mockReturnValue(room);
4043
cli.pushRules!.global = {
4144
override: [
4245
{
@@ -58,6 +61,28 @@ describe("pillify", () => {
5861
},
5962
],
6063
},
64+
{
65+
rule_id: ".m.rule.is_room_mention",
66+
default: true,
67+
enabled: true,
68+
conditions: [
69+
{
70+
kind: ConditionKind.EventPropertyIs,
71+
key: "content.m\\.mentions.room",
72+
value: true,
73+
},
74+
{
75+
kind: ConditionKind.SenderNotificationPermission,
76+
key: "room",
77+
},
78+
],
79+
actions: [
80+
PushRuleActionName.Notify,
81+
{
82+
set_tweak: TweakName.Highlight,
83+
},
84+
],
85+
},
6186
],
6287
};
6388

@@ -81,6 +106,29 @@ describe("pillify", () => {
81106
expect(container.querySelector(".mx_Pill.mx_AtRoomPill")?.textContent).toBe("!@room");
82107
});
83108

109+
it("should pillify @room in an intentional mentions world", () => {
110+
mocked(MatrixClientPeg.safeGet().supportsIntentionalMentions).mockReturnValue(true);
111+
const { container } = render(<div>@room</div>);
112+
const containers: Element[] = [];
113+
pillifyLinks(
114+
MatrixClientPeg.safeGet(),
115+
[container],
116+
new MatrixEvent({
117+
room_id: roomId,
118+
type: EventType.RoomMessage,
119+
content: {
120+
"body": "@room",
121+
"m.mentions": {
122+
room: true,
123+
},
124+
},
125+
}),
126+
containers,
127+
);
128+
expect(containers).toHaveLength(1);
129+
expect(container.querySelector(".mx_Pill.mx_AtRoomPill")?.textContent).toBe("!@room");
130+
});
131+
84132
it("should not double up pillification on repeated calls", () => {
85133
const { container } = render(<div>@room</div>);
86134
const containers: Element[] = [];

0 commit comments

Comments
 (0)