From 3de376650c700be81d893554b90ac8ce27646333 Mon Sep 17 00:00:00 2001 From: Fire Demon Date: Wed, 28 Oct 2020 21:22:04 -0500 Subject: Make push notifications optional --- app/controllers/settings/preferences_controller.rb | 1 + app/javascript/flavours/glitch/actions/notifications.js | 7 ++++++- .../glitch/actions/push_notifications/registerer.js | 4 +++- .../components/notifications_permission_banner.js | 7 ++++++- .../flavours/glitch/styles/components/columns.scss | 14 ++++++++------ app/javascript/flavours/glitch/util/initial_state.js | 2 ++ app/javascript/mastodon/locales/en-MP.json | 4 ++++ app/lib/user_settings_decorator.rb | 5 +++++ app/models/user.rb | 2 +- app/serializers/initial_state_serializer.rb | 1 + .../settings/preferences/notifications/show.html.haml | 5 +++++ config/locales/en-MP.yml | 2 ++ config/locales/simple_form.en-MP.yml | 2 ++ 13 files changed, 46 insertions(+), 10 deletions(-) diff --git a/app/controllers/settings/preferences_controller.rb b/app/controllers/settings/preferences_controller.rb index 3d6696fc4..189d48af6 100644 --- a/app/controllers/settings/preferences_controller.rb +++ b/app/controllers/settings/preferences_controller.rb @@ -79,6 +79,7 @@ class Settings::PreferencesController < Settings::BaseController :setting_home_reblogs, :setting_max_history_public, :setting_max_history_private, + :setting_web_push, notification_emails: %i(follow follow_request reblog favourite mention digest report pending_account trending_tag), interactions: %i(must_be_follower must_be_following must_be_following_dm) ) diff --git a/app/javascript/flavours/glitch/actions/notifications.js b/app/javascript/flavours/glitch/actions/notifications.js index eb7087027..583cc87a5 100644 --- a/app/javascript/flavours/glitch/actions/notifications.js +++ b/app/javascript/flavours/glitch/actions/notifications.js @@ -13,7 +13,7 @@ import { defineMessages } from 'react-intl'; import { List as ImmutableList } from 'immutable'; import { unescapeHTML } from 'flavours/glitch/util/html'; import { getFiltersRegex } from 'flavours/glitch/selectors'; -import { usePendingItems as preferPendingItems } from 'flavours/glitch/util/initial_state'; +import { usePendingItems as preferPendingItems, webPushEnabled } from 'flavours/glitch/util/initial_state'; import compareId from 'flavours/glitch/util/compare_id'; import { searchTextFromRawStatus } from 'flavours/glitch/actions/importer/normalizer'; import { requestNotificationPermission } from 'flavours/glitch/util/notifications'; @@ -336,6 +336,11 @@ export function markNotificationsAsRead() { // Browser support export function setupBrowserNotifications() { return dispatch => { + if (!webPushEnabled) { + dispatch(setBrowserSupport(false)); + return; + } + dispatch(setBrowserSupport('Notification' in window)); if ('Notification' in window) { dispatch(setBrowserPermission(Notification.permission)); diff --git a/app/javascript/flavours/glitch/actions/push_notifications/registerer.js b/app/javascript/flavours/glitch/actions/push_notifications/registerer.js index 8fdb239f7..1f9ebe569 100644 --- a/app/javascript/flavours/glitch/actions/push_notifications/registerer.js +++ b/app/javascript/flavours/glitch/actions/push_notifications/registerer.js @@ -1,7 +1,9 @@ import api from 'flavours/glitch/util/api'; +import { webPushEnabled } from 'flavours/glitch/util/initial_state'; import { pushNotificationsSetting } from 'flavours/glitch/util/settings'; import { setBrowserSupport, setSubscription, clearSubscription } from './setter'; + // Taken from https://www.npmjs.com/package/web-push const urlBase64ToUint8Array = (base64String) => { const padding = '='.repeat((4 - base64String.length % 4) % 4); @@ -49,7 +51,7 @@ const sendSubscriptionToBackend = (getState, subscription, me) => { }; // Last one checks for payload support: https://web-push-book.gauntface.com/chapter-06/01-non-standards-browsers/#no-payload -const supportsPushNotifications = ('serviceWorker' in navigator && 'PushManager' in window && 'getKey' in PushSubscription.prototype); +const supportsPushNotifications = (webPushEnabled && 'serviceWorker' in navigator && 'PushManager' in window && 'getKey' in PushSubscription.prototype); export function register () { return (dispatch, getState) => { diff --git a/app/javascript/flavours/glitch/features/notifications/components/notifications_permission_banner.js b/app/javascript/flavours/glitch/features/notifications/components/notifications_permission_banner.js index 8e77f5a03..0ba929711 100644 --- a/app/javascript/flavours/glitch/features/notifications/components/notifications_permission_banner.js +++ b/app/javascript/flavours/glitch/features/notifications/components/notifications_permission_banner.js @@ -17,12 +17,17 @@ class NotificationsPermissionBanner extends React.PureComponent { this.props.dispatch(requestBrowserPermission()); } + handleClickDisable = () => { + window.location = '/settings/preferences/notifications'; + } + render () { return (

}} />

- + +
); } diff --git a/app/javascript/flavours/glitch/styles/components/columns.scss b/app/javascript/flavours/glitch/styles/components/columns.scss index be32ae52e..1109f17ea 100644 --- a/app/javascript/flavours/glitch/styles/components/columns.scss +++ b/app/javascript/flavours/glitch/styles/components/columns.scss @@ -696,15 +696,12 @@ } .notifications-permission-banner { - padding: 30px; + padding: 14px; border-bottom: 1px solid lighten($ui-base-color, 8%); - display: flex; - flex-direction: column; - align-items: center; - justify-content: center; + text-align: center; h2 { - font-size: 16px; + font-size: 14px; font-weight: 500; margin-bottom: 15px; text-align: center; @@ -715,4 +712,9 @@ margin-bottom: 15px; text-align: center; } + + .button { + margin-left: 7px; + margin-right: 7px; + } } diff --git a/app/javascript/flavours/glitch/util/initial_state.js b/app/javascript/flavours/glitch/util/initial_state.js index 911468e6f..05868970c 100644 --- a/app/javascript/flavours/glitch/util/initial_state.js +++ b/app/javascript/flavours/glitch/util/initial_state.js @@ -11,6 +11,8 @@ const initialState = element && function () { const getMeta = (prop) => initialState && initialState.meta && initialState.meta[prop]; +export const webPushEnabled = getMeta('web_push') === true; + export const reduceMotion = getMeta('reduce_motion'); export const autoPlayGif = getMeta('auto_play_gif'); export const displaySensitiveMedia = getMeta('display_sensitive_media'); diff --git a/app/javascript/mastodon/locales/en-MP.json b/app/javascript/mastodon/locales/en-MP.json index 3f2ffb62b..3e021a5c1 100644 --- a/app/javascript/mastodon/locales/en-MP.json +++ b/app/javascript/mastodon/locales/en-MP.json @@ -102,6 +102,10 @@ "notifications.clear": "Clear notifications", "notifications.column_settings.favourite": "Admirations:", "notifications.filter.favourites": "Admirations", + "notifications_permission_banner.title": "You have desktop notifications enabled.", + "notifications_permission_banner.enable": "Request", + "notifications_permission_banner.disable": "Disable", + "notifications_permission_banner.how_to_control": "Permission is needed from your Web browser to use this feature.", "poll.total_people": "{count, plural, one {# creature} other {# creatures}}", "privacy.change": "Adjust roar privacy", "privacy.direct.long": "Visible for mentioned creatures only", diff --git a/app/lib/user_settings_decorator.rb b/app/lib/user_settings_decorator.rb index b87635dbc..22601ecaa 100644 --- a/app/lib/user_settings_decorator.rb +++ b/app/lib/user_settings_decorator.rb @@ -66,6 +66,7 @@ class UserSettingsDecorator user.settings['home_reblogs'] = home_reblogs_preference if change?('setting_home_reblogs') user.settings['max_history_public'] = max_history_public_preference if change?('setting_max_history_public') user.settings['max_history_private'] = max_history_private_preference if change?('setting_max_history_private') + user.settings['web_push'] = web_push_preferences if change?('setting_web_push') end def merged_notification_emails @@ -256,6 +257,10 @@ class UserSettingsDecorator settings['setting_max_history_private'].to_i end + def web_push_preferences + boolean_cast_setting 'setting_web_push' + end + def boolean_cast_setting(key) ActiveModel::Type::Boolean.new.cast(settings[key]) end diff --git a/app/models/user.rb b/app/models/user.rb index d887f44fe..b401e3346 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -125,7 +125,7 @@ class User < ApplicationRecord :style_wide_media, :publish_in, :unpublish_in, :unpublish_delete, :boost_every, :boost_jitter, :boost_random, :unpublish_on_delete, :rss_disabled, :home_reblogs, - :filter_unknown, :max_history_public, :max_history_private, + :filter_unknown, :max_history_public, :max_history_private, :web_push, to: :settings, prefix: :setting, allow_nil: false attr_reader :invite_code, :sign_in_token_attempt diff --git a/app/serializers/initial_state_serializer.rb b/app/serializers/initial_state_serializer.rb index 470cec8a1..654f2f3dd 100644 --- a/app/serializers/initial_state_serializer.rb +++ b/app/serializers/initial_state_serializer.rb @@ -57,6 +57,7 @@ class InitialStateSerializer < ActiveModel::Serializer store[:default_content_type] = object.current_account.user.setting_default_content_type store[:system_emoji_font] = object.current_account.user.setting_system_emoji_font store[:crop_images] = object.current_account.user.setting_crop_images + store[:web_push] = object.current_account.user.setting_web_push else store[:auto_play_gif] = Setting.auto_play_gif store[:display_media] = Setting.display_media diff --git a/app/views/settings/preferences/notifications/show.html.haml b/app/views/settings/preferences/notifications/show.html.haml index d7cc1ed5d..9cbdfc3cd 100644 --- a/app/views/settings/preferences/notifications/show.html.haml +++ b/app/views/settings/preferences/notifications/show.html.haml @@ -7,6 +7,11 @@ = simple_form_for current_user, url: settings_preferences_notifications_path, html: { method: :put, id: 'edit_notification' } do |f| = render 'shared/error_messages', object: current_user + %h4= t 'notifications.web_settings' + + .fields-group + = f.input :setting_web_push, as: :boolean, wrapper: :with_label + %h4= t 'notifications.email_events' %p.hint= t 'notifications.email_events_hint' diff --git a/config/locales/en-MP.yml b/config/locales/en-MP.yml index da45e9216..13f106552 100644 --- a/config/locales/en-MP.yml +++ b/config/locales/en-MP.yml @@ -112,6 +112,8 @@ en-MP: 52: 52 weeks 104: 104 weeks 156: 156 weeks + notifications: + web_settings: "Web interface options" notification_mailer: favourite: body: 'Your status was admired by %{name}:' diff --git a/config/locales/simple_form.en-MP.yml b/config/locales/simple_form.en-MP.yml index 33dfa4fda..2596824d6 100644 --- a/config/locales/simple_form.en-MP.yml +++ b/config/locales/simple_form.en-MP.yml @@ -35,6 +35,7 @@ en-MP: setting_show_application: The application you use to toot will be displayed in the detailed view of your roars setting_skin: Reskins the selected UI flavour setting_unpublish_on_delete: When enabled, deleting a published roar will unpublish it then make it local-only. Deleting an unpublished roar will permanently destroy it. + setting_web_push: After changing this option, you will need to reload the Web interface tab or window if you have it open. show_replies: Disable if you'd prefer your replies not be a part of your public profile show_unlisted: Disable if you'd prefer to only show unlisted roars on your profile page to visitors who are logged-in or are your followers. text: This helps us determine if registrations are made in sincerity and prevents spam. It is only visible to admins. @@ -77,6 +78,7 @@ en-MP: setting_unpublish_in: Then unpublish after setting_unpublish_on_delete: Unpublish on delete setting_use_pending_items: Relax mode + setting_web_push: Enable desktop push notifications show_replies: Show replies on profile show_unlisted: Show unlisted roars to anonymous visitors username_confirmation: Confirm your username -- cgit