-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Web Push Notifications #3243
Web Push Notifications #3243
Changes from 71 commits
eebfcd4
e2de68b
ea121eb
1574e0a
10cc5c0
3d8a853
d54e077
fc066d8
5439e88
5e1aca8
78e469c
28680db
20a6590
31ecfe6
0e96635
48c9a48
ca4bd93
cb80ab7
e62a088
ab3ebba
2869304
1a9e58f
551294d
74b623a
709783b
2e2f374
eab8055
aa1fbfb
2def72c
e854402
85063dc
1178d66
cd32e56
1654ec4
f994b6c
7faf799
b040b03
74655b1
b2a77fd
2b12685
02643dc
2fe1fff
7a73ab7
9d0718f
aefab25
c94097b
7397140
34be2b4
42bb7ee
7856c72
896d46b
7510c90
8956a9d
f2edbe4
0b7a014
1075327
44c5780
53ef817
4cce3b7
af5aea3
d57696f
7583c0f
b9a6113
e0d9ee5
7c3442b
79eba27
8bcb9b4
9c3c5b2
3969c85
52189e3
b40dced
a847236
bf3397d
0bf308a
be5ea12
bccb6e7
44785e0
28a2c0f
99a482b
e3e79f3
c4c0a7a
580fea4
5e6132d
5067d70
787279a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
class Api::Web::PushSubscriptionsController < Api::BaseController | ||
respond_to :json | ||
|
||
before_action :require_user! | ||
|
||
def create | ||
active_session = current_session | ||
|
||
unless active_session.web_push_subscription.nil? | ||
active_session.web_push_subscription.destroy! | ||
active_session.web_push_subscription = nil | ||
active_session.save! | ||
end | ||
|
||
web_subscription = ::Web::PushSubscription.new( | ||
endpoint: params[:data][:endpoint], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not |
||
key_p256dh: params[:data][:keys][:p256dh], | ||
key_auth: params[:data][:keys][:auth] | ||
) | ||
|
||
web_subscription.save! | ||
|
||
active_session.web_push_subscription = web_subscription | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as |
||
active_session.save! | ||
|
||
render json: web_subscription.as_payload | ||
end | ||
|
||
def update | ||
web_subscription = ::Web::PushSubscription.find(params[:id]) | ||
|
||
web_subscription.data = params[:data] | ||
web_subscription.save! | ||
|
||
render json: web_subscription.as_payload | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import axios from 'axios'; | ||
|
||
export const SET_BROWSER_SUPPORT = 'PUSH_NOTIFICATIONS_SET_BROWSER_SUPPORT'; | ||
export const SET_SUBSCRIPTION = 'PUSH_NOTIFICATIONS_SET_SUBSCRIPTION'; | ||
export const CLEAR_SUBSCRIPTION = 'PUSH_NOTIFICATIONS_CLEAR_SUBSCRIPTION'; | ||
export const ALERTS_CHANGE = 'PUSH_NOTIFICATIONS_ALERTS_CHANGE'; | ||
|
||
export function setBrowserSupport (value) { | ||
return { | ||
type: SET_BROWSER_SUPPORT, | ||
value, | ||
}; | ||
} | ||
|
||
export function setSubscription (subscription) { | ||
return { | ||
type: SET_SUBSCRIPTION, | ||
subscription, | ||
}; | ||
} | ||
|
||
export function clearSubscription () { | ||
return { | ||
type: CLEAR_SUBSCRIPTION, | ||
}; | ||
} | ||
|
||
export function changeAlerts(key, value) { | ||
return dispatch => { | ||
dispatch({ | ||
type: ALERTS_CHANGE, | ||
key, | ||
value, | ||
}); | ||
|
||
dispatch(saveSettings()); | ||
}; | ||
} | ||
|
||
export function saveSettings() { | ||
return (_, getState) => { | ||
const state = getState().get('push_notifications'); | ||
const subscription = state.get('subscription'); | ||
const alerts = state.get('alerts'); | ||
|
||
axios.put(`/api/web/push_subscriptions/${subscription.get('id')}`, { | ||
data: { | ||
alerts, | ||
}, | ||
}); | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { STORE_HYDRATE } from '../actions/store'; | ||
import { SET_BROWSER_SUPPORT, SET_SUBSCRIPTION, CLEAR_SUBSCRIPTION, ALERTS_CHANGE } from '../actions/push_notifications'; | ||
import Immutable from 'immutable'; | ||
|
||
const initialState = Immutable.Map({ | ||
subscription: null, | ||
alerts: new Immutable.Map({ | ||
follow: false, | ||
favourite: false, | ||
reblog: false, | ||
mention: false, | ||
}), | ||
isSubscribed: false, | ||
browserSupport: false, | ||
}); | ||
|
||
export default function push_subscriptions(state = initialState, action) { | ||
switch(action.type) { | ||
case STORE_HYDRATE: { | ||
const push_subscription = action.state.get('push_subscription'); | ||
|
||
console.log('Setting push subscription from the backend:', push_subscription && push_subscription.toJS()); | ||
|
||
if (push_subscription) { | ||
return state | ||
.set('subscription', new Immutable.Map({ | ||
id: push_subscription.get('id'), | ||
endpoint: push_subscription.get('endpoint'), | ||
})) | ||
.set('alerts', push_subscription.get('alerts') || initialState.get('alerts')) | ||
.set('isSubscribed', true); | ||
} | ||
|
||
return state; | ||
} | ||
case SET_SUBSCRIPTION: | ||
return state | ||
.set('subscription', new Immutable.Map({ | ||
id: action.subscription.id, | ||
endpoint: action.subscription.endpoint, | ||
})) | ||
.set('alerts', new Immutable.Map(action.subscription.alerts)) | ||
.set('isSubscribed', true); | ||
case SET_BROWSER_SUPPORT: | ||
return state.set('browserSupport', action.value); | ||
case CLEAR_SUBSCRIPTION: | ||
return initialState; | ||
case ALERTS_CHANGE: | ||
return state.setIn(action.key, action.value); | ||
default: | ||
return state; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import './web_push_notifications'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
create!
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, does not seem to work, as this is called from an endpoint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the problem with calling create from an endpoint?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, think I misunderstood the comment. I took it as to change the name of the method from
create
tocreate!
. Will update it after I fix the conflicts 🙄