From bd330a1089d4dd672181f718332adcf900404106 Mon Sep 17 00:00:00 2001 From: Josef Citrine Date: Tue, 7 Jun 2016 17:49:00 +0100 Subject: [PATCH] Mark as read and notification count --- .../Api/Web/NotificationsController.php | 3 +-- app/Models/Notification.php | 5 +++- .../directives/notification-list.html | 2 +- .../app/controllers/application.coffee | 13 ++++++---- .../app/directives/notification-list.coffee | 16 +++++++------ .../scripts/app/services/notifications.coffee | 24 +++++++++++++++---- resources/assets/styles/content.less | 9 +++++++ resources/views/shared/_app_layout.blade.php | 2 +- 8 files changed, 54 insertions(+), 20 deletions(-) diff --git a/app/Http/Controllers/Api/Web/NotificationsController.php b/app/Http/Controllers/Api/Web/NotificationsController.php index fc9ae2ed..c0a0d710 100644 --- a/app/Http/Controllers/Api/Web/NotificationsController.php +++ b/app/Http/Controllers/Api/Web/NotificationsController.php @@ -39,7 +39,6 @@ class NotificationsController extends ApiControllerBase ->take(20) ->get(); - return ['notifications' => $notifications->toArray()]; } @@ -52,7 +51,7 @@ class NotificationsController extends ApiControllerBase */ public function putMarkAsRead() { - $notificationIds = Input::get('notification_ids'); + $notificationIds = Input::json('notification_ids'); $numberOfUpdatedRows = Auth::user() ->notifications() ->whereIn('id', $notificationIds) diff --git a/app/Models/Notification.php b/app/Models/Notification.php index d3272221..8e2e5c99 100644 --- a/app/Models/Notification.php +++ b/app/Models/Notification.php @@ -60,7 +60,7 @@ class Notification extends Model { * @return Builder */ public function scopeForUser(Builder $query, User $user) { - return $query->with([ + $result = $query->with([ 'activity', 'activity.initiatingUser', 'activity.resource', @@ -68,7 +68,10 @@ class Notification extends Model { ]) ->join('activities', 'notifications.activity_id', '=', 'activities.id') ->where('notifications.user_id', $user->id) + ->select('*', 'notifications.id as id') ->orderBy('activities.created_at', 'DESC'); + + return $result; } public function toArray() { diff --git a/public/templates/directives/notification-list.html b/public/templates/directives/notification-list.html index 3447e0ac..036b2352 100644 --- a/public/templates/directives/notification-list.html +++ b/public/templates/directives/notification-list.html @@ -1,5 +1,5 @@
-
+ diff --git a/resources/assets/scripts/app/controllers/application.coffee b/resources/assets/scripts/app/controllers/application.coffee index fc0c3bed..c5e6cf37 100644 --- a/resources/assets/scripts/app/controllers/application.coffee +++ b/resources/assets/scripts/app/controllers/application.coffee @@ -36,17 +36,20 @@ module.exports = angular.module('ponyfm').controller "application", [ $scope.menuToggle = () -> $rootScope.$broadcast('sidebarToggled') + + if $scope.notifActive + notifications.markAllAsRead() + $scope.notifActive = false $scope.notifPulloutToggle = () -> $scope.notifActive = !$scope.notifActive - # Disabled for now - # if $scope.notifActive - # notifications.markAllAsRead() + if !$scope.notifActive + notifications.markAllAsRead() $rootScope.$on 'notificationsUpdated', () -> - $scope.nCount = notifications.getNotificationCount() + $scope.nCount = notifications.getUnreadCount() if $scope.nCount > 99 $scope.nCountFormatted = '99+' else @@ -89,6 +92,8 @@ module.exports = angular.module('ponyfm').controller "application", [ statesPreloaded = {} $scope.$on '$stateChangeStart', (e, newState, newParams, oldState, oldParams) -> $rootScope.$broadcast('sidebarHide') + if $scope.notifActive + notifications.markAllAsRead() $scope.notifActive = false $scope.isPinnedPlaylistSelected = false diff --git a/resources/assets/scripts/app/directives/notification-list.coffee b/resources/assets/scripts/app/directives/notification-list.coffee index 411d774f..63abd0d8 100644 --- a/resources/assets/scripts/app/directives/notification-list.coffee +++ b/resources/assets/scripts/app/directives/notification-list.coffee @@ -21,24 +21,28 @@ module.exports = angular.module('ponyfm').directive 'pfmNotificationList', () -> scope: {} controller: [ - '$scope', 'notifications', '$timeout' - ($scope, notifications, $timeout) -> + '$scope', 'notifications', '$timeout', '$rootScope' + ($scope, notifications, $timeout, $rootScope) -> $scope.notifications = [] isTimeoutScheduled = false # TODO: ADD REFRESH BUTTON + $rootScope.$on 'shouldUpdateNotifications', () -> + refreshNotifications() + refreshNotifications = () -> notifications.getNotifications().done (result) -> if $scope.notifications.length > 0 - if result[0].text != $scope.notifications[0].text + if result[0].id != $scope.notifications[0].id || result[0].is_read != $scope.notifications[0].is_read $scope.notifications = result else if result.length > 0 $scope.notifications = result $scope.nCount = $scope.notifications.length - - scheduleTimeout() + + if !isTimeoutScheduled + scheduleTimeout() scheduleTimeout = () -> isTimeoutScheduled = true @@ -47,7 +51,5 @@ module.exports = angular.module('ponyfm').directive 'pfmNotificationList', () -> isTimeoutScheduled = false , 60000) - - refreshNotifications() ] diff --git a/resources/assets/scripts/app/services/notifications.coffee b/resources/assets/scripts/app/services/notifications.coffee index 03704ca0..7fa2ad8b 100644 --- a/resources/assets/scripts/app/services/notifications.coffee +++ b/resources/assets/scripts/app/services/notifications.coffee @@ -32,18 +32,34 @@ module.exports = angular.module('ponyfm').factory('notifications', [ getNotificationCount: () -> return self.notificationList.length + + getUnreadCount: () -> + count = 0 + + for n, notifObject of self.notificationList + if !notifObject.is_read + count++ + + return count markAllAsRead: () -> + def = new $.Deferred() unread = [] for n, notifObject of self.notificationList if !notifObject.is_read - unread.push notifObject.id.toString() + unread.push notifObject.id - $http.put('/api/web/notifications/mark-as-read', {notification_ids: unread}).success (response) -> - console.log response + if unread.length > 0 + $http.put('/api/web/notifications/mark-as-read', {notification_ids: unread}).success (response) -> + if response.notifications_updated != 0 + $rootScope.$broadcast 'shouldUpdateNotifications' - console.log unread + def.resolve response.notifications_updated + + def.promise() + else + return 0 self ]) diff --git a/resources/assets/styles/content.less b/resources/assets/styles/content.less index e43cdbfd..d0ec0316 100644 --- a/resources/assets/styles/content.less +++ b/resources/assets/styles/content.less @@ -567,6 +567,10 @@ html { border-radius: 20px; padding: 0 3px; display: none; + + &.show { + display: block; + } } } @@ -575,6 +579,7 @@ html { margin-bottom: 10px; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.2); + border-radius: 2px; .img-link { float: left; @@ -588,6 +593,10 @@ html { p { margin: 0; } + + &.unread { + border: 1px solid #b885bd; + } } .notification-pullout { diff --git a/resources/views/shared/_app_layout.blade.php b/resources/views/shared/_app_layout.blade.php index 15cf1ea7..46f6a07c 100644 --- a/resources/views/shared/_app_layout.blade.php +++ b/resources/views/shared/_app_layout.blade.php @@ -68,7 +68,7 @@
-
@{{ nCountFormatted }}
+
@{{ nCountFormatted }}
@endif