ProfileController.php 7.9 KB

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