InternalApiController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\{
  5. Hashtag,
  6. Like,
  7. Media,
  8. Notification,
  9. Profile,
  10. StatusHashtag,
  11. Status,
  12. };
  13. use Auth,Cache;
  14. use Carbon\Carbon;
  15. use League\Fractal;
  16. use App\Transformer\Api\{
  17. AccountTransformer,
  18. StatusTransformer,
  19. };
  20. use App\Jobs\StatusPipeline\NewStatusPipeline;
  21. use League\Fractal\Serializer\ArraySerializer;
  22. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  23. class InternalApiController extends Controller
  24. {
  25. protected $fractal;
  26. public function __construct()
  27. {
  28. $this->middleware('auth');
  29. $this->fractal = new Fractal\Manager();
  30. $this->fractal->setSerializer(new ArraySerializer());
  31. }
  32. public function status(Request $request, $username, int $postid)
  33. {
  34. $auth = Auth::user()->profile;
  35. $profile = Profile::whereUsername($username)->first();
  36. $status = Status::whereProfileId($profile->id)->find($postid);
  37. $status = new Fractal\Resource\Item($status, new StatusTransformer());
  38. $user = new Fractal\Resource\Item($auth, new AccountTransformer());
  39. $res = [];
  40. $res['status'] = $this->fractal->createData($status)->toArray();
  41. $res['user'] = $this->fractal->createData($user)->toArray();
  42. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  43. }
  44. public function statusComments(Request $request, $username, int $postId)
  45. {
  46. $this->validate($request, [
  47. 'min_id' => 'nullable|integer|min:1',
  48. 'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
  49. 'limit' => 'nullable|integer|min:5|max:50'
  50. ]);
  51. $limit = $request->limit ?? 10;
  52. $auth = Auth::user()->profile;
  53. $profile = Profile::whereUsername($username)->first();
  54. $status = Status::whereProfileId($profile->id)->find($postId);
  55. if($request->filled('min_id') || $request->filled('max_id')) {
  56. if($request->filled('min_id')) {
  57. $replies = $status->comments()
  58. ->select('id', 'caption', 'rendered', 'profile_id', 'in_reply_to_id', 'created_at')
  59. ->where('id', '>=', $request->min_id)
  60. ->orderBy('id', 'desc')
  61. ->paginate($limit);
  62. }
  63. if($request->filled('max_id')) {
  64. $replies = $status->comments()
  65. ->select('id', 'caption', 'rendered', 'profile_id', 'in_reply_to_id', 'created_at')
  66. ->where('id', '<=', $request->max_id)
  67. ->orderBy('id', 'desc')
  68. ->paginate($limit);
  69. }
  70. } else {
  71. $replies = $status->comments()
  72. ->select('id', 'caption', 'rendered', 'profile_id', 'in_reply_to_id', 'created_at')
  73. ->orderBy('id', 'desc')
  74. ->paginate($limit);
  75. }
  76. $resource = new Fractal\Resource\Collection($replies, new StatusTransformer(), 'data');
  77. $resource->setPaginator(new IlluminatePaginatorAdapter($replies));
  78. $res = $this->fractal->createData($resource)->toArray();
  79. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  80. }
  81. public function compose(Request $request)
  82. {
  83. $this->validate($request, [
  84. 'caption' => 'nullable|string',
  85. 'media.*' => 'required',
  86. 'media.*.id' => 'required|integer|min:1',
  87. 'media.*.filter' => 'nullable|string|max:30',
  88. 'media.*.license' => 'nullable|string|max:80',
  89. 'visibility' => 'required|string|in:public,private|min:2|max:10'
  90. ]);
  91. $profile = Auth::user()->profile;
  92. $visibility = $request->input('visibility');
  93. $medias = $request->input('media');
  94. $attachments = [];
  95. $status = new Status;
  96. foreach($medias as $k => $media) {
  97. $m = Media::findOrFail($media['id']);
  98. if($m->profile_id !== $profile->id || $m->status_id) {
  99. abort(403, 'Invalid media id');
  100. }
  101. $m->filter_class = $media['filter'];
  102. $m->license = $media['license'];
  103. $m->caption = strip_tags($media['alt']);
  104. $m->order = isset($media['cursor']) && is_int($media['cursor']) ? (int) $media['cursor'] : $k;
  105. if($media['cw'] == true) {
  106. $m->is_nsfw = true;
  107. $status->is_nsfw = true;
  108. }
  109. $m->save();
  110. $attachments[] = $m;
  111. }
  112. $status->caption = strip_tags($request->caption);
  113. $status->visibility = 'draft';
  114. $status->scope = 'draft';
  115. $status->profile_id = $profile->id;
  116. $status->save();
  117. foreach($attachments as $media) {
  118. $media->status_id = $status->id;
  119. $media->save();
  120. }
  121. $status->visibility = $visibility;
  122. $status->scope = $visibility;
  123. $status->save();
  124. NewStatusPipeline::dispatch($status);
  125. return $status->url();
  126. }
  127. public function notifications(Request $request)
  128. {
  129. $this->validate($request, [
  130. 'page' => 'nullable|min:1|max:3',
  131. ]);
  132. $profile = Auth::user()->profile;
  133. $timeago = Carbon::now()->subMonths(6);
  134. $notifications = Notification::with('actor')
  135. ->whereProfileId($profile->id)
  136. ->whereDate('created_at', '>', $timeago)
  137. ->orderBy('id', 'desc')
  138. ->simplePaginate(30);
  139. $notifications = $notifications->map(function($k, $v) {
  140. return [
  141. 'id' => $k->id,
  142. 'action' => $k->action,
  143. 'message' => $k->message,
  144. 'rendered' => $k->rendered,
  145. 'actor' => [
  146. 'avatar' => $k->actor->avatarUrl(),
  147. 'username' => $k->actor->username,
  148. 'url' => $k->actor->url(),
  149. ],
  150. // 'item' => [
  151. // 'url' => $k->item->url(),
  152. // 'thumb' => $k->item->thumb(),
  153. // ],
  154. 'url' => $k->item->url()
  155. ];
  156. });
  157. return response()->json($notifications, 200, [], JSON_PRETTY_PRINT);
  158. }
  159. public function discover(Request $request)
  160. {
  161. $profile = Auth::user()->profile;
  162. $following = Cache::get('feature:discover:following:'.$profile->id, []);
  163. $people = Profile::select('id', 'name', 'username')
  164. ->with('avatar')
  165. ->inRandomOrder()
  166. ->whereHas('statuses')
  167. ->whereNull('domain')
  168. ->whereNotIn('id', $following)
  169. ->whereIsPrivate(false)
  170. ->take(3)
  171. ->get();
  172. $posts = Status::select('id', 'caption', 'profile_id')
  173. ->whereHas('media')
  174. ->whereHas('profile', function($q) {
  175. $q->where('is_private', false);
  176. })
  177. ->whereIsNsfw(false)
  178. ->whereVisibility('public')
  179. ->where('profile_id', '<>', $profile->id)
  180. ->whereNotIn('profile_id', $following)
  181. ->withCount(['comments', 'likes'])
  182. ->orderBy('created_at', 'desc')
  183. ->take(21)
  184. ->get();
  185. $res = [
  186. 'people' => $people->map(function($profile) {
  187. return [
  188. 'avatar' => $profile->avatarUrl(),
  189. 'name' => $profile->name,
  190. 'username' => $profile->username,
  191. 'url' => $profile->url(),
  192. ];
  193. }),
  194. 'posts' => $posts->map(function($post) {
  195. return [
  196. 'url' => $post->url(),
  197. 'thumb' => $post->thumb(),
  198. 'comments_count' => $post->comments_count,
  199. 'likes_count' => $post->likes_count,
  200. ];
  201. })
  202. ];
  203. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  204. }
  205. }