DiscoverController.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\{
  4. DiscoverCategory,
  5. Follower,
  6. Hashtag,
  7. Profile,
  8. Status,
  9. StatusHashtag,
  10. UserFilter
  11. };
  12. use Auth, DB, Cache;
  13. use Illuminate\Http\Request;
  14. class DiscoverController extends Controller
  15. {
  16. public function __construct()
  17. {
  18. $this->middleware('auth');
  19. }
  20. public function home(Request $request)
  21. {
  22. return view('discover.home');
  23. }
  24. public function showTags(Request $request, $hashtag)
  25. {
  26. $this->validate($request, [
  27. 'page' => 'nullable|integer|min:1|max:10',
  28. ]);
  29. $tag = Hashtag::with('posts')
  30. ->withCount('posts')
  31. ->whereSlug($hashtag)
  32. ->firstOrFail();
  33. $posts = $tag->posts()
  34. ->whereNull('url')
  35. ->whereNull('uri')
  36. ->whereHas('media')
  37. ->withCount(['likes', 'comments'])
  38. ->whereIsNsfw(false)
  39. ->whereVisibility('public')
  40. ->orderBy('id', 'desc')
  41. ->simplePaginate(12);
  42. if($posts->count() == 0) {
  43. abort(404);
  44. }
  45. return view('discover.tags.show', compact('tag', 'posts'));
  46. }
  47. public function showCategory(Request $request, $slug)
  48. {
  49. $tag = DiscoverCategory::whereActive(true)
  50. ->whereSlug($slug)
  51. ->firstOrFail();
  52. // todo refactor this mess
  53. $tagids = $tag->hashtags->pluck('id')->toArray();
  54. $sids = StatusHashtag::whereIn('hashtag_id', $tagids)->orderByDesc('status_id')->take(500)->pluck('status_id')->toArray();
  55. $posts = Status::whereIn('id', $sids)->whereNull('uri')->whereType('photo')->whereNull('in_reply_to_id')->whereNull('reblog_of_id')->orderByDesc('created_at')->paginate(21);
  56. $tag->posts_count = $tag->posts()->count();
  57. return view('discover.tags.category', compact('tag', 'posts'));
  58. }
  59. public function showPersonal(Request $request)
  60. {
  61. $profile = Auth::user()->profile;
  62. // todo refactor this mess
  63. $tags = Hashtag::whereHas('posts')->orderByRaw('rand()')->take(5)->get();
  64. $following = $profile->following->pluck('id');
  65. $following = $following->push($profile->id)->toArray();
  66. $posts = Status::withCount(['likes','comments'])->whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->orderByDesc('created_at')->paginate(21);
  67. $posts->post_count = Status::whereNotIn('profile_id', $following)->whereHas('media')->whereType('photo')->count();
  68. return view('discover.personal', compact('posts', 'tags'));
  69. }
  70. }