InternalApiController.php 15 KB

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