ProfileController.php 7.7 KB

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