public function follow(Request $request, FeedModel $model, FeedRepository $repository)
{
if (is_null($user = $request->user('api'))) {
abort(401);
}
$limit = $request->query('limit', 15);
$after = $request->query('after');
$feeds = $model->leftJoin('user_follow', function ($join) use ($user) {
$join->where('user_follow.user_id', $user->id);
})
->whereDoesntHave('blacks', function ($query) use ($user) {
$query->where('user_id', $user);
})
->where(function ($query) use ($user) {
$query->whereColumn('feeds.user_id', '=', 'user_follow.target')
->orWhere('feeds.user_id', $user->id);
})
->with([
'user' => function ($query) {
return $query->withTrashed();
},
'topics' => function ($query) {
return $query->select('id', 'name');
},
'user.certification',
'pinnedComments' => function ($query) {
return $query->with([
'user',
'user.certification',
])
->where('expires_at', '>', new Carbon)
->orderBy('amount', 'desc')
->orderBy('created_at', 'desc');
},
])
->when((bool) $after, function ($query) use ($after) {
return $query->where('feeds.id', '<', $after);
})
->distinct()
->select('feeds.*')
->orderBy('feeds.id', 'desc')
->limit($limit)
->get();
return $feeds->map(function (FeedModel $feed) use ($repository, $user) {
$feed->feed_view_count += 1;
$feed->hot = $feed->makeHotValue();
$feed->save();
$repository->setModel($feed);
$repository->images();
$repository->format($user->id);
$repository->previewComments();
$feed->has_collect = $feed->collected($user->id);
$feed->has_like = $feed->liked($user);
return $feed;
});
}