Add error messages for repeated follows and unfollows
authordtluna <dtluna@openmailbox.org>
Wed, 12 Apr 2017 14:34:36 +0000 (17:34 +0300)
committerdtluna <dtluna@openmailbox.org>
Wed, 12 Apr 2017 14:34:36 +0000 (17:34 +0300)
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 c77704db0c9b0184f78d072548b719a079b58cb5..5f5bc1c384bbc9470a033ce536050a0286bc89c6 100644 (file)
@@ -31,22 +31,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 0a942e880e538d1e92fd67f80a4b0ee1c1080828..66f78f3407b80cee58a182ddfa66c7c5703cf44f 100644 (file)
@@ -103,7 +103,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,
@@ -112,6 +112,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
          })
     do
       { :ok, follower, followed, activity }
+    else
+      err -> err
     end
   end
 
@@ -120,6 +122,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
          { :ok, follower } <- User.unfollow(follower, followed)
     do
       { :ok, follower, followed }
+    else
+      err -> err
     end
   end
 
index f2c893e96d3c5c08ef886471de614eb0a0de32c9..f6574b8de7284ad5f362188a9cdc2f14b891273a 100644 (file)
@@ -44,21 +44,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})
+    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
 
-    conn
-    |> json_reply(200, response)
   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
@@ -88,4 +91,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 ad932131ac2522ccf153d158368f0562ba7b4aaa..c1f5881b8eb9bd1b541dfd2aed951005889461ad 100644 (file)
@@ -105,26 +105,31 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
 
   test "Follow another user" do
     { :ok, user } = UserBuilder.insert
-    { :ok, following } = UserBuilder.insert(%{nickname: "guy"})
+    { :ok, followed } = UserBuilder.insert(%{nickname: "guy"})
 
-    {: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
-    { :ok, following } = UserBuilder.insert(%{nickname: "guy"})
-    { :ok, user } = UserBuilder.insert(%{following: [User.ap_followers(following)]})
+    { :ok, followed } = UserBuilder.insert(%{nickname: "guy"})
+    { :ok, user } = UserBuilder.insert(%{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