Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix merging of default push rules #4135

Merged
merged 5 commits into from
Mar 28, 2024
Merged

Fix merging of default push rules #4135

merged 5 commits into from
Mar 28, 2024

Conversation

t3chguy
Copy link
Member

@t3chguy t3chguy commented Mar 28, 2024

Fixes element-hq/element-web#27173
Regressed by #4100

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
@t3chguy t3chguy added T-Defect X-Release-Blocker backport staging Label to automatically backport PR to staging branch labels Mar 28, 2024
@t3chguy t3chguy marked this pull request as ready for review March 28, 2024 12:03
@t3chguy t3chguy requested a review from a team as a code owner March 28, 2024 12:03
Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few thoughts

src/pushprocessor.ts Outdated Show resolved Hide resolved
src/pushprocessor.ts Outdated Show resolved Hide resolved
src/pushprocessor.ts Show resolved Hide resolved
src/pushprocessor.ts Outdated Show resolved Hide resolved
src/pushprocessor.ts Outdated Show resolved Hide resolved
src/pushprocessor.ts Outdated Show resolved Hide resolved
src/pushprocessor.ts Outdated Show resolved Hide resolved
Comment on lines 184 to 189
// Find the indices of the edges of the user-defined rules in the incoming rules
const incomingRulesEnabled = incomingRules.map((rule) => rule.enabled);
const userDefinedRulesRange: [number, number] = [
incomingRulesEnabled.indexOf(false),
incomingRulesEnabled.lastIndexOf(false),
];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're actually doing quite a lot of iterating and copying of incomingRules. I'd instead start just by partitioning them:

// Split the incomingRules into defaults and custom
const incomingDefaultRules = incomingRules.filter((rule) => rule.default);
const incomingCustomRules = incomingRules.filter((rule) => !rule.default);

... and I think everything else can then be more easily written in terms of those, instead of incomingRules.slice.

Not a blocker, just a thought.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splitting them as per your example would mean you lose track of the location of the user-defined rules. Right now only .m.rule.master precedes them but the server could send other default rules as they are added in that position. This codepath is only hit once for each incoming m.push_rules event down /sync, those should be quite rare

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really follow. We insert the custom rules into newRules just before copying RuleId.SuppressNotices either way?

This codepath is only hit once for each incoming m.push_rules event down /sync, those should be quite rare

Yeah, I'm arguing this rather more from the PoV of clarity than performance. Just saying: it's not like doing the indexOfs here rather than copying them is saving you much.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really follow. We insert the custom rules into newRules just before copying RuleId.SuppressNotices either way?

incomingRules is in the order of:

some default rules (currently just master)
user defined rules
other default rules

Your proposed code would merge the default rules into one list and the default orders wouldn't account for any rules in the first bucket other than master so the order of any additional rules specified by the server there in later spec versions go into the wrong spot

// Split the incomingRules into defaults and custom
const incomingDefaultRules = incomingRules.filter((rule) => rule.default);
const incomingCustomRules = incomingRules.filter((rule) => !rule.default);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your proposed code would merge the default rules into one list and the default orders wouldn't account for any rules in the first bucket other than master so the order of any additional rules specified by the server there in later spec versions go into the wrong spot

I don't think so. In such a scenario, the new rule would end up as the second entry in incomingDefaultRules (after master, and before suppress_notices). Assuming we change the loop below to iterate over incomingDefaultRules instead of the current pair of slices of incomingRules:

  • We'll see master; it will match the expectation, and we will copy it over.
  • We'll see the hypothesized new rule; it is unrecognized, and so we will copy it over.
  • We'll see suppress_notices. Its index in orderedRuleIds is higher than the expectation, so first we copy over the custom rules. Then we carry on.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making the proposed change the test fails as follows:

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out this was due to missing ".org.matrix.msc3914.rule.room.call" in the expected defaults in the last PR

Comment on lines 21 to 31
kind: "event_match",
kind: ConditionKind.EventMatch,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ftr I am generally deeply ambivalent about using symbolic constants in test code: using the literal gives us a chance to check that the constant is doing what we expect.

(This brought to you on the back of a Synapse bug where we had a test which used what we thought was a constant but turned out not to be.)

YMMV though, not a particular request to change this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ftr I am generally deeply ambivalent about using symbolic constants in test code: using the literal gives us a chance to check that the constant is doing what we expect.

Its preferable to @ts-ignore.

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean I'd argue your problem here is forcing msc3914RoomCallRule to be an IPushRule, but whatever

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if I remove that type cast there, then I'd get the failure in the call to rewritePushRules

The only reason it was fine before this PR is because we passed matrixClient.pushRules! to rewritePushRules and had
image to make TS happy with the types of pushRules within the mock matrixClient

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

spec/unit/pushprocessor.spec.ts Outdated Show resolved Hide resolved
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
spec/unit/pushprocessor.spec.ts Outdated Show resolved Hide resolved
src/pushprocessor.ts Show resolved Hide resolved
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think you should do #4135 (comment), but I'm not going to block this.

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
@t3chguy t3chguy enabled auto-merge March 28, 2024 15:00
@t3chguy t3chguy added this pull request to the merge queue Mar 28, 2024
@t3chguy t3chguy removed this pull request from the merge queue due to a manual request Mar 28, 2024
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
@t3chguy t3chguy enabled auto-merge March 28, 2024 15:52
@t3chguy t3chguy added this pull request to the merge queue Mar 28, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 28, 2024
@dbkr
Copy link
Member

dbkr commented Mar 28, 2024

Force merging because of e2e flake

@dbkr dbkr merged commit 78a2257 into develop Mar 28, 2024
23 checks passed
@dbkr dbkr deleted the t3chguy/fix/27173 branch March 28, 2024 16:23
@RiotRobot
Copy link
Contributor

The backport to staging failed:

The process '/usr/bin/git' failed with exit code 128

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-staging staging
# Navigate to the new working tree
cd .worktrees/backport-staging
# Create a new branch
git switch --create backport-4135-to-staging
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 78a225795bb99d44f7ae04f408a9c0213d7c2c47
# Push it to GitHub
git push --set-upstream origin backport-4135-to-staging
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-staging

Then, create a pull request where the base branch is staging and the compare/head branch is backport-4135-to-staging.

dbkr added a commit that referenced this pull request Mar 28, 2024
Fix merging of default push rules

(cherry picked from commit 78a2257)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport staging Label to automatically backport PR to staging branch T-Defect X-Release-Blocker
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Repeated setting of .m.rule.is_room_mention push rule
5 participants