InternalApiController.php 9.7 KB

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