From f6ed560e78acc44f3486a03be0ae3b9a75654b0d Mon Sep 17 00:00:00 2001
From: Daniel Supernault <danielsupernault@gmail.com>
Date: Wed, 17 Aug 2022 23:08:40 -0600
Subject: [PATCH] Limit NotificationService to 400 items

---
 app/Http/Controllers/Api/ApiV1Controller.php |  2 +-
 app/Services/NotificationService.php         | 15 ++++++++++++++-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/app/Http/Controllers/Api/ApiV1Controller.php b/app/Http/Controllers/Api/ApiV1Controller.php
index 68558a3c2..276b1fb2d 100644
--- a/app/Http/Controllers/Api/ApiV1Controller.php
+++ b/app/Http/Controllers/Api/ApiV1Controller.php
@@ -2449,7 +2449,7 @@ class ApiV1Controller extends Controller
 			'media_ids' => 'sometimes|array|max:' . config_cache('pixelfed.max_album_length'),
 			'sensitive' => 'nullable',
 			'visibility' => 'string|in:private,unlisted,public',
-			'spoiler_text' => 'sometimes|string|max:140',
+			'spoiler_text' => 'sometimes|max:140',
 		]);
 
 		if(config('costar.enabled') == true) {
diff --git a/app/Services/NotificationService.php b/app/Services/NotificationService.php
index cd0044bb7..264f9df31 100644
--- a/app/Services/NotificationService.php
+++ b/app/Services/NotificationService.php
@@ -194,6 +194,9 @@ class NotificationService {
 
 	public static function set($id, $val)
 	{
+		if(self::count($id) > 400) {
+			Redis::zpopmin(self::CACHE_KEY . $id);
+		}
 		return Redis::zadd(self::CACHE_KEY . $id, $val, $val);
 	}
 
@@ -220,7 +223,7 @@ class NotificationService {
 
 	public static function getNotification($id)
 	{
-		return Cache::remember('service:notification:'.$id, 86400, function() use($id) {
+		$notification = Cache::remember('service:notification:'.$id, 86400, function() use($id) {
 			$n = Notification::with('item')->find($id);
 
 			if(!$n) {
@@ -238,6 +241,16 @@ class NotificationService {
 			$resource = new Fractal\Resource\Item($n, new NotificationTransformer());
 			return $fractal->createData($resource)->toArray();
 		});
+
+		if(!$notification) {
+			return;
+		}
+
+		if(isset($notification['account'])) {
+			$notification['account'] = AccountService::get($notification['account']['id'], true);
+		}
+
+		return $notification;
 	}
 
 	public static function setNotification(Notification $notification)
-- 
GitLab