ProfileController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. $settings = User::whereUsername($username)->firstOrFail()->settings;
  19. $mimes = [
  20. 'application/activity+json',
  21. 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
  22. ];
  23. if(str_contains($request->header('accept'), $mimes) && config('pixelfed.activitypub_enabled')) {
  24. return $this->showActivityPub($request, $user);
  25. }
  26. if($user->is_private == true) {
  27. $can_access = $this->privateProfileCheck($user);
  28. if($can_access !== true) {
  29. abort(403);
  30. }
  31. }
  32. // TODO: refactor this mess
  33. $owner = Auth::check() && Auth::id() === $user->user_id;
  34. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  35. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  36. $timeline = $user->statuses()
  37. ->whereHas('media')
  38. ->whereNull('in_reply_to_id')
  39. ->orderBy('created_at','desc')
  40. ->withCount(['comments', 'likes'])
  41. ->simplePaginate(21);
  42. return view('profile.show', compact('user', 'settings', 'owner', 'is_following', 'is_admin', 'timeline'));
  43. }
  44. public function permalinkRedirect(Request $request, $username)
  45. {
  46. $user = Profile::whereUsername($username)->firstOrFail();
  47. $settings = User::whereUsername($username)->firstOrFail()->settings;
  48. $mimes = [
  49. 'application/activity+json',
  50. 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
  51. ];
  52. if(str_contains($request->header('accept'), $mimes) && config('pixelfed.activitypub_enabled')) {
  53. return $this->showActivityPub($request, $user);
  54. }
  55. return redirect($user->url());
  56. }
  57. protected function privateProfileCheck(Profile $profile)
  58. {
  59. if(Auth::check() === false) {
  60. return false;
  61. }
  62. $follower_ids = (array) $profile->followers()->pluck('followers.profile_id');
  63. $pid = Auth::user()->profile->id;
  64. if(!in_array($pid, $follower_ids) && $pid !== $profile->id) {
  65. return false;
  66. }
  67. return true;
  68. }
  69. public function showActivityPub(Request $request, $user)
  70. {
  71. $fractal = new Fractal\Manager();
  72. $resource = new Fractal\Resource\Item($user, new ProfileTransformer);
  73. $res = $fractal->createData($resource)->toArray();
  74. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  75. }
  76. public function showAtomFeed(Request $request, $user)
  77. {
  78. $profile = Profile::whereUsername($user)->firstOrFail();
  79. $items = $profile->statuses()->orderBy('created_at', 'desc')->take(10)->get();
  80. return response()->view('atom.user', compact('profile', 'items'))
  81. ->header('Content-Type', 'application/atom+xml');
  82. }
  83. public function followers(Request $request, $username)
  84. {
  85. $profile = Profile::whereUsername($username)->firstOrFail();
  86. // TODO: fix $profile/$user mismatch in profile & follower templates
  87. $user = $profile;
  88. $owner = Auth::check() && Auth::id() === $user->user_id;
  89. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  90. $followers = $profile->followers()->orderBy('created_at','desc')->simplePaginate(12);
  91. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  92. return view('profile.followers', compact('user', 'profile', 'followers', 'owner', 'is_following', 'is_admin'));
  93. }
  94. public function following(Request $request, $username)
  95. {
  96. $profile = Profile::whereUsername($username)->firstOrFail();
  97. // TODO: fix $profile/$user mismatch in profile & follower templates
  98. $user = $profile;
  99. $owner = Auth::check() && Auth::id() === $user->user_id;
  100. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  101. $following = $profile->following()->orderBy('created_at','desc')->simplePaginate(12);
  102. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  103. return view('profile.following', compact('user', 'profile', 'following', 'owner', 'is_following', 'is_admin'));
  104. }
  105. public function savedBookmarks(Request $request, $username)
  106. {
  107. if(Auth::check() === false || $username !== Auth::user()->username) {
  108. abort(403);
  109. }
  110. $user = Auth::user()->profile;
  111. $settings = User::whereUsername($username)->firstOrFail()->settings;
  112. $owner = true;
  113. $following = false;
  114. $timeline = $user->bookmarks()->withCount(['likes','comments'])->orderBy('created_at','desc')->simplePaginate(10);
  115. $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
  116. $is_admin = is_null($user->domain) ? $user->user->is_admin : false;
  117. return view('profile.show', compact('user', 'settings', 'owner', 'following', 'timeline', 'is_following', 'is_admin'));
  118. }
  119. }