1
0

ProfileController.php 9.0 KB

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