ProfileController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Follower;
  4. use App\Profile;
  5. use App\Transformer\ActivityPub\ProfileTransformer;
  6. use App\User;
  7. use Auth;
  8. use Illuminate\Http\Request;
  9. use League\Fractal;
  10. class ProfileController extends Controller
  11. {
  12. public function show(Request $request, $username)
  13. {
  14. $user = Profile::whereUsername($username)->firstOrFail();
  15. if ($user->remote_url) {
  16. $settings = new \StdClass();
  17. $settings->crawlable = false;
  18. } else {
  19. $settings = User::whereUsername($username)->firstOrFail()->settings;
  20. }
  21. if ($request->wantsJson() && config('pixelfed.activitypub_enabled')) {
  22. return $this->showActivityPub($request, $user);
  23. }
  24. if ($user->is_private == true) {
  25. $can_access = $this->privateProfileCheck($user);
  26. if ($can_access !== true) {
  27. abort(403);
  28. }
  29. }
  30. // TODO: refactor this mess
  31. $owner = Auth::check() && Auth::id() === $user->user_id;
  32. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  33. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  34. $timeline = $user->statuses()
  35. ->whereHas('media')
  36. ->whereNull('in_reply_to_id')
  37. ->whereNull('reblog_of_id')
  38. ->orderBy('created_at', 'desc')
  39. ->withCount(['comments', 'likes'])
  40. ->simplePaginate(21);
  41. return view('profile.show', compact('user', 'settings', 'owner', 'is_following', 'is_admin', 'timeline'));
  42. }
  43. public function permalinkRedirect(Request $request, $username)
  44. {
  45. $user = Profile::whereUsername($username)->firstOrFail();
  46. $settings = User::whereUsername($username)->firstOrFail()->settings;
  47. if ($request->wantsJson() && config('pixelfed.activitypub_enabled')) {
  48. return $this->showActivityPub($request, $user);
  49. }
  50. return redirect($user->url());
  51. }
  52. protected function privateProfileCheck(Profile $profile)
  53. {
  54. if (Auth::check() === false) {
  55. return false;
  56. }
  57. $follower_ids = (array) $profile->followers()->pluck('followers.profile_id');
  58. $pid = Auth::user()->profile->id;
  59. if (!in_array($pid, $follower_ids) && $pid !== $profile->id) {
  60. return false;
  61. }
  62. return true;
  63. }
  64. public function showActivityPub(Request $request, $user)
  65. {
  66. $fractal = new Fractal\Manager();
  67. $resource = new Fractal\Resource\Item($user, new ProfileTransformer());
  68. $res = $fractal->createData($resource)->toArray();
  69. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  70. }
  71. public function showAtomFeed(Request $request, $user)
  72. {
  73. $profile = Profile::whereUsername($user)->firstOrFail();
  74. $items = $profile->statuses()->orderBy('created_at', 'desc')->take(10)->get();
  75. return response()->view('atom.user', compact('profile', 'items'))
  76. ->header('Content-Type', 'application/atom+xml');
  77. }
  78. public function followers(Request $request, $username)
  79. {
  80. $profile = Profile::whereUsername($username)->firstOrFail();
  81. // TODO: fix $profile/$user mismatch in profile & follower templates
  82. $user = $profile;
  83. $owner = Auth::check() && Auth::id() === $user->user_id;
  84. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  85. $followers = $profile->followers()->orderBy('created_at', 'desc')->simplePaginate(12);
  86. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  87. return view('profile.followers', compact('user', 'profile', 'followers', 'owner', 'is_following', 'is_admin'));
  88. }
  89. public function following(Request $request, $username)
  90. {
  91. $profile = Profile::whereUsername($username)->firstOrFail();
  92. // TODO: fix $profile/$user mismatch in profile & follower templates
  93. $user = $profile;
  94. $owner = Auth::check() && Auth::id() === $user->user_id;
  95. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  96. $following = $profile->following()->orderBy('created_at', 'desc')->simplePaginate(12);
  97. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  98. return view('profile.following', compact('user', 'profile', 'following', 'owner', 'is_following', 'is_admin'));
  99. }
  100. public function savedBookmarks(Request $request, $username)
  101. {
  102. if (Auth::check() === false || $username !== Auth::user()->username) {
  103. abort(403);
  104. }
  105. $user = Auth::user()->profile;
  106. $settings = User::whereUsername($username)->firstOrFail()->settings;
  107. $owner = true;
  108. $following = false;
  109. $timeline = $user->bookmarks()->withCount(['likes', 'comments'])->orderBy('created_at', 'desc')->simplePaginate(10);
  110. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  111. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  112. return view('profile.show', compact('user', 'settings', 'owner', 'following', 'timeline', 'is_following', 'is_admin'));
  113. }
  114. }