Use settings store

pull/21833/head
Will Hunt 2019-04-19 22:31:51 +01:00
parent d33df45c5e
commit 776210c135
3 changed files with 27 additions and 29 deletions

View File

@ -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?

View File

@ -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",

View File

@ -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,