Skip to content

Commit

Permalink
gui: Store notification enabled in config
Browse files Browse the repository at this point in the history
  • Loading branch information
tuarrep committed Mar 3, 2020
1 parent bb18be5 commit 3e34dd7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 32 deletions.
42 changes: 13 additions & 29 deletions gui/default/syncthing/core/pushNotificationsService.js
@@ -1,5 +1,5 @@
angular.module('syncthing.core')
.service('PushNotifications', ['Events', function (Events) {
.service('PushNotifications', function() {
'use strict';

var self = this;
Expand All @@ -14,39 +14,23 @@ angular.module('syncthing.core')
return true;
}

function detectLocalStorage() {
// Feature detect localStorage; https://mathiasbynens.be/notes/localstorage-pattern
try {
var uid = new Date();
var storage = window.localStorage;
storage.setItem(uid, uid);
storage.removeItem(uid);
return storage;
} catch (exception) {
return undefined;
}
}

var _localStorage = detectLocalStorage();
var _SYNPN = "SYN_PN"; // const key for localStorage

angular.extend(self, {
enabled: null,
isEnabled: function () {
return self.isSupported() &&_localStorage && _localStorage[_SYNPN] === 'true'
return self.isSupported() && self.enabled;
},
setEnabled: function (enabled) {
if (!_localStorage || !self.isSupported()) return;
setEnabled: function (newEnabled) {
if(!self.isSupported()) return;

var showWelcomeNotification = false;
if (enabled && !self.isEnabled())
showWelcomeNotification = true;
console.log('setNotifications', newEnabled);

_localStorage[_SYNPN] = enabled;
if (newEnabled && self.enabled === false)
self.notify('Notifications will be shown like this');

if (showWelcomeNotification) self.notify('Notifications will be shown like this')
self.enabled = newEnabled;
},
isSupported: function() {
return "Notification" in window && Notification.permission !== 'denied';
isSupported: function () {
return "Notification" in window && Notification.permission !== 'denied';
},
checkPermission: function () {
function handlePermission(permission) {
Expand All @@ -58,7 +42,7 @@ angular.module('syncthing.core')

return new Promise(function (resolve) {
// Let's check if the browser supports notifications and user has enabled them
if (!self.isEnabled() || !self.isSupported()) {
if (!self.isEnabled() || !self.isSupported()) {
resolve(false)
} else {
// Check if browser uses Promises or callbacks (Safari)
Expand All @@ -83,4 +67,4 @@ angular.module('syncthing.core')
})
}
})
}]);
});
5 changes: 3 additions & 2 deletions gui/default/syncthing/core/syncthingController.js
Expand Up @@ -265,6 +265,8 @@ angular.module('syncthing.core')
}
}
}

PushNotifications.setEnabled($scope.config.gui.enableNotifications);
});

$scope.$on(Events.CONFIG_SAVED, function (event, arg) {
Expand Down Expand Up @@ -380,7 +382,6 @@ angular.module('syncthing.core')
$scope.config.options._globalAnnounceServersStr = $scope.config.options.globalAnnounceServers.join(', ');
$scope.config.options._urAcceptedStr = "" + $scope.config.options.urAccepted;

$scope.config.gui._enableNotifications = PushNotifications.isEnabled();
$scope.config.gui._supportNotifications = PushNotifications.isSupported();

$scope.devices = $scope.config.devices;
Expand Down Expand Up @@ -1341,7 +1342,7 @@ angular.module('syncthing.core')
}

// Save push notifications preferences in LocalStorage
PushNotifications.setEnabled($scope.tmpGUI._enableNotifications);
PushNotifications.setEnabled($scope.tmpGUI.enableNotifications);

// Parse strings to arrays before copying over
['listenAddresses', 'globalAnnounceServers'].forEach(function (key) {
Expand Down
2 changes: 1 addition & 1 deletion gui/default/syncthing/settings/settingsModalView.html
Expand Up @@ -259,7 +259,7 @@
<div class="form-group">
<div class="checkbox">
<label>
<input id="EnableNotifications" type="checkbox" ng-model="tmpGUI._enableNotifications" ng-disabled="!tmpGUI._supportNotifications" /> <span translate>Enable push notifications</span>
<input id="EnableNotifications" type="checkbox" ng-model="tmpGUI.enableNotifications" ng-disabled="!tmpGUI._supportNotifications" /> <span translate>Enable push notifications</span>
</label>
<p class="help-block" ng-if="!tmpGUI._supportNotifications">
<span translate>Your browser do not support push notifications or your blocked them</span>
Expand Down
1 change: 1 addition & 0 deletions lib/config/guiconfiguration.go
Expand Up @@ -27,6 +27,7 @@ type GUIConfiguration struct {
Debugging bool `xml:"debugging,attr" json:"debugging"`
InsecureSkipHostCheck bool `xml:"insecureSkipHostcheck,omitempty" json:"insecureSkipHostcheck"`
InsecureAllowFrameLoading bool `xml:"insecureAllowFrameLoading,omitempty" json:"insecureAllowFrameLoading"`
EnableNotifications bool `xml:"enableNotifications,omitempty" json:"enableNotifications"`
}

func (c GUIConfiguration) IsAuthEnabled() bool {
Expand Down

0 comments on commit 3e34dd7

Please sign in to comment.