ApiV1Dot1Controller.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use Cache;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\Http\Request;
  6. use League\Fractal;
  7. use League\Fractal\Serializer\ArraySerializer;
  8. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  9. use App\Status;
  10. use App\Report;
  11. use App\Profile;
  12. use App\Services\AccountService;
  13. class ApiV1Dot1Controller extends Controller
  14. {
  15. protected $fractal;
  16. public function __construct()
  17. {
  18. $this->fractal = new Fractal\Manager();
  19. $this->fractal->setSerializer(new ArraySerializer());
  20. }
  21. public function json($res, $code = 200, $headers = [])
  22. {
  23. return response()->json($res, $code, $headers, JSON_UNESCAPED_SLASHES);
  24. }
  25. public function error($msg, $code = 400, $extra = [], $headers = [])
  26. {
  27. $res = [
  28. "msg" => $msg,
  29. "code" => $code
  30. ];
  31. return response()->json(array_merge($res, $extra), $code, $headers, JSON_UNESCAPED_SLASHES);
  32. }
  33. public function report(Request $request)
  34. {
  35. $user = $request->user();
  36. abort_if(!$user, 403);
  37. abort_if($user->status != null, 403);
  38. $report_type = $request->input('report_type');
  39. $object_id = $request->input('object_id');
  40. $object_type = $request->input('object_type');
  41. $types = [
  42. 'spam',
  43. 'sensitive',
  44. 'abusive',
  45. 'underage',
  46. 'violence',
  47. 'copyright',
  48. 'impersonation',
  49. 'scam',
  50. 'terrorism'
  51. ];
  52. if (!$report_type || !$object_id || !$object_type) {
  53. return $this->error("Invalid or missing parameters", 400, ["error_code" => "ERROR_INVALID_PARAMS"]);
  54. }
  55. if (!in_array($report_type, $types)) {
  56. return $this->error("Invalid report type", 400, ["error_code" => "ERROR_TYPE_INVALID"]);
  57. }
  58. if ($object_type === "user" && $object_id == $user->profile_id) {
  59. return $this->error("Cannot self report", 400, ["error_code" => "ERROR_NO_SELF_REPORTS"]);
  60. }
  61. $rpid = null;
  62. switch ($object_type) {
  63. case 'post':
  64. $object = Status::find($object_id);
  65. if (!$object) {
  66. return $this->error("Invalid object id", 400, ["error_code" => "ERROR_INVALID_OBJECT_ID"]);
  67. }
  68. $object_type = 'App\Status';
  69. $exists = Report::whereUserId($user->id)
  70. ->whereObjectId($object->id)
  71. ->whereObjectType('App\Status')
  72. ->count();
  73. $rpid = $object->profile_id;
  74. break;
  75. case 'user':
  76. $object = Profile::find($object_id);
  77. if (!$object) {
  78. return $this->error("Invalid object id", 400, ["error_code" => "ERROR_INVALID_OBJECT_ID"]);
  79. }
  80. $object_type = 'App\Profile';
  81. $exists = Report::whereUserId($user->id)
  82. ->whereObjectId($object->id)
  83. ->whereObjectType('App\Profile')
  84. ->count();
  85. $rpid = $object->id;
  86. break;
  87. default:
  88. return $this->error("Invalid report type", 400, ["error_code" => "ERROR_REPORT_OBJECT_TYPE_INVALID"]);
  89. break;
  90. }
  91. if ($exists !== 0) {
  92. return $this->error("Duplicate report", 400, ["error_code" => "ERROR_REPORT_DUPLICATE"]);
  93. }
  94. if ($object->profile_id == $user->profile_id) {
  95. return $this->error("Cannot self report", 400, ["error_code" => "ERROR_NO_SELF_REPORTS"]);
  96. }
  97. $report = new Report;
  98. $report->profile_id = $user->profile_id;
  99. $report->user_id = $user->id;
  100. $report->object_id = $object->id;
  101. $report->object_type = $object_type;
  102. $report->reported_profile_id = $rpid;
  103. $report->type = $report_type;
  104. $report->save();
  105. $res = [
  106. "msg" => "Successfully sent report",
  107. "code" => 200
  108. ];
  109. return $this->json($res);
  110. }
  111. /**
  112. * DELETE /api/v1.1/accounts/avatar
  113. *
  114. * @return \App\Transformer\Api\AccountTransformer
  115. */
  116. public function deleteAvatar(Request $request)
  117. {
  118. $user = $request->user();
  119. abort_if(!$user, 403);
  120. abort_if($user->status != null, 403);
  121. $avatar = $user->profile->avatar;
  122. if( $avatar->media_path == 'public/avatars/default.png' ||
  123. $avatar->media_path == 'public/avatars/default.jpg'
  124. ) {
  125. return AccountService::get($user->profile_id);
  126. }
  127. if(is_file(storage_path('app/' . $avatar->media_path))) {
  128. @unlink(storage_path('app/' . $avatar->media_path));
  129. }
  130. $avatar->media_path = 'public/avatars/default.jpg';
  131. $avatar->change_count = $avatar->change_count + 1;
  132. $avatar->save();
  133. Cache::forget('avatar:' . $user->profile_id);
  134. Cache::forget("avatar:{$user->profile_id}");
  135. Cache::forget('user:account:id:'.$user->id);
  136. AccountService::del($user->profile_id);
  137. return AccountService::get($user->profile_id);
  138. }
  139. }