Merge branch 'bugfix/repeated-follow-unfollow' into 'develop'
authorlambadalambda <gitgud@rogerbraun.net>
Mon, 17 Apr 2017 09:46:59 +0000 (05:46 -0400)
committerlambadalambda <gitgud@rogerbraun.net>
Mon, 17 Apr 2017 09:46:59 +0000 (05:46 -0400)
Add error messages for repeated follows and unfollows

See merge request !7

lib/pleroma/user.ex
lib/pleroma/web/twitter_api/twitter_api.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/web/twitter_api/twitter_api_test.exs

index 1ab4e40cbaf8840f1b37e90d623504d6a89bfbe0..86b4b8b5eab301d1428c091472ec28f63de7aca6 100644 (file)
@@ -55,22 +55,31 @@ defmodule Pleroma.User do
 
   def follow(%User{} = follower, %User{} = followed) do
     ap_followers = User.ap_followers(followed)
-    following = [ap_followers | follower.following]
-    |> Enum.uniq
+    if following?(follower, followed) do
+      { :error,
+        "Could not follow user: #{followed.nickname} is already on your list." }
+    else
+      following = [ap_followers | follower.following]
+      |> Enum.uniq
 
-    follower
-    |> follow_changeset(%{following: following})
-    |> Repo.update
+      follower
+      |> follow_changeset(%{following: following})
+      |> Repo.update
+    end
   end
 
   def unfollow(%User{} = follower, %User{} = followed) do
     ap_followers = User.ap_followers(followed)
-    following = follower.following
-    |> List.delete(ap_followers)
+    if following?(follower, followed) do
+      following = follower.following
+      |> List.delete(ap_followers)
 
-    follower
-    |> follow_changeset(%{following: following})
-    |> Repo.update
+      follower
+      |> follow_changeset(%{following: following})
+      |> Repo.update
+    else
+      { :error, "Not subscribed!" }
+    end
   end
 
   def following?(%User{} = follower, %User{} = followed) do
index 735d88832e5bf962c7e46c1cc4d44fca78dd7047..7984b2e63e0b22f822b571d49ae115b2281c6d60 100644 (file)
@@ -105,7 +105,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
 
   def follow(%User{} = follower, followed_id) do
     with %User{} = followed <- Repo.get(User, followed_id),
-         { :ok, follower } <- User.follow(follower, followed),
+    { :ok, follower } <- User.follow(follower, followed),
          { :ok, activity } <- ActivityPub.insert(%{
            "type" => "Follow",
            "actor" => follower.ap_id,
@@ -114,6 +114,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
          })
     do
       { :ok, follower, followed, activity }
+    else
+      err -> err
     end
   end
 
@@ -122,6 +124,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
          { :ok, follower } <- User.unfollow(follower, followed)
     do
       { :ok, follower, followed }
+    else
+      err -> err
     end
   end
 
index 6014aedc4650d7d9548092e5dec65ce5d7429fbc..32d352d797641d8e727244004cad0741152b41cf 100644 (file)
@@ -46,21 +46,24 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
   end
 
   def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
-    { :ok, user, follower, _activity } = TwitterAPI.follow(user, followed_id)
-
-    response = follower |> UserRepresenter.to_json(%{for: user})
-
-    conn
-    |> json_reply(200, response)
+    case TwitterAPI.follow(user, followed_id) do
+      { :ok, user, followed, _activity } ->
+        response = followed |> UserRepresenter.to_json(%{for: user})
+        conn
+        |> json_reply(200, response)
+      { :error, msg } -> forbidden_json_reply(conn, msg)
+    end
   end
 
   def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
-    { :ok, user, follower } = TwitterAPI.unfollow(user, followed_id)
-
-    response = follower |> UserRepresenter.to_json(%{for: user})
+    case TwitterAPI.unfollow(user, followed_id) do
+      { :ok, user, followed } ->
+        response = followed |> UserRepresenter.to_json(%{for: user})
 
-    conn
-    |> json_reply(200, response)
+        conn
+        |> json_reply(200, response)
+      { :error, msg } -> forbidden_json_reply(conn, msg)
+    end
   end
 
   def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
@@ -164,4 +167,11 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
     |> put_resp_content_type("application/json")
     |> send_resp(status, json)
   end
+
+  defp forbidden_json_reply(conn, error_message) do
+    json = %{"error" => error_message, "request" => conn.request_path}
+    |> Poison.encode!
+
+    json_reply(conn, 403, json)
+  end
 end
index 8b15b0ed498b3c1796dc98bbdda2e92b94e8f4b6..48a2c3ba0cb0fd94fce56a710f0b35f1657591cd 100644 (file)
@@ -114,26 +114,31 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
 
   test "Follow another user" do
     user = insert(:user)
-    following = insert(:user)
+    followed = insert(:user)
 
-    {:ok, user, following, activity } = TwitterAPI.follow(user, following.id)
+    { :ok, user, followed, activity } = TwitterAPI.follow(user, followed.id)
 
     user = Repo.get(User, user.id)
     follow = Repo.get(Activity, activity.id)
 
-    assert user.following == [User.ap_followers(following)]
+    assert user.following == [User.ap_followers(followed)]
     assert follow == activity
+
+    { :error, msg } = TwitterAPI.follow(user, followed.id)
+    assert msg == "Could not follow user: #{followed.nickname} is already on your list."
   end
 
   test "Unfollow another user" do
-    following = insert(:user)
-    user = insert(:user, %{following: [User.ap_followers(following)]})
+    followed = insert(:user)
+    user = insert(:user, %{following: [User.ap_followers(followed)]})
 
-    {:ok, user, _following } = TwitterAPI.unfollow(user, following.id)
+    { :ok, user, _followed } = TwitterAPI.unfollow(user, followed.id)
 
     user = Repo.get(User, user.id)
 
     assert user.following == []
+    { :error, msg } = TwitterAPI.unfollow(user, followed.id)
+    assert msg == "Not subscribed!"
   end
 
   test "fetch statuses in a context using the conversation id" do