Update PublicApiController, add AccountService

This commit is contained in:
Daniel Supernault 2019-11-22 22:43:20 -07:00
parent 7b00eba398
commit 5ebd2c8a1b
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -22,7 +22,11 @@ use App\Transformer\Api\{
RelationshipTransformer, RelationshipTransformer,
StatusTransformer, StatusTransformer,
}; };
use App\Services\UserFilterService; use App\Services\{
AccountService,
PublicTimelineService,
UserFilterService
};
use App\Jobs\StatusPipeline\NewStatusPipeline; use App\Jobs\StatusPipeline\NewStatusPipeline;
use League\Fractal\Serializer\ArraySerializer; use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter; use League\Fractal\Pagination\IlluminatePaginatorAdapter;
@ -38,17 +42,12 @@ class PublicApiController extends Controller
$this->fractal->setSerializer(new ArraySerializer()); $this->fractal->setSerializer(new ArraySerializer());
} }
protected function getUserData() protected function getUserData($user)
{ {
if(false == Auth::check()) { if(!$user) {
return []; return [];
} else { } else {
$profile = Auth::user()->profile; return AccountService::get($user->profile_id);
if($profile->status) {
return [];
}
$user = new Fractal\Resource\Item($profile, new AccountTransformer());
return $this->fractal->createData($user)->toArray();
} }
} }
@ -90,7 +89,7 @@ class PublicApiController extends Controller
$item = new Fractal\Resource\Item($status, new StatusTransformer()); $item = new Fractal\Resource\Item($status, new StatusTransformer());
$res = [ $res = [
'status' => $this->fractal->createData($item)->toArray(), 'status' => $this->fractal->createData($item)->toArray(),
'user' => $this->getUserData(), 'user' => $this->getUserData($request->user()),
'likes' => $this->getLikes($status), 'likes' => $this->getLikes($status),
'shares' => $this->getShares($status), 'shares' => $this->getShares($status),
'reactions' => [ 'reactions' => [
@ -235,12 +234,13 @@ class PublicApiController extends Controller
$max = $request->input('max_id'); $max = $request->input('max_id');
$limit = $request->input('limit') ?? 3; $limit = $request->input('limit') ?? 3;
// $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() { $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
// return Profile::whereIsPrivate(true) return Profile::whereIsPrivate(true)
// ->orWhere('unlisted', true) ->orWhere('unlisted', true)
// ->orWhere('status', '!=', null) ->orWhere('status', '!=', null)
// ->pluck('id'); ->pluck('id')
// }); ->toArray();
});
// if(Auth::check()) { // if(Auth::check()) {
// // $pid = Auth::user()->profile->id; // // $pid = Auth::user()->profile->id;
@ -255,7 +255,17 @@ class PublicApiController extends Controller
// $filtered = []; // $filtered = [];
// } // }
$filtered = Auth::check() ? UserFilterService::filters(Auth::user()->profile_id) : []; $filtered = Auth::check() ? array_merge($private, UserFilterService::filters(Auth::user()->profile_id)) : [];
if($max == 0) {
$res = PublicTimelineService::count();
if($res == 0) {
PublicTimelineService::warmCache();
$res = PublicTimelineService::get(0,4);
} else {
$res = PublicTimelineService::get(0,4);
}
return response($res);
}
if($min || $max) { if($min || $max) {
$dir = $min ? '>' : '<'; $dir = $min ? '>' : '<';
@ -439,98 +449,7 @@ class PublicApiController extends Controller
public function networkTimelineApi(Request $request) public function networkTimelineApi(Request $request)
{ {
if(!Auth::check()) { return response()->json([]);
return abort(403);
}
$this->validate($request,[
'page' => 'nullable|integer|max:40',
'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
'limit' => 'nullable|integer|max:20'
]);
$page = $request->input('page');
$min = $request->input('min_id');
$max = $request->input('max_id');
$limit = $request->input('limit') ?? 3;
// TODO: Use redis for timelines
// $timeline = Timeline::build()->local();
$pid = Auth::user()->profile->id;
$private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
return Profile::whereIsPrivate(true)
->orWhere('unlisted', true)
->orWhere('status', '!=', null)
->pluck('id');
});
$filters = UserFilter::whereUserId($pid)
->whereFilterableType('App\Profile')
->whereIn('filter_type', ['mute', 'block'])
->pluck('filterable_id')->toArray();
$filtered = array_merge($private->toArray(), $filters);
if($min || $max) {
$dir = $min ? '>' : '<';
$id = $min ?? $max;
$timeline = Status::select(
'id',
'uri',
'caption',
'rendered',
'profile_id',
'type',
'in_reply_to_id',
'reblog_of_id',
'is_nsfw',
'scope',
'local',
'reply_count',
'comments_disabled',
'created_at',
'updated_at'
)->where('id', $dir, $id)
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
->whereNotIn('profile_id', $filtered)
->whereNotNull('uri')
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->whereVisibility('public')
->latest()
->limit($limit)
->get();
} else {
$timeline = Status::select(
'id',
'uri',
'caption',
'rendered',
'profile_id',
'type',
'in_reply_to_id',
'reblog_of_id',
'is_nsfw',
'scope',
'local',
'reply_count',
'comments_disabled',
'created_at',
'updated_at'
)->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
->whereNotIn('profile_id', $filtered)
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->whereNotNull('uri')
->whereVisibility('public')
->latest()
->simplePaginate($limit);
}
$fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
$res = $this->fractal->createData($fractal)->toArray();
return response()->json($res);
} }
public function relationships(Request $request) public function relationships(Request $request)
@ -555,10 +474,7 @@ class PublicApiController extends Controller
public function account(Request $request, $id) public function account(Request $request, $id)
{ {
$profile = Profile::whereNull('status')->findOrFail($id); $res = AccountService::get($id);
$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
$res = $this->fractal->createData($resource)->toArray();
return response()->json($res); return response()->json($res);
} }