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

Commit

Permalink
Fix: sync intentional mentions push rules with legacy rules (#11667)
Browse files Browse the repository at this point in the history
* fix monitorSyncedPushRules when primary rule is disabled

* sync intentional mentions rules

* remove debug log
  • Loading branch information
Kerry committed Sep 27, 2023
1 parent 4d0d024 commit 0d367a7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/notifications/VectorPushRulesDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export const VectorPushRulesDefinitions: Record<string, VectorPushRuleDefinition
[VectorState.Loud]: StandardActions.ACTION_HIGHLIGHT_DEFAULT_SOUND,
[VectorState.Off]: StandardActions.ACTION_DISABLED,
},
syncedRuleIds: [RuleId.IsUserMention],
}),

// Messages containing @room
Expand All @@ -122,6 +123,7 @@ export const VectorPushRulesDefinitions: Record<string, VectorPushRuleDefinition
[VectorState.Loud]: StandardActions.ACTION_HIGHLIGHT,
[VectorState.Off]: StandardActions.ACTION_DISABLED,
},
syncedRuleIds: [RuleId.IsRoomMention],
}),

// Messages just sent to the user in a 1:1 room
Expand Down
6 changes: 4 additions & 2 deletions src/utils/pushRules/monitorSyncedPushRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,17 @@ const monitorSyncedRule = async (
const primaryRuleVectorState = definition.ruleToVectorState(primaryRule);

const outOfSyncRules = syncedRules.filter(
(syncedRule) => definition.ruleToVectorState(syncedRule) !== primaryRuleVectorState,
(syncedRule) =>
syncedRule.enabled !== primaryRule.enabled ||
definition.ruleToVectorState(syncedRule) !== primaryRuleVectorState,
);

if (outOfSyncRules.length) {
await updateExistingPushRulesWithActions(
matrixClient,
// eslint-disable-next-line camelcase, @typescript-eslint/naming-convention
outOfSyncRules.map(({ rule_id }) => rule_id),
primaryRule.actions,
primaryRule.enabled ? primaryRule.actions : undefined,
);
}
};
Expand Down
62 changes: 61 additions & 1 deletion test/components/structures/LoggedInView-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

import React from "react";
import { render, RenderResult } from "@testing-library/react";
import { ConditionKind, EventType, IPushRule, MatrixEvent, ClientEvent } from "matrix-js-sdk/src/matrix";
import { ConditionKind, EventType, IPushRule, MatrixEvent, ClientEvent, PushRuleKind } from "matrix-js-sdk/src/matrix";
import { MediaHandler } from "matrix-js-sdk/src/webrtc/mediaHandler";
import { logger } from "matrix-js-sdk/src/logger";

Expand Down Expand Up @@ -81,6 +81,11 @@ describe("<LoggedInView />", () => {
enabled: true,
} as IPushRule;

const oneToOneRuleDisabled = {
...oneToOneRule,
enabled: false,
};

const groupRule = {
conditions: [{ kind: ConditionKind.EventMatch, key: "type", pattern: "m.room.message" }],
actions: StandardActions.ACTION_NOTIFY,
Expand Down Expand Up @@ -221,6 +226,36 @@ describe("<LoggedInView />", () => {
);
});

it("updates all mismatched rules from synced rules when primary rule is disabled", async () => {
setPushRules([
// poll 1-1 rules are synced with oneToOneRule
oneToOneRuleDisabled, // off
pollStartOneToOne, // on
pollEndOneToOne, // loud
// poll group rules are synced with groupRule
groupRule, // on
pollStartGroup, // loud
]);

getComponent();

await flushPromises();

// set to match primary rule
expect(mockClient.setPushRuleEnabled).toHaveBeenCalledWith(
"global",
PushRuleKind.Underride,
pollStartOneToOne.rule_id,
false,
);
expect(mockClient.setPushRuleEnabled).toHaveBeenCalledWith(
"global",
PushRuleKind.Underride,
pollEndOneToOne.rule_id,
false,
);
});

it("catches and logs errors while updating a rule", async () => {
mockClient.setPushRuleActions.mockRejectedValueOnce("oups").mockResolvedValueOnce({});

Expand Down Expand Up @@ -302,6 +337,31 @@ describe("<LoggedInView />", () => {
);
});

it("updates all mismatched rules from synced rules on a change to push rules account data when primary rule is disabled", async () => {
// setup a push rule state with mismatched rules
setPushRules([
// poll 1-1 rules are synced with oneToOneRule
oneToOneRuleDisabled, // off
pollEndOneToOne, // loud
]);

getComponent();

await flushPromises();

mockClient.setPushRuleEnabled.mockClear();

mockClient.emit(ClientEvent.AccountData, pushRulesEvent);

// set to match primary rule
expect(mockClient.setPushRuleEnabled).toHaveBeenCalledWith(
"global",
"underride",
pollEndOneToOne.rule_id,
false,
);
});

it("stops listening to account data events on unmount", () => {
// setup a push rule state with mismatched rules
setPushRules([
Expand Down

0 comments on commit 0d367a7

Please sign in to comment.