InternalApiController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. $q = false;
  57. $limit = 50;
  58. if($request->filled('min_id')) {
  59. $replies = $status->comments()
  60. ->select('id', 'caption', 'rendered', 'profile_id', 'created_at')
  61. ->where('id', '>=', $request->min_id)
  62. ->orderBy('id', 'desc')
  63. ->paginate($limit);
  64. }
  65. if($request->filled('max_id')) {
  66. $replies = $status->comments()
  67. ->select('id', 'caption', 'rendered', 'profile_id', 'created_at')
  68. ->where('id', '<=', $request->max_id)
  69. ->orderBy('id', 'desc')
  70. ->paginate($limit);
  71. }
  72. } else {
  73. $replies = $status->comments()
  74. ->select('id', 'caption', 'rendered', 'profile_id', 'created_at')
  75. ->orderBy('id', 'desc')
  76. ->paginate($limit);
  77. }
  78. $resource = new Fractal\Resource\Collection($replies, new StatusTransformer(), 'data');
  79. $resource->setPaginator(new IlluminatePaginatorAdapter($replies));
  80. $res = $this->fractal->createData($resource)->toArray();
  81. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  82. }
  83. public function compose(Request $request)
  84. {
  85. $this->validate($request, [
  86. 'caption' => 'nullable|string',
  87. 'media.*' => 'required',
  88. 'media.*.id' => 'required|integer|min:1',
  89. 'media.*.filter' => 'nullable|string|max:30',
  90. 'media.*.license' => 'nullable|string|max:80',
  91. 'visibility' => 'required|string|in:public,private|min:2|max:10'
  92. ]);
  93. $profile = Auth::user()->profile;
  94. $visibility = $request->input('visibility');
  95. $medias = $request->input('media');
  96. $attachments = [];
  97. $status = new Status;
  98. foreach($medias as $k => $media) {
  99. $m = Media::findOrFail($media['id']);
  100. if($m->profile_id !== $profile->id || $m->status_id) {
  101. abort(403, 'Invalid media id');
  102. }
  103. $m->filter_class = $media['filter'];
  104. $m->license = $media['license'];
  105. $m->caption = strip_tags($media['alt']);
  106. $m->order = isset($media['cursor']) && is_int($media['cursor']) ? (int) $media['cursor'] : $k;
  107. if($media['cw'] == true) {
  108. $m->is_nsfw = true;
  109. $status->is_nsfw = true;
  110. }
  111. $m->save();
  112. $attachments[] = $m;
  113. }
  114. $status->caption = strip_tags($request->caption);
  115. $status->visibility = 'draft';
  116. $status->scope = 'draft';
  117. $status->profile_id = $profile->id;
  118. $status->save();
  119. foreach($attachments as $media) {
  120. $media->status_id = $status->id;
  121. $media->save();
  122. }
  123. $status->visibility = $visibility;
  124. $status->scope = $visibility;
  125. $status->save();
  126. NewStatusPipeline::dispatch($status);
  127. return $status->url();
  128. }
  129. public function notifications(Request $request)
  130. {
  131. $this->validate($request, [
  132. 'page' => 'nullable|min:1|max:3',
  133. ]);
  134. $profile = Auth::user()->profile;
  135. $timeago = Carbon::now()->subMonths(6);
  136. $notifications = Notification::with('actor')
  137. ->whereProfileId($profile->id)
  138. ->whereDate('created_at', '>', $timeago)
  139. ->orderBy('id', 'desc')
  140. ->simplePaginate(30);
  141. $notifications = $notifications->map(function($k, $v) {
  142. return [
  143. 'id' => $k->id,
  144. 'action' => $k->action,
  145. 'message' => $k->message,
  146. 'rendered' => $k->rendered,
  147. 'actor' => [
  148. 'avatar' => $k->actor->avatarUrl(),
  149. 'username' => $k->actor->username,
  150. 'url' => $k->actor->url(),
  151. ],
  152. // 'item' => [
  153. // 'url' => $k->item->url(),
  154. // 'thumb' => $k->item->thumb(),
  155. // ],
  156. 'url' => $k->item->url()
  157. ];
  158. });
  159. return response()->json($notifications, 200, [], JSON_PRETTY_PRINT);
  160. }
  161. public function discover(Request $request)
  162. {
  163. $profile = Auth::user()->profile;
  164. $following = Cache::get('feature:discover:following:'.$profile->id, []);
  165. $people = Profile::select('id', 'name', 'username')
  166. ->with('avatar')
  167. ->inRandomOrder()
  168. ->whereHas('statuses')
  169. ->whereNull('domain')
  170. ->whereNotIn('id', $following)
  171. ->whereIsPrivate(false)
  172. ->take(3)
  173. ->get();
  174. $posts = Status::select('id', 'caption', 'profile_id')
  175. ->whereHas('media')
  176. ->whereHas('profile', function($q) {
  177. $q->where('is_private', false);
  178. })
  179. ->whereIsNsfw(false)
  180. ->whereVisibility('public')
  181. ->where('profile_id', '<>', $profile->id)
  182. ->whereNotIn('profile_id', $following)
  183. ->withCount(['comments', 'likes'])
  184. ->orderBy('created_at', 'desc')
  185. ->take(21)
  186. ->get();
  187. $res = [
  188. 'people' => $people->map(function($profile) {
  189. return [
  190. 'avatar' => $profile->avatarUrl(),
  191. 'name' => $profile->name,
  192. 'username' => $profile->username,
  193. 'url' => $profile->url(),
  194. ];
  195. }),
  196. 'posts' => $posts->map(function($post) {
  197. return [
  198. 'url' => $post->url(),
  199. 'thumb' => $post->thumb(),
  200. 'comments_count' => $post->comments_count,
  201. 'likes_count' => $post->likes_count,
  202. ];
  203. })
  204. ];
  205. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  206. }
  207. }