From 151dc17c25aa30a56940585120b7c95b987967ea Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 13 Feb 2022 00:23:29 -0700 Subject: [PATCH] Update ApiV1Controller, improve favourites endpoint --- app/Http/Controllers/Api/ApiV1Controller.php | 31 ++++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/Api/ApiV1Controller.php b/app/Http/Controllers/Api/ApiV1Controller.php index 70ae2f408..6b3935496 100644 --- a/app/Http/Controllers/Api/ApiV1Controller.php +++ b/app/Http/Controllers/Api/ApiV1Controller.php @@ -1026,33 +1026,38 @@ class ApiV1Controller extends Controller $user = $request->user(); - $status = Status::findOrFail($id); + $status = StatusService::getMastodon($id, false); - if($status->profile_id !== $user->profile_id) { - if($status->scope == 'private') { - abort_if(!$status->profile->followedBy($user->profile), 403); + abort_unless($status, 400); + + $spid = $status['account']['id']; + + if($spid !== $user->profile_id) { + if($status['visibility'] == 'private') { + abort_if(!FollowerService::follows($user->profile_id, $spid), 403); } else { - abort_if(!in_array($status->scope, ['public','unlisted']), 403); + abort_if(!in_array($status['visibility'], ['public','unlisted']), 403); } } $like = Like::firstOrCreate([ 'profile_id' => $user->profile_id, - 'status_id' => $status->id + 'status_id' => $status['id'] ]); if($like->wasRecentlyCreated == true) { - $like->status_profile_id = $status->profile_id; - $like->is_comment = !empty($status->in_reply_to_id); + $like->status_profile_id = $spid; + $like->is_comment = !empty($status['in_reply_to_id']); $like->save(); - $status->likes_count = $status->likes()->count(); - $status->save(); + Status::findOrFail($status['id'])->update([ + 'favourites_count' => ($status['favourites_count'] ?? 0) + 1 + ]); LikePipeline::dispatch($like); } - $res = StatusService::getMastodon($status->id, false); - $res['favourited'] = true; - return response()->json($res); + $status['favourited'] = true; + $status['favourites_count'] = $status['favourites_count'] + 1; + return response()->json($status); } /**