ProfileController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Auth, Cache;
  5. use App\{Follower, Profile, User};
  6. use League\Fractal;
  7. use App\Util\Lexer\Nickname;
  8. use App\Util\Webfinger\Webfinger;
  9. use App\Transformer\ActivityPub\{
  10. ProfileOutbox,
  11. ProfileTransformer
  12. };
  13. class ProfileController extends Controller
  14. {
  15. public function show(Request $request, $username)
  16. {
  17. $user = Profile::whereUsername($username)->firstOrFail();
  18. if($user->remote_url) {
  19. $settings = new \StdClass;
  20. $settings->crawlable = false;
  21. } else {
  22. $settings = User::whereUsername($username)->firstOrFail()->settings;
  23. }
  24. if($request->wantsJson() && config('pixelfed.activitypub_enabled')) {
  25. return $this->showActivityPub($request, $user);
  26. }
  27. if($user->is_private == true) {
  28. $can_access = $this->privateProfileCheck($user);
  29. if($can_access !== true) {
  30. abort(403);
  31. }
  32. }
  33. // TODO: refactor this mess
  34. $owner = Auth::check() && Auth::id() === $user->user_id;
  35. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  36. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  37. $timeline = $user->statuses()
  38. ->whereHas('media')
  39. ->whereNull('in_reply_to_id')
  40. ->whereNull('reblog_of_id')
  41. ->orderBy('created_at','desc')
  42. ->withCount(['comments', 'likes'])
  43. ->simplePaginate(21);
  44. return view('profile.show', compact('user', 'settings', 'owner', 'is_following', 'is_admin', 'timeline'));
  45. }
  46. public function permalinkRedirect(Request $request, $username)
  47. {
  48. $user = Profile::whereUsername($username)->firstOrFail();
  49. $settings = User::whereUsername($username)->firstOrFail()->settings;
  50. if($request->wantsJson() && config('pixelfed.activitypub_enabled')) {
  51. return $this->showActivityPub($request, $user);
  52. }
  53. return redirect($user->url());
  54. }
  55. protected function privateProfileCheck(Profile $profile)
  56. {
  57. if(Auth::check() === false) {
  58. return false;
  59. }
  60. $follower_ids = (array) $profile->followers()->pluck('followers.profile_id');
  61. $pid = Auth::user()->profile->id;
  62. if(!in_array($pid, $follower_ids) && $pid !== $profile->id) {
  63. return false;
  64. }
  65. return true;
  66. }
  67. public function showActivityPub(Request $request, $user)
  68. {
  69. $fractal = new Fractal\Manager();
  70. $resource = new Fractal\Resource\Item($user, new ProfileTransformer);
  71. $res = $fractal->createData($resource)->toArray();
  72. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  73. }
  74. public function showAtomFeed(Request $request, $user)
  75. {
  76. $profile = Profile::whereUsername($user)->firstOrFail();
  77. $items = $profile->statuses()->orderBy('created_at', 'desc')->take(10)->get();
  78. return response()->view('atom.user', compact('profile', 'items'))
  79. ->header('Content-Type', 'application/atom+xml');
  80. }
  81. public function followers(Request $request, $username)
  82. {
  83. $profile = Profile::whereUsername($username)->firstOrFail();
  84. // TODO: fix $profile/$user mismatch in profile & follower templates
  85. $user = $profile;
  86. $owner = Auth::check() && Auth::id() === $user->user_id;
  87. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  88. $followers = $profile->followers()->orderBy('created_at','desc')->simplePaginate(12);
  89. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  90. return view('profile.followers', compact('user', 'profile', 'followers', 'owner', 'is_following', 'is_admin'));
  91. }
  92. public function following(Request $request, $username)
  93. {
  94. $profile = Profile::whereUsername($username)->firstOrFail();
  95. // TODO: fix $profile/$user mismatch in profile & follower templates
  96. $user = $profile;
  97. $owner = Auth::check() && Auth::id() === $user->user_id;
  98. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  99. $following = $profile->following()->orderBy('created_at','desc')->simplePaginate(12);
  100. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  101. return view('profile.following', compact('user', 'profile', 'following', 'owner', 'is_following', 'is_admin'));
  102. }
  103. public function savedBookmarks(Request $request, $username)
  104. {
  105. if(Auth::check() === false || $username !== Auth::user()->username) {
  106. abort(403);
  107. }
  108. $user = Auth::user()->profile;
  109. $settings = User::whereUsername($username)->firstOrFail()->settings;
  110. $owner = true;
  111. $following = false;
  112. $timeline = $user->bookmarks()->withCount(['likes','comments'])->orderBy('created_at','desc')->simplePaginate(10);
  113. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  114. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  115. return view('profile.show', compact('user', 'settings', 'owner', 'following', 'timeline', 'is_following', 'is_admin'));
  116. }
  117. }