ProfileController.php 9.1 KB

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