Page MenuHomePhorge

No OneTemporary

Authored By
Unknown
Size
5 KB
Referenced Files
None
Subscribers
None
diff --git a/src/modules/config.js b/src/modules/config.js
index f23cacb776..1ad9df078d 100644
--- a/src/modules/config.js
+++ b/src/modules/config.js
@@ -1,66 +1,67 @@
import { set, delete as del } from 'vue'
import StyleSetter from '../services/style_setter/style_setter.js'
const browserLocale = (window.navigator.language || 'en').split('-')[0]
const defaultState = {
colors: {},
collapseMessageWithSubject: undefined, // instance default
hideAttachments: false,
hideAttachmentsInConv: false,
hideNsfw: true,
loopVideo: true,
loopVideoSilentOnly: true,
autoLoad: true,
streaming: false,
hoverPreview: true,
pauseOnUnfocused: true,
stopGifs: false,
replyVisibility: 'all',
notificationVisibility: {
follows: true,
mentions: true,
likes: true,
repeats: true
},
+ webPushNotifications: true,
muteWords: [],
highlight: {},
interfaceLanguage: browserLocale,
scopeCopy: undefined, // instance default
subjectLineBehavior: undefined, // instance default
alwaysShowSubjectInput: undefined // instance default
}
const config = {
state: defaultState,
mutations: {
setOption (state, { name, value }) {
set(state, name, value)
},
setHighlight (state, { user, color, type }) {
const data = this.state.config.highlight[user]
if (color || type) {
set(state.highlight, user, { color: color || data.color, type: type || data.type })
} else {
del(state.highlight, user)
}
}
},
actions: {
setHighlight ({ commit, dispatch }, { user, color, type }) {
commit('setHighlight', {user, color, type})
},
setOption ({ commit, dispatch }, { name, value }) {
commit('setOption', {name, value})
switch (name) {
case 'theme':
StyleSetter.setPreset(value, commit)
break
case 'customTheme':
StyleSetter.setColors(value, commit)
}
}
}
}
export default config
diff --git a/src/services/push/push.js b/src/services/push/push.js
index 7d99648aec..2895494889 100644
--- a/src/services/push/push.js
+++ b/src/services/push/push.js
@@ -1,104 +1,107 @@
-
function urlBase64ToUint8Array (base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4)
const base64 = (base64String + padding)
.replace(/-/g, '+')
.replace(/_/g, '/')
const rawData = window.atob(base64)
return Uint8Array.from([...rawData].map((char) => char.charCodeAt(0)))
}
function isPushSupported () {
return 'serviceWorker' in navigator && 'PushManager' in window
}
function registerServiceWorker () {
return navigator.serviceWorker.register('/static/sw.js')
.then(function (registration) {
console.log('Service worker successfully registered.')
return registration
})
.catch(function (err) {
console.error('Unable to register service worker.', err)
})
}
function askPermission () {
return new Promise(function (resolve, reject) {
if (!window.Notification) return reject(new Error('Notifications disabled'))
if (window.Notification.permission !== 'default') {
return resolve(window.Notification.permission)
}
const permissionResult = window.Notification.requestPermission(function (result) {
resolve(result)
})
if (permissionResult) permissionResult.then(resolve, reject)
}).then(function (permissionResult) {
if (permissionResult !== 'granted') {
throw new Error('We weren\'t granted permission.')
}
return permissionResult
})
}
function subscribe (registration, store) {
+ if (!store.rootState.config.webPushNotifications) {
+ return Promise.reject(new Error('Web Push is disabled in config'))
+ }
+
if (!store.rootState.instance.vapidPublicKey) {
return Promise.reject(new Error('VAPID publick key is not found'))
}
const subscribeOptions = {
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(store.rootState.instance.vapidPublicKey)
}
return registration.pushManager.subscribe(subscribeOptions)
}
function sendSubscriptionToBackEnd (subscription, store) {
return window.fetch('/api/v1/push/subscription/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${store.rootState.oauth.token}`
},
body: JSON.stringify({
subscription,
data: {
alerts: {
follow: true,
favourite: true,
mention: true,
reblog: true
}
}
})
})
.then(function (response) {
if (!response.ok) {
throw new Error('Bad status code from server.')
}
return response.json()
})
.then(function (responseData) {
if (!responseData.id) {
throw new Error('Bad response from server.')
}
return responseData
})
}
export default function registerPushNotifications (store) {
if (isPushSupported()) {
registerServiceWorker()
.then(function (registration) {
return askPermission()
.then(() => subscribe(registration, store))
.then((subscription) => sendSubscriptionToBackEnd(subscription, store))
.catch((e) => console.warn(`Failed to setup Web Push Notifications: ${e.message}`))
})
}
}

File Metadata

Mime Type
text/x-diff
Expires
Sun, Nov 1, 5:54 AM (13 h, 43 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1802606
Default Alt Text
(5 KB)

Event Timeline