pixelfed/app/Http/Controllers/DiscoverController.php

83 lines
2.5 KiB
PHP
Raw Normal View History

<?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,
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;
class DiscoverController extends Controller
{
2018-05-30 03:04:26 +00:00
public function __construct()
{
2018-08-28 03:07:36 +00:00
$this->middleware('auth');
2018-05-30 03:04:26 +00:00
}
2018-09-22 05:31:21 +00:00
public function home(Request $request)
{
2018-11-09 04:14:35 +00:00
return view('discover.home');
}
public function showTags(Request $request, $hashtag)
{
2018-08-28 03:07:36 +00:00
$this->validate($request, [
'page' => 'nullable|integer|min:1|max:10',
2019-02-10 20:25:37 +00:00
]);
2018-08-10 02:58:52 +00:00
2018-08-28 03:07:36 +00:00
$tag = Hashtag::with('posts')
2018-08-10 02:58:52 +00:00
->withCount('posts')
->whereSlug($hashtag)
->firstOrFail();
2018-08-28 03:07:36 +00:00
$posts = $tag->posts()
2018-12-21 19:53:10 +00:00
->whereNull('url')
->whereNull('uri')
2018-11-19 04:26:52 +00:00
->whereHas('media')
2018-10-20 18:29:37 +00:00
->withCount(['likes', 'comments'])
2018-08-10 02:58:52 +00:00
->whereIsNsfw(false)
->whereVisibility('public')
2018-08-28 03:07:36 +00:00
->orderBy('id', 'desc')
2018-08-10 02:58:52 +00:00
->simplePaginate(12);
2018-11-19 04:26:52 +00:00
if($posts->count() == 0) {
abort(404);
}
2018-08-28 03:07:36 +00:00
return view('discover.tags.show', compact('tag', 'posts'));
}
2019-02-10 20:25:37 +00:00
public function showCategory(Request $request, $slug)
{
$tag = DiscoverCategory::whereActive(true)
->whereSlug($slug)
->firstOrFail();
// todo refactor this mess
$tagids = $tag->hashtags->pluck('id')->toArray();
$sids = StatusHashtag::whereIn('hashtag_id', $tagids)->orderByDesc('status_id')->take(500)->pluck('status_id')->toArray();
$posts = Status::whereIn('id', $sids)->whereNull('uri')->whereType('photo')->whereNull('in_reply_to_id')->whereNull('reblog_of_id')->orderByDesc('created_at')->paginate(21);
$tag->posts_count = $tag->posts()->count();
return view('discover.tags.category', compact('tag', 'posts'));
}
public function showPersonal(Request $request)
{
$profile = Auth::user()->profile;
// todo refactor this mess
$tags = Hashtag::whereHas('posts')->orderByRaw('rand()')->take(5)->get();
$following = $profile->following->pluck('id');
$following = $following->push($profile->id)->toArray();
$posts = Status::withCount(['likes','comments'])->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->orderByDesc('created_at')->paginate(21);
$posts->post_count = Status::whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->count();
return view('discover.personal', compact('posts', 'tags'));
}
}