ProfileController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Auth;
  5. use Cache;
  6. use App\Follower;
  7. use App\Profile;
  8. use App\User;
  9. use App\UserFilter;
  10. use League\Fractal;
  11. use App\Util\Lexer\Nickname;
  12. use App\Util\Webfinger\Webfinger;
  13. use App\Transformer\ActivityPub\ProfileOutbox;
  14. use App\Transformer\ActivityPub\ProfileTransformer;
  15. class ProfileController extends Controller
  16. {
  17. public function show(Request $request, $username)
  18. {
  19. $user = Profile::whereUsername($username)->firstOrFail();
  20. return $this->buildProfile($request, $user);
  21. }
  22. // TODO: refactor this mess
  23. protected function buildProfile(Request $request, $user)
  24. {
  25. $username = $user->username;
  26. $loggedIn = Auth::check();
  27. $isPrivate = false;
  28. if ($user->remote_url) {
  29. $settings = new \StdClass;
  30. $settings->crawlable = false;
  31. } else {
  32. $settings = User::whereUsername($username)->firstOrFail()->settings;
  33. }
  34. if ($request->wantsJson() && config('pixelfed.activitypub_enabled')) {
  35. return $this->showActivityPub($request, $user);
  36. }
  37. if ($user->is_private == true) {
  38. $isPrivate = $this->privateProfileCheck($user, $loggedIn);
  39. }
  40. if ($loggedIn == true) {
  41. $isBlocked = $this->blockedProfileCheck($user);
  42. }
  43. if ($isPrivate == true || $isBlocked == true) {
  44. return view('profile.private', compact('user'));
  45. }
  46. $owner = $loggedIn && Auth::id() === $user->user_id;
  47. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  48. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  49. $timeline = $user->statuses()
  50. ->whereHas('media')
  51. ->whereNull('in_reply_to_id')
  52. ->whereNull('reblog_of_id')
  53. ->orderBy('created_at', 'desc')
  54. ->withCount(['comments', 'likes'])
  55. ->simplePaginate(21);
  56. return view('profile.show', compact('user', 'settings', 'owner', 'is_following', 'is_admin', 'timeline'));
  57. }
  58. public function permalinkRedirect(Request $request, $username)
  59. {
  60. $user = Profile::whereUsername($username)->firstOrFail();
  61. $settings = User::whereUsername($username)->firstOrFail()->settings;
  62. if ($request->wantsJson() && config('pixelfed.activitypub_enabled')) {
  63. return $this->showActivityPub($request, $user);
  64. }
  65. return redirect($user->url());
  66. }
  67. protected function privateProfileCheck(Profile $profile, $loggedIn)
  68. {
  69. if (!Auth::check()) {
  70. return true;
  71. }
  72. $user = Auth::user()->profile;
  73. if($user->id == $profile->id || !$profile->is_private) {
  74. return false;
  75. }
  76. $follows = Follower::whereProfileId($user->id)->whereFollowingId($profile->id)->exists();
  77. if ($follows == false) {
  78. return true;
  79. }
  80. return false;
  81. }
  82. protected function blockedProfileCheck(Profile $profile)
  83. {
  84. $pid = Auth::user()->profile->id;
  85. $blocks = UserFilter::whereUserId($profile->id)
  86. ->whereFilterType('block')
  87. ->whereFilterableType('App\Profile')
  88. ->pluck('filterable_id')
  89. ->toArray();
  90. if (in_array($pid, $blocks)) {
  91. return true;
  92. }
  93. return false;
  94. }
  95. public function showActivityPub(Request $request, $user)
  96. {
  97. $fractal = new Fractal\Manager();
  98. $resource = new Fractal\Resource\Item($user, new ProfileTransformer);
  99. $res = $fractal->createData($resource)->toArray();
  100. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  101. }
  102. public function showAtomFeed(Request $request, $user)
  103. {
  104. $profile = $user = Profile::whereUsername($user)->firstOrFail();
  105. if($profile->is_private || Auth::check()) {
  106. $blocked = $this->blockedProfileCheck($profile);
  107. $check = $this->privateProfileCheck($profile, null);
  108. if($check || $blocked) {
  109. return view('profile.private', compact('user'));
  110. }
  111. }
  112. $items = $profile->statuses()->orderBy('created_at', 'desc')->take(10)->get();
  113. return response()->view('atom.user', compact('profile', 'items'))
  114. ->header('Content-Type', 'application/atom+xml');
  115. }
  116. public function followers(Request $request, $username)
  117. {
  118. $profile = $user = Profile::whereUsername($username)->firstOrFail();
  119. // TODO: fix $profile/$user mismatch in profile & follower templates
  120. if($profile->is_private || Auth::check()) {
  121. $blocked = $this->blockedProfileCheck($profile);
  122. $check = $this->privateProfileCheck($profile, null);
  123. if($check || $blocked) {
  124. return view('profile.private', compact('user'));
  125. }
  126. }
  127. $owner = Auth::check() && Auth::id() === $user->user_id;
  128. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  129. $followers = $profile->followers()->orderBy('created_at', 'desc')->simplePaginate(12);
  130. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  131. if ($user->remote_url) {
  132. $settings = new \StdClass;
  133. $settings->crawlable = false;
  134. } else {
  135. $settings = User::whereUsername($username)->firstOrFail()->settings;
  136. }
  137. return view('profile.followers', compact('user', 'profile', 'followers', 'owner', 'is_following', 'is_admin', 'settings'));
  138. }
  139. public function following(Request $request, $username)
  140. {
  141. $profile = $user = Profile::whereUsername($username)->firstOrFail();
  142. // TODO: fix $profile/$user mismatch in profile & follower templates
  143. if($profile->is_private || Auth::check()) {
  144. $blocked = $this->blockedProfileCheck($profile);
  145. $check = $this->privateProfileCheck($profile, null);
  146. if($check || $blocked) {
  147. return view('profile.private', compact('user'));
  148. }
  149. }
  150. $user = $profile;
  151. $owner = Auth::check() && Auth::id() === $user->user_id;
  152. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  153. $following = $profile->following()->orderBy('created_at', 'desc')->simplePaginate(12);
  154. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  155. if ($user->remote_url) {
  156. $settings = new \StdClass;
  157. $settings->crawlable = false;
  158. } else {
  159. $settings = User::whereUsername($username)->firstOrFail()->settings;
  160. }
  161. return view('profile.following', compact('user', 'profile', 'following', 'owner', 'is_following', 'is_admin', 'settings'));
  162. }
  163. public function savedBookmarks(Request $request, $username)
  164. {
  165. if (Auth::check() === false || $username !== Auth::user()->username) {
  166. abort(403);
  167. }
  168. $user = Auth::user()->profile;
  169. $settings = User::whereUsername($username)->firstOrFail()->settings;
  170. $owner = true;
  171. $following = false;
  172. $timeline = $user->bookmarks()->withCount(['likes','comments'])->orderBy('created_at', 'desc')->simplePaginate(10);
  173. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  174. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  175. return view('profile.show', compact('user', 'settings', 'owner', 'following', 'timeline', 'is_following', 'is_admin'));
  176. }
  177. }