From 9d7c28625240f8a0846bf8333218965945469353 Mon Sep 17 00:00:00 2001 From: epriestley Date: Sat, 4 Jun 2016 15:01:28 -0700 Subject: [PATCH] Allow users to turn off desktop notifications Summary: Fixes T8846. Ref T4103. I just took the shortest reasonable path here, this panel could use some attention on the next Conpherence iteration. Test Plan: Turned on/off desktop notifications. Observed corresponding behavior in test notifications. Reviewers: chad Reviewed By: chad Maniphest Tasks: T4103, T8846 Differential Revision: https://secure.phabricator.com/D16036 --- src/__phutil_library_map__.php | 2 ++ .../PhabricatorNotificationBuilder.php | 8 +++++-- ...icatorNotificationIndividualController.php | 4 +++- ...PhabricatorNotificationPanelController.php | 4 +++- ...catorDesktopNotificationsSettingsPanel.php | 22 +++++++++++-------- ...habricatorHomePreferencesSettingsPanel.php | 13 +---------- .../panel/PhabricatorSettingsPanel.php | 18 +++++++++++++++ ...PhabricatorDesktopNotificationsSetting.php | 12 ++++++++++ .../storage/PhabricatorUserPreferences.php | 2 -- 9 files changed, 58 insertions(+), 27 deletions(-) create mode 100644 src/applications/settings/setting/PhabricatorDesktopNotificationsSetting.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 11ad46e5bf..b42577f211 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -2305,6 +2305,7 @@ phutil_register_library_map(array( 'PhabricatorDebugController' => 'applications/system/controller/PhabricatorDebugController.php', 'PhabricatorDefaultRequestExceptionHandler' => 'aphront/handler/PhabricatorDefaultRequestExceptionHandler.php', 'PhabricatorDefaultSyntaxStyle' => 'infrastructure/syntax/PhabricatorDefaultSyntaxStyle.php', + 'PhabricatorDesktopNotificationsSetting' => 'applications/settings/setting/PhabricatorDesktopNotificationsSetting.php', 'PhabricatorDesktopNotificationsSettingsPanel' => 'applications/settings/panel/PhabricatorDesktopNotificationsSettingsPanel.php', 'PhabricatorDestructibleInterface' => 'applications/system/interface/PhabricatorDestructibleInterface.php', 'PhabricatorDestructionEngine' => 'applications/system/engine/PhabricatorDestructionEngine.php', @@ -6885,6 +6886,7 @@ phutil_register_library_map(array( 'PhabricatorDebugController' => 'PhabricatorController', 'PhabricatorDefaultRequestExceptionHandler' => 'PhabricatorRequestExceptionHandler', 'PhabricatorDefaultSyntaxStyle' => 'PhabricatorSyntaxStyle', + 'PhabricatorDesktopNotificationsSetting' => 'PhabricatorInternalSetting', 'PhabricatorDesktopNotificationsSettingsPanel' => 'PhabricatorSettingsPanel', 'PhabricatorDestructionEngine' => 'Phobject', 'PhabricatorDestructionEngineExtension' => 'Phobject', diff --git a/src/applications/notification/builder/PhabricatorNotificationBuilder.php b/src/applications/notification/builder/PhabricatorNotificationBuilder.php index dd4e19dcfb..a8af4ff00e 100644 --- a/src/applications/notification/builder/PhabricatorNotificationBuilder.php +++ b/src/applications/notification/builder/PhabricatorNotificationBuilder.php @@ -131,10 +131,14 @@ final class PhabricatorNotificationBuilder extends Phobject { $stories = $this->parseStories(); $dict = array(); + $viewer = $this->user; + $desktop_key = PhabricatorDesktopNotificationsSetting::SETTINGKEY; + $desktop_enabled = $viewer->getUserSetting($desktop_key); + foreach ($stories as $story) { if ($story instanceof PhabricatorApplicationTransactionFeedStory) { $dict[] = array( - 'desktopReady' => true, + 'desktopReady' => $desktop_enabled, 'title' => $story->renderText(), 'body' => $story->renderTextBody(), 'href' => $story->getURI(), @@ -142,7 +146,7 @@ final class PhabricatorNotificationBuilder extends Phobject { ); } else if ($story instanceof PhabricatorNotificationTestFeedStory) { $dict[] = array( - 'desktopReady' => true, + 'desktopReady' => $desktop_enabled, 'title' => pht('Test Notification'), 'body' => $story->renderText(), 'href' => null, diff --git a/src/applications/notification/controller/PhabricatorNotificationIndividualController.php b/src/applications/notification/controller/PhabricatorNotificationIndividualController.php index 68214d1965..bfaebaf606 100644 --- a/src/applications/notification/controller/PhabricatorNotificationIndividualController.php +++ b/src/applications/notification/controller/PhabricatorNotificationIndividualController.php @@ -30,7 +30,9 @@ final class PhabricatorNotificationIndividualController return $this->buildEmptyResponse(); } - $builder = new PhabricatorNotificationBuilder(array($story)); + $builder = id(new PhabricatorNotificationBuilder(array($story))) + ->setUser($viewer); + $content = $builder->buildView()->render(); $dict = $builder->buildDict(); $data = $dict[0]; diff --git a/src/applications/notification/controller/PhabricatorNotificationPanelController.php b/src/applications/notification/controller/PhabricatorNotificationPanelController.php index be68cc2de7..c9c3d9b94a 100644 --- a/src/applications/notification/controller/PhabricatorNotificationPanelController.php +++ b/src/applications/notification/controller/PhabricatorNotificationPanelController.php @@ -16,7 +16,9 @@ final class PhabricatorNotificationPanelController $clear_ui_class = 'phabricator-notification-clear-all'; $clear_uri = id(new PhutilURI('/notification/clear/')); if ($stories) { - $builder = new PhabricatorNotificationBuilder($stories); + $builder = id(new PhabricatorNotificationBuilder($stories)) + ->setUser($viewer); + $notifications_view = $builder->buildView(); $content = $notifications_view->render(); $clear_uri->setQueryParam( diff --git a/src/applications/settings/panel/PhabricatorDesktopNotificationsSettingsPanel.php b/src/applications/settings/panel/PhabricatorDesktopNotificationsSettingsPanel.php index 65beabec6a..207d2300b2 100644 --- a/src/applications/settings/panel/PhabricatorDesktopNotificationsSettingsPanel.php +++ b/src/applications/settings/panel/PhabricatorDesktopNotificationsSettingsPanel.php @@ -26,15 +26,19 @@ final class PhabricatorDesktopNotificationsSettingsPanel } public function processRequest(AphrontRequest $request) { - $user = $request->getUser(); - $preferences = $user->loadPreferences(); + $viewer = $this->getViewer(); + $preferences = $this->loadTargetPreferences(); - $pref = PhabricatorUserPreferences::PREFERENCE_DESKTOP_NOTIFICATIONS; + $notifications_key = PhabricatorDesktopNotificationsSetting::SETTINGKEY; + $notifications_value = $preferences->getSettingValue($notifications_key); if ($request->isFormPost()) { - $notifications = $request->getInt($pref); - $preferences->setPreference($pref, $notifications); - $preferences->save(); + + $this->writeSetting( + $preferences, + $notifications_key, + $request->getInt($notifications_key)); + return id(new AphrontRedirectResponse()) ->setURI($this->getPanelURI('?saved=true')); } @@ -106,13 +110,13 @@ final class PhabricatorDesktopNotificationsSettingsPanel ); $form = id(new AphrontFormView()) - ->setUser($user) + ->setUser($viewer) ->appendChild( id(new AphrontFormSelectControl()) ->setLabel($title) ->setControlID($control_id) - ->setName($pref) - ->setValue($preferences->getPreference($pref)) + ->setName($notifications_key) + ->setValue($notifications_value) ->setOptions( array( 1 => pht('Send Desktop Notifications Too'), diff --git a/src/applications/settings/panel/PhabricatorHomePreferencesSettingsPanel.php b/src/applications/settings/panel/PhabricatorHomePreferencesSettingsPanel.php index 5369a2b90c..4aa45abdb5 100644 --- a/src/applications/settings/panel/PhabricatorHomePreferencesSettingsPanel.php +++ b/src/applications/settings/panel/PhabricatorHomePreferencesSettingsPanel.php @@ -208,19 +208,8 @@ final class PhabricatorHomePreferencesSettingsPanel PhabricatorUserPreferences $preferences, $pinned) { - $viewer = $this->getViewer(); - $request = $this->getController()->getRequest(); $pinned_key = PhabricatorPinnedApplicationsSetting::SETTINGKEY; - - $editor = id(new PhabricatorUserPreferencesEditor()) - ->setActor($viewer) - ->setContentSourceFromRequest($request) - ->setContinueOnNoEffect(true) - ->setContinueOnMissingFields(true); - - $xactions = array(); - $xactions[] = $preferences->newTransaction($pinned_key, $pinned); - $editor->applyTransactions($preferences, $xactions); + $this->writeSetting($preferences, $pinned_key, $pinned); } } diff --git a/src/applications/settings/panel/PhabricatorSettingsPanel.php b/src/applications/settings/panel/PhabricatorSettingsPanel.php index 86925bf4a7..e2cdc91f00 100644 --- a/src/applications/settings/panel/PhabricatorSettingsPanel.php +++ b/src/applications/settings/panel/PhabricatorSettingsPanel.php @@ -235,4 +235,22 @@ abstract class PhabricatorSettingsPanel extends Phobject { return $this->getController()->newDialog(); } + protected function writeSetting( + PhabricatorUserPreferences $preferences, + $key, + $value) { + $viewer = $this->getViewer(); + $request = $this->getController()->getRequest(); + + $editor = id(new PhabricatorUserPreferencesEditor()) + ->setActor($viewer) + ->setContentSourceFromRequest($request) + ->setContinueOnNoEffect(true) + ->setContinueOnMissingFields(true); + + $xactions = array(); + $xactions[] = $preferences->newTransaction($key, $value); + $editor->applyTransactions($preferences, $xactions); + } + } diff --git a/src/applications/settings/setting/PhabricatorDesktopNotificationsSetting.php b/src/applications/settings/setting/PhabricatorDesktopNotificationsSetting.php new file mode 100644 index 0000000000..f590d37325 --- /dev/null +++ b/src/applications/settings/setting/PhabricatorDesktopNotificationsSetting.php @@ -0,0 +1,12 @@ +