Update PublicApiController, use account service

This commit is contained in:
Daniel Supernault 2021-07-26 18:47:40 -06:00
parent 2376580eb7
commit ee0028bc57
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
3 changed files with 22 additions and 30 deletions

View file

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

View file

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

View file

@ -2,31 +2,15 @@
namespace App\Services; namespace App\Services;
use Cache; class ProfileService
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 {
public static function get($id) public static function get($id)
{ {
$key = 'profile:model:' . $id; return AccountService::get($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;
} }
public static function del($id)
{
return AccountService::del($id);
}
} }