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;
$scope.Config = Config;
@ -186,14 +186,14 @@
};
$scope.notificationsPermissionsEnabled = Notification
$scope.notificationsPermissionsEnabled = window['Notification']
&& 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 = () => {
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({
"message": 'Desktop Notifications unsupported in this browser',
"title": 'Unsupported Option',
@ -208,24 +208,24 @@
return;
}
if (localStorage.getItem('quay.enabledDesktopNotifications') === 'on') {
localStorage.setItem('quay.enabledDesktopNotifications', 'off');
localStorage.removeItem('quay.notifications.mostRecentTimestamp');
if (CookieService.get('quay.enabledDesktopNotifications') === 'on') {
CookieService.putPermanent('quay.enabledDesktopNotifications', 'off');
CookieService.clear('quay.notifications.mostRecentTimestamp');
$scope.notificationsPermissionsEnabled = false;
} else {
if (Notification.permission === 'default') {
Notification.requestPermission()
.then((newPermission) => {
if (newPermission === 'granted') {
localStorage.setItem('quay.enabledDesktopNotifications', 'on');
CookieService.putPermanent('quay.enabledDesktopNotifications', 'on');
$scope.notificationsPermissionsEnabled = true;
} else {
$scope.notificationsPermissionsEnabled = false;
}
});
} else if (Notification.permission === 'granted') {
localStorage.setItem('quay.enabledDesktopNotifications', 'on');
localStorage.setItem('quay.notifications.mostRecentTimestamp', new Date().getTime().toString());
CookieService.putPermanent('quay.enabledDesktopNotifications', 'on');
CookieService.putPermanent('quay.notifications.mostRecentTimestamp', new Date().getTime().toString());
$scope.notificationsPermissionsEnabled = true;
}
}

View file

@ -3,9 +3,9 @@
* in the sidebar) and provides helper methods for working with them.
*/
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 = {
'user': null,
'notifications': [],
@ -267,8 +267,7 @@ function($rootScope, $interval, UserService, ApiService, StringBuilderService, P
notificationService.additionalNotifications = resp['additional'];
notificationService.notificationClasses = notificationService.getClasses(notificationService.notifications);
if (notificationService.notifications.length > 0 && localStorage.getItem('quay.enabledDesktopNotifications') === 'on') {
console.log(notificationService);
if (notificationService.notifications.length > 0 && CookieService.get('quay.enabledDesktopNotifications') === 'on') {
notificationService.sendBrowserNotifications();
}
});
@ -284,7 +283,7 @@ function($rootScope, $interval, UserService, ApiService, StringBuilderService, P
};
notificationService.sendBrowserNotifications = () => {
let mostRecentTimestamp = parseInt(localStorage.getItem('quay.notifications.mostRecentTimestamp'), 10);
let mostRecentTimestamp = parseInt(CookieService.get('quay.notifications.mostRecentTimestamp'), 10);
if (!mostRecentTimestamp) {
mostRecentTimestamp = new Date(notificationService.notifications[0].created).getTime();
}
@ -300,12 +299,12 @@ function($rootScope, $interval, UserService, ApiService, StringBuilderService, P
}
new Notification(message, {
icon: 'http://quay.io/static/img/quay-logo.png',
image: 'http://quay.io/static/img/quay-logo.png',
icon: window.location.origin + '/static/img/quay-logo.png',
image: window.location.origin + '/static/img/quay-logo.png',
});
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 class="help-text"
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>
</td>
</tr>