mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-30 10:13:16 +00:00
169 lines
5.1 KiB
PHP
169 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use Cache;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use League\Fractal;
|
|
use League\Fractal\Serializer\ArraySerializer;
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
|
use App\Status;
|
|
use App\Report;
|
|
use App\Profile;
|
|
use App\Services\AccountService;
|
|
|
|
class ApiV1Dot1Controller extends Controller
|
|
{
|
|
protected $fractal;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->fractal = new Fractal\Manager();
|
|
$this->fractal->setSerializer(new ArraySerializer());
|
|
}
|
|
|
|
public function json($res, $code = 200, $headers = [])
|
|
{
|
|
return response()->json($res, $code, $headers, JSON_UNESCAPED_SLASHES);
|
|
}
|
|
|
|
public function error($msg, $code = 400, $extra = [], $headers = [])
|
|
{
|
|
$res = [
|
|
"msg" => $msg,
|
|
"code" => $code
|
|
];
|
|
return response()->json(array_merge($res, $extra), $code, $headers, JSON_UNESCAPED_SLASHES);
|
|
}
|
|
|
|
public function report(Request $request)
|
|
{
|
|
$user = $request->user();
|
|
|
|
abort_if(!$user, 403);
|
|
abort_if($user->status != null, 403);
|
|
|
|
$report_type = $request->input('report_type');
|
|
$object_id = $request->input('object_id');
|
|
$object_type = $request->input('object_type');
|
|
|
|
$types = [
|
|
'spam',
|
|
'sensitive',
|
|
'abusive',
|
|
'underage',
|
|
'violence',
|
|
'copyright',
|
|
'impersonation',
|
|
'scam',
|
|
'terrorism'
|
|
];
|
|
|
|
if (!$report_type || !$object_id || !$object_type) {
|
|
return $this->error("Invalid or missing parameters", 400, ["error_code" => "ERROR_INVALID_PARAMS"]);
|
|
}
|
|
|
|
if (!in_array($report_type, $types)) {
|
|
return $this->error("Invalid report type", 400, ["error_code" => "ERROR_TYPE_INVALID"]);
|
|
}
|
|
|
|
if ($object_type === "user" && $object_id == $user->profile_id) {
|
|
return $this->error("Cannot self report", 400, ["error_code" => "ERROR_NO_SELF_REPORTS"]);
|
|
}
|
|
|
|
$rpid = null;
|
|
|
|
switch ($object_type) {
|
|
case 'post':
|
|
$object = Status::find($object_id);
|
|
if (!$object) {
|
|
return $this->error("Invalid object id", 400, ["error_code" => "ERROR_INVALID_OBJECT_ID"]);
|
|
}
|
|
$object_type = 'App\Status';
|
|
$exists = Report::whereUserId($user->id)
|
|
->whereObjectId($object->id)
|
|
->whereObjectType('App\Status')
|
|
->count();
|
|
|
|
$rpid = $object->profile_id;
|
|
break;
|
|
|
|
case 'user':
|
|
$object = Profile::find($object_id);
|
|
if (!$object) {
|
|
return $this->error("Invalid object id", 400, ["error_code" => "ERROR_INVALID_OBJECT_ID"]);
|
|
}
|
|
$object_type = 'App\Profile';
|
|
$exists = Report::whereUserId($user->id)
|
|
->whereObjectId($object->id)
|
|
->whereObjectType('App\Profile')
|
|
->count();
|
|
$rpid = $object->id;
|
|
break;
|
|
|
|
default:
|
|
return $this->error("Invalid report type", 400, ["error_code" => "ERROR_REPORT_OBJECT_TYPE_INVALID"]);
|
|
break;
|
|
}
|
|
|
|
if ($exists !== 0) {
|
|
return $this->error("Duplicate report", 400, ["error_code" => "ERROR_REPORT_DUPLICATE"]);
|
|
}
|
|
|
|
if ($object->profile_id == $user->profile_id) {
|
|
return $this->error("Cannot self report", 400, ["error_code" => "ERROR_NO_SELF_REPORTS"]);
|
|
}
|
|
|
|
$report = new Report;
|
|
$report->profile_id = $user->profile_id;
|
|
$report->user_id = $user->id;
|
|
$report->object_id = $object->id;
|
|
$report->object_type = $object_type;
|
|
$report->reported_profile_id = $rpid;
|
|
$report->type = $report_type;
|
|
$report->save();
|
|
|
|
$res = [
|
|
"msg" => "Successfully sent report",
|
|
"code" => 200
|
|
];
|
|
return $this->json($res);
|
|
}
|
|
|
|
/**
|
|
* DELETE /api/v1.1/accounts/avatar
|
|
*
|
|
* @return \App\Transformer\Api\AccountTransformer
|
|
*/
|
|
public function deleteAvatar(Request $request)
|
|
{
|
|
$user = $request->user();
|
|
|
|
abort_if(!$user, 403);
|
|
abort_if($user->status != null, 403);
|
|
|
|
$avatar = $user->profile->avatar;
|
|
|
|
if( $avatar->media_path == 'public/avatars/default.png' ||
|
|
$avatar->media_path == 'public/avatars/default.jpg'
|
|
) {
|
|
return AccountService::get($user->profile_id);
|
|
}
|
|
|
|
if(is_file(storage_path('app/' . $avatar->media_path))) {
|
|
@unlink(storage_path('app/' . $avatar->media_path));
|
|
}
|
|
|
|
$avatar->media_path = 'public/avatars/default.jpg';
|
|
$avatar->change_count = $avatar->change_count + 1;
|
|
$avatar->save();
|
|
|
|
Cache::forget('avatar:' . $user->profile_id);
|
|
Cache::forget("avatar:{$user->profile_id}");
|
|
Cache::forget('user:account:id:'.$user->id);
|
|
AccountService::del($user->profile_id);
|
|
|
|
return AccountService::get($user->profile_id);
|
|
}
|
|
}
|