Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: separate creation of browser notifications and sounds from SFC c…
…omponent

Signed-off-by: Maksim Sukharev <[email protected]>
  • Loading branch information
Antreesy committed Jun 13, 2024
commit d3ad4b13cc5446bf7a9acdc02cb8ef775175a633
69 changes: 4 additions & 65 deletions src/Components/Notification.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,14 @@ import NcRichText from '@nextcloud/vue/dist/Components/NcRichText.js'
import Close from 'vue-material-design-icons/Close.vue'
import Message from 'vue-material-design-icons/Message.vue'
import { showError } from '@nextcloud/dialogs'
import { loadState } from '@nextcloud/initial-state'
import { Howl } from 'howler'
import Action from './Action.vue'
import { generateOcsUrl, generateFilePath } from '@nextcloud/router'
import { generateOcsUrl } from '@nextcloud/router'
import moment from '@nextcloud/moment'
import DefaultParameter from './Parameters/DefaultParameter.vue'
import File from './Parameters/File.vue'
import User from './Parameters/User.vue'
import { emit } from '@nextcloud/event-bus'
import { createWebNotification } from '../services/webNotificationsService.js'

export default {
name: 'Notification',
Expand Down Expand Up @@ -267,32 +266,9 @@ export default {
emit('notifications:notification:received', event)
}

if (this.shouldNotify
&& this.$parent.$parent.$parent.showBrowserNotifications
if (this.$parent.$parent.$parent.showBrowserNotifications
&& this.$parent.$parent.$parent.webNotificationsThresholdId < this.notificationId) {
this._createWebNotification()

if (this.app === 'spreed' && this.objectType === 'call') {
if (loadState('notifications', 'sound_talk')) {
const sound = new Howl({
src: [
generateFilePath('notifications', 'img', 'talk.ogg'),
],
volume: 0.5,
})

sound.play()
}
} else if (loadState('notifications', 'sound_notification')) {
const sound = new Howl({
src: [
generateFilePath('notifications', 'img', 'notification.ogg'),
],
volume: 0.5,
})

sound.play()
}
createWebNotification(this)
}
},

Expand Down Expand Up @@ -341,43 +317,6 @@ export default {
showError(t('notifications', 'Failed to dismiss notification'))
})
},

/**
* Create a browser notification
*
* @see https://developer.mozilla.org/en/docs/Web/API/notification
*/
_createWebNotification() {
const n = new Notification(this.subject, {
title: this.subject,
lang: OC.getLocale(),
body: this.message,
icon: this.icon,
tag: this.notificationId,
})

if (this.link) {
n.onclick = async function(e) {
const event = {
cancelAction: false,
notification: this.$props,
action: {
url: this.link,
type: 'WEB',
},
}
await emit('notifications:action:execute', event)

if (!event.cancelAction) {
console.debug('Redirecting because of a click onto a notification', this.link)
window.location.href = this.link
}

// Best effort try to bring the tab to the foreground (works at least in Chrome, not in Firefox)
window.focus()
}.bind(this)
}
},
},
}
</script>
Expand Down
85 changes: 85 additions & 0 deletions src/services/webNotificationsService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { emit } from '@nextcloud/event-bus'
import { loadState } from '@nextcloud/initial-state'
import { generateFilePath } from '@nextcloud/router'
import { Howl } from 'howler'

/**
* Create a browser notification
*
* @param {object} notification notification object
* @see https://developer.mozilla.org/en/docs/Web/API/notification
*/
const createWebNotification = (notification) => {
if (!notification.shouldNotify) {
return
}

const n = new Notification(notification.subject, {
title: notification.subject,
lang: OC.getLocale(),
body: notification.message,
icon: notification.icon,
tag: notification.notificationId,
})

if (notification.link) {
n.onclick = async function(e) {
const event = {
cancelAction: false,
notification,
action: {
url: notification.link,
type: 'WEB',
},
}
await emit('notifications:action:execute', event)

if (!event.cancelAction) {
console.debug('Redirecting because of a click onto a notification', notification.link)
window.location.href = notification.link
}

// Best effort try to bring the tab to the foreground (works at least in Chrome, not in Firefox)
window.focus()
}
}

playNotificationSound(notification)
}

/**
* Play a notification sound (if enabled on instance)
* @param {object} notification notification object
*/
const playNotificationSound = (notification) => {
if (notification.app === 'spreed' && notification.objectType === 'call') {
if (loadState('notifications', 'sound_talk')) {
const sound = new Howl({
src: [
generateFilePath('notifications', 'img', 'talk.ogg'),
],
volume: 0.5,
})

sound.play()
}
} else if (loadState('notifications', 'sound_notification')) {
const sound = new Howl({
src: [
generateFilePath('notifications', 'img', 'notification.ogg'),
],
volume: 0.5,
})

sound.play()
}
}

export {
createWebNotification,
}