1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2025-03-04 02:28:08 +00:00
pixelfed/app/Http/Controllers/DiscoverController.php

95 lines
3.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
2018-09-21 23:23:16 -06:00
use App\{
2019-02-10 13:25:37 -07:00
DiscoverCategory,
2018-09-21 23:23:16 -06:00
Follower,
Hashtag,
Profile,
Status,
2019-02-10 13:25:37 -07:00
StatusHashtag,
2018-09-21 23:23:16 -06:00
UserFilter
};
use Auth, DB, Cache;
2018-08-28 03:07:36 +00:00
use Illuminate\Http\Request;
class DiscoverController extends Controller
{
2018-05-29 21:04:26 -06:00
public function __construct()
{
2018-08-28 03:07:36 +00:00
$this->middleware('auth');
2018-05-29 21:04:26 -06:00
}
2018-09-21 23:31:21 -06:00
public function home(Request $request)
{
2018-11-08 21:14:35 -07:00
return view('discover.home');
}
public function showTags(Request $request, $hashtag)
{
2019-03-02 12:11:24 -07:00
$tag = Hashtag::whereSlug($hashtag)
2018-08-09 20:58:52 -06:00
->firstOrFail();
2019-03-02 21:46:49 -07:00
$page = 1;
2019-03-02 01:38:44 -07:00
$key = 'discover:tag-'.$tag->id.':page-'.$page;
2019-03-02 21:46:49 -07:00
$keyMinutes = 15;
2019-03-02 01:38:44 -07:00
$posts = Cache::remember($key, now()->addMinutes($keyMinutes), function() use ($tag, $request) {
return $tag->posts()
2018-12-21 12:53:10 -07:00
->whereNull('url')
->whereNull('uri')
2018-11-18 21:26:52 -07:00
->whereHas('media')
2018-10-20 12:29:37 -06:00
->withCount(['likes', 'comments'])
2018-08-09 20:58:52 -06:00
->whereIsNsfw(false)
->whereVisibility('public')
2018-08-28 03:07:36 +00:00
->orderBy('id', 'desc')
2019-03-02 21:46:49 -07:00
->take(24)
->get();
2019-03-02 01:38:44 -07:00
});
2018-08-09 20:58:52 -06:00
2018-11-18 21:26:52 -07: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 13:25:37 -07:00
public function showCategory(Request $request, $slug)
{
$tag = DiscoverCategory::whereActive(true)
->whereSlug($slug)
->firstOrFail();
2019-03-02 00:18:57 -07:00
$posts = Cache::remember('discover:category-'.$tag->id.':posts', now()->addMinutes(15), function() use ($tag) {
$tagids = $tag->hashtags->pluck('id')->toArray();
$sids = StatusHashtag::whereIn('hashtag_id', $tagids)->orderByDesc('status_id')->take(500)->pluck('status_id')->toArray();
$posts = Status::whereScope('public')->whereIn('id', $sids)->whereNull('uri')->whereType('photo')->whereNull('in_reply_to_id')->whereNull('reblog_of_id')->orderByDesc('created_at')->take(39)->get();
return $posts;
});
$tag->posts_count = Cache::remember('discover:category-'.$tag->id.':posts_count', now()->addMinutes(30), function() use ($tag) {
return $tag->posts()->whereScope('public')->count();
});
2019-02-10 13:25:37 -07:00
return view('discover.tags.category', compact('tag', 'posts'));
}
public function showPersonal(Request $request)
{
$profile = Auth::user()->profile;
2019-03-02 00:18:57 -07:00
$tags = Cache::remember('profile-'.$profile->id.':hashtags', now()->addMinutes(15), function() use ($profile){
return $profile->hashtags()->groupBy('hashtag_id')->inRandomOrder()->take(8)->get();
});
$following = Cache::remember('profile:following:'.$profile->id, now()->addMinutes(60), function() use ($profile) {
$res = Follower::whereProfileId($profile->id)->pluck('following_id');
return $res->push($profile->id)->toArray();
});
$posts = Cache::remember('profile-'.$profile->id.':hashtag-posts', now()->addMinutes(5), function() use ($profile, $following) {
$posts = Status::whereScope('public')->withCount(['likes','comments'])->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->orderByDesc('created_at')->take(39)->get();
$posts->post_count = Status::whereScope('public')->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->count();
return $posts;
});
2019-02-10 13:25:37 -07:00
return view('discover.personal', compact('posts', 'tags'));
}
}