Browse Source

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

Daniel Supernault 5 years ago
parent
commit
fadc96b2d6
2 changed files with 68 additions and 2 deletions
  1. 67 2
      app/Http/Controllers/Api/ApiV1Controller.php
  2. 1 0
      routes/web.php

+ 67 - 2
app/Http/Controllers/Api/ApiV1Controller.php

@@ -328,9 +328,12 @@ class ApiV1Controller extends Controller
             ->whereFollowingId($target->id)
             ->whereFollowingId($target->id)
             ->exists();
             ->exists();
 
 
-        // Following already, return empty response
+        // Following already, return empty relationship
         if($isFollowing == true) {
         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
         // Rate limits, max 7500 followers per account
@@ -377,6 +380,68 @@ class ApiV1Controller extends Controller
         return response()->json($res);
         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)
     public function statusById(Request $request, $id)
     {
     {
         $status = Status::whereVisibility('public')->findOrFail($id);
         $status = Status::whereVisibility('public')->findOrFail($id);

+ 1 - 0
routes/web.php

@@ -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}/following', 'Api\ApiV1Controller@accountFollowingById')->middleware('auth:api');
             Route::get('accounts/{id}/followers', 'Api\ApiV1Controller@accountFollowersById')->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}/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}', 'PublicApiController@account');
             Route::get('accounts/{id}', 'Api\ApiV1Controller@accountById');
             Route::get('accounts/{id}', 'Api\ApiV1Controller@accountById');
             Route::post('avatar/update', 'ApiController@avatarUpdate')->middleware('auth:api');
             Route::post('avatar/update', 'ApiController@avatarUpdate')->middleware('auth:api');