Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable29] fix: separate creation of browser notifications and sounds from Vue rendering #1948

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions js/notifications-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/notifications-main.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/notifications-src_NotificationsApp_vue.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/notifications-src_NotificationsApp_vue.js.map

Large diffs are not rendered by default.

85 changes: 1 addition & 84 deletions src/Components/Notification.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,12 @@
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'

export default {
name: 'Notification',
Expand Down Expand Up @@ -246,50 +243,7 @@
},

mounted() {
this._$el = $(this.$el)

Check warning on line 246 in src/Components/Notification.vue

View workflow job for this annotation

GitHub Actions / NPM lint

The global property or function $ was deprecated in Nextcloud 19.0.0

// Parents: TransitionGroup > Transition > HeaderMenu
if (typeof this.$parent.$parent.$parent.showBrowserNotifications === 'undefined') {
console.error('Failed to read showBrowserNotifications property from App component')
}

if (this.$parent.$parent.$parent.backgroundFetching) {
// Can not rely on showBrowserNotifications because each tab should
// be able to utilize the data from the notification in events.
const event = {
notification: this.$props,
}

emit('notifications:notification:received', event)
}

if (this.shouldNotify
&& 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()
}
}
},

methods: {
Expand Down Expand Up @@ -337,43 +291,6 @@
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
19 changes: 18 additions & 1 deletion src/NotificationsApp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
import Close from 'vue-material-design-icons/Close.vue'
import axios from '@nextcloud/axios'
import { getCurrentUser } from '@nextcloud/auth'
import { subscribe, unsubscribe } from '@nextcloud/event-bus'
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
import { showError } from '@nextcloud/dialogs'
import { loadState } from '@nextcloud/initial-state'
import {
Expand All @@ -113,6 +113,7 @@
import NcEmptyContent from '@nextcloud/vue/dist/Components/NcEmptyContent.js'
import { getCapabilities } from '@nextcloud/capabilities'
import NcHeaderMenu from '@nextcloud/vue/dist/Components/NcHeaderMenu.js'
import { createWebNotification } from './services/webNotificationsService.js'

export default {
name: 'NotificationsApp',
Expand Down Expand Up @@ -229,12 +230,12 @@

mounted() {
this.tabId = OC.requestToken || ('' + Math.random())
this._$icon = $(this.$refs.icon)

Check warning on line 233 in src/NotificationsApp.vue

View workflow job for this annotation

GitHub Actions / NPM lint

The global property or function $ was deprecated in Nextcloud 19.0.0
this._oldcount = 0

// Bind the button click event
console.debug('Registering notifications container as a menu')
OC.registerMenu($(this.$refs.button), $(this.$refs.container), undefined, true)

Check warning on line 238 in src/NotificationsApp.vue

View workflow job for this annotation

GitHub Actions / NPM lint

The global property or function $ was deprecated in Nextcloud 19.0.0

Check warning on line 238 in src/NotificationsApp.vue

View workflow job for this annotation

GitHub Actions / NPM lint

The global property or function $ was deprecated in Nextcloud 19.0.0

this.checkWebNotificationPermissions()

Expand Down Expand Up @@ -394,6 +395,7 @@
this.lastETag = response.headers.etag
this.lastTabId = response.tabId
this.notifications = response.data
this.processWebNotifications(response.data)
console.debug('Got notification data, restoring default polling interval.')
this._setPollingInterval(this.pollIntervalBase)
this._updateDocTitleOnNewNotifications(this.notifications)
Expand Down Expand Up @@ -510,6 +512,21 @@
this.webNotificationsGranted = permissions === 'granted'
})
},

processWebNotifications(notifications) {
notifications.forEach(notification => {
if (this.backgroundFetching) {
// Can not rely on showBrowserNotifications because each tab should
// be able to utilize the data from the notification in events.
const event = { notification }
emit('notifications:notification:received', event)
}

if (this.showBrowserNotifications && this.webNotificationsThresholdId < notification.notificationId) {
createWebNotification(notification)
}
})
},
},
}
</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,
}
Loading