forked from mirror/pixelfed
Update PublicApiController
This commit is contained in:
parent
59f8292e07
commit
02b573018b
1 changed files with 124 additions and 0 deletions
|
@ -12,6 +12,7 @@ use App\{
|
|||
Profile,
|
||||
StatusHashtag,
|
||||
Status,
|
||||
UserFilter
|
||||
};
|
||||
use Auth,Cache;
|
||||
use Carbon\Carbon;
|
||||
|
@ -194,4 +195,127 @@ class PublicApiController extends Controller
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public function publicTimelineApi(Request $request)
|
||||
{
|
||||
if(!Auth::check()) {
|
||||
return abort(403);
|
||||
}
|
||||
|
||||
$this->validate($request,[
|
||||
'page' => 'nullable|integer|max:40',
|
||||
'min_id' => 'nullable|integer',
|
||||
'max_id' => 'nullable|integer',
|
||||
'limit' => 'nullable|integer|max:20'
|
||||
]);
|
||||
|
||||
$page = $request->input('page');
|
||||
$min = $request->input('min_id');
|
||||
$max = $request->input('max_id');
|
||||
$limit = $request->input('limit') ?? 10;
|
||||
|
||||
// TODO: Use redis for timelines
|
||||
// $timeline = Timeline::build()->local();
|
||||
$pid = Auth::user()->profile->id;
|
||||
|
||||
$private = Profile::whereIsPrivate(true)->where('id', '!=', $pid)->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::whereHas('media')
|
||||
->where('id', $dir, $id)
|
||||
->whereNotIn('profile_id', $filtered)
|
||||
->whereNull('in_reply_to_id')
|
||||
->whereNull('reblog_of_id')
|
||||
->whereVisibility('public')
|
||||
->withCount(['comments', 'likes'])
|
||||
->orderBy('created_at', 'desc')
|
||||
->limit($limit)
|
||||
->get();
|
||||
} else {
|
||||
$timeline = Status::whereHas('media')
|
||||
->whereNotIn('profile_id', $filtered)
|
||||
->whereNull('in_reply_to_id')
|
||||
->whereNull('reblog_of_id')
|
||||
->whereVisibility('public')
|
||||
->withCount(['comments', 'likes'])
|
||||
->orderBy('created_at', 'desc')
|
||||
->simplePaginate($limit);
|
||||
}
|
||||
|
||||
$fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
|
||||
$res = $this->fractal->createData($fractal)->toArray();
|
||||
return response()->json($res);
|
||||
|
||||
}
|
||||
|
||||
public function homeTimelineApi(Request $request)
|
||||
{
|
||||
if(!Auth::check()) {
|
||||
return abort(403);
|
||||
}
|
||||
|
||||
$this->validate($request,[
|
||||
'page' => 'nullable|integer|max:40',
|
||||
'min_id' => 'nullable|integer',
|
||||
'max_id' => 'nullable|integer',
|
||||
'limit' => 'nullable|integer|max:20'
|
||||
]);
|
||||
|
||||
$page = $request->input('page');
|
||||
$min = $request->input('min_id');
|
||||
$max = $request->input('max_id');
|
||||
$limit = $request->input('limit') ?? 10;
|
||||
|
||||
// TODO: Use redis for timelines
|
||||
// $timeline = Timeline::build()->local();
|
||||
$pid = Auth::user()->profile->id;
|
||||
|
||||
$following = Follower::whereProfileId($pid)->pluck('following_id');
|
||||
$following->push($pid)->toArray();
|
||||
|
||||
$private = Profile::whereIsPrivate(true)->where('id', '!=', $pid)->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::whereHas('media')
|
||||
->where('id', $dir, $id)
|
||||
->whereIn('profile_id', $following)
|
||||
->whereNotIn('profile_id', $filtered)
|
||||
->whereNull('in_reply_to_id')
|
||||
->whereNull('reblog_of_id')
|
||||
->whereVisibility('public')
|
||||
->withCount(['comments', 'likes'])
|
||||
->orderBy('created_at', 'desc')
|
||||
->limit($limit)
|
||||
->get();
|
||||
} else {
|
||||
$timeline = Status::whereHas('media')
|
||||
->whereIn('profile_id', $following)
|
||||
->whereNotIn('profile_id', $filtered)
|
||||
->whereNull('in_reply_to_id')
|
||||
->whereNull('reblog_of_id')
|
||||
->whereVisibility('public')
|
||||
->withCount(['comments', 'likes'])
|
||||
->orderBy('created_at', 'desc')
|
||||
->simplePaginate($limit);
|
||||
}
|
||||
|
||||
$fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
|
||||
$res = $this->fractal->createData($fractal)->toArray();
|
||||
return response()->json($res);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue