This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 828
Sync polls push rules on changes to account_data #10287
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
6f31ac0
basic sync setup
2f99655
formatting
a993092
Merge branch 'develop' into psg-955/polls-push-sync
cfac7f7
get loudest value for synced rules
ca49e31
more types
19dbc95
test synced rules in notifications settings
be25984
Merge branch 'develop' into psg-955/polls-push-sync
e69c65a
type fixes
8c58724
noimplicitany fixes
6c8994f
remove debug
4188cb0
tidying
7d5f800
extract updatePushRuleActions fn to utils
5e752d0
extract update synced rules
8d39557
just synchronise in one place?
4602ed4
monitor account data changes AND trigger changes sync in notification…
0af76dd
Merge branch 'develop' into psg-1135/polls-push-sync-on-start
1a8b003
lint
0e2eb9e
setup LoggedInView test with enough mocks
55bb323
test rule syncing in LoggedInView
2443dd4
strict fixes
c994972
more comments
5afce3f
one more comment
060fa96
Merge branch 'develop' into psg-1135/polls-push-sync-on-start
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { MatrixClient } from "matrix-js-sdk/src/client"; | ||
import { MatrixEvent, EventType } from "matrix-js-sdk/src/matrix"; | ||
import { PushProcessor } from "matrix-js-sdk/src/pushprocessor"; | ||
import { RuleId, IAnnotatedPushRule } from "matrix-js-sdk/src/@types/PushRules"; | ||
import { logger } from "matrix-js-sdk/src/logger"; | ||
|
||
import { VectorPushRulesDefinitions, VectorPushRuleDefinition } from "../../notifications"; | ||
import { updateExistingPushRulesWithActions } from "./updatePushRuleActions"; | ||
|
||
const pushRuleAndKindToAnnotated = ( | ||
ruleAndKind: ReturnType<PushProcessor["getPushRuleAndKindById"]>, | ||
): IAnnotatedPushRule | undefined => | ||
ruleAndKind | ||
? { | ||
...ruleAndKind.rule, | ||
kind: ruleAndKind.kind, | ||
} | ||
: undefined; | ||
|
||
/** | ||
* Checks that any synced rules that exist a given rule are in sync | ||
* 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<void> => { | ||
const primaryRule = pushRuleAndKindToAnnotated(pushProcessor.getPushRuleAndKindById(ruleId)); | ||
|
||
if (!primaryRule) { | ||
return; | ||
} | ||
const syncedRules: IAnnotatedPushRule[] | undefined = definition.syncedRuleIds | ||
?.map((ruleId) => pushRuleAndKindToAnnotated(pushProcessor.getPushRuleAndKindById(ruleId))) | ||
.filter((n?: IAnnotatedPushRule): n is IAnnotatedPushRule => Boolean(n)); | ||
|
||
// no synced rules to manage | ||
if (!syncedRules?.length) { | ||
return; | ||
} | ||
|
||
const primaryRuleVectorState = definition.ruleToVectorState(primaryRule); | ||
|
||
const outOfSyncRules = syncedRules.filter( | ||
(syncedRule) => 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, | ||
); | ||
} | ||
}; | ||
|
||
/** | ||
* On changes to m.push_rules account data, | ||
* check that synced push rules are in sync with their primary rule, | ||
* and update any out of sync rules. | ||
* synced rules are defined in VectorPushRulesDefinitions | ||
* If updating a rule fails for any reason, | ||
* the error is caught and handled silently | ||
* @param accountDataEvent - MatrixEvent | ||
* @param matrixClient - cli | ||
* @returns Resolves when updates are complete | ||
*/ | ||
export const monitorSyncedPushRules = async ( | ||
accountDataEvent: MatrixEvent | undefined, | ||
matrixClient: MatrixClient, | ||
): Promise<void> => { | ||
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); | ||
} catch (error) { | ||
logger.error(`Failed to fully synchronise push rules for ${ruleId}`, error); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { MatrixClient } from "matrix-js-sdk/src/client"; | ||
import { IPushRule, PushRuleAction, 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 | ||
* When actions are falsy, disables the rule | ||
* @param matrixClient - cli | ||
* @param ruleId - rule id to update | ||
* @param kind - PushRuleKind | ||
* @param actions - push rule actions to set for rule | ||
*/ | ||
export const updatePushRuleActions = async ( | ||
matrixClient: MatrixClient, | ||
ruleId: IPushRule["rule_id"], | ||
kind: PushRuleKind, | ||
actions?: PushRuleAction[], | ||
): Promise<void> => { | ||
if (!actions) { | ||
await matrixClient.setPushRuleEnabled("global", kind, ruleId, false); | ||
} else { | ||
await matrixClient.setPushRuleActions("global", kind, ruleId, actions); | ||
await matrixClient.setPushRuleEnabled("global", kind, ruleId, true); | ||
} | ||
}; | ||
|
||
interface PushRuleAndKind { | ||
rule: IPushRule; | ||
kind: PushRuleKind; | ||
} | ||
|
||
/** | ||
* Update push rules with given actions | ||
* Where they already exist for current user | ||
* Rules are updated sequentially and stop at first error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does an error case mean? Will there be an error thrown? |
||
* @param matrixClient - cli | ||
* @param ruleIds - RuleIds of push rules to attempt to set actions for | ||
* @param actions - push rule actions to set for rule | ||
* @returns resolves when all rules have been updated | ||
* @returns rejects when a rule update fails | ||
*/ | ||
export const updateExistingPushRulesWithActions = async ( | ||
matrixClient: MatrixClient, | ||
ruleIds?: IPushRule["rule_id"][], | ||
actions?: PushRuleAction[], | ||
): Promise<void> => { | ||
const pushProcessor = new PushProcessor(matrixClient); | ||
|
||
const rules: PushRuleAndKind[] | undefined = ruleIds | ||
?.map((ruleId) => pushProcessor.getPushRuleAndKindById(ruleId)) | ||
.filter((n: PushRuleAndKind | null): n is PushRuleAndKind => Boolean(n)); | ||
|
||
if (!rules?.length) { | ||
return; | ||
} | ||
for (const { kind, rule } of rules) { | ||
await updatePushRuleActions(matrixClient, rule.rule_id, kind, actions); | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you currently worked on this, can you add some prose about what it does?