From c0e5b3459fa7c53abf6969584b3298184e0094bd Mon Sep 17 00:00:00 2001 From: dtluna Date: Mon, 10 Apr 2017 16:45:47 +0300 Subject: [PATCH] Add unfollowing using screen_name parameter --- lib/pleroma/web/twitter_api/twitter_api.ex | 10 +++++++++- .../web/twitter_api/twitter_api_controller.ex | 4 ++-- test/web/twitter_api/twitter_api_test.exs | 15 +++++++++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 0a942e880..897adf5ee 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -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) diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index f2c893e96..835461af0 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -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}) diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index ad932131a..7bce7806e 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -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) -- 2.45.2