ApiV1Dot1Controller.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use League\Fractal;
  6. use League\Fractal\Serializer\ArraySerializer;
  7. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  8. use App\Status;
  9. use App\Report;
  10. use App\Profile;
  11. class ApiV1Dot1Controller extends Controller
  12. {
  13. protected $fractal;
  14. public function __construct()
  15. {
  16. $this->fractal = new Fractal\Manager();
  17. $this->fractal->setSerializer(new ArraySerializer());
  18. }
  19. public function json($res, $code = 200, $headers = [])
  20. {
  21. return response()->json($res, $code, $headers, JSON_UNESCAPED_SLASHES);
  22. }
  23. public function error($msg, $code = 400, $extra = [], $headers = [])
  24. {
  25. $res = [
  26. "msg" => $msg,
  27. "code" => $code
  28. ];
  29. return response()->json(array_merge($res, $extra), $code, $headers, JSON_UNESCAPED_SLASHES);
  30. }
  31. public function report(Request $request)
  32. {
  33. $user = $request->user();
  34. abort_if(!$user, 403);
  35. abort_if($user->status != null, 403);
  36. $report_type = $request->input('report_type');
  37. $object_id = $request->input('object_id');
  38. $object_type = $request->input('object_type');
  39. $types = [
  40. 'spam',
  41. 'sensitive',
  42. 'abusive',
  43. 'underage',
  44. 'violence',
  45. 'copyright',
  46. 'impersonation',
  47. 'scam',
  48. 'terrorism'
  49. ];
  50. if (!$report_type || !$object_id || !$object_type) {
  51. return $this->error("Invalid or missing parameters", 400, ["error_code" => "ERROR_INVALID_PARAMS"]);
  52. }
  53. if (!in_array($report_type, $types)) {
  54. return $this->error("Invalid report type", 400, ["error_code" => "ERROR_TYPE_INVALID"]);
  55. }
  56. if ($object_type === "user" && $object_id == $user->profile_id) {
  57. return $this->error("Cannot self report", 400, ["error_code" => "ERROR_NO_SELF_REPORTS"]);
  58. }
  59. $rpid = null;
  60. switch ($object_type) {
  61. case 'post':
  62. $object = Status::find($object_id);
  63. if (!$object) {
  64. return $this->error("Invalid object id", 400, ["error_code" => "ERROR_INVALID_OBJECT_ID"]);
  65. }
  66. $object_type = 'App\Status';
  67. $exists = Report::whereUserId($user->id)
  68. ->whereObjectId($object->id)
  69. ->whereObjectType('App\Status')
  70. ->count();
  71. $rpid = $object->profile_id;
  72. break;
  73. case 'user':
  74. $object = Profile::find($object_id);
  75. if (!$object) {
  76. return $this->error("Invalid object id", 400, ["error_code" => "ERROR_INVALID_OBJECT_ID"]);
  77. }
  78. $object_type = 'App\Profile';
  79. $exists = Report::whereUserId($user->id)
  80. ->whereObjectId($object->id)
  81. ->whereObjectType('App\Profile')
  82. ->count();
  83. $rpid = $object->id;
  84. break;
  85. default:
  86. return $this->error("Invalid report type", 400, ["error_code" => "ERROR_REPORT_OBJECT_TYPE_INVALID"]);
  87. break;
  88. }
  89. if ($exists !== 0) {
  90. return $this->error("Duplicate report", 400, ["error_code" => "ERROR_REPORT_DUPLICATE"]);
  91. }
  92. if ($object->profile_id == $user->profile_id) {
  93. return $this->error("Cannot self report", 400, ["error_code" => "ERROR_NO_SELF_REPORTS"]);
  94. }
  95. $report = new Report;
  96. $report->profile_id = $user->profile_id;
  97. $report->user_id = $user->id;
  98. $report->object_id = $object->id;
  99. $report->object_type = $object_type;
  100. $report->reported_profile_id = $rpid;
  101. $report->type = $report_type;
  102. $report->save();
  103. $res = [
  104. "msg" => "Successfully sent report",
  105. "code" => 200
  106. ];
  107. return $this->json($res);
  108. }
  109. }