ProfileController.php 8.7 KB

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