Use cookie service and small cleanup for pr

This commit is contained in:
Sam Chow 2018-05-04 17:11:47 -04:00
parent 2d3583fb44
commit 6fe579119b
3 changed files with 19 additions and 20 deletions

View file

@ -10,7 +10,7 @@
}) })
}]); }]);
function UserViewCtrl($scope, $routeParams, $timeout, ApiService, UserService, UIService, AvatarService, Config, ExternalLoginService) { function UserViewCtrl($scope, $routeParams, $timeout, ApiService, UserService, UIService, AvatarService, Config, ExternalLoginService, CookieService) {
var username = $routeParams.username; var username = $routeParams.username;
$scope.Config = Config; $scope.Config = Config;
@ -186,14 +186,14 @@
}; };
$scope.notificationsPermissionsEnabled = Notification $scope.notificationsPermissionsEnabled = window['Notification']
&& Notification.permission === 'granted' && Notification.permission === 'granted'
&& localStorage.getItem('quay.enabledDesktopNotifications') === 'on'; && CookieService.get('quay.enabledDesktopNotifications') === 'on';
$scope.desktopNotificationsPermissionIsDisabled = () => Notification.permission === 'denied'; $scope.desktopNotificationsPermissionIsDisabled = () => window['Notification'] && Notification.permission === 'denied';
$scope.toggleDesktopNotifications = () => { $scope.toggleDesktopNotifications = () => {
if (!Notification) { // unsupported in IE & some older browsers, we'll just tell the user it's not available if (!window['Notification']) { // unsupported in IE & some older browsers, we'll just tell the user it's not available
bootbox.dialog({ bootbox.dialog({
"message": 'Desktop Notifications unsupported in this browser', "message": 'Desktop Notifications unsupported in this browser',
"title": 'Unsupported Option', "title": 'Unsupported Option',
@ -208,24 +208,24 @@
return; return;
} }
if (localStorage.getItem('quay.enabledDesktopNotifications') === 'on') { if (CookieService.get('quay.enabledDesktopNotifications') === 'on') {
localStorage.setItem('quay.enabledDesktopNotifications', 'off'); CookieService.putPermanent('quay.enabledDesktopNotifications', 'off');
localStorage.removeItem('quay.notifications.mostRecentTimestamp'); CookieService.clear('quay.notifications.mostRecentTimestamp');
$scope.notificationsPermissionsEnabled = false; $scope.notificationsPermissionsEnabled = false;
} else { } else {
if (Notification.permission === 'default') { if (Notification.permission === 'default') {
Notification.requestPermission() Notification.requestPermission()
.then((newPermission) => { .then((newPermission) => {
if (newPermission === 'granted') { if (newPermission === 'granted') {
localStorage.setItem('quay.enabledDesktopNotifications', 'on'); CookieService.putPermanent('quay.enabledDesktopNotifications', 'on');
$scope.notificationsPermissionsEnabled = true; $scope.notificationsPermissionsEnabled = true;
} else { } else {
$scope.notificationsPermissionsEnabled = false; $scope.notificationsPermissionsEnabled = false;
} }
}); });
} else if (Notification.permission === 'granted') { } else if (Notification.permission === 'granted') {
localStorage.setItem('quay.enabledDesktopNotifications', 'on'); CookieService.putPermanent('quay.enabledDesktopNotifications', 'on');
localStorage.setItem('quay.notifications.mostRecentTimestamp', new Date().getTime().toString()); CookieService.putPermanent('quay.notifications.mostRecentTimestamp', new Date().getTime().toString());
$scope.notificationsPermissionsEnabled = true; $scope.notificationsPermissionsEnabled = true;
} }
} }

View file

@ -3,9 +3,9 @@
* in the sidebar) and provides helper methods for working with them. * in the sidebar) and provides helper methods for working with them.
*/ */
angular.module('quay').factory('NotificationService', angular.module('quay').factory('NotificationService',
['$rootScope', '$interval', 'UserService', 'ApiService', 'StringBuilderService', 'PlanService', 'UserService', 'Features', 'Config', '$location', 'VulnerabilityService', ['$rootScope', '$interval', 'UserService', 'ApiService', 'StringBuilderService', 'PlanService', 'CookieService', 'Features', 'Config', '$location', 'VulnerabilityService',
function($rootScope, $interval, UserService, ApiService, StringBuilderService, PlanService, UserService, Features, Config, $location, VulnerabilityService) { function($rootScope, $interval, UserService, ApiService, StringBuilderService, PlanService, CookieService, Features, Config, $location, VulnerabilityService) {
var notificationService = { var notificationService = {
'user': null, 'user': null,
'notifications': [], 'notifications': [],
@ -267,8 +267,7 @@ function($rootScope, $interval, UserService, ApiService, StringBuilderService, P
notificationService.additionalNotifications = resp['additional']; notificationService.additionalNotifications = resp['additional'];
notificationService.notificationClasses = notificationService.getClasses(notificationService.notifications); notificationService.notificationClasses = notificationService.getClasses(notificationService.notifications);
if (notificationService.notifications.length > 0 && localStorage.getItem('quay.enabledDesktopNotifications') === 'on') { if (notificationService.notifications.length > 0 && CookieService.get('quay.enabledDesktopNotifications') === 'on') {
console.log(notificationService);
notificationService.sendBrowserNotifications(); notificationService.sendBrowserNotifications();
} }
}); });
@ -284,7 +283,7 @@ function($rootScope, $interval, UserService, ApiService, StringBuilderService, P
}; };
notificationService.sendBrowserNotifications = () => { notificationService.sendBrowserNotifications = () => {
let mostRecentTimestamp = parseInt(localStorage.getItem('quay.notifications.mostRecentTimestamp'), 10); let mostRecentTimestamp = parseInt(CookieService.get('quay.notifications.mostRecentTimestamp'), 10);
if (!mostRecentTimestamp) { if (!mostRecentTimestamp) {
mostRecentTimestamp = new Date(notificationService.notifications[0].created).getTime(); mostRecentTimestamp = new Date(notificationService.notifications[0].created).getTime();
} }
@ -300,12 +299,12 @@ function($rootScope, $interval, UserService, ApiService, StringBuilderService, P
} }
new Notification(message, { new Notification(message, {
icon: 'http://quay.io/static/img/quay-logo.png', icon: window.location.origin + '/static/img/quay-logo.png',
image: 'http://quay.io/static/img/quay-logo.png', image: window.location.origin + '/static/img/quay-logo.png',
}); });
const newTimestamp = new Date(newNotifications[0].created).getTime(); const newTimestamp = new Date(newNotifications[0].created).getTime();
localStorage.setItem('quay.notifications.mostRecentTimestamp', newTimestamp.toString()); CookieService.putPermanent('quay.notifications.mostRecentTimestamp', newTimestamp.toString());
} }
}; };

View file

@ -183,7 +183,7 @@
</span> </span>
<span class="help-text" <span class="help-text"
ng-if="desktopNotificationsPermissionIsDisabled()" ng-if="desktopNotificationsPermissionIsDisabled()"
>Note: Desktop notifications have been disabled in your browser. >Note: Desktop notifications have been disabled, or are unavailable, in your browser.
</span> </span>
</td> </td>
</tr> </tr>