ProfileController.php 9.0 KB

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