1
0
mirror of https://github.com/flarum/core.git synced 2025-07-30 21:20:24 +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:
David Wheatley
2022-07-28 19:14:32 +01:00
committed by GitHub
parent 7d67cfd1e7
commit cad6280a45

View File

@@ -439,7 +439,21 @@ class User extends AbstractModel
*/
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()
{
static $cached = [];
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];
return $this->unreadNotifications()->get();
}
/**
@@ -470,9 +473,9 @@ class User extends AbstractModel
*/
public function getNewNotificationCount()
{
return $this->getUnreadNotifications()->filter(function ($notification) {
return $notification->created_at > $this->read_notifications_at ?: 0;
})->count();
return $this->unreadNotifications()
->where('created_at', '>', $this->read_notifications_at ?? 0)
->count();
}
/**