You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-26 17:03:12 +03:00
Support rewriting push rules when our internal defaults change
and change our internal default for tombstones
This commit is contained in:
@@ -29,6 +29,7 @@ import logger from './logger';
|
|||||||
|
|
||||||
const httpApi = require("./http-api");
|
const httpApi = require("./http-api");
|
||||||
const utils = require("./utils");
|
const utils = require("./utils");
|
||||||
|
const PushProcessor = require("./pushprocessor");
|
||||||
|
|
||||||
function termsUrlForService(serviceType, baseUrl) {
|
function termsUrlForService(serviceType, baseUrl) {
|
||||||
switch (serviceType) {
|
switch (serviceType) {
|
||||||
@@ -1477,7 +1478,9 @@ MatrixBaseApis.prototype.setPusher = function(pusher, callback) {
|
|||||||
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
* @return {module:http-api.MatrixError} Rejects: with an error response.
|
||||||
*/
|
*/
|
||||||
MatrixBaseApis.prototype.getPushRules = function(callback) {
|
MatrixBaseApis.prototype.getPushRules = function(callback) {
|
||||||
return this._http.authedRequest(callback, "GET", "/pushrules/");
|
return this._http.authedRequest(callback, "GET", "/pushrules/").then(rules => {
|
||||||
|
return PushProcessor.rewriteDefaultRules(rules);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -43,6 +43,11 @@ const DEFAULT_OVERRIDE_RULES = [
|
|||||||
key: "type",
|
key: "type",
|
||||||
pattern: "m.room.tombstone",
|
pattern: "m.room.tombstone",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
kind: "event_match",
|
||||||
|
key: "state_key",
|
||||||
|
pattern: "",
|
||||||
|
}
|
||||||
],
|
],
|
||||||
actions: [
|
actions: [
|
||||||
"notify",
|
"notify",
|
||||||
@@ -455,6 +460,32 @@ PushProcessor.actionListToActionsObject = function(actionlist) {
|
|||||||
return actionobj;
|
return actionobj;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rewrites conditions on a client's push rules to match the defaults
|
||||||
|
* where applicable. Useful for upgrading push rules to more strict
|
||||||
|
* conditions when the server is falling behind on defaults.
|
||||||
|
* @param {object} incomingRules The client's existing push rules
|
||||||
|
* @returns {object} The rewritten rules
|
||||||
|
*/
|
||||||
|
PushProcessor.rewriteDefaultRules = function(incomingRules) {
|
||||||
|
const newRules = JSON.parse(JSON.stringify(incomingRules)); // deep clone
|
||||||
|
|
||||||
|
// Fix default override rules
|
||||||
|
newRules.global.override = newRules.global.override.map(r => {
|
||||||
|
const defaultRule = DEFAULT_OVERRIDE_RULES.find(d => d.rule_id === r.rule_id);
|
||||||
|
if (!defaultRule) return r;
|
||||||
|
|
||||||
|
// Copy over the actions, default, and conditions. Don't touch the user's
|
||||||
|
// preference.
|
||||||
|
r.default = defaultRule.default;
|
||||||
|
r.conditions = defaultRule.conditions;
|
||||||
|
r.actions = defaultRule.actions;
|
||||||
|
return r;
|
||||||
|
});
|
||||||
|
|
||||||
|
return newRules;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} PushAction
|
* @typedef {Object} PushAction
|
||||||
* @type {Object}
|
* @type {Object}
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ const Group = require('./models/group');
|
|||||||
const utils = require("./utils");
|
const utils = require("./utils");
|
||||||
const Filter = require("./filter");
|
const Filter = require("./filter");
|
||||||
const EventTimeline = require("./models/event-timeline");
|
const EventTimeline = require("./models/event-timeline");
|
||||||
|
const PushProcessor = require("./pushprocessor");
|
||||||
import logger from '../src/logger';
|
import logger from '../src/logger';
|
||||||
|
|
||||||
import {InvalidStoreError} from './errors';
|
import {InvalidStoreError} from './errors';
|
||||||
@@ -1030,8 +1031,9 @@ SyncApi.prototype._processSyncResponse = async function(
|
|||||||
// honour push rules that were previously cached. Base rules
|
// honour push rules that were previously cached. Base rules
|
||||||
// will be updated when we recieve push rules via getPushRules
|
// will be updated when we recieve push rules via getPushRules
|
||||||
// (see SyncApi.prototype.sync) before syncing over the network.
|
// (see SyncApi.prototype.sync) before syncing over the network.
|
||||||
if (accountDataEvent.getType() == 'm.push_rules') {
|
if (accountDataEvent.getType() === 'm.push_rules') {
|
||||||
client.pushRules = accountDataEvent.getContent();
|
const rules = accountDataEvent.getContent();
|
||||||
|
client.pushRules = PushProcessor.rewriteDefaultRules(rules);
|
||||||
}
|
}
|
||||||
client.emit("accountData", accountDataEvent);
|
client.emit("accountData", accountDataEvent);
|
||||||
return accountDataEvent;
|
return accountDataEvent;
|
||||||
|
|||||||
Reference in New Issue
Block a user