1
0

InternalApiController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\{
  5. AccountInterstitial,
  6. DirectMessage,
  7. DiscoverCategory,
  8. Hashtag,
  9. Follower,
  10. Like,
  11. Media,
  12. MediaTag,
  13. Notification,
  14. Profile,
  15. StatusHashtag,
  16. Status,
  17. UserFilter,
  18. };
  19. use Auth,Cache;
  20. use Carbon\Carbon;
  21. use League\Fractal;
  22. use App\Transformer\Api\{
  23. AccountTransformer,
  24. StatusTransformer,
  25. // StatusMediaContainerTransformer,
  26. };
  27. use App\Util\Media\Filter;
  28. use App\Jobs\StatusPipeline\NewStatusPipeline;
  29. use App\Jobs\ModPipeline\HandleSpammerPipeline;
  30. use League\Fractal\Serializer\ArraySerializer;
  31. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  32. use Illuminate\Validation\Rule;
  33. use Illuminate\Support\Str;
  34. use App\Services\MediaTagService;
  35. use App\Services\ModLogService;
  36. use App\Services\PublicTimelineService;
  37. use App\Services\SnowflakeService;
  38. use App\Services\StatusService;
  39. class InternalApiController extends Controller
  40. {
  41. protected $fractal;
  42. public function __construct()
  43. {
  44. $this->middleware('auth');
  45. $this->fractal = new Fractal\Manager();
  46. $this->fractal->setSerializer(new ArraySerializer());
  47. }
  48. // deprecated v2 compose api
  49. public function compose(Request $request)
  50. {
  51. return redirect('/');
  52. }
  53. // deprecated
  54. public function discover(Request $request)
  55. {
  56. return;
  57. }
  58. public function discoverPosts(Request $request)
  59. {
  60. $profile = Auth::user()->profile;
  61. $pid = $profile->id;
  62. $following = Cache::remember('feature:discover:following:'.$pid, now()->addMinutes(15), function() use ($pid) {
  63. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  64. });
  65. $filters = Cache::remember("user:filter:list:$pid", now()->addMinutes(15), function() use($pid) {
  66. $private = Profile::whereIsPrivate(true)
  67. ->orWhere('unlisted', true)
  68. ->orWhere('status', '!=', null)
  69. ->pluck('id')
  70. ->toArray();
  71. $filters = UserFilter::whereUserId($pid)
  72. ->whereFilterableType('App\Profile')
  73. ->whereIn('filter_type', ['mute', 'block'])
  74. ->pluck('filterable_id')
  75. ->toArray();
  76. return array_merge($private, $filters);
  77. });
  78. $following = array_merge($following, $filters);
  79. $sql = config('database.default') !== 'pgsql';
  80. $min_id = SnowflakeService::byDate(now()->subMonths(3));
  81. $posts = Status::select(
  82. 'id',
  83. 'is_nsfw',
  84. 'profile_id',
  85. 'type',
  86. 'uri',
  87. )
  88. ->whereNull('uri')
  89. ->whereIn('type', ['photo','photo:album', 'video'])
  90. ->whereIsNsfw(false)
  91. ->whereVisibility('public')
  92. ->whereNotIn('profile_id', $following)
  93. ->where('id', '>', $min_id)
  94. ->inRandomOrder()
  95. ->take(39)
  96. ->pluck('id');
  97. $res = [
  98. 'posts' => $posts->map(function($post) {
  99. return StatusService::get($post);
  100. })
  101. ];
  102. return response()->json($res);
  103. }
  104. public function directMessage(Request $request, $profileId, $threadId)
  105. {
  106. $profile = Auth::user()->profile;
  107. if($profileId != $profile->id) {
  108. abort(403);
  109. }
  110. $msg = DirectMessage::whereToId($profile->id)
  111. ->orWhere('from_id',$profile->id)
  112. ->findOrFail($threadId);
  113. $thread = DirectMessage::with('status')->whereIn('to_id', [$profile->id, $msg->from_id])
  114. ->whereIn('from_id', [$profile->id,$msg->from_id])
  115. ->orderBy('created_at', 'asc')
  116. ->paginate(30);
  117. return response()->json(compact('msg', 'profile', 'thread'), 200, [], JSON_PRETTY_PRINT);
  118. }
  119. public function statusReplies(Request $request, int $id)
  120. {
  121. $this->validate($request, [
  122. 'limit' => 'nullable|int|min:1|max:6'
  123. ]);
  124. $parent = Status::whereScope('public')->findOrFail($id);
  125. $limit = $request->input('limit') ?? 3;
  126. $children = Status::whereInReplyToId($parent->id)
  127. ->orderBy('created_at', 'desc')
  128. ->take($limit)
  129. ->get();
  130. $resource = new Fractal\Resource\Collection($children, new StatusTransformer());
  131. $res = $this->fractal->createData($resource)->toArray();
  132. return response()->json($res);
  133. }
  134. public function stories(Request $request)
  135. {
  136. }
  137. public function discoverCategories(Request $request)
  138. {
  139. $categories = DiscoverCategory::whereActive(true)->orderBy('order')->take(10)->get();
  140. $res = $categories->map(function($item) {
  141. return [
  142. 'name' => $item->name,
  143. 'url' => $item->url(),
  144. 'thumb' => $item->thumb()
  145. ];
  146. });
  147. return response()->json($res);
  148. }
  149. public function modAction(Request $request)
  150. {
  151. abort_unless(Auth::user()->is_admin, 400);
  152. $this->validate($request, [
  153. 'action' => [
  154. 'required',
  155. 'string',
  156. Rule::in([
  157. 'addcw',
  158. 'remcw',
  159. 'unlist',
  160. 'spammer'
  161. ])
  162. ],
  163. 'item_id' => 'required|integer|min:1',
  164. 'item_type' => [
  165. 'required',
  166. 'string',
  167. Rule::in(['profile', 'status'])
  168. ]
  169. ]);
  170. $action = $request->input('action');
  171. $item_id = $request->input('item_id');
  172. $item_type = $request->input('item_type');
  173. switch($action) {
  174. case 'addcw':
  175. $status = Status::findOrFail($item_id);
  176. $status->is_nsfw = true;
  177. $status->save();
  178. ModLogService::boot()
  179. ->user(Auth::user())
  180. ->objectUid($status->profile->user_id)
  181. ->objectId($status->id)
  182. ->objectType('App\Status::class')
  183. ->action('admin.status.moderate')
  184. ->metadata([
  185. 'action' => 'cw',
  186. 'message' => 'Success!'
  187. ])
  188. ->accessLevel('admin')
  189. ->save();
  190. if($status->uri == null) {
  191. $media = $status->media;
  192. $ai = new AccountInterstitial;
  193. $ai->user_id = $status->profile->user_id;
  194. $ai->type = 'post.cw';
  195. $ai->view = 'account.moderation.post.cw';
  196. $ai->item_type = 'App\Status';
  197. $ai->item_id = $status->id;
  198. $ai->has_media = (bool) $media->count();
  199. $ai->blurhash = $media->count() ? $media->first()->blurhash : null;
  200. $ai->meta = json_encode([
  201. 'caption' => $status->caption,
  202. 'created_at' => $status->created_at,
  203. 'type' => $status->type,
  204. 'url' => $status->url(),
  205. 'is_nsfw' => $status->is_nsfw,
  206. 'scope' => $status->scope,
  207. 'reblog' => $status->reblog_of_id,
  208. 'likes_count' => $status->likes_count,
  209. 'reblogs_count' => $status->reblogs_count,
  210. ]);
  211. $ai->save();
  212. $u = $status->profile->user;
  213. $u->has_interstitial = true;
  214. $u->save();
  215. }
  216. break;
  217. case 'remcw':
  218. $status = Status::findOrFail($item_id);
  219. $status->is_nsfw = false;
  220. $status->save();
  221. ModLogService::boot()
  222. ->user(Auth::user())
  223. ->objectUid($status->profile->user_id)
  224. ->objectId($status->id)
  225. ->objectType('App\Status::class')
  226. ->action('admin.status.moderate')
  227. ->metadata([
  228. 'action' => 'remove_cw',
  229. 'message' => 'Success!'
  230. ])
  231. ->accessLevel('admin')
  232. ->save();
  233. if($status->uri == null) {
  234. $ai = AccountInterstitial::whereUserId($status->profile->user_id)
  235. ->whereType('post.cw')
  236. ->whereItemId($status->id)
  237. ->whereItemType('App\Status')
  238. ->first();
  239. $ai->delete();
  240. }
  241. break;
  242. case 'unlist':
  243. $status = Status::whereScope('public')->findOrFail($item_id);
  244. $status->scope = $status->visibility = 'unlisted';
  245. $status->save();
  246. PublicTimelineService::del($status->id);
  247. ModLogService::boot()
  248. ->user(Auth::user())
  249. ->objectUid($status->profile->user_id)
  250. ->objectId($status->id)
  251. ->objectType('App\Status::class')
  252. ->action('admin.status.moderate')
  253. ->metadata([
  254. 'action' => 'unlist',
  255. 'message' => 'Success!'
  256. ])
  257. ->accessLevel('admin')
  258. ->save();
  259. if($status->uri == null) {
  260. $media = $status->media;
  261. $ai = new AccountInterstitial;
  262. $ai->user_id = $status->profile->user_id;
  263. $ai->type = 'post.unlist';
  264. $ai->view = 'account.moderation.post.unlist';
  265. $ai->item_type = 'App\Status';
  266. $ai->item_id = $status->id;
  267. $ai->has_media = (bool) $media->count();
  268. $ai->blurhash = $media->count() ? $media->first()->blurhash : null;
  269. $ai->meta = json_encode([
  270. 'caption' => $status->caption,
  271. 'created_at' => $status->created_at,
  272. 'type' => $status->type,
  273. 'url' => $status->url(),
  274. 'is_nsfw' => $status->is_nsfw,
  275. 'scope' => $status->scope,
  276. 'reblog' => $status->reblog_of_id,
  277. 'likes_count' => $status->likes_count,
  278. 'reblogs_count' => $status->reblogs_count,
  279. ]);
  280. $ai->save();
  281. $u = $status->profile->user;
  282. $u->has_interstitial = true;
  283. $u->save();
  284. }
  285. break;
  286. case 'spammer':
  287. $status = Status::findOrFail($item_id);
  288. HandleSpammerPipeline::dispatch($status->profile);
  289. ModLogService::boot()
  290. ->user(Auth::user())
  291. ->objectUid($status->profile->user_id)
  292. ->objectId($status->id)
  293. ->objectType('App\User::class')
  294. ->action('admin.status.moderate')
  295. ->metadata([
  296. 'action' => 'spammer',
  297. 'message' => 'Success!'
  298. ])
  299. ->accessLevel('admin')
  300. ->save();
  301. break;
  302. }
  303. Cache::forget('_api:statuses:recent_9:' . $status->profile_id);
  304. Cache::forget('profile:embed:' . $status->profile_id);
  305. StatusService::del($status->id);
  306. return ['msg' => 200];
  307. }
  308. public function composePost(Request $request)
  309. {
  310. abort(400, 'Endpoint deprecated');
  311. }
  312. public function bookmarks(Request $request)
  313. {
  314. $statuses = Auth::user()->profile
  315. ->bookmarks()
  316. ->withCount(['likes','comments'])
  317. ->orderBy('created_at', 'desc')
  318. ->simplePaginate(10);
  319. $resource = new Fractal\Resource\Collection($statuses, new StatusTransformer());
  320. $res = $this->fractal->createData($resource)->toArray();
  321. return response()->json($res);
  322. }
  323. public function accountStatuses(Request $request, $id)
  324. {
  325. $this->validate($request, [
  326. 'only_media' => 'nullable',
  327. 'pinned' => 'nullable',
  328. 'exclude_replies' => 'nullable',
  329. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  330. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  331. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  332. 'limit' => 'nullable|integer|min:1|max:24'
  333. ]);
  334. $profile = Profile::whereNull('status')->findOrFail($id);
  335. $limit = $request->limit ?? 9;
  336. $max_id = $request->max_id;
  337. $min_id = $request->min_id;
  338. $scope = $request->only_media == true ?
  339. ['photo', 'photo:album', 'video', 'video:album'] :
  340. ['photo', 'photo:album', 'video', 'video:album', 'share', 'reply'];
  341. if($profile->is_private) {
  342. if(!Auth::check()) {
  343. return response()->json([]);
  344. }
  345. $pid = Auth::user()->profile->id;
  346. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  347. $following = Follower::whereProfileId($pid)->pluck('following_id');
  348. return $following->push($pid)->toArray();
  349. });
  350. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : [];
  351. } else {
  352. if(Auth::check()) {
  353. $pid = Auth::user()->profile->id;
  354. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  355. $following = Follower::whereProfileId($pid)->pluck('following_id');
  356. return $following->push($pid)->toArray();
  357. });
  358. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  359. } else {
  360. $visibility = ['public', 'unlisted'];
  361. }
  362. }
  363. $dir = $min_id ? '>' : '<';
  364. $id = $min_id ?? $max_id;
  365. $timeline = Status::select(
  366. 'id',
  367. 'uri',
  368. 'caption',
  369. 'rendered',
  370. 'profile_id',
  371. 'type',
  372. 'in_reply_to_id',
  373. 'reblog_of_id',
  374. 'is_nsfw',
  375. 'likes_count',
  376. 'reblogs_count',
  377. 'scope',
  378. 'local',
  379. 'created_at',
  380. 'updated_at'
  381. )->whereProfileId($profile->id)
  382. ->whereIn('type', $scope)
  383. ->where('id', $dir, $id)
  384. ->whereIn('visibility', $visibility)
  385. ->latest()
  386. ->limit($limit)
  387. ->get();
  388. $resource = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  389. $res = $this->fractal->createData($resource)->toArray();
  390. return response()->json($res);
  391. }
  392. public function remoteProfile(Request $request, $id)
  393. {
  394. $profile = Profile::whereNull('status')
  395. ->whereNotNull('domain')
  396. ->findOrFail($id);
  397. $user = Auth::user();
  398. return view('profile.remote', compact('profile', 'user'));
  399. }
  400. public function remoteStatus(Request $request, $profileId, $statusId)
  401. {
  402. $user = Profile::whereNull('status')
  403. ->whereNotNull('domain')
  404. ->findOrFail($profileId);
  405. $status = Status::whereProfileId($user->id)
  406. ->whereNull('reblog_of_id')
  407. ->whereIn('visibility', ['public', 'unlisted'])
  408. ->findOrFail($statusId);
  409. $template = $status->in_reply_to_id ? 'status.reply' : 'status.remote';
  410. return view($template, compact('user', 'status'));
  411. }
  412. }