PublicApiController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\{
  5. Hashtag,
  6. Follower,
  7. Like,
  8. Media,
  9. Notification,
  10. Profile,
  11. StatusHashtag,
  12. Status,
  13. };
  14. use Auth,Cache;
  15. use Carbon\Carbon;
  16. use League\Fractal;
  17. use App\Transformer\Api\{
  18. AccountTransformer,
  19. StatusTransformer,
  20. };
  21. use App\Jobs\StatusPipeline\NewStatusPipeline;
  22. use League\Fractal\Serializer\ArraySerializer;
  23. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  24. class PublicApiController extends Controller
  25. {
  26. protected $fractal;
  27. public function __construct()
  28. {
  29. $this->middleware('throttle:200, 30');
  30. $this->fractal = new Fractal\Manager();
  31. $this->fractal->setSerializer(new ArraySerializer());
  32. }
  33. protected function getUserData()
  34. {
  35. if(false == Auth::check()) {
  36. return [];
  37. } else {
  38. $profile = Auth::user()->profile;
  39. $user = new Fractal\Resource\Item($profile, new AccountTransformer());
  40. return $this->fractal->createData($user)->toArray();
  41. }
  42. }
  43. public function status(Request $request, $username, int $postid)
  44. {
  45. $profile = Profile::whereUsername($username)->first();
  46. $status = Status::whereProfileId($profile->id)->find($postid);
  47. $this->scopeCheck($profile, $status);
  48. $item = new Fractal\Resource\Item($status, new StatusTransformer());
  49. $res = [
  50. 'status' => $this->fractal->createData($item)->toArray(),
  51. 'user' => $this->getUserData(),
  52. 'reactions' => [
  53. 'liked' => $status->liked(),
  54. 'shared' => $status->shared(),
  55. 'bookmarked' => $status->bookmarked(),
  56. ],
  57. ];
  58. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  59. }
  60. public function statusComments(Request $request, $username, int $postId)
  61. {
  62. $this->validate($request, [
  63. 'min_id' => 'nullable|integer|min:1',
  64. 'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
  65. 'limit' => 'nullable|integer|min:5|max:50'
  66. ]);
  67. $limit = $request->limit ?? 10;
  68. $profile = Profile::whereUsername($username)->first();
  69. $status = Status::whereProfileId($profile->id)->find($postId);
  70. $this->scopeCheck($profile, $status);
  71. if($request->filled('min_id') || $request->filled('max_id')) {
  72. if($request->filled('min_id')) {
  73. $replies = $status->comments()
  74. ->select('id', 'caption', 'rendered', 'profile_id', 'in_reply_to_id', 'created_at')
  75. ->where('id', '>=', $request->min_id)
  76. ->orderBy('id', 'desc')
  77. ->paginate($limit);
  78. }
  79. if($request->filled('max_id')) {
  80. $replies = $status->comments()
  81. ->select('id', 'caption', 'rendered', 'profile_id', 'in_reply_to_id', 'created_at')
  82. ->where('id', '<=', $request->max_id)
  83. ->orderBy('id', 'desc')
  84. ->paginate($limit);
  85. }
  86. } else {
  87. $replies = $status->comments()
  88. ->select('id', 'caption', 'rendered', 'profile_id', 'in_reply_to_id', 'created_at')
  89. ->orderBy('id', 'desc')
  90. ->paginate($limit);
  91. }
  92. $resource = new Fractal\Resource\Collection($replies, new StatusTransformer(), 'data');
  93. $resource->setPaginator(new IlluminatePaginatorAdapter($replies));
  94. $res = $this->fractal->createData($resource)->toArray();
  95. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  96. }
  97. protected function scopeCheck(Profile $profile, Status $status)
  98. {
  99. if($profile->is_private == true && Auth::check() == false) {
  100. abort(404);
  101. }
  102. switch ($status->scope) {
  103. case 'public':
  104. case 'unlisted':
  105. $user = Auth::check() ? Auth::user() : false;
  106. if($user && $profile->is_private) {
  107. $follows = Follower::whereProfileId($user->profile->id)
  108. ->whereFollowingId($profile->id)
  109. ->exists();
  110. if($follows == false && $profile->id !== $user->profile->id) {
  111. abort(404);
  112. }
  113. }
  114. break;
  115. case 'private':
  116. $follows = Follower::whereProfileId($user->profile->id)
  117. ->whereFollowingId($profile->id)
  118. ->exists();
  119. if($follows == false && $profile->id !== $user->profile->id) {
  120. abort(404);
  121. }
  122. break;
  123. case 'direct':
  124. abort(404);
  125. break;
  126. case 'draft':
  127. abort(404);
  128. break;
  129. default:
  130. abort(404);
  131. break;
  132. }
  133. }
  134. }