InternalApiController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. $parent = Status::whereScope('public')->findOrFail($id);
  121. $children = Status::whereInReplyToId($parent->id)
  122. ->orderBy('created_at', 'desc')
  123. ->take(3)
  124. ->get();
  125. $resource = new Fractal\Resource\Collection($children, new StatusTransformer());
  126. $res = $this->fractal->createData($resource)->toArray();
  127. return response()->json($res);
  128. }
  129. public function stories(Request $request)
  130. {
  131. }
  132. public function discoverCategories(Request $request)
  133. {
  134. $categories = DiscoverCategory::whereActive(true)->orderBy('order')->take(10)->get();
  135. $res = $categories->map(function($item) {
  136. return [
  137. 'name' => $item->name,
  138. 'url' => $item->url(),
  139. 'thumb' => $item->thumb()
  140. ];
  141. });
  142. return response()->json($res);
  143. }
  144. public function modAction(Request $request)
  145. {
  146. abort_unless(Auth::user()->is_admin, 400);
  147. $this->validate($request, [
  148. 'action' => [
  149. 'required',
  150. 'string',
  151. Rule::in([
  152. 'addcw',
  153. 'remcw',
  154. 'unlist'
  155. ])
  156. ],
  157. 'item_id' => 'required|integer|min:1',
  158. 'item_type' => [
  159. 'required',
  160. 'string',
  161. Rule::in(['profile', 'status'])
  162. ]
  163. ]);
  164. $action = $request->input('action');
  165. $item_id = $request->input('item_id');
  166. $item_type = $request->input('item_type');
  167. switch($action) {
  168. case 'addcw':
  169. $status = Status::findOrFail($item_id);
  170. $status->is_nsfw = true;
  171. $status->save();
  172. ModLogService::boot()
  173. ->user(Auth::user())
  174. ->objectUid($status->profile->user_id)
  175. ->objectId($status->id)
  176. ->objectType('App\Status::class')
  177. ->action('admin.status.moderate')
  178. ->metadata([
  179. 'action' => 'cw',
  180. 'message' => 'Success!'
  181. ])
  182. ->accessLevel('admin')
  183. ->save();
  184. if($status->uri == null) {
  185. $media = $status->media;
  186. $ai = new AccountInterstitial;
  187. $ai->user_id = $status->profile->user_id;
  188. $ai->type = 'post.cw';
  189. $ai->view = 'account.moderation.post.cw';
  190. $ai->item_type = 'App\Status';
  191. $ai->item_id = $status->id;
  192. $ai->has_media = (bool) $media->count();
  193. $ai->blurhash = $media->count() ? $media->first()->blurhash : null;
  194. $ai->meta = json_encode([
  195. 'caption' => $status->caption,
  196. 'created_at' => $status->created_at,
  197. 'type' => $status->type,
  198. 'url' => $status->url(),
  199. 'is_nsfw' => $status->is_nsfw,
  200. 'scope' => $status->scope,
  201. 'reblog' => $status->reblog_of_id,
  202. 'likes_count' => $status->likes_count,
  203. 'reblogs_count' => $status->reblogs_count,
  204. ]);
  205. $ai->save();
  206. $u = $status->profile->user;
  207. $u->has_interstitial = true;
  208. $u->save();
  209. }
  210. break;
  211. case 'remcw':
  212. $status = Status::findOrFail($item_id);
  213. $status->is_nsfw = false;
  214. $status->save();
  215. ModLogService::boot()
  216. ->user(Auth::user())
  217. ->objectUid($status->profile->user_id)
  218. ->objectId($status->id)
  219. ->objectType('App\Status::class')
  220. ->action('admin.status.moderate')
  221. ->metadata([
  222. 'action' => 'remove_cw',
  223. 'message' => 'Success!'
  224. ])
  225. ->accessLevel('admin')
  226. ->save();
  227. if($status->uri == null) {
  228. $ai = AccountInterstitial::whereUserId($status->profile->user_id)
  229. ->whereType('post.cw')
  230. ->whereItemId($status->id)
  231. ->whereItemType('App\Status')
  232. ->first();
  233. $ai->delete();
  234. }
  235. break;
  236. case 'unlist':
  237. $status = Status::whereScope('public')->findOrFail($item_id);
  238. $status->scope = $status->visibility = 'unlisted';
  239. $status->save();
  240. PublicTimelineService::del($status->id);
  241. ModLogService::boot()
  242. ->user(Auth::user())
  243. ->objectUid($status->profile->user_id)
  244. ->objectId($status->id)
  245. ->objectType('App\Status::class')
  246. ->action('admin.status.moderate')
  247. ->metadata([
  248. 'action' => 'unlist',
  249. 'message' => 'Success!'
  250. ])
  251. ->accessLevel('admin')
  252. ->save();
  253. if($status->uri == null) {
  254. $media = $status->media;
  255. $ai = new AccountInterstitial;
  256. $ai->user_id = $status->profile->user_id;
  257. $ai->type = 'post.unlist';
  258. $ai->view = 'account.moderation.post.unlist';
  259. $ai->item_type = 'App\Status';
  260. $ai->item_id = $status->id;
  261. $ai->has_media = (bool) $media->count();
  262. $ai->blurhash = $media->count() ? $media->first()->blurhash : null;
  263. $ai->meta = json_encode([
  264. 'caption' => $status->caption,
  265. 'created_at' => $status->created_at,
  266. 'type' => $status->type,
  267. 'url' => $status->url(),
  268. 'is_nsfw' => $status->is_nsfw,
  269. 'scope' => $status->scope,
  270. 'reblog' => $status->reblog_of_id,
  271. 'likes_count' => $status->likes_count,
  272. 'reblogs_count' => $status->reblogs_count,
  273. ]);
  274. $ai->save();
  275. $u = $status->profile->user;
  276. $u->has_interstitial = true;
  277. $u->save();
  278. }
  279. break;
  280. }
  281. return ['msg' => 200];
  282. }
  283. public function composePost(Request $request)
  284. {
  285. abort(400, 'Endpoint deprecated');
  286. }
  287. public function bookmarks(Request $request)
  288. {
  289. $statuses = Auth::user()->profile
  290. ->bookmarks()
  291. ->withCount(['likes','comments'])
  292. ->orderBy('created_at', 'desc')
  293. ->simplePaginate(10);
  294. $resource = new Fractal\Resource\Collection($statuses, new StatusTransformer());
  295. $res = $this->fractal->createData($resource)->toArray();
  296. return response()->json($res);
  297. }
  298. public function accountStatuses(Request $request, $id)
  299. {
  300. $this->validate($request, [
  301. 'only_media' => 'nullable',
  302. 'pinned' => 'nullable',
  303. 'exclude_replies' => 'nullable',
  304. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  305. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  306. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  307. 'limit' => 'nullable|integer|min:1|max:24'
  308. ]);
  309. $profile = Profile::whereNull('status')->findOrFail($id);
  310. $limit = $request->limit ?? 9;
  311. $max_id = $request->max_id;
  312. $min_id = $request->min_id;
  313. $scope = $request->only_media == true ?
  314. ['photo', 'photo:album', 'video', 'video:album'] :
  315. ['photo', 'photo:album', 'video', 'video:album', 'share', 'reply'];
  316. if($profile->is_private) {
  317. if(!Auth::check()) {
  318. return response()->json([]);
  319. }
  320. $pid = Auth::user()->profile->id;
  321. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  322. $following = Follower::whereProfileId($pid)->pluck('following_id');
  323. return $following->push($pid)->toArray();
  324. });
  325. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : [];
  326. } else {
  327. if(Auth::check()) {
  328. $pid = Auth::user()->profile->id;
  329. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  330. $following = Follower::whereProfileId($pid)->pluck('following_id');
  331. return $following->push($pid)->toArray();
  332. });
  333. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  334. } else {
  335. $visibility = ['public', 'unlisted'];
  336. }
  337. }
  338. $dir = $min_id ? '>' : '<';
  339. $id = $min_id ?? $max_id;
  340. $timeline = Status::select(
  341. 'id',
  342. 'uri',
  343. 'caption',
  344. 'rendered',
  345. 'profile_id',
  346. 'type',
  347. 'in_reply_to_id',
  348. 'reblog_of_id',
  349. 'is_nsfw',
  350. 'likes_count',
  351. 'reblogs_count',
  352. 'scope',
  353. 'local',
  354. 'created_at',
  355. 'updated_at'
  356. )->whereProfileId($profile->id)
  357. ->whereIn('type', $scope)
  358. ->where('id', $dir, $id)
  359. ->whereIn('visibility', $visibility)
  360. ->latest()
  361. ->limit($limit)
  362. ->get();
  363. $resource = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  364. $res = $this->fractal->createData($resource)->toArray();
  365. return response()->json($res);
  366. }
  367. public function remoteProfile(Request $request, $id)
  368. {
  369. $profile = Profile::whereNull('status')
  370. ->whereNotNull('domain')
  371. ->findOrFail($id);
  372. $user = Auth::user();
  373. return view('profile.remote', compact('profile', 'user'));
  374. }
  375. public function remoteStatus(Request $request, $profileId, $statusId)
  376. {
  377. $user = Profile::whereNull('status')
  378. ->whereNotNull('domain')
  379. ->findOrFail($profileId);
  380. $status = Status::whereProfileId($user->id)
  381. ->whereNull('reblog_of_id')
  382. ->whereIn('visibility', ['public', 'unlisted'])
  383. ->findOrFail($statusId);
  384. $template = $status->in_reply_to_id ? 'status.reply' : 'status.remote';
  385. return view($template, compact('user', 'status'));
  386. }
  387. }