mirror of
https://github.com/flarum/core.git
synced 2025-08-02 14:37:49 +02:00
fix: potential static caching memory exhaustion (#3548)
* perf: get notification counts through relation, not model filtering * chore: rename `queryUnreadNotifications` to `unreadNotifications` * fix: null coalesce to 0 for notif read time
This commit is contained in:
@@ -439,7 +439,21 @@ class User extends AbstractModel
|
|||||||
*/
|
*/
|
||||||
public function getUnreadNotificationCount()
|
public function getUnreadNotificationCount()
|
||||||
{
|
{
|
||||||
return $this->getUnreadNotifications()->count();
|
return $this->unreadNotifications()->count();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return query builder for all notifications that have not been read yet.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||||
|
*/
|
||||||
|
protected function unreadNotifications()
|
||||||
|
{
|
||||||
|
return $this->notifications()
|
||||||
|
->whereIn('type', $this->getAlertableNotificationTypes())
|
||||||
|
->whereNull('read_at')
|
||||||
|
->where('is_deleted', false)
|
||||||
|
->whereSubjectVisibleTo($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -449,18 +463,7 @@ class User extends AbstractModel
|
|||||||
*/
|
*/
|
||||||
protected function getUnreadNotifications()
|
protected function getUnreadNotifications()
|
||||||
{
|
{
|
||||||
static $cached = [];
|
return $this->unreadNotifications()->get();
|
||||||
|
|
||||||
if (! isset($cached[$this->id])) {
|
|
||||||
$cached[$this->id] = $this->notifications()
|
|
||||||
->whereIn('type', $this->getAlertableNotificationTypes())
|
|
||||||
->whereNull('read_at')
|
|
||||||
->where('is_deleted', false)
|
|
||||||
->whereSubjectVisibleTo($this)
|
|
||||||
->get();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $cached[$this->id];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -470,9 +473,9 @@ class User extends AbstractModel
|
|||||||
*/
|
*/
|
||||||
public function getNewNotificationCount()
|
public function getNewNotificationCount()
|
||||||
{
|
{
|
||||||
return $this->getUnreadNotifications()->filter(function ($notification) {
|
return $this->unreadNotifications()
|
||||||
return $notification->created_at > $this->read_notifications_at ?: 0;
|
->where('created_at', '>', $this->read_notifications_at ?? 0)
|
||||||
})->count();
|
->count();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user