Add /api/v1/accounts/{id}/unfollow endpoint

This commit is contained in:
Daniel Supernault 2019-09-24 22:58:29 -06:00
parent c09f94b3e4
commit fadc96b2d6
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
2 changed files with 68 additions and 2 deletions

View file

@ -328,9 +328,12 @@ class ApiV1Controller extends Controller
->whereFollowingId($target->id)
->exists();
// Following already, return empty response
// Following already, return empty relationship
if($isFollowing == true) {
return response()->json([]);
$resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
$res = $this->fractal->createData($resource)->toArray();
return response()->json($res);
}
// Rate limits, max 7500 followers per account
@ -377,6 +380,68 @@ class ApiV1Controller extends Controller
return response()->json($res);
}
/**
* POST /api/v1/accounts/{id}/unfollow
*
* @param integer $id
*
* @return \App\Transformer\Api\RelationshipTransformer
*/
public function accountUnfollowById(Request $request, $id)
{
abort_if(!$request->user(), 403);
$user = $request->user();
$target = Profile::where('id', '!=', $user->id)
->whereNull('status')
->findOrFail($item);
$private = (bool) $target->is_private;
$remote = (bool) $target->domain;
$isFollowing = Follower::whereProfileId($user->id)
->whereFollowingId($target->id)
->exists();
if($isFollowing == false) {
$resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
$res = $this->fractal->createData($resource)->toArray();
return response()->json($res);
}
// Rate limits, follow 30 accounts per hour max
if($user->following()->where('followers.updated_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) {
abort(400, 'You can only follow or unfollow ' . Follower::FOLLOW_PER_HOUR . ' users per hour');
}
FollowRequest::whereFollowerId($user->id)
->whereFollowingId($target->id)
->delete();
Follower::whereProfileId($user->id)
->whereFollowingId($target->id)
->delete();
if($remote == true && config('federation.activitypub.remoteFollow') == true) {
(new FollowerController())->sendUndoFollow($user, $target);
}
Cache::forget('profile:following:'.$target->id);
Cache::forget('profile:followers:'.$target->id);
Cache::forget('profile:following:'.$user->id);
Cache::forget('profile:followers:'.$user->id);
Cache::forget('api:local:exp:rec:'.$user->id);
Cache::forget('user:account:id:'.$target->user_id);
Cache::forget('user:account:id:'.$user->user_id);
$resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
$res = $this->fractal->createData($resource)->toArray();
return response()->json($res);
}
public function statusById(Request $request, $id)
{
$status = Status::whereVisibility('public')->findOrFail($id);

View file

@ -84,6 +84,7 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact
Route::get('accounts/{id}/following', 'Api\ApiV1Controller@accountFollowingById')->middleware('auth:api');
Route::get('accounts/{id}/followers', 'Api\ApiV1Controller@accountFollowersById')->middleware('auth:api');
Route::post('accounts/{id}/follow', 'Api\ApiV1Controller@accountFollowById')->middleware('auth:api');
Route::post('accounts/{id}/unfollow', 'Api\ApiV1Controller@accountUnfollowById')->middleware('auth:api');
// Route::get('accounts/{id}', 'PublicApiController@account');
Route::get('accounts/{id}', 'Api\ApiV1Controller@accountById');
Route::post('avatar/update', 'ApiController@avatarUpdate')->middleware('auth:api');