From 752cf1506b55dac449cb67070204b0cf4be28405 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Thu, 20 Oct 2022 19:02:12 +0200 Subject: [PATCH] Add settings page for push notifications --- app/controllers/ajax/web_push_controller.rb | 2 ++ .../settings/push_notifications_controller.rb | 9 +++++++++ .../retrospring/features/webpush/enable.ts | 2 +- app/javascript/styles/application.scss | 1 + .../styles/components/_push-settings.scss | 7 +++++++ app/views/settings/_push_notifications.haml | 17 +++++++++++++++++ .../settings/push_notifications/index.haml | 4 ++++ app/views/tabs/_settings.html.haml | 1 + config/locales/views.en.yml | 14 ++++++++++++++ config/routes.rb | 2 ++ 10 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 app/controllers/settings/push_notifications_controller.rb create mode 100644 app/javascript/styles/components/_push-settings.scss create mode 100644 app/views/settings/_push_notifications.haml create mode 100644 app/views/settings/push_notifications/index.haml diff --git a/app/controllers/ajax/web_push_controller.rb b/app/controllers/ajax/web_push_controller.rb index af4e715f..49d8a497 100644 --- a/app/controllers/ajax/web_push_controller.rb +++ b/app/controllers/ajax/web_push_controller.rb @@ -4,6 +4,8 @@ class Ajax::WebPushController < AjaxController def key certificate = Rpush::Webpush::App.find_by(name: "webpush").certificate + @response[:status] = :okay + @response[:success] = true @response[:key] = JSON.parse(certificate)["public_key"] end diff --git a/app/controllers/settings/push_notifications_controller.rb b/app/controllers/settings/push_notifications_controller.rb new file mode 100644 index 00000000..551ae46e --- /dev/null +++ b/app/controllers/settings/push_notifications_controller.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class Settings::PushNotificationsController < ApplicationController + before_action :authenticate_user! + + def index + @subscriptions = current_user.web_push_subscriptions + end +end diff --git a/app/javascript/retrospring/features/webpush/enable.ts b/app/javascript/retrospring/features/webpush/enable.ts index a17b6f2c..c52ca51c 100644 --- a/app/javascript/retrospring/features/webpush/enable.ts +++ b/app/javascript/retrospring/features/webpush/enable.ts @@ -14,7 +14,7 @@ export function enableHandler (event: Event): void { return; } - post('/ajax/web_push', { + post('/ajax/webpush', { body: { subscription }, diff --git a/app/javascript/styles/application.scss b/app/javascript/styles/application.scss index b5765b62..3500dd56 100644 --- a/app/javascript/styles/application.scss +++ b/app/javascript/styles/application.scss @@ -108,6 +108,7 @@ "components/mobile-nav", "components/notifications", "components/profile", +"components/push-settings", "components/question", "components/smiles", "components/themes", diff --git a/app/javascript/styles/components/_push-settings.scss b/app/javascript/styles/components/_push-settings.scss new file mode 100644 index 00000000..c5d38d94 --- /dev/null +++ b/app/javascript/styles/components/_push-settings.scss @@ -0,0 +1,7 @@ +.push-notifications { + &-unavailable { + body.cap-service-worker.cap-notification & { + display: none; + } + } +} diff --git a/app/views/settings/_push_notifications.haml b/app/views/settings/_push_notifications.haml new file mode 100644 index 00000000..05753739 --- /dev/null +++ b/app/views/settings/_push_notifications.haml @@ -0,0 +1,17 @@ +.card.push-notifications-settings + .card-body + %p= t('.description') + %p= t('.subscription_count', count: subscriptions.count) + + .push-notifications-unavailable.text-danger + %i.fa.fa-warning + = t('.unsupported') + + .push-notifications-current-target.d-none.text-success + %i.fa.fa-check + = t('.current_target') + + .button-group{ role: 'group' } + %button.btn.btn-primary{ data: { action: 'push-enable' } }= t('.subscribe') + %button.btn.btn-primary{ data: { action: 'push-disable' } }= t('.unsubscribe_current') + %button.btn.btn-danger{ data: { action: 'push-remove-all' } }= t('.unsubscribe_all') diff --git a/app/views/settings/push_notifications/index.haml b/app/views/settings/push_notifications/index.haml new file mode 100644 index 00000000..9985eeff --- /dev/null +++ b/app/views/settings/push_notifications/index.haml @@ -0,0 +1,4 @@ += render "settings/push_notifications", subscriptions: @subscriptions + +- provide(:title, generate_title(t(".title"))) +- parent_layout "user/settings" diff --git a/app/views/tabs/_settings.html.haml b/app/views/tabs/_settings.html.haml index 49da5ae5..366aafc1 100644 --- a/app/views/tabs/_settings.html.haml +++ b/app/views/tabs/_settings.html.haml @@ -8,6 +8,7 @@ = list_group_item t(".mutes"), settings_muted_path = list_group_item t(".blocks"), settings_blocks_path = list_group_item t(".theme"), edit_settings_theme_path + = list_group_item t(".push_notifications"), settings_push_notifications_path = list_group_item t(".data"), settings_data_path = list_group_item t(".export"), settings_export_path diff --git a/config/locales/views.en.yml b/config/locales/views.en.yml index 552e7821..0c3aca6c 100644 --- a/config/locales/views.en.yml +++ b/config/locales/views.en.yml @@ -531,6 +531,18 @@ en: body: "Raised content includes all the different boxes and panels you can see across the site." accent: example: "Some text on top of a accented area on a raised element!" + push_notifications: + index: + title: "Push Notifications" + subscription_count: + zero: "You are not currently subscribed to push notifications on any devices." + one: "You are currently receiving push notifications on one device." + other: "You are currently receiving push notifications on %{count} devices." + unsupported: "This browser does not support push notifications." + current_target: "You are currently receiving push notifications on this device." + subscribe: "Enable on this device" + unsubscribe_current: "Disable on this device" + unsubscribe_all: "Disable on all devices" shared: links: about: "About" @@ -594,6 +606,8 @@ en: theme: "Theme" data: "Your Data" export: "Export" + blocks: "Blocks" + push_notifications: "Push Notifications" moderation: inbox: header: diff --git a/config/routes.rb b/config/routes.rb index fe0ef852..c3770c08 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -89,6 +89,8 @@ Rails.application.routes.draw do get :data, to: "data#index" + resources :push_notifications, only: %i[index] + namespace :two_factor_authentication do get :otp_authentication, to: "otp_authentication#index" patch :otp_authentication, to: "otp_authentication#update"