2018-08-16 22:30:59 +00:00
|
|
|
/*
|
2021-02-04 22:04:35 +00:00
|
|
|
* Copyright (c) 2010-2021 Tim Düsterhus.
|
2018-08-16 22:30:59 +00:00
|
|
|
*
|
|
|
|
* Use of this software is governed by the Business Source License
|
|
|
|
* included in the LICENSE file.
|
|
|
|
*
|
2023-02-22 16:45:50 +00:00
|
|
|
* Change Date: 2027-02-22
|
2018-08-16 22:30:59 +00:00
|
|
|
*
|
|
|
|
* On the date above, in accordance with the Business Source
|
|
|
|
* License, use of this software will be governed by version 2
|
|
|
|
* or later of the General Public License.
|
|
|
|
*/
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
define(['WoltLabSuite/Core/Language'], function (Language) {
|
|
|
|
'use strict'
|
2018-08-16 22:30:59 +00:00
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
const DEPENDENCIES = ['ProfileStore']
|
2018-08-16 22:30:59 +00:00
|
|
|
class Notification {
|
|
|
|
constructor(profileStore) {
|
|
|
|
this.profileStore = profileStore
|
|
|
|
|
|
|
|
this.unread = 0
|
|
|
|
this.active = true
|
|
|
|
this.browserTitle = document.title
|
|
|
|
this.systemEnabled = false
|
|
|
|
this.lastSeen = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
bootstrap() {
|
2020-11-01 16:41:19 +00:00
|
|
|
document.addEventListener(
|
|
|
|
'visibilitychange',
|
|
|
|
this.onVisibilitychange.bind(this)
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get systemSupported() {
|
2020-11-01 16:41:19 +00:00
|
|
|
return 'Notification' in window
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get systemDenied() {
|
|
|
|
return window.Notification.permission === 'denied'
|
|
|
|
}
|
|
|
|
|
|
|
|
get systemGranted() {
|
|
|
|
if (this.systemDenied) {
|
2020-11-01 16:41:19 +00:00
|
|
|
console.warn(
|
|
|
|
'[Notification]',
|
|
|
|
'System Notifications: permission denied'
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return window.Notification.permission === 'granted'
|
|
|
|
}
|
|
|
|
|
|
|
|
onVisibilitychange() {
|
|
|
|
this.active = !document.hidden
|
|
|
|
|
|
|
|
if (this.active) {
|
|
|
|
this.unread = 0
|
|
|
|
this.updateBrowserTitle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ingest(messages) {
|
|
|
|
if (!this.active) {
|
2020-11-01 16:41:19 +00:00
|
|
|
messages.forEach((message) => {
|
2018-08-16 22:30:59 +00:00
|
|
|
const body = message.getMessageType().renderPlainText(message)
|
|
|
|
|
|
|
|
if (body === false) return
|
|
|
|
if (message.messageID < this.lastSeen) return
|
|
|
|
|
|
|
|
this.lastSeen = message.messageID
|
|
|
|
this.unread++
|
|
|
|
|
|
|
|
if (this.systemEnabled && this.systemGranted) {
|
|
|
|
// The user information is guaranteed to be cached at this point
|
|
|
|
const user = this.profileStore.get(message.userID)
|
|
|
|
const title = Language.get('chat.notification.title', { message })
|
2020-11-01 16:41:19 +00:00
|
|
|
const options = { body, icon: user.imageUrl, badge: user.imageUrl }
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
const notification = new window.Notification(title, options)
|
|
|
|
|
|
|
|
setTimeout(notification.close.bind(notification), 5e3)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateBrowserTitle()
|
|
|
|
}
|
|
|
|
|
|
|
|
updateBrowserTitle() {
|
|
|
|
if (this.unread > 0) {
|
|
|
|
document.title = `(${this.unread}) ${this.browserTitle}`
|
2020-11-01 16:41:19 +00:00
|
|
|
} else {
|
2018-08-16 22:30:59 +00:00
|
|
|
document.title = this.browserTitle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enableSystemNotifications() {
|
2020-11-01 16:41:19 +00:00
|
|
|
if (!this.systemSupported)
|
|
|
|
return Promise.reject(new Error('Notifications are not supported'))
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
if (this.systemGranted) {
|
|
|
|
this.systemEnabled = true
|
|
|
|
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-11-01 16:41:19 +00:00
|
|
|
window.Notification.requestPermission((permission) => {
|
2018-08-16 22:30:59 +00:00
|
|
|
this.systemEnabled = permission === 'granted'
|
|
|
|
|
|
|
|
if (this.systemEnabled) {
|
|
|
|
resolve()
|
2020-11-01 16:41:19 +00:00
|
|
|
} else {
|
2018-08-16 22:30:59 +00:00
|
|
|
reject(new Error(permission))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
disableSystemNotifications() {
|
|
|
|
this.systemEnabled = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Notification.DEPENDENCIES = DEPENDENCIES
|
|
|
|
|
|
|
|
return Notification
|
2020-11-01 16:41:19 +00:00
|
|
|
})
|