ProfileController.php 5.0 KB

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