diff --git a/js/forum/src/components/NotificationsDropdown.js b/js/forum/src/components/NotificationsDropdown.js index f6704aa34..e03ae2a8c 100644 --- a/js/forum/src/components/NotificationsDropdown.js +++ b/js/forum/src/components/NotificationsDropdown.js @@ -70,13 +70,11 @@ export default class NotificationsDropdown extends Dropdown { } getUnreadCount() { - return app.cache.notifications ? - app.cache.notifications.filter(notification => !notification.isRead()).length : - 0; + return app.session.user.unreadNotificationsCount(); } getNewCount() { - return app.session.user.unreadNotificationsCount(); + return app.session.user.newNotificationsCount(); } menuClick(e) { diff --git a/js/lib/models/User.js b/js/lib/models/User.js index 43ee39f64..7e3fb5b9e 100644 --- a/js/lib/models/User.js +++ b/js/lib/models/User.js @@ -23,6 +23,7 @@ export default class User extends mixin(Model, { lastSeenTime: Model.attribute('lastSeenTime', Model.transformDate), readTime: Model.attribute('readTime', Model.transformDate), unreadNotificationsCount: Model.attribute('unreadNotificationsCount'), + newNotificationsCount: Model.attribute('newNotificationsCount'), discussionsCount: Model.attribute('discussionsCount'), commentsCount: Model.attribute('commentsCount'), diff --git a/src/Api/Serializers/CurrentUserSerializer.php b/src/Api/Serializers/CurrentUserSerializer.php index 2a7e62d22..53ec7ddf4 100644 --- a/src/Api/Serializers/CurrentUserSerializer.php +++ b/src/Api/Serializers/CurrentUserSerializer.php @@ -23,6 +23,7 @@ class CurrentUserSerializer extends UserSerializer $attributes += [ 'readTime' => $user->read_time ? $user->read_time->toRFC3339String() : null, 'unreadNotificationsCount' => $user->getUnreadNotificationsCount(), + 'newNotificationsCount' => $user->getNewNotificationsCount(), 'preferences' => $user->preferences ]; } diff --git a/src/Core/Users/User.php b/src/Core/Users/User.php index 639a06550..34dbdb2c0 100755 --- a/src/Core/Users/User.php +++ b/src/Core/Users/User.php @@ -432,16 +432,43 @@ class User extends Model /** * Get the number of unread notifications for the user. * - * @return mixed + * @return int */ public function getUnreadNotificationsCount() { - return $this->notifications() - ->whereIn('type', $this->getAlertableNotificationTypes()) - ->where('time', '>', $this->notifications_read_time ?: 0) - ->where('is_read', 0) - ->where('is_deleted', 0) - ->count($this->getConnection()->raw('DISTINCT type, subject_id')); + return $this->getUnreadNotifications()->count(); + } + + /** + * Get all notifications that have not been read yet + * + * @return \Illuminate\Database\Eloquent\Collection + */ + protected function getUnreadNotifications() + { + static $cached = null; + + if (is_null($cached)) { + $cached = $this->notifications() + ->whereIn('type', $this->getAlertableNotificationTypes()) + ->where('is_read', 0) + ->where('is_deleted', 0) + ->get(); + } + + return $cached; + } + + /** + * Get the number of new, unseen notifications for the user. + * + * @return int + */ + public function getNewNotificationsCount() + { + return $this->getUnreadNotifications()->filter(function($notification) { + return $notification->time > $this->notifications_read_time ?: 0; + })->count(); } /**