AccountInterstitialController.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Status;
  5. use App\AccountInterstitial;
  6. class AccountInterstitialController extends Controller
  7. {
  8. public function __construct()
  9. {
  10. $this->middleware('auth');
  11. }
  12. public function get(Request $request)
  13. {
  14. $interstitial = $request->user()
  15. ->interstitials()
  16. ->whereNull('read_at')
  17. ->first();
  18. if(!$interstitial) {
  19. $user = $request->user();
  20. $user->has_interstitial = false;
  21. $user->save();
  22. return redirect('/');
  23. }
  24. $meta = json_decode($interstitial->meta);
  25. $view = $interstitial->view;
  26. return view($view, compact('interstitial', 'meta'));
  27. }
  28. public function read(Request $request)
  29. {
  30. $this->validate($request, [
  31. 'id' => 'required',
  32. 'type' => 'required|in:post.cw,post.removed,post.unlist',
  33. 'action' => 'required|in:appeal,confirm',
  34. 'appeal_message' => 'nullable|max:500'
  35. ]);
  36. $redirect = '/';
  37. $id = decrypt($request->input('id'));
  38. $action = $request->input('action');
  39. $user = $request->user();
  40. $ai = AccountInterstitial::whereUserId($user->id)
  41. ->whereType($request->input('type'))
  42. ->findOrFail($id);
  43. if($action == 'appeal') {
  44. $ai->appeal_requested_at = now();
  45. $ai->appeal_message = $request->input('appeal_message');
  46. }
  47. $ai->read_at = now();
  48. $ai->save();
  49. $more = AccountInterstitial::whereUserId($user->id)
  50. ->whereNull('read_at')
  51. ->exists();
  52. if(!$more) {
  53. $user->has_interstitial = false;
  54. $user->save();
  55. }
  56. if(in_array($ai->type, ['post.cw', 'post.unlist'])) {
  57. $redirect = Status::findOrFail($ai->item_id)->url();
  58. }
  59. return redirect($redirect);
  60. }
  61. }