1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-07 23:02:56 +03:00

Iterate PR based on feedback

This commit is contained in:
Michael Telatynski
2021-08-11 21:45:51 +01:00
parent 9b50455049
commit 69ba32683c
4 changed files with 32 additions and 33 deletions

View File

@@ -90,6 +90,8 @@ export interface ISenderNotificationPermissionCondition
key: string; key: string;
} }
// XXX: custom conditions are possible but always fail, and break the typescript discriminated union so ignore them here
// IPushRuleCondition<Exclude<string, ConditionKind>> unfortunately does not resolve this at the time of writing.
export type PushRuleCondition = IEventMatchCondition export type PushRuleCondition = IEventMatchCondition
| IContainsDisplayNameCondition | IContainsDisplayNameCondition
| IRoomMemberCountCondition | IRoomMemberCountCondition

View File

@@ -81,6 +81,15 @@ export interface IAuthDict {
threepidCreds?: any; threepidCreds?: any;
} }
class NoAuthFlowFoundError extends Error {
public name = "NoAuthFlowFoundError";
// eslint-disable-next-line @typescript-eslint/naming-convention, camelcase
constructor(m: string, public readonly required_stages: string[], public readonly flows: IFlow[]) {
super(m);
}
}
interface IOpts { interface IOpts {
matrixClient: MatrixClient; matrixClient: MatrixClient;
authData?: IAuthData; authData?: IAuthData;
@@ -574,16 +583,13 @@ export class InteractiveAuth {
return flow; return flow;
} }
} }
// Throw an error with a fairly generic description, but with more
// information such that the app can give a better one if so desired.
const err = new Error("No appropriate authentication flow found");
err.name = 'NoAuthFlowFoundError';
const requiredStages: string[] = []; const requiredStages: string[] = [];
if (haveEmail) requiredStages.push(EMAIL_STAGE_TYPE); if (haveEmail) requiredStages.push(EMAIL_STAGE_TYPE);
if (haveMsisdn) requiredStages.push(MSISDN_STAGE_TYPE); if (haveMsisdn) requiredStages.push(MSISDN_STAGE_TYPE);
(err as any).required_stages = requiredStages; // Throw an error with a fairly generic description, but with more
(err as any).available_flows = flows; // information such that the app can give a better one if so desired.
throw err; throw new NoAuthFlowFoundError("No appropriate authentication flow found", requiredStages, flows);
} }
/** /**

View File

@@ -24,12 +24,14 @@ import {
IContainsDisplayNameCondition, IContainsDisplayNameCondition,
IEventMatchCondition, IEventMatchCondition,
IPushRule, IPushRule,
IPushRules,
IRoomMemberCountCondition, IRoomMemberCountCondition,
ISenderNotificationPermissionCondition, ISenderNotificationPermissionCondition,
PushRuleAction, PushRuleAction,
PushRuleActionName, PushRuleActionName,
PushRuleCondition, PushRuleCondition,
PushRuleKind, PushRuleKind,
PushRuleSet,
TweakName, TweakName,
} from "./@types/PushRules"; } from "./@types/PushRules";
@@ -45,18 +47,6 @@ const RULEKINDS_IN_ORDER = [
PushRuleKind.Underride, PushRuleKind.Underride,
]; ];
interface IRuleset {
[PushRuleKind.ContentSpecific]: IPushRule[];
[PushRuleKind.Override]: IPushRule[];
[PushRuleKind.RoomSpecific]: IPushRule[];
[PushRuleKind.SenderSpecific]: IPushRule[];
[PushRuleKind.Underride]: IPushRule[];
}
export interface IRulesets {
global: IRuleset;
}
// The default override rules to apply to the push rules that arrive from the server. // The default override rules to apply to the push rules that arrive from the server.
// We do this for two reasons: // We do this for two reasons:
// 1. Synapse is unlikely to send us the push rule in an incremental sync - see // 1. Synapse is unlikely to send us the push rule in an incremental sync - see
@@ -114,6 +104,13 @@ export interface IActionsObject {
} }
export class PushProcessor { export class PushProcessor {
/**
* Construct a Push Processor.
* @constructor
* @param {Object} client The Matrix client object to use
*/
constructor(private readonly client: MatrixClient) {}
/** /**
* Convert a list of actions into a object with the actions as keys and their values * Convert a list of actions into a object with the actions as keys and their values
* eg. [ 'notify', { set_tweak: 'sound', value: 'default' } ] * eg. [ 'notify', { set_tweak: 'sound', value: 'default' } ]
@@ -145,13 +142,13 @@ export class PushProcessor {
* @param {object} incomingRules The client's existing push rules * @param {object} incomingRules The client's existing push rules
* @returns {object} The rewritten rules * @returns {object} The rewritten rules
*/ */
public static rewriteDefaultRules(incomingRules: IRulesets): IRulesets { public static rewriteDefaultRules(incomingRules: IPushRules): IPushRules {
let newRules: IRulesets = JSON.parse(JSON.stringify(incomingRules)); // deep clone let newRules: IPushRules = JSON.parse(JSON.stringify(incomingRules)); // deep clone
// These lines are mostly to make the tests happy. We shouldn't run into these // These lines are mostly to make the tests happy. We shouldn't run into these
// properties missing in practice. // properties missing in practice.
if (!newRules) newRules = {} as IRulesets; if (!newRules) newRules = {} as IPushRules;
if (!newRules.global) newRules.global = {} as IRuleset; if (!newRules.global) newRules.global = {} as PushRuleSet;
if (!newRules.global.override) newRules.global.override = []; if (!newRules.global.override) newRules.global.override = [];
// Merge the client-level defaults with the ones from the server // Merge the client-level defaults with the ones from the server
@@ -179,14 +176,7 @@ export class PushProcessor {
private static cachedGlobToRegex: Record<string, RegExp> = {}; // $glob: RegExp private static cachedGlobToRegex: Record<string, RegExp> = {}; // $glob: RegExp
/** private matchingRuleFromKindSet(ev: MatrixEvent, kindset: PushRuleSet): IAnnotatedPushRule {
* Construct a Push Processor.
* @constructor
* @param {Object} client The Matrix client object to use
*/
constructor(private readonly client: MatrixClient) {}
private matchingRuleFromKindSet(ev: MatrixEvent, kindset: IRuleset): IAnnotatedPushRule {
for (let ruleKindIndex = 0; ruleKindIndex < RULEKINDS_IN_ORDER.length; ++ruleKindIndex) { for (let ruleKindIndex = 0; ruleKindIndex < RULEKINDS_IN_ORDER.length; ++ruleKindIndex) {
const kind = RULEKINDS_IN_ORDER[ruleKindIndex]; const kind = RULEKINDS_IN_ORDER[ruleKindIndex];
const ruleset = kindset[kind]; const ruleset = kindset[kind];

View File

@@ -30,7 +30,7 @@ import * as utils from "./utils";
import { IDeferred } from "./utils"; import { IDeferred } from "./utils";
import { Filter } from "./filter"; import { Filter } from "./filter";
import { EventTimeline } from "./models/event-timeline"; import { EventTimeline } from "./models/event-timeline";
import { IRulesets, PushProcessor } from "./pushprocessor"; import { PushProcessor } from "./pushprocessor";
import { logger } from './logger'; import { logger } from './logger';
import { InvalidStoreError } from './errors'; import { InvalidStoreError } from './errors';
import { IStoredClientOpts, MatrixClient, PendingEventOrdering } from "./client"; import { IStoredClientOpts, MatrixClient, PendingEventOrdering } from "./client";
@@ -53,6 +53,7 @@ import { MatrixEvent } from "./models/event";
import { MatrixError } from "./http-api"; import { MatrixError } from "./http-api";
import { ISavedSync } from "./store"; import { ISavedSync } from "./store";
import { EventType } from "./@types/event"; import { EventType } from "./@types/event";
import { IPushRules } from "./@types/PushRules";
const DEBUG = true; const DEBUG = true;
@@ -1067,7 +1068,7 @@ export class SyncApi {
// will be updated when we receive push rules via getPushRules // will be updated when we receive push rules via getPushRules
// (see sync) before syncing over the network. // (see sync) before syncing over the network.
if (accountDataEvent.getType() === EventType.PushRules) { if (accountDataEvent.getType() === EventType.PushRules) {
const rules = accountDataEvent.getContent<IRulesets>(); const rules = accountDataEvent.getContent<IPushRules>();
client.pushRules = PushProcessor.rewriteDefaultRules(rules); client.pushRules = PushProcessor.rewriteDefaultRules(rules);
} }
const prevEvent = prevEventsMap[accountDataEvent.getId()]; const prevEvent = prevEventsMap[accountDataEvent.getId()];