mirror of
https://github.com/flarum/framework.git
synced 2025-05-22 22:59:57 +08:00
Return both unread and new notification count from the API
Related to #500.
This commit is contained in:
@ -70,13 +70,11 @@ export default class NotificationsDropdown extends Dropdown {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getUnreadCount() {
|
getUnreadCount() {
|
||||||
return app.cache.notifications ?
|
return app.session.user.unreadNotificationsCount();
|
||||||
app.cache.notifications.filter(notification => !notification.isRead()).length :
|
|
||||||
0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getNewCount() {
|
getNewCount() {
|
||||||
return app.session.user.unreadNotificationsCount();
|
return app.session.user.newNotificationsCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
menuClick(e) {
|
menuClick(e) {
|
||||||
|
@ -23,6 +23,7 @@ export default class User extends mixin(Model, {
|
|||||||
lastSeenTime: Model.attribute('lastSeenTime', Model.transformDate),
|
lastSeenTime: Model.attribute('lastSeenTime', Model.transformDate),
|
||||||
readTime: Model.attribute('readTime', Model.transformDate),
|
readTime: Model.attribute('readTime', Model.transformDate),
|
||||||
unreadNotificationsCount: Model.attribute('unreadNotificationsCount'),
|
unreadNotificationsCount: Model.attribute('unreadNotificationsCount'),
|
||||||
|
newNotificationsCount: Model.attribute('newNotificationsCount'),
|
||||||
|
|
||||||
discussionsCount: Model.attribute('discussionsCount'),
|
discussionsCount: Model.attribute('discussionsCount'),
|
||||||
commentsCount: Model.attribute('commentsCount'),
|
commentsCount: Model.attribute('commentsCount'),
|
||||||
|
@ -23,6 +23,7 @@ class CurrentUserSerializer extends UserSerializer
|
|||||||
$attributes += [
|
$attributes += [
|
||||||
'readTime' => $user->read_time ? $user->read_time->toRFC3339String() : null,
|
'readTime' => $user->read_time ? $user->read_time->toRFC3339String() : null,
|
||||||
'unreadNotificationsCount' => $user->getUnreadNotificationsCount(),
|
'unreadNotificationsCount' => $user->getUnreadNotificationsCount(),
|
||||||
|
'newNotificationsCount' => $user->getNewNotificationsCount(),
|
||||||
'preferences' => $user->preferences
|
'preferences' => $user->preferences
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -432,16 +432,43 @@ class User extends Model
|
|||||||
/**
|
/**
|
||||||
* Get the number of unread notifications for the user.
|
* Get the number of unread notifications for the user.
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getUnreadNotificationsCount()
|
public function getUnreadNotificationsCount()
|
||||||
{
|
{
|
||||||
return $this->notifications()
|
return $this->getUnreadNotifications()->count();
|
||||||
->whereIn('type', $this->getAlertableNotificationTypes())
|
}
|
||||||
->where('time', '>', $this->notifications_read_time ?: 0)
|
|
||||||
->where('is_read', 0)
|
/**
|
||||||
->where('is_deleted', 0)
|
* Get all notifications that have not been read yet
|
||||||
->count($this->getConnection()->raw('DISTINCT type, subject_id'));
|
*
|
||||||
|
* @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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user