1
0
Эх сурвалжийг харах

Update PublicApiController, use account service

Daniel Supernault 4 жил өмнө
parent
commit
ee0028bc57

+ 11 - 3
app/Http/Controllers/PublicApiController.php

@@ -29,6 +29,7 @@ use App\Services\{
     AccountService,
     LikeService,
     PublicTimelineService,
+    ProfileService,
     StatusService,
     SnowflakeService,
     UserFilterService
@@ -593,6 +594,7 @@ class PublicApiController extends Controller
         abort_unless(Auth::check(), 403);
         $profile = Profile::with('user')->whereNull('status')->findOrFail($id);
         $owner = Auth::id() == $profile->user_id;
+
         if(Auth::id() != $profile->user_id && $profile->is_private) {
             return response()->json([]);
         }
@@ -602,9 +604,15 @@ class PublicApiController extends Controller
         if(!$owner && $request->page > 5) {
         	return [];
         }
-        $followers = $profile->followers()->orderByDesc('followers.created_at')->paginate(10);
-        $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
-        $res = $this->fractal->createData($resource)->toArray();
+
+        $res = Follower::select('id', 'profile_id', 'following_id')
+            ->whereFollowingId($profile->id)
+            ->orderByDesc('id')
+            ->simplePaginate(10)
+            ->map(function($follower) {
+                return ProfileService::get($follower['profile_id']);
+            })
+            ->toArray();
 
         return response()->json($res);
     }

+ 4 - 4
app/Services/AccountService.php

@@ -8,8 +8,8 @@ use App\Transformer\Api\AccountTransformer;
 use League\Fractal;
 use League\Fractal\Serializer\ArraySerializer;
 
-class AccountService {
-
+class AccountService
+{
 	const CACHE_KEY = 'pf:services:account:';
 
 	public static function get($id)
@@ -19,7 +19,7 @@ class AccountService {
 		}
 
 		$key = self::CACHE_KEY . $id;
-		$ttl = now()->addMinutes(15);
+		$ttl = now()->addHours(12);
 
 		return Cache::remember($key, $ttl, function() use($id) {
 			$fractal = new Fractal\Manager();
@@ -35,4 +35,4 @@ class AccountService {
 		return Cache::forget(self::CACHE_KEY . $id);
 	}
 
-}
+}

+ 7 - 23
app/Services/ProfileService.php

@@ -2,31 +2,15 @@
 
 namespace App\Services;
 
-use Cache;
-use Illuminate\Support\Facades\Redis;
-use App\Transformer\Api\AccountTransformer;
-use League\Fractal;
-use League\Fractal\Serializer\ArraySerializer;
-use League\Fractal\Pagination\IlluminatePaginatorAdapter;
-use App\Profile;
-
-class ProfileService {
-
+class ProfileService
+{
 	public static function get($id)
 	{
-		$key = 'profile:model:' . $id;
-		$ttl = now()->addHours(4);
-		$res = Cache::remember($key, $ttl, function() use($id) {
-			$profile = Profile::find($id);
-			if(!$profile) {
-				return false;
-			}
-			$fractal = new Fractal\Manager();
-			$fractal->setSerializer(new ArraySerializer());
-			$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
-			return $fractal->createData($resource)->toArray();
-		});
-		return $res;
+		return AccountService::get($id);
 	}
 
+	public static function del($id)
+	{
+		return AccountService::del($id);
+	}
 }