SiteController.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App;
  4. use App\Follower;
  5. use App\Profile;
  6. use App\Status;
  7. use App\User;
  8. use App\UserFilter;
  9. use App\Util\Lexer\PrettyNumber;
  10. use Auth;
  11. use Cache;
  12. use Illuminate\Http\Request;
  13. class SiteController extends Controller
  14. {
  15. public function home()
  16. {
  17. if (Auth::check()) {
  18. return $this->homeTimeline();
  19. } else {
  20. return $this->homeGuest();
  21. }
  22. }
  23. public function homeGuest()
  24. {
  25. return view('site.index');
  26. }
  27. public function homeTimeline()
  28. {
  29. $pid = Auth::user()->profile->id;
  30. // TODO: Use redis for timelines
  31. $following = Follower::whereProfileId(Auth::user()->profile->id)->pluck('following_id');
  32. $following->push(Auth::user()->profile->id);
  33. $filtered = UserFilter::whereUserId($pid)
  34. ->whereFilterableType('App\Profile')
  35. ->whereIn('filter_type', ['mute', 'block'])
  36. ->pluck('filterable_id');
  37. $timeline = Status::whereIn('profile_id', $following)
  38. ->whereNotIn('profile_id', $filtered)
  39. ->whereHas('media')
  40. ->orderBy('id', 'desc')
  41. ->withCount(['comments', 'likes', 'shares'])
  42. ->simplePaginate(20);
  43. $type = 'personal';
  44. return view('timeline.template', compact('timeline', 'type'));
  45. }
  46. public function changeLocale(Request $request, $locale)
  47. {
  48. if (!App::isLocale($locale)) {
  49. return redirect()->back();
  50. }
  51. App::setLocale($locale);
  52. return redirect()->back();
  53. }
  54. public function about()
  55. {
  56. $res = Cache::remember('site:page:about', 15, function () {
  57. $statuses = Status::whereHas('media')
  58. ->whereNull('in_reply_to_id')
  59. ->whereNull('reblog_of_id')
  60. ->count();
  61. $statusCount = PrettyNumber::convert($statuses);
  62. $userCount = PrettyNumber::convert(User::count());
  63. $remoteCount = PrettyNumber::convert(Profile::whereNotNull('remote_url')->count());
  64. $adminContact = User::whereIsAdmin(true)->first();
  65. return view('site.about')->with(compact('statusCount', 'userCount', 'remoteCount', 'adminContact'))->render();
  66. });
  67. return $res;
  68. }
  69. }