فهرست منبع

Add ReblogService, improve reblogged state for api entities

Daniel Supernault 3 سال پیش
والد
کامیت
6cfd6be523

+ 7 - 2
app/Http/Controllers/Api/ApiV1Controller.php

@@ -65,6 +65,7 @@ use App\Services\{
 	NotificationService,
 	MediaPathService,
 	PublicTimelineService,
+	ReblogService,
 	RelationshipService,
 	SearchApiV2Service,
 	StatusService,
@@ -1646,6 +1647,7 @@ class ApiV1Controller extends Controller
 
 				if($pid) {
 					$status['favourited'] = (bool) LikeService::liked($pid, $s['id']);
+					$status['reblogged'] = (bool) ReblogService::get($pid, $status['id']);
 				}
 				return $status;
 			})
@@ -1676,6 +1678,7 @@ class ApiV1Controller extends Controller
 
 				if($pid) {
 					$status['favourited'] = (bool) LikeService::liked($pid, $s['id']);
+					$status['reblogged'] = (bool) ReblogService::get($pid, $status['id']);
 				}
 				return $status;
 			})
@@ -1798,6 +1801,7 @@ class ApiV1Controller extends Controller
 
 			if($user) {
 				$status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
+				$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $status['id']);
 			}
 			return $status;
 		})
@@ -1839,7 +1843,7 @@ class ApiV1Controller extends Controller
 		}
 
 		$res['favourited'] = LikeService::liked($user->profile_id, $res['id']);
-		$res['reblogged'] = false;
+		$res['reblogged'] = ReblogService::get($user->profile_id, $res['id']);
 		return response()->json($res);
 	}
 
@@ -2238,7 +2242,7 @@ class ApiV1Controller extends Controller
 		}
 
 		StatusService::del($status->id);
-
+		ReblogService::add($user->profile_id, $status->id);
 		$res = StatusService::getMastodon($status->id);
 		$res['reblogged'] = true;
 
@@ -2278,6 +2282,7 @@ class ApiV1Controller extends Controller
 		}
 
 		UndoSharePipeline::dispatch($reblog);
+		ReblogService::del($user->profile_id, $status->id);
 
 		$res = StatusService::getMastodon($status->id);
 		$res['reblogged'] = true;

+ 10 - 1
app/Http/Controllers/PublicApiController.php

@@ -32,6 +32,7 @@ use App\Services\{
     LikeService,
     PublicTimelineService,
     ProfileService,
+    ReblogService,
     RelationshipService,
     StatusService,
     SnowflakeService,
@@ -329,6 +330,7 @@ class PublicApiController extends Controller
                                }
                                $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
                                $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
+                               $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
                                return $status;
                           })
                           ->filter(function($s) use($filtered) {
@@ -372,6 +374,7 @@ class PublicApiController extends Controller
                                }
                                $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
                                $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
+                               $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
                                return $status;
                           })
                           ->filter(function($s) use($filtered) {
@@ -402,7 +405,7 @@ class PublicApiController extends Controller
                 if($user) {
                     $status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
                     $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $k);
-
+                    $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k);
                     $status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
                 }
                 return $status;
@@ -524,6 +527,7 @@ class PublicApiController extends Controller
                            }
                            $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
                            $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
+                           $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
                            return $status;
                       })
                       ->filter(function($s) use($filtered) {
@@ -569,6 +573,7 @@ class PublicApiController extends Controller
                            }
                            $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
                            $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
+                           $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
                            return $status;
                       })
                       ->filter(function($s) use($filtered) {
@@ -623,6 +628,8 @@ class PublicApiController extends Controller
                      ->map(function($s) use ($user) {
                             $status = StatusService::get($s->id);
                             $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
+                            $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
+                            $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
                             return $status;
                       });
             $res = $timeline->toArray();
@@ -646,6 +653,8 @@ class PublicApiController extends Controller
                           ->map(function($s) use ($user) {
                                 $status = StatusService::get($s->id);
                                 $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
+                                $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
+                                $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
                                 return $status;
                           });
                 $res = $timeline->toArray();

+ 3 - 0
app/Http/Controllers/StatusController.php

@@ -25,6 +25,7 @@ use Illuminate\Support\Str;
 use App\Services\HashidService;
 use App\Services\StatusService;
 use App\Util\Media\License;
+use App\Services\ReblogService;
 
 class StatusController extends Controller
 {
@@ -245,6 +246,7 @@ class StatusController extends Controller
 				  ->get();
 			foreach ($shares as $share) {
 				UndoSharePipeline::dispatch($share);
+				ReblogService::del($profile->id, $status->id);
 				$count--;
 			}
 		} else {
@@ -255,6 +257,7 @@ class StatusController extends Controller
 			$share->save();
 			$count++;
 			SharePipeline::dispatch($share);
+			ReblogService::add($profile->id, $status->id);
 		}
 
 		Cache::forget('status:'.$status->id.':sharedby:userid:'.$user->id);

+ 29 - 0
app/Services/ReblogService.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Services;
+
+use Illuminate\Support\Facades\Redis;
+
+class ReblogService
+{
+	const CACHE_KEY = 'pf:services:reblogs:';
+
+	public static function get($profileId, $statusId)
+	{
+		if (!Redis::zcard(self::CACHE_KEY . $profileId)) {
+			return false;
+		}
+
+		return Redis::zscore(self::CACHE_KEY . $profileId, $statusId) != null;
+	}
+
+	public static function add($profileId, $statusId)
+	{
+		return Redis::zadd(self::CACHE_KEY . $profileId, $statusId, $statusId);
+	}
+
+	public static function del($profileId, $statusId)
+	{
+		return Redis::zrem(self::CACHE_KEY . $profileId, $statusId);
+	}
+}

+ 1 - 4
app/Services/StatusService.php

@@ -165,10 +165,7 @@ class StatusService
 	public static function isShared($id, $pid = null)
 	{
 		return $pid ?
-			DB::table('statuses')
-				->where('reblog_of_id', $id)
-				->where('profile_id', $pid)
-				->exists() :
+			ReblogService::get($pid, $id) :
 			false;
 	}