Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
pleroma-fe
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Joseph Nuthalapati
pleroma-fe
Commits
73b17d70
Commit
73b17d70
authored
6 years ago
by
minibikini
Browse files
Options
Downloads
Patches
Plain Diff
improve push notifications code
parent
11716a7a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/modules/pushNotifications.js
+11
-4
11 additions, 4 deletions
src/modules/pushNotifications.js
src/services/push/push.js
+22
-43
22 additions, 43 deletions
src/services/push/push.js
static/sw.js
+15
-18
15 additions, 18 deletions
static/sw.js
with
48 additions
and
65 deletions
src/modules/pushNotifications.js
+
11
−
4
View file @
73b17d70
...
...
@@ -12,16 +12,23 @@ const subscribe = {
setVapidPublicKey
(
state
,
vapidPublicKey
)
{
state
.
vapidPublicKey
=
vapidPublicKey
}
},
actions
:
{
setInstanceOption
(
store
,
{
name
,
value
})
{
store
.
commit
(
'
setVapidPublicKey
'
,
value
)
if
(
store
.
state
.
token
)
registerPushNotifications
(
this
)
if
(
name
===
'
vapidPublicKey
'
)
{
store
.
commit
(
'
setVapidPublicKey
'
,
value
)
if
(
store
.
state
.
token
)
{
registerPushNotifications
(
store
.
rootState
.
config
.
webPushNotifications
,
value
,
store
.
state
.
token
)
}
}
},
setCurrentUser
(
store
,
user
)
{
store
.
commit
(
'
setApiToken
'
,
user
.
credentials
)
if
(
store
.
state
.
vapidPublicKey
)
registerPushNotifications
(
this
)
if
(
store
.
state
.
vapidPublicKey
)
{
registerPushNotifications
(
store
.
rootState
.
config
.
webPushNotifications
,
store
.
state
.
vapidPublicKey
,
user
.
credentials
)
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
src/services/push/push.js
+
22
−
43
View file @
73b17d70
...
...
@@ -14,58 +14,42 @@ function isPushSupported () {
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
)
})
.
catch
((
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
'
))
return
new
Promise
((
resolve
,
reject
)
=>
{
const
Notification
=
window
.
Notification
if
(
window
.
Notification
.
permission
!==
'
default
'
)
{
return
resolve
(
window
.
Notification
.
permission
)
}
if
(
!
Notification
)
return
reject
(
new
Error
(
'
Notifications disabled
'
))
if
(
Notification
.
permission
!==
'
default
'
)
return
resolve
(
Notification
.
permission
)
const
permissionResult
=
window
.
Notification
.
requestPermission
(
function
(
result
)
{
resolve
(
result
)
})
const
permissionResult
=
Notification
.
requestPermission
(
resolve
)
if
(
permissionResult
)
permissionResult
.
then
(
resolve
,
reject
)
}).
then
(
function
(
permissionResult
)
{
if
(
permissionResult
!==
'
granted
'
)
{
throw
new
Error
(
'
We weren
\'
t granted permission.
'
)
}
}).
then
((
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 public key is not found
'
))
}
function
subscribe
(
registration
,
isEnabled
,
vapidPublicKey
)
{
if
(
!
isEnabled
)
return
Promise
.
reject
(
new
Error
(
'
Web Push is disabled in config
'
))
if
(
!
vapidPublicKey
)
return
Promise
.
reject
(
new
Error
(
'
VAPID public key is not found
'
))
const
subscribeOptions
=
{
userVisibleOnly
:
true
,
applicationServerKey
:
urlBase64ToUint8Array
(
store
.
rootState
.
instance
.
vapidPublicKey
)
applicationServerKey
:
urlBase64ToUint8Array
(
vapidPublicKey
)
}
return
registration
.
pushManager
.
subscribe
(
subscribeOptions
)
}
function
sendSubscriptionToBackEnd
(
subscription
,
s
to
re
)
{
function
sendSubscriptionToBackEnd
(
subscription
,
to
ken
)
{
return
window
.
fetch
(
'
/api/v1/push/subscription/
'
,
{
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
,
'
Authorization
'
:
`Bearer
${
store
.
rootState
.
oauth
.
token
}
`
'
Authorization
'
:
`Bearer
${
token
}
`
},
body
:
JSON
.
stringify
({
subscription
,
...
...
@@ -79,28 +63,23 @@ function sendSubscriptionToBackEnd (subscription, store) {
}
})
})
.
then
(
function
(
response
)
{
if
(
!
response
.
ok
)
{
throw
new
Error
(
'
Bad status code from server.
'
)
}
.
then
((
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.
'
)
}
.
then
((
responseData
)
=>
{
if
(
!
responseData
.
id
)
throw
new
Error
(
'
Bad response from server.
'
)
return
responseData
})
}
export
default
function
registerPushNotifications
(
store
)
{
export
default
function
registerPushNotifications
(
isEnabled
,
vapidPublicKey
,
token
)
{
if
(
isPushSupported
())
{
registerServiceWorker
()
.
then
(
function
(
registration
)
{
.
then
((
registration
)
=>
{
return
askPermission
()
.
then
(()
=>
subscribe
(
registration
,
store
))
.
then
((
subscription
)
=>
sendSubscriptionToBackEnd
(
subscription
,
s
to
re
))
.
then
(()
=>
subscribe
(
registration
,
isEnabled
,
vapidPublicKey
))
.
then
((
subscription
)
=>
sendSubscriptionToBackEnd
(
subscription
,
to
ken
))
.
catch
((
e
)
=>
console
.
warn
(
`Failed to setup Web Push Notifications:
${
e
.
message
}
`
))
})
}
...
...
This diff is collapsed.
Click to expand it.
static/sw.js
+
15
−
18
View file @
73b17d70
/* eslint-env serviceworker */
self
.
addEventListener
(
'
push
'
,
function
(
event
)
{
if
(
event
.
data
)
{
const
data
=
event
.
data
.
json
()
const
promiseChain
=
clients
.
matchAll
({
includeUncontrolled
:
true
}).
then
(
function
(
clientList
)
{
const
list
=
clientList
.
filter
((
item
)
=>
item
.
type
===
'
window
'
)
if
(
list
.
length
)
return
return
self
.
registration
.
showNotification
(
data
.
title
,
data
)
})
function
getWindowClients
()
{
return
clients
.
matchAll
({
includeUncontrolled
:
true
})
.
then
((
clientList
)
=>
clientList
.
filter
(({
type
})
=>
type
===
'
window
'
))
}
self
.
addEventListener
(
'
push
'
,
(
event
)
=>
{
if
(
event
.
data
)
{
event
.
waitUntil
(
getWindowClients
().
then
((
list
)
=>
{
const
data
=
event
.
data
.
json
()
event
.
waitUntil
(
promiseChain
)
if
(
list
.
length
===
0
)
return
self
.
registration
.
showNotification
(
data
.
title
,
data
)
}))
}
})
self
.
addEventListener
(
'
notificationclick
'
,
function
(
event
)
{
self
.
addEventListener
(
'
notificationclick
'
,
(
event
)
=>
{
event
.
notification
.
close
()
event
.
waitUntil
(
clients
.
matchAll
({
includeUncontrolled
:
true
}).
then
(
function
(
clientList
)
{
const
list
=
clientList
.
filter
((
item
)
=>
item
.
type
===
'
window
'
)
event
.
waitUntil
(
getWindowClients
().
then
((
list
)
=>
{
for
(
var
i
=
0
;
i
<
list
.
length
;
i
++
)
{
var
client
=
list
[
i
]
if
(
client
.
url
===
'
/
'
&&
'
focus
'
in
client
)
{
return
client
.
focus
()
}
}
if
(
clients
.
openWindow
)
{
return
clients
.
openWindow
(
'
/
'
)
}
if
(
clients
.
openWindow
)
return
clients
.
openWindow
(
'
/
'
)
}))
})
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment