RoomNotifs.js moved to react-sdk

Since it's now used in RoomTile. Remove the vector prefix since it's no longer really a 'vector' thing
pull/1982/head
David Baker 2016-08-17 18:29:38 +01:00
parent cd0ed879e3
commit 6b0aeefc66
2 changed files with 10 additions and 156 deletions

View File

@ -19,9 +19,8 @@ limitations under the License.
var q = require("q"); var q = require("q");
var React = require('react'); var React = require('react');
var classNames = require('classnames'); var classNames = require('classnames');
var RoomNotifs = require('matrix-react-sdk/lib/RoomNotifs');
var MatrixClientPeg = require('matrix-react-sdk/lib/MatrixClientPeg'); var MatrixClientPeg = require('matrix-react-sdk/lib/MatrixClientPeg');
var dis = require('matrix-react-sdk/lib/dispatcher');
var RoomNotifs = require('../../../notifications/RoomNotifs');
module.exports = React.createClass({ module.exports = React.createClass({
displayName: 'NotificationStateContextMenu', displayName: 'NotificationStateContextMenu',
@ -34,12 +33,12 @@ module.exports = React.createClass({
getInitialState() { getInitialState() {
return { return {
vectorRoomNotifState: RoomNotifs.getVectorRoomNotifsState(this.props.room.roomId), roomNotifState: RoomNotifs.getRoomNotifsState(this.props.room.roomId),
} }
}, },
_save: function(newState) { _save: function(newState) {
const oldState = this.state.vectorRoomNotifState; const oldState = this.state.roomNotifState;
const roomId = this.props.room.roomId; const roomId = this.props.room.roomId;
var cli = MatrixClientPeg.get(); var cli = MatrixClientPeg.get();
@ -47,20 +46,12 @@ module.exports = React.createClass({
// Wrapping this in a q promise, as setRoomMutePushRule can return // Wrapping this in a q promise, as setRoomMutePushRule can return
// a promise or a value // a promise or a value
this.setState({ this.setState({
vectorRoomNotifState: newState, roomNotifState: newState,
}); });
RoomNotifs.setVectorRoomNotifsState(this.props.room.roomId, newState).done(() => { RoomNotifs.setRoomNotifsState(this.props.room.roomId, newState).done(() => {
// delay slightly so that the user can see their state change // delay slightly so that the user can see their state change
// before closing the menu // before closing the menu
return q.delay(500).then(() => { return q.delay(500).then(() => {
// tell everyone that wants to know of the change in
// notification state
dis.dispatch({
action: 'notification_change',
roomId: this.props.room.roomId,
//areNotifsMuted: areNotifsMuted,
});
// Close the context menu // Close the context menu
if (this.props.onFinished) { if (this.props.onFinished) {
this.props.onFinished(); this.props.onFinished();
@ -71,7 +62,7 @@ module.exports = React.createClass({
// to inform them that their state change failed. // to inform them that their state change failed.
// For now we at least set the state back // For now we at least set the state back
this.setState({ this.setState({
vectorRoomNotifState: oldState, roomNotifState: oldState,
}); });
}); });
} }
@ -96,22 +87,22 @@ module.exports = React.createClass({
render: function() { render: function() {
var alertMeClasses = classNames({ var alertMeClasses = classNames({
'mx_NotificationStateContextMenu_field': true, 'mx_NotificationStateContextMenu_field': true,
'mx_NotificationStateContextMenu_fieldSet': this.state.vectorRoomNotifState == 'all_messages_loud', 'mx_NotificationStateContextMenu_fieldSet': this.state.roomNotifState == 'all_messages_loud',
}); });
var allNotifsClasses = classNames({ var allNotifsClasses = classNames({
'mx_NotificationStateContextMenu_field': true, 'mx_NotificationStateContextMenu_field': true,
'mx_NotificationStateContextMenu_fieldSet': this.state.vectorRoomNotifState == 'all_messages', 'mx_NotificationStateContextMenu_fieldSet': this.state.roomNotifState == 'all_messages',
}); });
var mentionsClasses = classNames({ var mentionsClasses = classNames({
'mx_NotificationStateContextMenu_field': true, 'mx_NotificationStateContextMenu_field': true,
'mx_NotificationStateContextMenu_fieldSet': this.state.vectorRoomNotifState == 'mentions_only', 'mx_NotificationStateContextMenu_fieldSet': this.state.roomNotifState == 'mentions_only',
}); });
var muteNotifsClasses = classNames({ var muteNotifsClasses = classNames({
'mx_NotificationStateContextMenu_field': true, 'mx_NotificationStateContextMenu_field': true,
'mx_NotificationStateContextMenu_fieldSet': this.state.vectorRoomNotifState == 'mute', 'mx_NotificationStateContextMenu_fieldSet': this.state.roomNotifState == 'mute',
}); });
return ( return (

View File

@ -1,137 +0,0 @@
/*
Copyright 2016 OpenMarket Ltd
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 MatrixClientPeg from 'matrix-react-sdk/lib/MatrixClientPeg';
import PushProcessor from 'matrix-js-sdk/lib/pushprocessor';
import q from 'q';
export function getVectorRoomNotifsState(roomId) {
// look through the override rules for a rule affecting this room:
// if one exists, it will take precedence.
const muteRule = findOverrideMuteRule(roomId);
if (muteRule && muteRule.enabled) {
return 'mute';
}
// for everything else, look at the room rule.
const roomRule = MatrixClientPeg.get().getRoomPushRule('global', roomId);
// XXX: We have to assume the default is to notify for all messages
// (in particular this will be 'wrong' for one to one rooms because
// they will notify loudly for all messages)
if (!roomRule || !roomRule.enabled) return 'all_messages';
// a mute at the room level will still allow mentions
// to notify
if (isMuteRule(roomRule)) return 'mentions_only';
const actionsObject = PushProcessor.actionListToActionsObject(roomRule.actions);
if (actionsObject.tweaks.sound) return 'all_messages_loud';
return null;
}
export function setVectorRoomNotifsState(roomId, newState) {
const cli = MatrixClientPeg.get();
const promises = [];
if (newState == 'mute') {
// delete the room rule
const roomRule = MatrixClientPeg.get().getRoomPushRule('global', roomId);
if (roomRule) {
promises.push(cli.deletePushRule('global', 'room', roomRule.rule_id));
}
// add an override rule to squelch everything in this room
promises.push(cli.addPushRule('global', 'override', roomId, {
conditions: [
{
kind: 'event_match',
key: 'room_id',
pattern: roomId,
}
],
actions: [
'dont_notify',
]
}));
} else {
const overrideMuteRule = findOverrideMuteRule(roomId);
if (overrideMuteRule) {
promises.push(cli.deletePushRule('global', 'override', overrideMuteRule.rule_id));
}
if (newState == 'all_messages') {
promises.push(cli.deletePushRule('global', 'room', roomId));
} else if (newState == 'mentions_only') {
promises.push(cli.addPushRule('global', 'room', roomId, {
actions: [
'dont_notify',
]
}));
// https://matrix.org/jira/browse/SPEC-400
promises.push(cli.setPushRuleEnabled('global', 'room', roomId, true));
} else if ('all_messages_loud') {
promises.push(cli.addPushRule('global', 'room', roomId, {
actions: [
'notify',
{
set_tweak: 'sound',
value: 'default',
}
]
}));
// https://matrix.org/jira/browse/SPEC-400
promises.push(cli.setPushRuleEnabled('global', 'room', roomId, true));
}
}
return q.all(promises);
}
function findOverrideMuteRule(roomId) {
for (const rule of MatrixClientPeg.get().pushRules['global'].override) {
if (isRuleForRoom(roomId, rule)) {
if (isMuteRule(rule)) {
return rule;
}
}
}
return null;
}
function isRuleForRoom(roomId, rule) {
if (rule.conditions.length !== 1) {
return false;
}
const cond = rule.conditions[0];
if (
cond.kind == 'event_match' &&
cond.key == 'room_id' &&
cond.pattern == roomId
) {
return true;
}
return false;
}
function isMuteRule(rule) {
return (
rule.actions.length == 1 &&
rule.actions[0] == 'dont_notify'
);
}