ApiV1Dot1Controller.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use Cache;
  4. use DB;
  5. use App\Http\Controllers\Controller;
  6. use Illuminate\Http\Request;
  7. use League\Fractal;
  8. use League\Fractal\Serializer\ArraySerializer;
  9. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  10. use App\AccountLog;
  11. use App\EmailVerification;
  12. use App\Place;
  13. use App\Status;
  14. use App\Report;
  15. use App\Profile;
  16. use App\StatusArchived;
  17. use App\User;
  18. use App\Services\AccountService;
  19. use App\Services\StatusService;
  20. use App\Services\ProfileStatusService;
  21. use App\Services\PublicTimelineService;
  22. use App\Services\NetworkTimelineService;
  23. use App\Util\Lexer\RestrictedNames;
  24. use App\Services\EmailService;
  25. use Illuminate\Support\Str;
  26. use Illuminate\Support\Facades\Hash;
  27. use Jenssegers\Agent\Agent;
  28. use Mail;
  29. use App\Mail\PasswordChange;
  30. use App\Mail\ConfirmAppEmail;
  31. use App\Http\Resources\StatusStateless;
  32. use App\Jobs\StatusPipeline\StatusDelete;
  33. class ApiV1Dot1Controller extends Controller
  34. {
  35. protected $fractal;
  36. public function __construct()
  37. {
  38. $this->fractal = new Fractal\Manager();
  39. $this->fractal->setSerializer(new ArraySerializer());
  40. }
  41. public function json($res, $code = 200, $headers = [])
  42. {
  43. return response()->json($res, $code, $headers, JSON_UNESCAPED_SLASHES);
  44. }
  45. public function error($msg, $code = 400, $extra = [], $headers = [])
  46. {
  47. $res = [
  48. "msg" => $msg,
  49. "code" => $code
  50. ];
  51. return response()->json(array_merge($res, $extra), $code, $headers, JSON_UNESCAPED_SLASHES);
  52. }
  53. public function report(Request $request)
  54. {
  55. $user = $request->user();
  56. abort_if(!$user, 403);
  57. abort_if($user->status != null, 403);
  58. $report_type = $request->input('report_type');
  59. $object_id = $request->input('object_id');
  60. $object_type = $request->input('object_type');
  61. $types = [
  62. 'spam',
  63. 'sensitive',
  64. 'abusive',
  65. 'underage',
  66. 'violence',
  67. 'copyright',
  68. 'impersonation',
  69. 'scam',
  70. 'terrorism'
  71. ];
  72. if (!$report_type || !$object_id || !$object_type) {
  73. return $this->error("Invalid or missing parameters", 400, ["error_code" => "ERROR_INVALID_PARAMS"]);
  74. }
  75. if (!in_array($report_type, $types)) {
  76. return $this->error("Invalid report type", 400, ["error_code" => "ERROR_TYPE_INVALID"]);
  77. }
  78. if ($object_type === "user" && $object_id == $user->profile_id) {
  79. return $this->error("Cannot self report", 400, ["error_code" => "ERROR_NO_SELF_REPORTS"]);
  80. }
  81. $rpid = null;
  82. switch ($object_type) {
  83. case 'post':
  84. $object = Status::find($object_id);
  85. if (!$object) {
  86. return $this->error("Invalid object id", 400, ["error_code" => "ERROR_INVALID_OBJECT_ID"]);
  87. }
  88. $object_type = 'App\Status';
  89. $exists = Report::whereUserId($user->id)
  90. ->whereObjectId($object->id)
  91. ->whereObjectType('App\Status')
  92. ->count();
  93. $rpid = $object->profile_id;
  94. break;
  95. case 'user':
  96. $object = Profile::find($object_id);
  97. if (!$object) {
  98. return $this->error("Invalid object id", 400, ["error_code" => "ERROR_INVALID_OBJECT_ID"]);
  99. }
  100. $object_type = 'App\Profile';
  101. $exists = Report::whereUserId($user->id)
  102. ->whereObjectId($object->id)
  103. ->whereObjectType('App\Profile')
  104. ->count();
  105. $rpid = $object->id;
  106. break;
  107. default:
  108. return $this->error("Invalid report type", 400, ["error_code" => "ERROR_REPORT_OBJECT_TYPE_INVALID"]);
  109. break;
  110. }
  111. if ($exists !== 0) {
  112. return $this->error("Duplicate report", 400, ["error_code" => "ERROR_REPORT_DUPLICATE"]);
  113. }
  114. if ($object->profile_id == $user->profile_id) {
  115. return $this->error("Cannot self report", 400, ["error_code" => "ERROR_NO_SELF_REPORTS"]);
  116. }
  117. $report = new Report;
  118. $report->profile_id = $user->profile_id;
  119. $report->user_id = $user->id;
  120. $report->object_id = $object->id;
  121. $report->object_type = $object_type;
  122. $report->reported_profile_id = $rpid;
  123. $report->type = $report_type;
  124. $report->save();
  125. $res = [
  126. "msg" => "Successfully sent report",
  127. "code" => 200
  128. ];
  129. return $this->json($res);
  130. }
  131. /**
  132. * DELETE /api/v1.1/accounts/avatar
  133. *
  134. * @return \App\Transformer\Api\AccountTransformer
  135. */
  136. public function deleteAvatar(Request $request)
  137. {
  138. $user = $request->user();
  139. abort_if(!$user, 403);
  140. abort_if($user->status != null, 403);
  141. $avatar = $user->profile->avatar;
  142. if( $avatar->media_path == 'public/avatars/default.png' ||
  143. $avatar->media_path == 'public/avatars/default.jpg'
  144. ) {
  145. return AccountService::get($user->profile_id);
  146. }
  147. if(is_file(storage_path('app/' . $avatar->media_path))) {
  148. @unlink(storage_path('app/' . $avatar->media_path));
  149. }
  150. $avatar->media_path = 'public/avatars/default.jpg';
  151. $avatar->change_count = $avatar->change_count + 1;
  152. $avatar->save();
  153. Cache::forget('avatar:' . $user->profile_id);
  154. Cache::forget("avatar:{$user->profile_id}");
  155. Cache::forget('user:account:id:'.$user->id);
  156. AccountService::del($user->profile_id);
  157. return AccountService::get($user->profile_id);
  158. }
  159. /**
  160. * GET /api/v1.1/accounts/{id}/posts
  161. *
  162. * @return \App\Transformer\Api\StatusTransformer
  163. */
  164. public function accountPosts(Request $request, $id)
  165. {
  166. $user = $request->user();
  167. abort_if(!$user, 403);
  168. abort_if($user->status != null, 403);
  169. $account = AccountService::get($id);
  170. if(!$account || $account['username'] !== $request->input('username')) {
  171. return $this->json([]);
  172. }
  173. $posts = ProfileStatusService::get($id);
  174. if(!$posts) {
  175. return $this->json([]);
  176. }
  177. $res = collect($posts)
  178. ->map(function($id) {
  179. return StatusService::get($id);
  180. })
  181. ->filter(function($post) {
  182. return $post && isset($post['account']);
  183. })
  184. ->toArray();
  185. return $this->json($res);
  186. }
  187. /**
  188. * POST /api/v1.1/accounts/change-password
  189. *
  190. * @return \App\Transformer\Api\AccountTransformer
  191. */
  192. public function accountChangePassword(Request $request)
  193. {
  194. $user = $request->user();
  195. abort_if(!$user, 403);
  196. abort_if($user->status != null, 403);
  197. $this->validate($request, [
  198. 'current_password' => 'bail|required|current_password',
  199. 'new_password' => 'required|min:' . config('pixelfed.min_password_length', 8),
  200. 'confirm_password' => 'required|same:new_password'
  201. ],[
  202. 'current_password' => 'The password you entered is incorrect'
  203. ]);
  204. $user->password = bcrypt($request->input('new_password'));
  205. $user->save();
  206. $log = new AccountLog;
  207. $log->user_id = $user->id;
  208. $log->item_id = $user->id;
  209. $log->item_type = 'App\User';
  210. $log->action = 'account.edit.password';
  211. $log->message = 'Password changed';
  212. $log->link = null;
  213. $log->ip_address = $request->ip();
  214. $log->user_agent = $request->userAgent();
  215. $log->save();
  216. Mail::to($request->user())->send(new PasswordChange($user));
  217. return $this->json(AccountService::get($user->profile_id));
  218. }
  219. /**
  220. * GET /api/v1.1/accounts/login-activity
  221. *
  222. * @return array
  223. */
  224. public function accountLoginActivity(Request $request)
  225. {
  226. $user = $request->user();
  227. abort_if(!$user, 403);
  228. abort_if($user->status != null, 403);
  229. $agent = new Agent();
  230. $currentIp = $request->ip();
  231. $activity = AccountLog::whereUserId($user->id)
  232. ->whereAction('auth.login')
  233. ->orderBy('created_at', 'desc')
  234. ->groupBy('ip_address')
  235. ->limit(10)
  236. ->get()
  237. ->map(function($item) use($agent, $currentIp) {
  238. $agent->setUserAgent($item->user_agent);
  239. return [
  240. 'id' => $item->id,
  241. 'action' => $item->action,
  242. 'ip' => $item->ip_address,
  243. 'ip_current' => $item->ip_address === $currentIp,
  244. 'is_mobile' => $agent->isMobile(),
  245. 'device' => $agent->device(),
  246. 'browser' => $agent->browser(),
  247. 'platform' => $agent->platform(),
  248. 'created_at' => $item->created_at->format('c')
  249. ];
  250. });
  251. return $this->json($activity);
  252. }
  253. /**
  254. * GET /api/v1.1/accounts/two-factor
  255. *
  256. * @return array
  257. */
  258. public function accountTwoFactor(Request $request)
  259. {
  260. $user = $request->user();
  261. abort_if(!$user, 403);
  262. abort_if($user->status != null, 403);
  263. $res = [
  264. 'active' => (bool) $user->{'2fa_enabled'},
  265. 'setup_at' => $user->{'2fa_setup_at'}
  266. ];
  267. return $this->json($res);
  268. }
  269. /**
  270. * GET /api/v1.1/accounts/emails-from-pixelfed
  271. *
  272. * @return array
  273. */
  274. public function accountEmailsFromPixelfed(Request $request)
  275. {
  276. $user = $request->user();
  277. abort_if(!$user, 403);
  278. abort_if($user->status != null, 403);
  279. $from = config('mail.from.address');
  280. $emailVerifications = EmailVerification::whereUserId($user->id)
  281. ->orderByDesc('id')
  282. ->where('created_at', '>', now()->subDays(14))
  283. ->limit(10)
  284. ->get()
  285. ->map(function($mail) use($user, $from) {
  286. return [
  287. 'type' => 'Email Verification',
  288. 'subject' => 'Confirm Email',
  289. 'to_address' => $user->email,
  290. 'from_address' => $from,
  291. 'created_at' => str_replace('@', 'at', $mail->created_at->format('M j, Y @ g:i:s A'))
  292. ];
  293. })
  294. ->toArray();
  295. $passwordResets = DB::table('password_resets')
  296. ->whereEmail($user->email)
  297. ->where('created_at', '>', now()->subDays(14))
  298. ->orderByDesc('created_at')
  299. ->limit(10)
  300. ->get()
  301. ->map(function($mail) use($user, $from) {
  302. return [
  303. 'type' => 'Password Reset',
  304. 'subject' => 'Reset Password Notification',
  305. 'to_address' => $user->email,
  306. 'from_address' => $from,
  307. 'created_at' => str_replace('@', 'at', now()->parse($mail->created_at)->format('M j, Y @ g:i:s A'))
  308. ];
  309. })
  310. ->toArray();
  311. $passwordChanges = AccountLog::whereUserId($user->id)
  312. ->whereAction('account.edit.password')
  313. ->where('created_at', '>', now()->subDays(14))
  314. ->orderByDesc('created_at')
  315. ->limit(10)
  316. ->get()
  317. ->map(function($mail) use($user, $from) {
  318. return [
  319. 'type' => 'Password Change',
  320. 'subject' => 'Password Change',
  321. 'to_address' => $user->email,
  322. 'from_address' => $from,
  323. 'created_at' => str_replace('@', 'at', now()->parse($mail->created_at)->format('M j, Y @ g:i:s A'))
  324. ];
  325. })
  326. ->toArray();
  327. $res = collect([])
  328. ->merge($emailVerifications)
  329. ->merge($passwordResets)
  330. ->merge($passwordChanges)
  331. ->sortByDesc('created_at')
  332. ->values();
  333. return $this->json($res);
  334. }
  335. /**
  336. * GET /api/v1.1/accounts/apps-and-applications
  337. *
  338. * @return array
  339. */
  340. public function accountApps(Request $request)
  341. {
  342. $user = $request->user();
  343. abort_if(!$user, 403);
  344. abort_if($user->status != null, 403);
  345. $res = $user->tokens->sortByDesc('created_at')->take(10)->map(function($token, $key) {
  346. return [
  347. 'id' => $key + 1,
  348. 'did' => encrypt($token->id),
  349. 'name' => $token->client->name,
  350. 'scopes' => $token->scopes,
  351. 'revoked' => $token->revoked,
  352. 'created_at' => str_replace('@', 'at', now()->parse($token->created_at)->format('M j, Y @ g:i:s A')),
  353. 'expires_at' => str_replace('@', 'at', now()->parse($token->expires_at)->format('M j, Y @ g:i:s A'))
  354. ];
  355. });
  356. return $this->json($res);
  357. }
  358. public function inAppRegistrationPreFlightCheck(Request $request)
  359. {
  360. return [
  361. 'open' => config('pixelfed.open_registration'),
  362. 'iara' => config('pixelfed.allow_app_registration')
  363. ];
  364. }
  365. public function inAppRegistration(Request $request)
  366. {
  367. abort_if($request->user(), 404);
  368. abort_unless(config('pixelfed.open_registration'), 404);
  369. abort_unless(config('pixelfed.allow_app_registration'), 404);
  370. abort_unless($request->hasHeader('X-PIXELFED-APP'), 403);
  371. $this->validate($request, [
  372. 'email' => [
  373. 'required',
  374. 'string',
  375. 'email',
  376. 'max:255',
  377. 'unique:users',
  378. function ($attribute, $value, $fail) {
  379. $banned = EmailService::isBanned($value);
  380. if($banned) {
  381. return $fail('Email is invalid.');
  382. }
  383. },
  384. ],
  385. 'username' => [
  386. 'required',
  387. 'min:2',
  388. 'max:15',
  389. 'unique:users',
  390. function ($attribute, $value, $fail) {
  391. $dash = substr_count($value, '-');
  392. $underscore = substr_count($value, '_');
  393. $period = substr_count($value, '.');
  394. if(ends_with($value, ['.php', '.js', '.css'])) {
  395. return $fail('Username is invalid.');
  396. }
  397. if(($dash + $underscore + $period) > 1) {
  398. return $fail('Username is invalid. Can only contain one dash (-), period (.) or underscore (_).');
  399. }
  400. if (!ctype_alnum($value[0])) {
  401. return $fail('Username is invalid. Must start with a letter or number.');
  402. }
  403. if (!ctype_alnum($value[strlen($value) - 1])) {
  404. return $fail('Username is invalid. Must end with a letter or number.');
  405. }
  406. $val = str_replace(['_', '.', '-'], '', $value);
  407. if(!ctype_alnum($val)) {
  408. return $fail('Username is invalid. Username must be alpha-numeric and may contain dashes (-), periods (.) and underscores (_).');
  409. }
  410. $restricted = RestrictedNames::get();
  411. if (in_array(strtolower($value), array_map('strtolower', $restricted))) {
  412. return $fail('Username cannot be used.');
  413. }
  414. },
  415. ],
  416. 'password' => 'required|string|min:8',
  417. ]);
  418. $email = $request->input('email');
  419. $username = $request->input('username');
  420. $password = $request->input('password');
  421. if(config('database.default') == 'pgsql') {
  422. $username = strtolower($username);
  423. $email = strtolower($email);
  424. }
  425. $user = new User;
  426. $user->name = $username;
  427. $user->username = $username;
  428. $user->email = $email;
  429. $user->password = Hash::make($password);
  430. $user->register_source = 'app';
  431. $user->app_register_ip = $request->ip();
  432. $user->app_register_token = Str::random(32);
  433. $user->save();
  434. $rtoken = Str::random(mt_rand(64, 70));
  435. $verify = new EmailVerification();
  436. $verify->user_id = $user->id;
  437. $verify->email = $user->email;
  438. $verify->user_token = $user->app_register_token;
  439. $verify->random_token = $rtoken;
  440. $verify->save();
  441. $appUrl = url('/api/v1.1/auth/iarer?ut=' . $user->app_register_token . '&rt=' . $rtoken);
  442. Mail::to($user->email)->send(new ConfirmAppEmail($verify, $appUrl));
  443. return response()->json([
  444. 'success' => true,
  445. ]);
  446. }
  447. public function inAppRegistrationEmailRedirect(Request $request)
  448. {
  449. $this->validate($request, [
  450. 'ut' => 'required',
  451. 'rt' => 'required'
  452. ]);
  453. $ut = $request->input('ut');
  454. $rt = $request->input('rt');
  455. $url = 'pixelfed://confirm-account/'. $ut . '?rt=' . $rt;
  456. return redirect()->away($url);
  457. }
  458. public function inAppRegistrationConfirm(Request $request)
  459. {
  460. abort_if($request->user(), 404);
  461. abort_unless(config('pixelfed.open_registration'), 404);
  462. abort_unless(config('pixelfed.allow_app_registration'), 404);
  463. abort_unless($request->hasHeader('X-PIXELFED-APP'), 403);
  464. $this->validate($request, [
  465. 'user_token' => 'required',
  466. 'random_token' => 'required',
  467. 'email' => 'required'
  468. ]);
  469. $verify = EmailVerification::whereEmail($request->input('email'))
  470. ->whereUserToken($request->input('user_token'))
  471. ->whereRandomToken($request->input('random_token'))
  472. ->first();
  473. if(!$verify) {
  474. return response()->json(['error' => 'Invalid tokens'], 403);
  475. }
  476. if($verify->created_at->lt(now()->subHours(24))) {
  477. $verify->delete();
  478. return response()->json(['error' => 'Invalid tokens'], 403);
  479. }
  480. $user = User::findOrFail($verify->user_id);
  481. $user->email_verified_at = now();
  482. $user->last_active_at = now();
  483. $user->save();
  484. $token = $user->createToken('Pixelfed');
  485. return response()->json([
  486. 'access_token' => $token->accessToken
  487. ]);
  488. }
  489. public function archive(Request $request, $id)
  490. {
  491. abort_if(!$request->user(), 403);
  492. $status = Status::whereNull('in_reply_to_id')
  493. ->whereNull('reblog_of_id')
  494. ->whereProfileId($request->user()->profile_id)
  495. ->findOrFail($id);
  496. if($status->scope === 'archived') {
  497. return [200];
  498. }
  499. $archive = new StatusArchived;
  500. $archive->status_id = $status->id;
  501. $archive->profile_id = $status->profile_id;
  502. $archive->original_scope = $status->scope;
  503. $archive->save();
  504. $status->scope = 'archived';
  505. $status->visibility = 'draft';
  506. $status->save();
  507. StatusService::del($status->id, true);
  508. AccountService::syncPostCount($status->profile_id);
  509. return [200];
  510. }
  511. public function unarchive(Request $request, $id)
  512. {
  513. abort_if(!$request->user(), 403);
  514. $status = Status::whereNull('in_reply_to_id')
  515. ->whereNull('reblog_of_id')
  516. ->whereProfileId($request->user()->profile_id)
  517. ->findOrFail($id);
  518. if($status->scope !== 'archived') {
  519. return [200];
  520. }
  521. $archive = StatusArchived::whereStatusId($status->id)
  522. ->whereProfileId($status->profile_id)
  523. ->firstOrFail();
  524. $status->scope = $archive->original_scope;
  525. $status->visibility = $archive->original_scope;
  526. $status->save();
  527. $archive->delete();
  528. StatusService::del($status->id, true);
  529. AccountService::syncPostCount($status->profile_id);
  530. return [200];
  531. }
  532. public function archivedPosts(Request $request)
  533. {
  534. abort_if(!$request->user(), 403);
  535. $statuses = Status::whereProfileId($request->user()->profile_id)
  536. ->whereScope('archived')
  537. ->orderByDesc('id')
  538. ->cursorPaginate(10);
  539. return StatusStateless::collection($statuses);
  540. }
  541. public function placesById(Request $request, $id, $slug)
  542. {
  543. abort_if(!$request->user(), 403);
  544. $place = Place::whereSlug($slug)->findOrFail($id);
  545. $posts = Cache::remember('pf-api:v1.1:places-by-id:' . $place->id, 3600, function() use($place) {
  546. return Status::wherePlaceId($place->id)
  547. ->whereNull('uri')
  548. ->whereScope('public')
  549. ->orderByDesc('created_at')
  550. ->limit(60)
  551. ->pluck('id');
  552. });
  553. $posts = $posts->map(function($id) {
  554. return StatusService::get($id);
  555. })
  556. ->filter()
  557. ->values();
  558. return [
  559. 'place' =>
  560. [
  561. 'id' => $place->id,
  562. 'name' => $place->name,
  563. 'slug' => $place->slug,
  564. 'country' => $place->country,
  565. 'lat' => $place->lat,
  566. 'long' => $place->long
  567. ],
  568. 'posts' => $posts];
  569. }
  570. public function moderatePost(Request $request, $id)
  571. {
  572. abort_if(!$request->user(), 403);
  573. abort_if($request->user()->is_admin != true, 403);
  574. $this->validate($request, [
  575. 'action' => 'required|in:cw,mark-public,mark-unlisted,mark-private,mark-spammer,delete'
  576. ]);
  577. $action = $request->input('action');
  578. $status = Status::find($id);
  579. if(!$status) {
  580. return response()->json(['error' => 'Cannot find status'], 400);
  581. }
  582. if($status->uri == null) {
  583. if($status->profile->user && $status->profile->user->is_admin) {
  584. return response()->json(['error' => 'Cannot moderate admin accounts'], 400);
  585. }
  586. }
  587. if($action == 'mark-spammer') {
  588. $status->profile->update([
  589. 'unlisted' => true,
  590. 'cw' => true,
  591. 'no_autolink' => true
  592. ]);
  593. Status::whereProfileId($status->profile_id)
  594. ->get()
  595. ->each(function($s) {
  596. if(in_array($s->scope, ['public', 'unlisted'])) {
  597. $s->scope = 'private';
  598. $s->visibility = 'private';
  599. }
  600. $s->is_nsfw = true;
  601. $s->save();
  602. StatusService::del($s->id, true);
  603. });
  604. Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $status->profile_id);
  605. Cache::forget('pf:bouncer_v0:recent_by_pid:' . $status->profile_id);
  606. Cache::forget('admin-dash:reports:spam-count');
  607. } else if ($action == 'cw') {
  608. $state = $status->is_nsfw;
  609. $status->is_nsfw = !$state;
  610. $status->save();
  611. StatusService::del($status->id);
  612. } else if ($action == 'mark-public') {
  613. $state = $status->scope;
  614. $status->scope = 'public';
  615. $status->visibility = 'public';
  616. $status->save();
  617. StatusService::del($status->id, true);
  618. if($state !== 'public') {
  619. if($status->uri) {
  620. NetworkTimelineService::add($status->id);
  621. } else {
  622. PublicTimelineService::add($status->id);
  623. }
  624. }
  625. } else if ($action == 'mark-unlisted') {
  626. $state = $status->scope;
  627. $status->scope = 'unlisted';
  628. $status->visibility = 'unlisted';
  629. $status->save();
  630. StatusService::del($status->id);
  631. if($state == 'public') {
  632. PublicTimelineService::del($status->id);
  633. NetworkTimelineService::del($status->id);
  634. }
  635. } else if ($action == 'mark-private') {
  636. $state = $status->scope;
  637. $status->scope = 'private';
  638. $status->visibility = 'private';
  639. $status->save();
  640. StatusService::del($status->id);
  641. if($state == 'public') {
  642. PublicTimelineService::del($status->id);
  643. NetworkTimelineService::del($status->id);
  644. }
  645. } else if ($action == 'delete') {
  646. PublicTimelineService::del($status->id);
  647. NetworkTimelineService::del($status->id);
  648. Cache::forget('_api:statuses:recent_9:' . $status->profile_id);
  649. Cache::forget('profile:status_count:' . $status->profile_id);
  650. Cache::forget('profile:embed:' . $status->profile_id);
  651. StatusService::del($status->id, true);
  652. Cache::forget('profile:status_count:'.$status->profile_id);
  653. StatusDelete::dispatch($status);
  654. return [];
  655. }
  656. Cache::forget('_api:statuses:recent_9:'.$status->profile_id);
  657. return StatusService::get($status->id, false);
  658. }
  659. }