diff --git a/app/push-notifications/client/views/pushNotificationsFlexTab.js b/app/push-notifications/client/views/pushNotificationsFlexTab.js index 70ac4e2045848..a71609be41454 100644 --- a/app/push-notifications/client/views/pushNotificationsFlexTab.js +++ b/app/push-notifications/client/views/pushNotificationsFlexTab.js @@ -15,6 +15,18 @@ const notificationLabels = { nothing: 'Nothing', }; +const getAudioAssetsArray = () => CustomSounds.getList().map((audio) => ({ + id: `audioNotificationValue${ audio.name }`, + name: 'audioNotificationValue', + label: audio.name, + value: `${ audio._id } ${ audio.name }`, +})); + +const getAudioAssetValue = (value) => { + const asset = CustomSounds.getList().find((audio) => audio._id === value); + return asset ? `${ asset._id } ${ asset.name }` : '0 Default'; +}; + const call = (method, ...params) => new Promise((resolve, reject) => { Meteor.call(method, ...params, (err, result) => { if (err) { @@ -133,10 +145,11 @@ Template.pushNotificationsFlexTab.onCreated(function() { mobilePushNotifications = 'default', emailNotifications = 'default', desktopNotificationDuration = 0, - audioNotificationValue = null, muteGroupMentions = false, } = sub; + const audioNotificationValue = sub.audioNotificationValue && getAudioAssetValue(sub.audioNotificationValue); + this.original = { disableNotifications: new ReactiveVar(disableNotifications), hideUnreadStatus: new ReactiveVar(hideUnreadStatus), @@ -199,19 +212,22 @@ Template.pushNotificationsFlexTab.events({ 'click [data-play]'(e) { e.preventDefault(); - const user = Meteor.userId(); - const value = Template.instance().form.audioNotificationValue.get().split(' '); - if (value[0] === '0') { - value[0] = getUserPreference(user, 'newMessageNotification'); - } + const uid = Meteor.userId(); + const formValue = Template.instance().form.audioNotificationValue.get(); + + const value = formValue && formValue.split(' ')[0] && formValue.split(' ')[0] !== '0' + ? formValue.split(' ')[0] + : getUserPreference(uid, 'newMessageNotification'); - if (value && value[0] !== 'none') { - const audioVolume = getUserPreference(user, 'notificationsSoundVolume'); - CustomSounds.play(value[0], { - volume: Number((audioVolume / 100).toPrecision(2)), - }); + if (!value || value === 'none') { + return; } + + const audioVolume = getUserPreference(uid, 'notificationsSoundVolume'); + CustomSounds.play(value, { + volume: Number((audioVolume / 100).toPrecision(2)), + }); }, 'change input[type=checkbox]'(e, instance) { @@ -229,13 +245,7 @@ Template.pushNotificationsFlexTab.events({ switch (key) { case 'audioNotificationValue': - const audioAssets = (CustomSounds && CustomSounds.getList && CustomSounds.getList()) || []; - const audioAssetsArray = audioAssets.map((audio) => ({ - id: `audioNotificationValue${ audio.name }`, - name: 'audioNotificationValue', - label: audio.name, - value: `${ audio._id } ${ audio.name }`, - })); + const audioAssetsArray = getAudioAssetsArray(); options = [ { id: 'audioNotificationValueNone',