InternalApiController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\{
  5. DirectMessage,
  6. Hashtag,
  7. Follower,
  8. Like,
  9. Media,
  10. Notification,
  11. Profile,
  12. StatusHashtag,
  13. Status,
  14. UserFilter,
  15. };
  16. use Auth,Cache;
  17. use Carbon\Carbon;
  18. use League\Fractal;
  19. use App\Transformer\Api\{
  20. AccountTransformer,
  21. StatusTransformer,
  22. };
  23. use App\Jobs\StatusPipeline\NewStatusPipeline;
  24. use League\Fractal\Serializer\ArraySerializer;
  25. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  26. class InternalApiController extends Controller
  27. {
  28. protected $fractal;
  29. public function __construct()
  30. {
  31. $this->middleware('auth');
  32. $this->fractal = new Fractal\Manager();
  33. $this->fractal->setSerializer(new ArraySerializer());
  34. }
  35. public function compose(Request $request)
  36. {
  37. $this->validate($request, [
  38. 'caption' => 'nullable|string',
  39. 'media.*' => 'required',
  40. 'media.*.id' => 'required|integer|min:1',
  41. 'media.*.filter' => 'nullable|string|max:30',
  42. 'media.*.license' => 'nullable|string|max:80',
  43. 'visibility' => 'required|string|in:public,private|min:2|max:10'
  44. ]);
  45. $profile = Auth::user()->profile;
  46. $visibility = $request->input('visibility');
  47. $medias = $request->input('media');
  48. $attachments = [];
  49. $status = new Status;
  50. $mimes = [];
  51. foreach($medias as $k => $media) {
  52. $m = Media::findOrFail($media['id']);
  53. if($m->profile_id !== $profile->id || $m->status_id) {
  54. abort(403, 'Invalid media id');
  55. }
  56. $m->filter_class = $media['filter'];
  57. $m->license = $media['license'];
  58. $m->caption = strip_tags($media['alt']);
  59. $m->order = isset($media['cursor']) && is_int($media['cursor']) ? (int) $media['cursor'] : $k;
  60. if($media['cw'] == true) {
  61. $m->is_nsfw = true;
  62. $status->is_nsfw = true;
  63. }
  64. $m->save();
  65. $attachments[] = $m;
  66. array_push($mimes, $m->mime);
  67. }
  68. $status->caption = strip_tags($request->caption);
  69. $status->visibility = 'draft';
  70. $status->scope = 'draft';
  71. $status->profile_id = $profile->id;
  72. $status->save();
  73. foreach($attachments as $media) {
  74. $media->status_id = $status->id;
  75. $media->save();
  76. }
  77. $status->visibility = $visibility;
  78. $status->scope = $visibility;
  79. $status->type = StatusController::mimeTypeCheck($mimes);
  80. $status->save();
  81. NewStatusPipeline::dispatch($status);
  82. return $status->url();
  83. }
  84. // deprecated
  85. public function discover(Request $request)
  86. {
  87. $profile = Auth::user()->profile;
  88. $pid = $profile->id;
  89. $following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
  90. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  91. });
  92. $filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
  93. return UserFilter::whereUserId($pid)
  94. ->whereFilterableType('App\Profile')
  95. ->whereIn('filter_type', ['mute', 'block'])
  96. ->pluck('filterable_id')->toArray();
  97. });
  98. $following = array_merge($following, $filters);
  99. $people = Profile::select('id', 'name', 'username')
  100. ->with('avatar')
  101. ->whereNull('status')
  102. ->orderByRaw('rand()')
  103. ->whereHas('statuses')
  104. ->whereNull('domain')
  105. ->whereNotIn('id', $following)
  106. ->whereIsPrivate(false)
  107. ->take(3)
  108. ->get();
  109. $posts = Status::select('id', 'caption', 'profile_id')
  110. ->whereHas('media')
  111. ->whereIsNsfw(false)
  112. ->whereVisibility('public')
  113. ->whereNotIn('profile_id', $following)
  114. ->with('media')
  115. ->orderBy('created_at', 'desc')
  116. ->take(21)
  117. ->get();
  118. $res = [
  119. 'people' => $people->map(function($profile) {
  120. return [
  121. 'id' => $profile->id,
  122. 'avatar' => $profile->avatarUrl(),
  123. 'name' => $profile->name,
  124. 'username' => $profile->username,
  125. 'url' => $profile->url(),
  126. ];
  127. }),
  128. 'posts' => $posts->map(function($post) {
  129. return [
  130. 'url' => $post->url(),
  131. 'thumb' => $post->thumb(),
  132. ];
  133. })
  134. ];
  135. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  136. }
  137. public function discoverPeople(Request $request)
  138. {
  139. $profile = Auth::user()->profile;
  140. $pid = $profile->id;
  141. $following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
  142. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  143. });
  144. $filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
  145. return UserFilter::whereUserId($pid)
  146. ->whereFilterableType('App\Profile')
  147. ->whereIn('filter_type', ['mute', 'block'])
  148. ->pluck('filterable_id')->toArray();
  149. });
  150. $following = array_merge($following, $filters);
  151. $people = Profile::select('id', 'name', 'username')
  152. ->with('avatar')
  153. ->orderByRaw('rand()')
  154. ->whereHas('statuses')
  155. ->whereNull('domain')
  156. ->whereNotIn('id', $following)
  157. ->whereIsPrivate(false)
  158. ->take(3)
  159. ->get();
  160. $res = [
  161. 'people' => $people->map(function($profile) {
  162. return [
  163. 'id' => $profile->id,
  164. 'avatar' => $profile->avatarUrl(),
  165. 'name' => $profile->name,
  166. 'username' => $profile->username,
  167. 'url' => $profile->url(),
  168. ];
  169. })
  170. ];
  171. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  172. }
  173. public function discoverPosts(Request $request)
  174. {
  175. $profile = Auth::user()->profile;
  176. $pid = $profile->id;
  177. $following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
  178. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  179. });
  180. $filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
  181. return UserFilter::whereUserId($pid)
  182. ->whereFilterableType('App\Profile')
  183. ->whereIn('filter_type', ['mute', 'block'])
  184. ->pluck('filterable_id')->toArray();
  185. });
  186. $following = array_merge($following, $filters);
  187. $posts = Status::select('id', 'caption', 'profile_id')
  188. ->whereHas('media')
  189. ->whereHas('profile', function($q) {
  190. return $q->whereNull('status');
  191. })
  192. ->whereIsNsfw(false)
  193. ->whereVisibility('public')
  194. ->whereNotIn('profile_id', $following)
  195. ->with('media')
  196. ->orderBy('created_at', 'desc')
  197. ->take(21)
  198. ->get();
  199. $res = [
  200. 'posts' => $posts->map(function($post) {
  201. return [
  202. 'url' => $post->url(),
  203. 'thumb' => $post->thumb(),
  204. ];
  205. })
  206. ];
  207. return response()->json($res);
  208. }
  209. public function directMessage(Request $request, $profileId, $threadId)
  210. {
  211. $profile = Auth::user()->profile;
  212. if($profileId != $profile->id) {
  213. abort(403);
  214. }
  215. $msg = DirectMessage::whereToId($profile->id)
  216. ->orWhere('from_id',$profile->id)
  217. ->findOrFail($threadId);
  218. $thread = DirectMessage::with('status')->whereIn('to_id', [$profile->id, $msg->from_id])
  219. ->whereIn('from_id', [$profile->id,$msg->from_id])
  220. ->orderBy('created_at', 'asc')
  221. ->paginate(30);
  222. return response()->json(compact('msg', 'profile', 'thread'), 200, [], JSON_PRETTY_PRINT);
  223. }
  224. public function notificationMarkAllRead(Request $request)
  225. {
  226. $profile = Auth::user()->profile;
  227. $notifications = Notification::whereProfileId($profile->id)->get();
  228. foreach($notifications as $n) {
  229. $n->read_at = Carbon::now();
  230. $n->save();
  231. }
  232. return;
  233. }
  234. public function statusReplies(Request $request, int $id)
  235. {
  236. $parent = Status::findOrFail($id);
  237. $children = Status::whereInReplyToId($parent->id)
  238. ->orderBy('created_at', 'desc')
  239. ->take(3)
  240. ->get();
  241. $resource = new Fractal\Resource\Collection($children, new StatusTransformer());
  242. $res = $this->fractal->createData($resource)->toArray();
  243. return response()->json($res);
  244. }
  245. }