Browse Source

Improve CollectionService cache invalidation, fixes #3548

Daniel Supernault 3 years ago
parent
commit
44f4a9edd9

+ 0 - 1
.editorconfig

@@ -1,7 +1,6 @@
 root = true
 
 [*]
-indent_style = space
 indent_size = 4
 end_of_line = lf
 charset = utf-8

+ 12 - 0
app/Http/Controllers/CollectionController.php

@@ -132,6 +132,18 @@ class CollectionController extends Controller
         $collection = Collection::whereProfileId($profileId)->findOrFail($collectionId);
         $count = $collection->items()->count();
 
+        if($count) {
+            CollectionItem::whereCollectionId($collection->id)
+                ->get()
+                ->filter(function($col) {
+                    return StatusService::get($col->object_id, false) == null;
+                })
+                ->each(function($col) use($collectionId) {
+                    CollectionService::removeItem($collectionId, $col->object_id);
+                    $col->delete();
+                });
+        }
+
         $max = config('pixelfed.max_collection_length');
         if($count >= $max) {
             abort(400, 'You can only add '.$max.' posts per collection');

+ 15 - 0
app/Jobs/StatusPipeline/StatusDelete.php

@@ -5,6 +5,7 @@ namespace App\Jobs\StatusPipeline;
 use DB, Storage;
 use App\{
 	AccountInterstitial,
+	CollectionItem,
 	MediaTag,
 	Notification,
 	Report,
@@ -25,6 +26,7 @@ use GuzzleHttp\Pool;
 use GuzzleHttp\Client;
 use GuzzleHttp\Promise;
 use App\Util\ActivityPub\HttpSignature;
+use App\Services\CollectionService;
 use App\Services\StatusService;
 use App\Services\MediaStorageService;
 
@@ -89,6 +91,19 @@ class StatusDelete implements ShouldQueue
 				$parent->save();
 			});
 		}
+
+        DB::transaction(function() use($status) {
+            CollectionItem::whereObjectType('App\Status')
+                ->whereObjectId($status->id)
+                ->get()
+                ->each(function($col) {
+                    $id = $col->collection_id;
+                    $sid = $col->object_id;
+                    $col->delete();
+                    CollectionService::removeItem($id, $sid);
+                });
+        });
+
 		DB::transaction(function() use($status) {
 			$comments = Status::where('in_reply_to_id', $status->id)->get();
 			foreach ($comments as $comment) {

+ 6 - 2
app/Services/CollectionService.php

@@ -9,7 +9,7 @@ use Illuminate\Support\Facades\Redis;
 
 class CollectionService
 {
-	const CACHE_KEY = 'pf:services:collections:';
+	const CACHE_KEY = 'pf:services:collections-v1:';
 
 	public static function getItems($id, $start = 0, $stop = 10)
 	{
@@ -41,6 +41,9 @@ class CollectionService
 			return CollectionItem::whereCollectionId($id)
 				->orderBy('order')
 				->get()
+				->filter(function($item) use($id) {
+					return StatusService::get($item->object_id) != null;
+				})
 				->each(function($item) use ($id) {
 					self::addItem($id, $item->object_id, $item->order);
 				})
@@ -80,13 +83,14 @@ class CollectionService
 				'visibility' => $collection->visibility,
 				'title' => $collection->title,
 				'description' => $collection->description,
-				'thumb' => self::getThumb($id),
+				'thumb' => '/storage/no-preview.png',
 				'url' => $collection->url(),
 				'published_at' => $collection->published_at
 			];
 		});
 
 		if($collection) {
+			$collection['thumb'] = self::getThumb($id);
 			$collection['post_count'] = self::count($id);
 		}