Add unfollowing using screen_name parameter
authordtluna <dtluna@openmailbox.org>
Mon, 10 Apr 2017 13:45:47 +0000 (16:45 +0300)
committerdtluna <dtluna@openmailbox.org>
Mon, 10 Apr 2017 13:45:47 +0000 (16:45 +0300)
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 0a942e880e538d1e92fd67f80a4b0ee1c1080828..897adf5ee5a38503a435c544cc9268adfb78d041 100644 (file)
@@ -115,7 +115,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
     end
   end
 
-  def unfollow(%User{} = follower, followed_id) do
+  def unfollow(%User{} = follower, %{ "user_id" => followed_id }) do
     with %User{} = followed <- Repo.get(User, followed_id),
          { :ok, follower } <- User.unfollow(follower, followed)
     do
@@ -123,6 +123,14 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
     end
   end
 
+  def unfollow(%User{} = follower, %{ "screen_name" => followed_name }) do
+    with %User{} = followed <- Repo.get_by(User, nickname: followed_name),
+         { :ok, follower } <- User.unfollow(follower, followed)
+      do
+      { :ok, follower, followed }
+    end
+  end
+
   def upload(%Plug.Upload{} = file) do
     {:ok, object} = ActivityPub.upload(file)
 
index f2c893e96d3c5c08ef886471de614eb0a0de32c9..835461af07ae2581e20385a4e354cc140ca28a4f 100644 (file)
@@ -52,8 +52,8 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
     |> json_reply(200, response)
   end
 
-  def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
-    { :ok, user, follower } = TwitterAPI.unfollow(user, followed_id)
+  def unfollow(%{assigns: %{user: user}} = conn, params) do
+    { :ok, user, follower } = TwitterAPI.unfollow(user, params)
 
     response = follower |> UserRepresenter.to_json(%{for: user})
 
index ad932131ac2522ccf153d158368f0562ba7b4aaa..7bce7806ec7a31c834e2466a191a35c143614960 100644 (file)
@@ -116,11 +116,22 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
     assert follow == activity
   end
 
-  test "Unfollow another user" do
+  test "Unfollow another user using user_id" do
     { :ok, following } = UserBuilder.insert(%{nickname: "guy"})
     { :ok, user } = UserBuilder.insert(%{following: [User.ap_followers(following)]})
 
-    {:ok, user, _following } = TwitterAPI.unfollow(user, following.id)
+    {:ok, user, _following } = TwitterAPI.unfollow(user, %{"user_id" => following.id})
+
+    user = Repo.get(User, user.id)
+
+    assert user.following == []
+  end
+
+  test "Unfollow another user using screen_name" do
+    { :ok, following } = UserBuilder.insert(%{nickname: "guy"})
+    { :ok, user } = UserBuilder.insert(%{following: [User.ap_followers(following)]})
+
+    {:ok, user, _following } = TwitterAPI.unfollow(user, %{"screen_name" => following.nickname})
 
     user = Repo.get(User, user.id)