Mark as read and notification count

This commit is contained in:
Josef Citrine 2016-06-07 17:49:00 +01:00
parent 35886a57d7
commit bd330a1089
8 changed files with 54 additions and 20 deletions

View file

@ -39,7 +39,6 @@ class NotificationsController extends ApiControllerBase
->take(20) ->take(20)
->get(); ->get();
return ['notifications' => $notifications->toArray()]; return ['notifications' => $notifications->toArray()];
} }
@ -52,7 +51,7 @@ class NotificationsController extends ApiControllerBase
*/ */
public function putMarkAsRead() public function putMarkAsRead()
{ {
$notificationIds = Input::get('notification_ids'); $notificationIds = Input::json('notification_ids');
$numberOfUpdatedRows = Auth::user() $numberOfUpdatedRows = Auth::user()
->notifications() ->notifications()
->whereIn('id', $notificationIds) ->whereIn('id', $notificationIds)

View file

@ -60,7 +60,7 @@ class Notification extends Model {
* @return Builder * @return Builder
*/ */
public function scopeForUser(Builder $query, User $user) { public function scopeForUser(Builder $query, User $user) {
return $query->with([ $result = $query->with([
'activity', 'activity',
'activity.initiatingUser', 'activity.initiatingUser',
'activity.resource', 'activity.resource',
@ -68,7 +68,10 @@ class Notification extends Model {
]) ])
->join('activities', 'notifications.activity_id', '=', 'activities.id') ->join('activities', 'notifications.activity_id', '=', 'activities.id')
->where('notifications.user_id', $user->id) ->where('notifications.user_id', $user->id)
->select('*', 'notifications.id as id')
->orderBy('activities.created_at', 'DESC'); ->orderBy('activities.created_at', 'DESC');
return $result;
} }
public function toArray() { public function toArray() {

View file

@ -1,5 +1,5 @@
<div class="notif-list"> <div class="notif-list">
<div ng-repeat="notification in notifications" class="notification"> <div ng-repeat="notification in notifications" class="notification" ng-class="{'unread': !notification.is_read}">
<a href="{{ ::notification.url }}" class="img-link"><img pfm-src-loader="::notification.thumbnail_url" pfm-src-size="thumbnail"></a> <a href="{{ ::notification.url }}" class="img-link"><img pfm-src-loader="::notification.thumbnail_url" pfm-src-size="thumbnail"></a>
<a href="{{ ::notification.url }}" class="message"><p>{{ ::notification.text }}</p></a> <a href="{{ ::notification.url }}" class="message"><p>{{ ::notification.text }}</p></a>
</div> </div>

View file

@ -36,17 +36,20 @@ module.exports = angular.module('ponyfm').controller "application", [
$scope.menuToggle = () -> $scope.menuToggle = () ->
$rootScope.$broadcast('sidebarToggled') $rootScope.$broadcast('sidebarToggled')
if $scope.notifActive
notifications.markAllAsRead()
$scope.notifActive = false $scope.notifActive = false
$scope.notifPulloutToggle = () -> $scope.notifPulloutToggle = () ->
$scope.notifActive = !$scope.notifActive $scope.notifActive = !$scope.notifActive
# Disabled for now if !$scope.notifActive
# if $scope.notifActive notifications.markAllAsRead()
# notifications.markAllAsRead()
$rootScope.$on 'notificationsUpdated', () -> $rootScope.$on 'notificationsUpdated', () ->
$scope.nCount = notifications.getNotificationCount() $scope.nCount = notifications.getUnreadCount()
if $scope.nCount > 99 if $scope.nCount > 99
$scope.nCountFormatted = '99+' $scope.nCountFormatted = '99+'
else else
@ -89,6 +92,8 @@ module.exports = angular.module('ponyfm').controller "application", [
statesPreloaded = {} statesPreloaded = {}
$scope.$on '$stateChangeStart', (e, newState, newParams, oldState, oldParams) -> $scope.$on '$stateChangeStart', (e, newState, newParams, oldState, oldParams) ->
$rootScope.$broadcast('sidebarHide') $rootScope.$broadcast('sidebarHide')
if $scope.notifActive
notifications.markAllAsRead()
$scope.notifActive = false $scope.notifActive = false
$scope.isPinnedPlaylistSelected = false $scope.isPinnedPlaylistSelected = false

View file

@ -21,24 +21,28 @@ module.exports = angular.module('ponyfm').directive 'pfmNotificationList', () ->
scope: {} scope: {}
controller: [ controller: [
'$scope', 'notifications', '$timeout' '$scope', 'notifications', '$timeout', '$rootScope'
($scope, notifications, $timeout) -> ($scope, notifications, $timeout, $rootScope) ->
$scope.notifications = [] $scope.notifications = []
isTimeoutScheduled = false isTimeoutScheduled = false
# TODO: ADD REFRESH BUTTON # TODO: ADD REFRESH BUTTON
$rootScope.$on 'shouldUpdateNotifications', () ->
refreshNotifications()
refreshNotifications = () -> refreshNotifications = () ->
notifications.getNotifications().done (result) -> notifications.getNotifications().done (result) ->
if $scope.notifications.length > 0 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 $scope.notifications = result
else if result.length > 0 else if result.length > 0
$scope.notifications = result $scope.notifications = result
$scope.nCount = $scope.notifications.length $scope.nCount = $scope.notifications.length
scheduleTimeout() if !isTimeoutScheduled
scheduleTimeout()
scheduleTimeout = () -> scheduleTimeout = () ->
isTimeoutScheduled = true isTimeoutScheduled = true
@ -47,7 +51,5 @@ module.exports = angular.module('ponyfm').directive 'pfmNotificationList', () ->
isTimeoutScheduled = false isTimeoutScheduled = false
, 60000) , 60000)
refreshNotifications() refreshNotifications()
] ]

View file

@ -32,18 +32,34 @@ module.exports = angular.module('ponyfm').factory('notifications', [
getNotificationCount: () -> getNotificationCount: () ->
return self.notificationList.length return self.notificationList.length
getUnreadCount: () ->
count = 0
for n, notifObject of self.notificationList
if !notifObject.is_read
count++
return count
markAllAsRead: () -> markAllAsRead: () ->
def = new $.Deferred()
unread = [] unread = []
for n, notifObject of self.notificationList for n, notifObject of self.notificationList
if !notifObject.is_read 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) -> if unread.length > 0
console.log response $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 self
]) ])

View file

@ -567,6 +567,10 @@ html {
border-radius: 20px; border-radius: 20px;
padding: 0 3px; padding: 0 3px;
display: none; display: none;
&.show {
display: block;
}
} }
} }
@ -575,6 +579,7 @@ html {
margin-bottom: 10px; margin-bottom: 10px;
background: #fff; background: #fff;
box-shadow: 0 1px 2px rgba(0,0,0,.2); box-shadow: 0 1px 2px rgba(0,0,0,.2);
border-radius: 2px;
.img-link { .img-link {
float: left; float: left;
@ -588,6 +593,10 @@ html {
p { p {
margin: 0; margin: 0;
} }
&.unread {
border: 1px solid #b885bd;
}
} }
.notification-pullout { .notification-pullout {

View file

@ -68,7 +68,7 @@
</div> </div>
<div class="notification-menu"> <div class="notification-menu">
<a href="#" ng-click="notifPulloutToggle()"><i class="fa fa-bell fa-fw" aria-hidden="true"></i></a> <a href="#" ng-click="notifPulloutToggle()"><i class="fa fa-bell fa-fw" aria-hidden="true"></i></a>
<div class="counter" ng-show="nCount > 0">@{{ nCountFormatted }}</div> <div class="counter" ng-class="{'show': nCount > 0}">@{{ nCountFormatted }}</div>
</div> </div>
@endif @endif
<pfm-player></pfm-player> <pfm-player></pfm-player>