1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

Updated push rules methods after review

This commit is contained in:
manuroe
2016-01-18 17:22:35 +01:00
parent e3b4cb03e1
commit 2e664adb32

View File

@@ -2238,7 +2238,7 @@ MatrixClient.prototype.setPushRuleEnabled = function(scope, kind,
$ruleId: ruleId $ruleId: ruleId
}); });
return this._http.authedRequest( return this._http.authedRequest(
callback, "PUT", path, undefined, enabled ? 'true' : 'false' callback, "PUT", path, undefined, {"enabled": enabled}
); );
}; };
@@ -2259,6 +2259,11 @@ MatrixClient.prototype.getRoomPushRule = function(scope, roomId) {
} }
} }
} }
else {
throw new Error(
"SyncApi.sync() must be done before accessing to push rules."
);
}
}; };
/** /**
@@ -2283,7 +2288,7 @@ MatrixClient.prototype.setRoomMutePushRule = function(scope, roomId, mute) {
} }
if (!mute) { if (!mute) {
// Remove the rule only it is a muting rule // Remove the rule only if it is a muting rule
if (hasDontNotifyRule) { if (hasDontNotifyRule) {
deferred = this.deletePushRule(scope, "room", roomPushRule.rule_id); deferred = this.deletePushRule(scope, "room", roomPushRule.rule_id);
} }
@@ -2296,45 +2301,46 @@ MatrixClient.prototype.setRoomMutePushRule = function(scope, roomId, mute) {
} }
else if (!hasDontNotifyRule) { else if (!hasDontNotifyRule) {
// Remove the existing one before setting the mute push rule // Remove the existing one before setting the mute push rule
var deferred2 = q.defer(); // This is a workaround to SYN-590 (Push rule update fails)
deferred = q.defer();
this.deletePushRule(scope, "room", roomPushRule.rule_id) this.deletePushRule(scope, "room", roomPushRule.rule_id)
.done(function() { .done(function() {
self.addPushRule(scope, "room", roomId, { self.addPushRule(scope, "room", roomId, {
actions: ["dont_notify"] actions: ["dont_notify"]
}).done(function() { }).done(function() {
deferred2.resolve(); deferred.resolve();
}, function(err) { }, function(err) {
deferred2.reject(err); deferred.reject(err);
}); });
}, function(err) { }, function(err) {
deferred2.reject(err); deferred.reject(err);
}); });
deferred = deferred2.promise; deferred = deferred.promise;
} }
} }
if (deferred) { if (deferred) {
// Update this.pushRules when the operation completes // Update this.pushRules when the operation completes
var deferred3 = q.defer(); var ruleRefreshDeferred = q.defer();
deferred.done(function() { deferred.done(function() {
self.getPushRules().done(function(result) { self.getPushRules().done(function(result) {
self.pushRules = result; self.pushRules = result;
deferred3.resolve(); ruleRefreshDeferred.resolve();
}, function(err) { }, function(err) {
deferred3.reject(err); ruleRefreshDeferred.reject(err);
}); });
}, function(err) { }, function(err) {
// Update it even if the previous operation fails. This can help the // Update it even if the previous operation fails. This can help the
// app to recover when push settings has been modifed from another client // app to recover when push settings has been modifed from another client
self.getPushRules().done(function(result) { self.getPushRules().done(function(result) {
self.pushRules = result; self.pushRules = result;
deferred3.reject(err); ruleRefreshDeferred.reject(err);
}, function(err2) { }, function(err2) {
deferred3.reject(err); ruleRefreshDeferred.reject(err);
}); });
}); });
return deferred3.promise; return ruleRefreshDeferred.promise;
} }
}; };