Support account level custom sounds too

This commit is contained in:
Will Hunt 2019-04-19 14:10:10 +01:00
parent b0bacdba15
commit cd8647188f

View file

@ -99,36 +99,47 @@ const Notifier = {
_getSoundForRoom: async function(room) { _getSoundForRoom: async function(room) {
// We do no caching here because the SDK caches the event content // We do no caching here because the SDK caches the event content
// and the browser will cache the sound. // and the browser will cache the sound.
const ev = await room.getAccountData("uk.half-shot.notification.sound"); let ev = await room.getAccountData("uk.half-shot.notification.sound");
if (!ev) {
// Check the account data.
ev = await MatrixClientPeg.get().getAccountData("uk.half-shot.notification.sound");
if (!ev) { if (!ev) {
return null; return null;
} }
let url = ev.getContent().url; }
if (!url) { const content = ev.getContent();
if (!content.url) {
console.warn(`${room.roomId} has custom notification sound event, but no url key`); console.warn(`${room.roomId} has custom notification sound event, but no url key`);
return null; return null;
} }
url = MatrixClientPeg.get().mxcUrlToHttp(url); return {
return url; url: MatrixClientPeg.get().mxcUrlToHttp(content.url),
type: content.type,
};
}, },
_playAudioNotification: function(ev, room) { _playAudioNotification: function(ev, room) {
this._getSoundForRoom(room).then((soundUrl) => { this._getSoundForRoom(room).then((sound) => {
console.log(`Got sound ${soundUrl || "default"} for ${room.roomId}`); console.log(`Got sound ${sound || "default"} for ${room.roomId}`);
// XXX: How do we ensure this is a sound file and not // XXX: How do we ensure this is a sound file and not
// going to be exploited? // going to be exploited?
const selector = document.querySelector(soundUrl ? `audio[src='${soundUrl}']` : "#messageAudio"); const selector = document.querySelector(sound ? `audio[src='${sound.url}']` : "#messageAudio");
let audioElement = selector; let audioElement = selector;
if (!selector) { if (!selector) {
if (!soundUrl) { if (!sound) {
console.error("Tried to play alert sound but missing #messageAudio") console.error("Tried to play alert sound but missing #messageAudio")
return return;
}
audioElement = new Audio(sound.url);
if (sound.type) {
audioElement.type = sound.type;
} }
audioElement = new Audio(soundUrl);
document.body.appendChild(audioElement); document.body.appendChild(audioElement);
} }
audioElement.play(); audioElement.play();
}); }).catch((ex) => {
console.warn("Caught error when trying to fetch room notification sound:", ex);
})
}, },
start: function() { start: function() {