ApiV1Dot1Controller.php 6.1 KB

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