ProfileController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Auth;
  5. use Cache;
  6. use View;
  7. use App\Follower;
  8. use App\FollowRequest;
  9. use App\Profile;
  10. use App\User;
  11. use App\UserFilter;
  12. use League\Fractal;
  13. use App\Util\Lexer\Nickname;
  14. use App\Util\Webfinger\Webfinger;
  15. use App\Transformer\ActivityPub\ProfileOutbox;
  16. use App\Transformer\ActivityPub\ProfileTransformer;
  17. class ProfileController extends Controller
  18. {
  19. public function show(Request $request, $username)
  20. {
  21. $user = Profile::whereNull('domain')
  22. ->whereNull('status')
  23. ->whereUsername($username)
  24. ->firstOrFail();
  25. if($request->wantsJson() && config('federation.activitypub.enabled')) {
  26. return $this->showActivityPub($request, $user);
  27. }
  28. return $this->buildProfile($request, $user);
  29. }
  30. protected function buildProfile(Request $request, $user)
  31. {
  32. $username = $user->username;
  33. $loggedIn = Auth::check();
  34. $isPrivate = false;
  35. $isBlocked = false;
  36. if(!$loggedIn) {
  37. $key = 'profile:settings:' . $user->id;
  38. $ttl = now()->addHours(6);
  39. $settings = Cache::remember($key, $ttl, function() use($user) {
  40. return $user->user->settings;
  41. });
  42. if ($user->is_private == true) {
  43. abort(404);
  44. }
  45. $owner = false;
  46. $is_following = false;
  47. $is_admin = $user->user->is_admin;
  48. $profile = $user;
  49. $settings = [
  50. 'crawlable' => $settings->crawlable,
  51. 'following' => [
  52. 'count' => $settings->show_profile_following_count,
  53. 'list' => $settings->show_profile_following
  54. ],
  55. 'followers' => [
  56. 'count' => $settings->show_profile_follower_count,
  57. 'list' => $settings->show_profile_followers
  58. ]
  59. ];
  60. return view('profile.show', compact('profile', 'settings'));
  61. } else {
  62. $key = 'profile:settings:' . $user->id;
  63. $ttl = now()->addHours(6);
  64. $settings = Cache::remember($key, $ttl, function() use($user) {
  65. return $user->user->settings;
  66. });
  67. if ($user->is_private == true) {
  68. $isPrivate = $this->privateProfileCheck($user, $loggedIn);
  69. }
  70. $isBlocked = $this->blockedProfileCheck($user);
  71. $owner = $loggedIn && Auth::id() === $user->user_id;
  72. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  73. if ($isPrivate == true || $isBlocked == true) {
  74. $requested = Auth::check() ? FollowRequest::whereFollowerId(Auth::user()->profile_id)
  75. ->whereFollowingId($user->id)
  76. ->exists() : false;
  77. return view('profile.private', compact('user', 'is_following', 'requested'));
  78. }
  79. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  80. $profile = $user;
  81. $settings = [
  82. 'crawlable' => $settings->crawlable,
  83. 'following' => [
  84. 'count' => $settings->show_profile_following_count,
  85. 'list' => $settings->show_profile_following
  86. ],
  87. 'followers' => [
  88. 'count' => $settings->show_profile_follower_count,
  89. 'list' => $settings->show_profile_followers
  90. ]
  91. ];
  92. return view('profile.show', compact('profile', 'settings'));
  93. }
  94. }
  95. public function permalinkRedirect(Request $request, $username)
  96. {
  97. $user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  98. if ($request->wantsJson() && config('federation.activitypub.enabled')) {
  99. return $this->showActivityPub($request, $user);
  100. }
  101. return redirect($user->url());
  102. }
  103. protected function privateProfileCheck(Profile $profile, $loggedIn)
  104. {
  105. if (!Auth::check()) {
  106. return true;
  107. }
  108. $user = Auth::user()->profile;
  109. if($user->id == $profile->id || !$profile->is_private) {
  110. return false;
  111. }
  112. $follows = Follower::whereProfileId($user->id)->whereFollowingId($profile->id)->exists();
  113. if ($follows == false) {
  114. return true;
  115. }
  116. return false;
  117. }
  118. protected function blockedProfileCheck(Profile $profile)
  119. {
  120. $pid = Auth::user()->profile->id;
  121. $blocks = UserFilter::whereUserId($profile->id)
  122. ->whereFilterType('block')
  123. ->whereFilterableType('App\Profile')
  124. ->pluck('filterable_id')
  125. ->toArray();
  126. if (in_array($pid, $blocks)) {
  127. return true;
  128. }
  129. return false;
  130. }
  131. public function showActivityPub(Request $request, $user)
  132. {
  133. abort_if(!config('federation.activitypub.enabled'), 404);
  134. abort_if($user->domain, 404);
  135. $key = 'profile:ap:' . $user->id;
  136. $ttl = now()->addHours(6);
  137. return Cache::remember($key, $ttl, function() use($user) {
  138. $fractal = new Fractal\Manager();
  139. $resource = new Fractal\Resource\Item($user, new ProfileTransformer);
  140. $res = $fractal->createData($resource)->toArray();
  141. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  142. });
  143. }
  144. public function showAtomFeed(Request $request, $user)
  145. {
  146. abort_if(!config('federation.atom.enabled'), 404);
  147. $profile = $user = Profile::whereNull('status')->whereNull('domain')->whereUsername($user)->whereIsPrivate(false)->firstOrFail();
  148. if($profile->status != null) {
  149. return $this->accountCheck($profile);
  150. }
  151. if($profile->is_private || Auth::check()) {
  152. $blocked = $this->blockedProfileCheck($profile);
  153. $check = $this->privateProfileCheck($profile, null);
  154. if($check || $blocked) {
  155. return redirect($profile->url());
  156. }
  157. }
  158. $items = $profile->statuses()->whereHas('media')->whereIn('visibility',['public', 'unlisted'])->orderBy('created_at', 'desc')->take(10)->get();
  159. return response()->view('atom.user', compact('profile', 'items'))
  160. ->header('Content-Type', 'application/atom+xml');
  161. }
  162. public function meRedirect()
  163. {
  164. abort_if(!Auth::check(), 404);
  165. return redirect(Auth::user()->url());
  166. }
  167. public function embed(Request $request, $username)
  168. {
  169. $res = view('profile.embed-removed');
  170. if(strlen($username) > 15 || strlen($username) < 2) {
  171. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  172. }
  173. $profile = Profile::whereUsername($username)
  174. ->whereIsPrivate(false)
  175. ->whereNull('status')
  176. ->whereNull('domain')
  177. ->first();
  178. if(!$profile) {
  179. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  180. }
  181. $content = Cache::remember('profile:embed:'.$profile->id, now()->addHours(12), function() use($profile) {
  182. return View::make('profile.embed')->with(compact('profile'))->render();
  183. });
  184. return response($content)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  185. }
  186. }