From 776210c135f4904116c0f57177a9bd9514b11b6a Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Fri, 19 Apr 2019 22:31:51 +0100 Subject: [PATCH] Use settings store --- src/Notifier.js | 25 ++++++------------ .../tabs/room/NotificationSettingsTab.js | 26 ++++++++++--------- src/settings/Settings.js | 5 ++++ 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/Notifier.js b/src/Notifier.js index 43d599ae0d..d5810fe30d 100644 --- a/src/Notifier.js +++ b/src/Notifier.js @@ -96,28 +96,19 @@ const Notifier = { } }, - setRoomSound: function(room, soundData) { - return MatrixClientPeg.get().setRoomAccountData(room.roomId, "uk.half-shot.notification.sound", soundData); - }, - - clearRoomSound: function(room) { - return room.setAccountData("uk.half-shot.notification.sound", null); - }, - - getSoundForRoom: async function(room) { + getSoundForRoom: async function(roomId) { // We do no caching here because the SDK caches the event content // and the browser will cache the sound. - let ev = await room.getAccountData("uk.half-shot.notification.sound"); - if (!ev || !ev.getContent()) { - // Check the account data. - ev = await MatrixClientPeg.get().getAccountData("uk.half-shot.notification.sound"); - if (!ev) { + let content = SettingsStore.getValue("notificationSound", roomId); + if (!content) { + content = SettingsStore.getValue("notificationSound"); + if (!content) { return null; } } - const content = ev.getContent(); + if (!content.url) { - console.warn(`${room.roomId} has custom notification sound event, but no url key`); + console.warn(`${roomId} has custom notification sound event, but no url key`); return null; } @@ -130,7 +121,7 @@ const Notifier = { }, _playAudioNotification: function(ev, room) { - this.getSoundForRoom(room).then((sound) => { + this.getSoundForRoom(room.roomId).then((sound) => { console.log(`Got sound ${sound.name || "default"} for ${room.roomId}`); // XXX: How do we ensure this is a sound file and not // going to be exploited? diff --git a/src/components/views/settings/tabs/room/NotificationSettingsTab.js b/src/components/views/settings/tabs/room/NotificationSettingsTab.js index a911ec3e4f..062d54988a 100644 --- a/src/components/views/settings/tabs/room/NotificationSettingsTab.js +++ b/src/components/views/settings/tabs/room/NotificationSettingsTab.js @@ -20,6 +20,7 @@ import {_t} from "../../../../../languageHandler"; import MatrixClientPeg from "../../../../../MatrixClientPeg"; import AccessibleButton from "../../../elements/AccessibleButton"; import Notifier from "../../../../../Notifier"; +import SettingsStore from '../../../../../settings/SettingsStore'; export default class NotificationsSettingsTab extends React.Component { static propTypes = { @@ -37,8 +38,7 @@ export default class NotificationsSettingsTab extends React.Component { } componentWillMount() { - const room = MatrixClientPeg.get().getRoom(this.props.roomId); - Notifier.getSoundForRoom(room).then((soundData) => { + Notifier.getSoundForRoom(this.props.roomId).then((soundData) => { if (!soundData) { return; } @@ -79,14 +79,17 @@ export default class NotificationsSettingsTab extends React.Component { }, ); - const room = MatrixClientPeg.get().getRoom(this.props.roomId); - - await Notifier.setRoomSound(room, { - name: this.state.uploadedFile.name, - type: type, - size: this.state.uploadedFile.size, - url, - }); + await SettingsStore.setValue( + "notificationSound", + this.props.roomId, + "room-account", + { + name: this.state.uploadedFile.name, + type: type, + size: this.state.uploadedFile.size, + url, + }, + ); this.setState({ uploadedFile: null, @@ -98,8 +101,7 @@ export default class NotificationsSettingsTab extends React.Component { _clearSound(e) { e.stopPropagation(); e.preventDefault(); - const room = MatrixClientPeg.get().getRoom(this.props.roomId); - Notifier.clearRoomSound(room); + SettingsStore.setValue("notificationSound", this.props.roomId, "room-account", null); this.setState({ currentSound: "default", diff --git a/src/settings/Settings.js b/src/settings/Settings.js index 35baa718b9..b40213e514 100644 --- a/src/settings/Settings.js +++ b/src/settings/Settings.js @@ -26,6 +26,7 @@ import ThemeController from './controllers/ThemeController'; // These are just a bunch of helper arrays to avoid copy/pasting a bunch of times const LEVELS_ROOM_SETTINGS = ['device', 'room-device', 'room-account', 'account', 'config']; +const LEVELS_ROOM_OR_ACCOUNT = ['room-account', 'account']; const LEVELS_ROOM_SETTINGS_WITH_ROOM = ['device', 'room-device', 'room-account', 'account', 'config', 'room']; const LEVELS_ACCOUNT_SETTINGS = ['device', 'account', 'config']; const LEVELS_FEATURE = ['device', 'config']; @@ -315,6 +316,10 @@ export const SETTINGS = { default: false, controller: new NotificationsEnabledController(), }, + "notificationSound": { + supportedLevels: LEVELS_ROOM_OR_ACCOUNT, + default: false, + }, "notificationBodyEnabled": { supportedLevels: LEVELS_DEVICE_ONLY_SETTINGS, default: true,