Update ApiV1Controller, improve favourites endpoint

This commit is contained in:
Daniel Supernault 2022-02-13 00:23:29 -07:00
parent 0a8b9f61f5
commit 151dc17c25
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -1026,33 +1026,38 @@ class ApiV1Controller extends Controller
$user = $request->user(); $user = $request->user();
$status = Status::findOrFail($id); $status = StatusService::getMastodon($id, false);
if($status->profile_id !== $user->profile_id) { abort_unless($status, 400);
if($status->scope == 'private') {
abort_if(!$status->profile->followedBy($user->profile), 403); $spid = $status['account']['id'];
if($spid !== $user->profile_id) {
if($status['visibility'] == 'private') {
abort_if(!FollowerService::follows($user->profile_id, $spid), 403);
} else { } else {
abort_if(!in_array($status->scope, ['public','unlisted']), 403); abort_if(!in_array($status['visibility'], ['public','unlisted']), 403);
} }
} }
$like = Like::firstOrCreate([ $like = Like::firstOrCreate([
'profile_id' => $user->profile_id, 'profile_id' => $user->profile_id,
'status_id' => $status->id 'status_id' => $status['id']
]); ]);
if($like->wasRecentlyCreated == true) { if($like->wasRecentlyCreated == true) {
$like->status_profile_id = $status->profile_id; $like->status_profile_id = $spid;
$like->is_comment = !empty($status->in_reply_to_id); $like->is_comment = !empty($status['in_reply_to_id']);
$like->save(); $like->save();
$status->likes_count = $status->likes()->count(); Status::findOrFail($status['id'])->update([
$status->save(); 'favourites_count' => ($status['favourites_count'] ?? 0) + 1
]);
LikePipeline::dispatch($like); LikePipeline::dispatch($like);
} }
$res = StatusService::getMastodon($status->id, false); $status['favourited'] = true;
$res['favourited'] = true; $status['favourites_count'] = $status['favourites_count'] + 1;
return response()->json($res); return response()->json($status);
} }
/** /**