1
0
mirror of https://github.com/flarum/core.git synced 2025-08-02 06:30:53 +02:00

Return both unread and new notification count from the API

Related to #500.
This commit is contained in:
Franz Liedke
2015-09-29 01:26:28 +02:00
parent 56f9016ff7
commit 040ce52724
4 changed files with 38 additions and 11 deletions

View File

@@ -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();
}
/**