mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-22 14:31:26 +00:00
Update PublicApiController, use account service
This commit is contained in:
parent
2376580eb7
commit
ee0028bc57
3 changed files with 22 additions and 30 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue