ProfileController.php 9.4 KB

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