diff --git a/src/LegacyCallHandler.tsx b/src/LegacyCallHandler.tsx index 4361d3d027..608d396dda 100644 --- a/src/LegacyCallHandler.tsx +++ b/src/LegacyCallHandler.tsx @@ -22,7 +22,6 @@ import { type MatrixCall, } from "matrix-js-sdk/src/webrtc/call"; import { logger } from "matrix-js-sdk/src/logger"; -import { PushProcessor } from "matrix-js-sdk/src/pushprocessor"; import { CallEventHandlerEvent } from "matrix-js-sdk/src/webrtc/callEventHandler"; import { MatrixClientPeg } from "./MatrixClientPeg"; @@ -596,7 +595,7 @@ export default class LegacyCallHandler extends TypedEventEmitter { pushDetails.rule.kind === PushRuleKind.ContentSpecific && pushDetails.rule.pattern ) { - this.pillifyNotificationKeywords([content], this.regExpForKeywordPattern(pushDetails.rule.pattern)); + this.pillifyNotificationKeywords( + [content], + PushProcessor.getPushRuleGlobRegex(pushDetails.rule.pattern, true), + ); } } @@ -235,12 +238,12 @@ export default class TextualBody extends React.Component { continue; } const match = text.match(exp); - if (!match || match.length < 3) { + if (!match || match.length < 2) { node = node.nextSibling; continue; } - const keywordText = match[2]; - const idx = match.index! + match[1].length; + const keywordText = match[1]; + const idx = match.index!; const before = text.substring(0, idx); const after = text.substring(idx + keywordText.length); @@ -265,12 +268,6 @@ export default class TextualBody extends React.Component { } } - private regExpForKeywordPattern(pattern: string): RegExp { - // Reflects the push notification pattern-matching implementation at - // https://github.com/matrix-org/matrix-js-sdk/blob/dbd7d26968b94700827bac525c39afff2c198e61/src/pushprocessor.ts#L570 - return new RegExp("(^|\\W)(" + globToRegexp(pattern) + ")(\\W|$)", "i"); - } - private findLinks(nodes: ArrayLike): string[] { let links: string[] = []; diff --git a/src/settings/controllers/NotificationControllers.ts b/src/settings/controllers/NotificationControllers.ts index 20b0d7cf7c..8ec17977b5 100644 --- a/src/settings/controllers/NotificationControllers.ts +++ b/src/settings/controllers/NotificationControllers.ts @@ -8,8 +8,6 @@ Please see LICENSE files in the repository root for full details. */ import { logger } from "matrix-js-sdk/src/logger"; -// XXX: This feels wrong. -import { PushProcessor } from "matrix-js-sdk/src/pushprocessor"; import { PushRuleActionName } from "matrix-js-sdk/src/matrix"; import SettingController from "./SettingController"; @@ -20,8 +18,7 @@ import { type SettingLevel } from "../SettingLevel"; // default action on this rule is dont_notify, but it could be something else export function isPushNotifyDisabled(): boolean { // Return the value of the master push rule as a default - const processor = new PushProcessor(MatrixClientPeg.safeGet()); - const masterRule = processor.getPushRuleById(".m.rule.master"); + const masterRule = MatrixClientPeg.safeGet().pushProcessor.getPushRuleById(".m.rule.master"); if (!masterRule) { logger.warn("No master push rule! Notifications are disabled for this user."); diff --git a/src/utils/pillify.tsx b/src/utils/pillify.tsx index e39fd813bb..6c83ad6553 100644 --- a/src/utils/pillify.tsx +++ b/src/utils/pillify.tsx @@ -7,7 +7,6 @@ Please see LICENSE files in the repository root for full details. */ import React, { StrictMode } from "react"; -import { PushProcessor } from "matrix-js-sdk/src/pushprocessor"; import { type MatrixClient, type MatrixEvent, RuleId } from "matrix-js-sdk/src/matrix"; import { TooltipProvider } from "@vector-im/compound-web"; @@ -119,11 +118,10 @@ export function pillifyLinks( } if (roomNotifTextNodes.length > 0) { - const pushProcessor = new PushProcessor(matrixClient); - const atRoomRule = pushProcessor.getPushRuleById( + const atRoomRule = matrixClient.pushProcessor.getPushRuleById( mxEvent.getContent()["m.mentions"] !== undefined ? RuleId.IsRoomMention : RuleId.AtRoomNotification, ); - if (atRoomRule && pushProcessor.ruleMatchesEvent(atRoomRule, mxEvent)) { + if (atRoomRule && matrixClient.pushProcessor.ruleMatchesEvent(atRoomRule, mxEvent)) { // Now replace all those nodes with Pills for (const roomNotifTextNode of roomNotifTextNodes) { // Set the next node to be processed to the one after the node diff --git a/src/utils/pushRules/monitorSyncedPushRules.ts b/src/utils/pushRules/monitorSyncedPushRules.ts index 2c9684722c..0e9620fe13 100644 --- a/src/utils/pushRules/monitorSyncedPushRules.ts +++ b/src/utils/pushRules/monitorSyncedPushRules.ts @@ -12,15 +12,16 @@ import { EventType, type RuleId, type IAnnotatedPushRule, + type IPushRule, + type PushRuleKind, } from "matrix-js-sdk/src/matrix"; -import { PushProcessor } from "matrix-js-sdk/src/pushprocessor"; import { logger } from "matrix-js-sdk/src/logger"; import { VectorPushRulesDefinitions, type VectorPushRuleDefinition } from "../../notifications"; import { updateExistingPushRulesWithActions } from "./updatePushRuleActions"; const pushRuleAndKindToAnnotated = ( - ruleAndKind: ReturnType, + ruleAndKind: { rule: IPushRule; kind: PushRuleKind } | null, ): IAnnotatedPushRule | undefined => ruleAndKind ? { @@ -34,23 +35,21 @@ const pushRuleAndKindToAnnotated = ( * And updates any that are out of sync * Ignores ruleIds that do not exist for the user * @param matrixClient - cli - * @param pushProcessor - processor used to retrieve current state of rules * @param ruleId - primary rule * @param definition - VectorPushRuleDefinition of the primary rule */ const monitorSyncedRule = async ( matrixClient: MatrixClient, - pushProcessor: PushProcessor, ruleId: RuleId | string, definition: VectorPushRuleDefinition, ): Promise => { - const primaryRule = pushRuleAndKindToAnnotated(pushProcessor.getPushRuleAndKindById(ruleId)); + const primaryRule = pushRuleAndKindToAnnotated(matrixClient.pushProcessor.getPushRuleAndKindById(ruleId)); if (!primaryRule) { return; } const syncedRules: IAnnotatedPushRule[] | undefined = definition.syncedRuleIds - ?.map((ruleId) => pushRuleAndKindToAnnotated(pushProcessor.getPushRuleAndKindById(ruleId))) + ?.map((ruleId) => pushRuleAndKindToAnnotated(matrixClient.pushProcessor.getPushRuleAndKindById(ruleId))) .filter((n?: IAnnotatedPushRule): n is IAnnotatedPushRule => Boolean(n)); // no synced rules to manage @@ -94,11 +93,10 @@ export const monitorSyncedPushRules = async ( if (accountDataEvent?.getType() !== EventType.PushRules) { return; } - const pushProcessor = new PushProcessor(matrixClient); Object.entries(VectorPushRulesDefinitions).forEach(async ([ruleId, definition]) => { try { - await monitorSyncedRule(matrixClient, pushProcessor, ruleId, definition); + await monitorSyncedRule(matrixClient, ruleId, definition); } catch (error) { logger.error(`Failed to fully synchronise push rules for ${ruleId}`, error); } diff --git a/src/utils/pushRules/updatePushRuleActions.ts b/src/utils/pushRules/updatePushRuleActions.ts index 1d872e5207..8a383cace6 100644 --- a/src/utils/pushRules/updatePushRuleActions.ts +++ b/src/utils/pushRules/updatePushRuleActions.ts @@ -7,7 +7,6 @@ Please see LICENSE files in the repository root for full details. */ import { type MatrixClient, type IPushRule, type PushRuleAction, type PushRuleKind } from "matrix-js-sdk/src/matrix"; -import { PushProcessor } from "matrix-js-sdk/src/pushprocessor"; /** * Sets the actions for a given push rule id and kind @@ -51,10 +50,8 @@ export const updateExistingPushRulesWithActions = async ( ruleIds?: IPushRule["rule_id"][], actions?: PushRuleAction[], ): Promise => { - const pushProcessor = new PushProcessor(matrixClient); - const rules: PushRuleAndKind[] | undefined = ruleIds - ?.map((ruleId) => pushProcessor.getPushRuleAndKindById(ruleId)) + ?.map((ruleId) => matrixClient.pushProcessor.getPushRuleAndKindById(ruleId)) .filter((n: PushRuleAndKind | null): n is PushRuleAndKind => Boolean(n)); if (!rules?.length) { diff --git a/test/unit-tests/LegacyCallHandler-test.ts b/test/unit-tests/LegacyCallHandler-test.ts index d10c232b65..664e091393 100644 --- a/test/unit-tests/LegacyCallHandler-test.ts +++ b/test/unit-tests/LegacyCallHandler-test.ts @@ -22,6 +22,7 @@ import { mocked } from "jest-mock"; import { CallEventHandlerEvent } from "matrix-js-sdk/src/webrtc/callEventHandler"; import fetchMock from "fetch-mock-jest"; import { waitFor } from "jest-matrix-react"; +import { PushProcessor } from "matrix-js-sdk/src/pushprocessor"; import LegacyCallHandler, { AudioID, @@ -473,6 +474,9 @@ describe("LegacyCallHandler without third party protocols", () => { throw new Error("Endpoint unsupported."); }; + // @ts-expect-error + MatrixClientPeg.safeGet().pushProcessor = new PushProcessor(MatrixClientPeg.safeGet()); + audioElement = document.createElement("audio"); audioElement.id = "remoteAudio"; document.body.appendChild(audioElement); diff --git a/test/unit-tests/components/structures/LoggedInView-test.tsx b/test/unit-tests/components/structures/LoggedInView-test.tsx index beca86ddcf..6d24cbe416 100644 --- a/test/unit-tests/components/structures/LoggedInView-test.tsx +++ b/test/unit-tests/components/structures/LoggedInView-test.tsx @@ -19,6 +19,7 @@ import { import { MediaHandler } from "matrix-js-sdk/src/webrtc/mediaHandler"; import { logger } from "matrix-js-sdk/src/logger"; import userEvent from "@testing-library/user-event"; +import { PushProcessor } from "matrix-js-sdk/src/pushprocessor"; import LoggedInView from "../../../../src/components/structures/LoggedInView"; import { SDKContext } from "../../../../src/contexts/SDKContext"; @@ -75,6 +76,8 @@ describe("", () => { jest.clearAllMocks(); mockClient.getMediaHandler.mockReturnValue(mediaHandler); mockClient.setPushRuleActions.mockReset().mockResolvedValue({}); + // @ts-expect-error + mockClient.pushProcessor = new PushProcessor(mockClient); }); describe("synced push rules", () => { diff --git a/test/unit-tests/components/views/messages/TextualBody-test.tsx b/test/unit-tests/components/views/messages/TextualBody-test.tsx index 753534a93f..db4790976a 100644 --- a/test/unit-tests/components/views/messages/TextualBody-test.tsx +++ b/test/unit-tests/components/views/messages/TextualBody-test.tsx @@ -10,6 +10,7 @@ import React from "react"; import { type MatrixClient, type MatrixEvent, PushRuleKind } from "matrix-js-sdk/src/matrix"; import { mocked, type MockedObject } from "jest-mock"; import { render, waitFor } from "jest-matrix-react"; +import { PushProcessor } from "matrix-js-sdk/src/pushprocessor"; import { getMockClientWithEventEmitter, mkEvent, mkMessage, mkStubRoom } from "../../../../test-utils"; import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg"; @@ -85,6 +86,8 @@ describe("", () => { throw new Error("MockClient event not found"); }, }); + // @ts-expect-error + defaultMatrixClient.pushProcessor = new PushProcessor(defaultMatrixClient); mocked(defaultRoom).findEventById.mockImplementation((eventId: string) => { if (eventId === defaultEvent.getId()) return defaultEvent; diff --git a/test/unit-tests/components/views/settings/Notifications-test.tsx b/test/unit-tests/components/views/settings/Notifications-test.tsx index 00653e1a3b..1566013aba 100644 --- a/test/unit-tests/components/views/settings/Notifications-test.tsx +++ b/test/unit-tests/components/views/settings/Notifications-test.tsx @@ -36,6 +36,7 @@ import { } from "jest-matrix-react"; import { mocked } from "jest-mock"; import userEvent from "@testing-library/user-event"; +import { PushProcessor } from "matrix-js-sdk/src/pushprocessor"; import Notifications from "../../../../../src/components/views/settings/Notifications"; import SettingsStore from "../../../../../src/settings/SettingsStore"; @@ -301,6 +302,8 @@ describe("", () => { mockClient.getPushRules.mockClear().mockResolvedValue(pushRules); mockClient.addPushRule.mockClear(); mockClient.deletePushRule.mockClear(); + // @ts-expect-error + mockClient.pushProcessor = new PushProcessor(mockClient); userEvent.setup(); diff --git a/test/unit-tests/utils/pillify-test.tsx b/test/unit-tests/utils/pillify-test.tsx index 4cca311c57..2b5a0859bb 100644 --- a/test/unit-tests/utils/pillify-test.tsx +++ b/test/unit-tests/utils/pillify-test.tsx @@ -10,6 +10,7 @@ import React from "react"; import { act, render } from "jest-matrix-react"; import { MatrixEvent, ConditionKind, EventType, PushRuleActionName, Room, TweakName } from "matrix-js-sdk/src/matrix"; import { mocked } from "jest-mock"; +import { PushProcessor } from "matrix-js-sdk/src/pushprocessor"; import { pillifyLinks } from "../../../src/utils/pillify"; import { stubClient } from "../../test-utils"; @@ -78,6 +79,8 @@ describe("pillify", () => { }, ], }; + // @ts-expect-error + cli.pushProcessor = new PushProcessor(cli); DMRoomMap.makeShared(cli); });