123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- <?php
- namespace App\Http\Controllers\Api;
- use Cache;
- use DB;
- use App\Http\Controllers\Controller;
- use Illuminate\Http\Request;
- use League\Fractal;
- use League\Fractal\Serializer\ArraySerializer;
- use League\Fractal\Pagination\IlluminatePaginatorAdapter;
- use App\AccountLog;
- use App\EmailVerification;
- use App\Status;
- use App\Report;
- use App\Profile;
- use App\Services\AccountService;
- use App\Services\StatusService;
- use App\Services\ProfileStatusService;
- use Jenssegers\Agent\Agent;
- use Mail;
- use App\Mail\PasswordChange;
- 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);
- }
- /**
- * GET /api/v1.1/accounts/{id}/posts
- *
- * @return \App\Transformer\Api\StatusTransformer
- */
- public function accountPosts(Request $request, $id)
- {
- $user = $request->user();
- abort_if(!$user, 403);
- abort_if($user->status != null, 403);
- $account = AccountService::get($id);
- if(!$account || $account['username'] !== $request->input('username')) {
- return $this->json([]);
- }
- $posts = ProfileStatusService::get($id);
- if(!$posts) {
- return $this->json([]);
- }
- $res = collect($posts)
- ->map(function($id) {
- return StatusService::get($id);
- })
- ->filter(function($post) {
- return $post && isset($post['account']);
- })
- ->toArray();
- return $this->json($res);
- }
- /**
- * POST /api/v1.1/accounts/change-password
- *
- * @return \App\Transformer\Api\AccountTransformer
- */
- public function accountChangePassword(Request $request)
- {
- $user = $request->user();
- abort_if(!$user, 403);
- abort_if($user->status != null, 403);
- $this->validate($request, [
- 'current_password' => 'bail|required|current_password',
- 'new_password' => 'required|min:' . config('pixelfed.min_password_length', 8),
- 'confirm_password' => 'required|same:new_password'
- ],[
- 'current_password' => 'The password you entered is incorrect'
- ]);
- $user->password = bcrypt($request->input('new_password'));
- $user->save();
- $log = new AccountLog;
- $log->user_id = $user->id;
- $log->item_id = $user->id;
- $log->item_type = 'App\User';
- $log->action = 'account.edit.password';
- $log->message = 'Password changed';
- $log->link = null;
- $log->ip_address = $request->ip();
- $log->user_agent = $request->userAgent();
- $log->save();
- Mail::to($request->user())->send(new PasswordChange($user));
- return $this->json(AccountService::get($user->profile_id));
- }
- /**
- * GET /api/v1.1/accounts/login-activity
- *
- * @return array
- */
- public function accountLoginActivity(Request $request)
- {
- $user = $request->user();
- abort_if(!$user, 403);
- abort_if($user->status != null, 403);
- $agent = new Agent();
- $currentIp = $request->ip();
- $activity = AccountLog::whereUserId($user->id)
- ->whereAction('auth.login')
- ->orderBy('created_at', 'desc')
- ->limit(10)
- ->get()
- ->map(function($item) use($agent, $currentIp) {
- $agent->setUserAgent($item->user_agent);
- return [
- 'id' => $item->id,
- 'action' => $item->action,
- 'ip' => $item->ip_address,
- 'ip_current' => $item->ip_address === $currentIp,
- 'is_mobile' => $agent->isMobile(),
- 'device' => $agent->device(),
- 'browser' => $agent->browser(),
- 'platform' => $agent->platform(),
- 'created_at' => $item->created_at->format('c')
- ];
- });
- return $this->json($activity);
- }
- /**
- * GET /api/v1.1/accounts/two-factor
- *
- * @return array
- */
- public function accountTwoFactor(Request $request)
- {
- $user = $request->user();
- abort_if(!$user, 403);
- abort_if($user->status != null, 403);
- $res = [
- 'active' => (bool) $user->{'2fa_enabled'},
- 'setup_at' => $user->{'2fa_setup_at'}
- ];
- return $this->json($res);
- }
- /**
- * GET /api/v1.1/accounts/emails-from-pixelfed
- *
- * @return array
- */
- public function accountEmailsFromPixelfed(Request $request)
- {
- $user = $request->user();
- abort_if(!$user, 403);
- abort_if($user->status != null, 403);
- $emailVerifications = EmailVerification::whereUserId($user->id)
- ->orderByDesc('id')
- ->where('created_at', '>', now()->subDays(14))
- ->limit(10)
- ->get()
- ->map(function($mail) {
- return [
- 'type' => 'Email Verification',
- 'created_at' => $mail->created_at->format('c')
- ];
- })
- ->toArray();
- $passwordResets = DB::table('password_resets')
- ->whereEmail($user->email)
- ->where('created_at', '>', now()->subDays(14))
- ->orderByDesc('created_at')
- ->limit(10)
- ->get()
- ->map(function($mail) {
- return [
- 'type' => 'Password Reset',
- 'created_at' => now()->parse($mail->created_at)->format('c')
- ];
- })
- ->toArray();
- $passwordChanges = AccountLog::whereUserId($user->id)
- ->whereAction('account.edit.password')
- ->where('created_at', '>', now()->subDays(14))
- ->orderByDesc('created_at')
- ->limit(10)
- ->get()
- ->map(function($mail) {
- return [
- 'type' => 'Password Change',
- 'created_at' => $mail->created_at
- ];
- })
- ->toArray();
- $res = [
- 'email_verifications' => $emailVerifications,
- 'password_resets' => $passwordResets,
- 'password_changes' => $passwordChanges
- ];
- return $this->json($res);
- }
- /**
- * GET /api/v1.1/accounts/apps-and-applications
- *
- * @return array
- */
- public function accountApps(Request $request)
- {
- $user = $request->user();
- abort_if(!$user, 403);
- abort_if($user->status != null, 403);
- $res = $user->tokens->map(function($token, $key) {
- return [
- 'id' => $key + 1,
- 'did' => encrypt($token->id),
- 'name' => $token->name,
- 'scopes' => $token->scopes,
- 'revoked' => $token->revoked,
- 'created_at' => $token->created_at,
- 'expires_at' => $token->expires_at
- ];
- });
- return $this->json($res);
- }
- }
|