2018-05-26 22:49:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2018-09-22 05:23:16 +00:00
|
|
|
use App\{
|
2019-02-10 20:25:37 +00:00
|
|
|
DiscoverCategory,
|
2018-09-22 05:23:16 +00:00
|
|
|
Follower,
|
|
|
|
Hashtag,
|
2019-07-08 06:04:02 +00:00
|
|
|
HashtagFollow,
|
2018-09-22 05:23:16 +00:00
|
|
|
Profile,
|
|
|
|
Status,
|
2019-02-10 20:25:37 +00:00
|
|
|
StatusHashtag,
|
2018-09-22 05:23:16 +00:00
|
|
|
UserFilter
|
|
|
|
};
|
|
|
|
use Auth, DB, Cache;
|
2018-08-28 03:07:36 +00:00
|
|
|
use Illuminate\Http\Request;
|
2019-11-24 22:48:53 +00:00
|
|
|
use App\Transformer\Api\AccountTransformer;
|
|
|
|
use App\Transformer\Api\AccountWithStatusesTransformer;
|
2020-08-14 00:05:36 +00:00
|
|
|
use App\Transformer\Api\StatusTransformer;
|
2019-06-03 19:13:01 +00:00
|
|
|
use App\Transformer\Api\StatusStatelessTransformer;
|
|
|
|
use League\Fractal;
|
|
|
|
use League\Fractal\Serializer\ArraySerializer;
|
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
2019-07-05 02:21:20 +00:00
|
|
|
use App\Services\StatusHashtagService;
|
2021-01-13 04:52:07 +00:00
|
|
|
use App\Services\SnowflakeService;
|
|
|
|
use App\Services\StatusService;
|
2018-05-26 22:49:39 +00:00
|
|
|
|
|
|
|
class DiscoverController extends Controller
|
|
|
|
{
|
2019-06-03 19:13:01 +00:00
|
|
|
protected $fractal;
|
|
|
|
|
2018-05-30 03:04:26 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
2019-06-03 19:13:01 +00:00
|
|
|
$this->fractal = new Fractal\Manager();
|
|
|
|
$this->fractal->setSerializer(new ArraySerializer());
|
2018-05-30 03:04:26 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 05:31:21 +00:00
|
|
|
public function home(Request $request)
|
2018-05-26 22:49:39 +00:00
|
|
|
{
|
2021-02-06 04:09:03 +00:00
|
|
|
abort_if(!Auth::check() && config('instance.discover.public') == false, 403);
|
2018-11-09 04:14:35 +00:00
|
|
|
return view('discover.home');
|
2018-05-26 22:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function showTags(Request $request, $hashtag)
|
|
|
|
{
|
2019-07-08 06:04:02 +00:00
|
|
|
abort_if(!config('instance.discover.tags.is_public') && !Auth::check(), 403);
|
|
|
|
|
2020-02-15 05:11:42 +00:00
|
|
|
$tag = Hashtag::whereName($hashtag)
|
|
|
|
->orWhere('slug', $hashtag)
|
|
|
|
->firstOrFail();
|
2019-07-05 02:21:20 +00:00
|
|
|
$tagCount = StatusHashtagService::count($tag->id);
|
|
|
|
return view('discover.tags.show', compact('tag', 'tagCount'));
|
2018-05-26 22:49:39 +00:00
|
|
|
}
|
2019-02-10 20:25:37 +00:00
|
|
|
|
|
|
|
public function showCategory(Request $request, $slug)
|
|
|
|
{
|
2021-01-31 06:13:54 +00:00
|
|
|
abort(404);
|
2019-02-10 20:25:37 +00:00
|
|
|
}
|
|
|
|
|
2019-05-26 00:56:21 +00:00
|
|
|
public function showLoops(Request $request)
|
|
|
|
{
|
2021-01-31 06:13:54 +00:00
|
|
|
abort(404);
|
2019-05-26 00:56:21 +00:00
|
|
|
}
|
2019-06-03 19:13:01 +00:00
|
|
|
|
|
|
|
public function loopsApi(Request $request)
|
|
|
|
{
|
2021-01-31 06:13:54 +00:00
|
|
|
abort(404);
|
2019-06-03 19:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function loopWatch(Request $request)
|
|
|
|
{
|
|
|
|
return response()->json(200);
|
|
|
|
}
|
2019-07-05 02:21:20 +00:00
|
|
|
|
|
|
|
public function getHashtags(Request $request)
|
|
|
|
{
|
2019-07-08 06:04:02 +00:00
|
|
|
$auth = Auth::check();
|
2019-07-08 06:09:50 +00:00
|
|
|
abort_if(!config('instance.discover.tags.is_public') && !$auth, 403);
|
2019-07-08 06:04:02 +00:00
|
|
|
|
2019-07-05 02:21:20 +00:00
|
|
|
$this->validate($request, [
|
2020-08-14 00:52:20 +00:00
|
|
|
'hashtag' => 'required|string|min:1|max:124',
|
2019-12-05 02:54:26 +00:00
|
|
|
'page' => 'nullable|integer|min:1|max:' . ($auth ? 29 : 10)
|
2019-07-05 02:21:20 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$page = $request->input('page') ?? '1';
|
2019-07-11 00:58:52 +00:00
|
|
|
$end = $page > 1 ? $page * 9 : 0;
|
2019-07-05 02:21:20 +00:00
|
|
|
$tag = $request->input('hashtag');
|
|
|
|
|
|
|
|
$hashtag = Hashtag::whereName($tag)->firstOrFail();
|
2019-07-08 06:04:02 +00:00
|
|
|
if($page == 1) {
|
2021-01-30 23:35:52 +00:00
|
|
|
$res['follows'] = HashtagFollow::whereUserId(Auth::id())
|
|
|
|
->whereHashtagId($hashtag->id)
|
|
|
|
->exists();
|
2019-07-08 06:04:02 +00:00
|
|
|
}
|
2021-01-30 23:35:52 +00:00
|
|
|
$res['hashtag'] = [
|
|
|
|
'name' => $hashtag->name,
|
|
|
|
'url' => $hashtag->url()
|
|
|
|
];
|
|
|
|
$res['tags'] = StatusHashtagService::get($hashtag->id, $page, $end);
|
2019-07-05 02:21:20 +00:00
|
|
|
return $res;
|
|
|
|
}
|
2019-09-07 05:25:55 +00:00
|
|
|
|
|
|
|
public function profilesDirectory(Request $request)
|
|
|
|
{
|
2021-01-31 06:13:54 +00:00
|
|
|
return redirect('/')
|
|
|
|
->with('statusRedirect', 'The Profile Directory is unavailable at this time.');
|
2019-11-24 22:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function profilesDirectoryApi(Request $request)
|
|
|
|
{
|
2021-01-18 05:02:27 +00:00
|
|
|
return ['error' => 'Temporarily unavailable.'];
|
2019-09-07 05:25:55 +00:00
|
|
|
}
|
2020-08-14 00:05:36 +00:00
|
|
|
|
|
|
|
public function trendingApi(Request $request)
|
|
|
|
{
|
2021-02-06 04:09:03 +00:00
|
|
|
abort_if(config('instance.discover.public') == false && !Auth::check(), 403);
|
|
|
|
|
2020-08-14 00:05:36 +00:00
|
|
|
$this->validate($request, [
|
2021-01-11 07:31:21 +00:00
|
|
|
'range' => 'nullable|string|in:daily,monthly'
|
2020-08-14 00:05:36 +00:00
|
|
|
]);
|
|
|
|
|
2021-01-13 04:52:07 +00:00
|
|
|
$range = $request->input('range') == 'monthly' ? 31 : 1;
|
|
|
|
|
|
|
|
$key = ':api:discover:trending:v2.8:range:' . $range;
|
|
|
|
$ttl = now()->addMinutes(15);
|
|
|
|
|
|
|
|
$ids = Cache::remember($key, $ttl, function() use($range) {
|
|
|
|
$days = $range == 1 ? 2 : 31;
|
|
|
|
$min_id = SnowflakeService::byDate(now()->subDays($days));
|
|
|
|
return Status::select(
|
|
|
|
'id',
|
|
|
|
'scope',
|
|
|
|
'type',
|
|
|
|
'is_nsfw',
|
|
|
|
'likes_count',
|
|
|
|
'created_at'
|
|
|
|
)
|
|
|
|
->where('id', '>', $min_id)
|
2021-01-13 05:04:25 +00:00
|
|
|
->whereNull('uri')
|
2021-01-13 04:52:07 +00:00
|
|
|
->whereScope('public')
|
|
|
|
->whereIn('type', [
|
|
|
|
'photo',
|
|
|
|
'photo:album',
|
|
|
|
'video'
|
|
|
|
])
|
2020-11-26 00:59:13 +00:00
|
|
|
->whereIsNsfw(false)
|
2020-11-22 01:58:08 +00:00
|
|
|
->orderBy('likes_count','desc')
|
2021-01-13 04:52:07 +00:00
|
|
|
->take(15)
|
|
|
|
->pluck('id');
|
2020-08-14 00:05:36 +00:00
|
|
|
});
|
2021-01-13 04:52:07 +00:00
|
|
|
|
|
|
|
$res = $ids->map(function($s) {
|
|
|
|
return StatusService::get($s);
|
|
|
|
});
|
|
|
|
|
|
|
|
return response()->json($res);
|
2020-08-14 00:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function trendingHashtags(Request $request)
|
|
|
|
{
|
2021-01-18 05:02:27 +00:00
|
|
|
return [];
|
2020-08-14 00:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function trendingPlaces(Request $request)
|
|
|
|
{
|
2021-01-18 05:02:27 +00:00
|
|
|
return [];
|
2020-08-14 00:05:36 +00:00
|
|
|
}
|
2018-05-26 22:49:39 +00:00
|
|
|
}
|