-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add REST API for Web Push Notifications subscriptions (#7445)
- POST /api/v1/push/subscription - PUT /api/v1/push/subscription - DELETE /api/v1/push/subscription - New OAuth scope: "push" (required for the above methods)
- Loading branch information
Showing
20 changed files
with
258 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
class Api::V1::Push::SubscriptionsController < Api::BaseController | ||
before_action -> { doorkeeper_authorize! :push } | ||
before_action :require_user! | ||
before_action :set_web_push_subscription | ||
|
||
def create | ||
@web_subscription&.destroy! | ||
|
||
@web_subscription = ::Web::PushSubscription.create!( | ||
endpoint: subscription_params[:endpoint], | ||
key_p256dh: subscription_params[:keys][:p256dh], | ||
key_auth: subscription_params[:keys][:auth], | ||
data: data_params, | ||
user_id: current_user.id, | ||
access_token_id: doorkeeper_token.id | ||
) | ||
|
||
render json: @web_subscription, serializer: REST::WebPushSubscriptionSerializer | ||
end | ||
|
||
def update | ||
raise ActiveRecord::RecordNotFound if @web_subscription.nil? | ||
|
||
@web_subscription.update!(data: data_params) | ||
|
||
render json: @web_subscription, serializer: REST::WebPushSubscriptionSerializer | ||
end | ||
|
||
def destroy | ||
@web_subscription&.destroy! | ||
render_empty | ||
end | ||
|
||
private | ||
|
||
def set_web_push_subscription | ||
@web_subscription = ::Web::PushSubscription.find_by(access_token_id: doorkeeper_token.id) | ||
end | ||
|
||
def subscription_params | ||
params.require(:subscription).permit(:endpoint, keys: [:auth, :p256dh]) | ||
end | ||
|
||
def data_params | ||
return {} if params[:data].blank? | ||
params.require(:data).permit(alerts: [:follow, :favourite, :reblog, :mention]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
class REST::WebPushSubscriptionSerializer < ActiveModel::Serializer | ||
attributes :id, :endpoint, :alerts, :server_key | ||
|
||
def alerts | ||
object.data&.dig('alerts') || {} | ||
end | ||
|
||
def server_key | ||
Rails.configuration.x.vapid_public_key | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
class Web::PushNotificationWorker | ||
include Sidekiq::Worker | ||
|
||
sidekiq_options backtrace: true | ||
|
||
def perform(subscription_id, notification_id) | ||
subscription = ::Web::PushSubscription.find(subscription_id) | ||
notification = Notification.find(notification_id) | ||
|
||
subscription.push(notification) unless notification.activity.nil? | ||
rescue Webpush::InvalidSubscription, Webpush::ExpiredSubscription | ||
subscription.destroy! | ||
rescue ActiveRecord::RecordNotFound | ||
true | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
db/migrate/20180510214435_add_access_token_id_to_web_push_subscriptions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddAccessTokenIdToWebPushSubscriptions < ActiveRecord::Migration[5.2] | ||
def change | ||
add_reference :web_push_subscriptions, :access_token, null: true, default: nil, foreign_key: { on_delete: :cascade, to_table: :oauth_access_tokens }, index: false | ||
add_reference :web_push_subscriptions, :user, null: true, default: nil, foreign_key: { on_delete: :cascade }, index: false | ||
end | ||
end |
13 changes: 13 additions & 0 deletions
13
db/migrate/20180510230049_migrate_web_push_subscriptions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class MigrateWebPushSubscriptions < ActiveRecord::Migration[5.2] | ||
disable_ddl_transaction! | ||
|
||
def up | ||
add_index :web_push_subscriptions, :user_id, algorithm: :concurrently | ||
add_index :web_push_subscriptions, :access_token_id, algorithm: :concurrently | ||
end | ||
|
||
def down | ||
remove_index :web_push_subscriptions, :user_id | ||
remove_index :web_push_subscriptions, :access_token_id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.