From ee0028bc5734db07ff6adca4e5fc29b709c00bb1 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 26 Jul 2021 18:47:40 -0600 Subject: [PATCH] Update PublicApiController, use account service --- app/Http/Controllers/PublicApiController.php | 14 +++++++-- app/Services/AccountService.php | 8 +++--- app/Services/ProfileService.php | 30 +++++--------------- 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/app/Http/Controllers/PublicApiController.php b/app/Http/Controllers/PublicApiController.php index fdfae931c..5059f8161 100644 --- a/app/Http/Controllers/PublicApiController.php +++ b/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); } diff --git a/app/Services/AccountService.php b/app/Services/AccountService.php index 38e29169d..8f9f5c3b9 100644 --- a/app/Services/AccountService.php +++ b/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); } -} \ No newline at end of file +} diff --git a/app/Services/ProfileService.php b/app/Services/ProfileService.php index 67a0cb4c8..43f2ff0e4 100644 --- a/app/Services/ProfileService.php +++ b/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); + } }