From 896e40cd2bbd064548a1a9cb730f79d793e6d6f5 Mon Sep 17 00:00:00 2001 From: dtluna Date: Mon, 10 Apr 2017 16:38:21 +0300 Subject: [PATCH 1/4] Add following using screen_name parameter --- lib/pleroma/web/twitter_api/twitter_api.ex | 16 +++++++++++++++- .../web/twitter_api/twitter_api_controller.ex | 4 ++-- test/web/twitter_api/twitter_api_test.exs | 17 +++++++++++++++-- 3 files changed, 32 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..f6793cc21 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -101,7 +101,7 @@ def fetch_status(user, id) do end end - def follow(%User{} = follower, followed_id) do + def follow(%User{} = follower, %{ "user_id" => followed_id }) do with %User{} = followed <- Repo.get(User, followed_id), { :ok, follower } <- User.follow(follower, followed), { :ok, activity } <- ActivityPub.insert(%{ @@ -115,6 +115,20 @@ def follow(%User{} = follower, followed_id) do end end + def follow(%User{} = follower, %{ "screen_name" => followed_name }) do + with %User{} = followed <- Repo.get_by(User, nickname: followed_name), + { :ok, follower } <- User.follow(follower, followed), + { :ok, activity } <- ActivityPub.insert(%{ + "type" => "Follow", + "actor" => follower.ap_id, + "object" => followed.ap_id, + "published" => make_date() + }) + do + { :ok, follower, followed, activity } + end + end + def unfollow(%User{} = follower, followed_id) do with %User{} = followed <- Repo.get(User, followed_id), { :ok, follower } <- User.unfollow(follower, followed) diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index f2c893e96..dc53e09ec 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -43,8 +43,8 @@ def friends_timeline(%{assigns: %{user: user}} = conn, params) do |> json_reply(200, json) end - def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do - { :ok, _user, follower, _activity } = TwitterAPI.follow(user, followed_id) + def follow(%{assigns: %{user: user}} = conn, params) do + { :ok, _user, follower, _activity } = TwitterAPI.follow(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..65d7b0ca8 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -103,11 +103,24 @@ test "fetch a single status" do assert status == ActivityRepresenter.to_map(activity, %{for: user, user: actor}) end - test "Follow another user" do + test "Follow another user using user_id" do { :ok, user } = UserBuilder.insert { :ok, following } = UserBuilder.insert(%{nickname: "guy"}) - {:ok, user, following, activity } = TwitterAPI.follow(user, following.id) + {:ok, user, following, activity } = TwitterAPI.follow(user, %{"user_id" => following.id}) + + user = Repo.get(User, user.id) + follow = Repo.get(Activity, activity.id) + + assert user.following == [User.ap_followers(following)] + assert follow == activity + end + + test "Follow another user using screen_name" do + { :ok, user } = UserBuilder.insert + { :ok, following } = UserBuilder.insert(%{nickname: "guy"}) + + {:ok, user, following, activity } = TwitterAPI.follow(user, %{"screen_name" => following.nickname}) user = Repo.get(User, user.id) follow = Repo.get(Activity, activity.id) From 594dd01ab2864482d8c1cdb0153d7b4b0972b3b4 Mon Sep 17 00:00:00 2001 From: dtluna Date: Thu, 13 Apr 2017 15:32:13 +0300 Subject: [PATCH 2/4] Refactor follow API --- lib/pleroma/web/twitter_api/twitter_api.ex | 27 ++++++++----------- .../web/twitter_api/twitter_api_controller.ex | 8 +++--- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 425ff4ad2..1456aea0b 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -101,8 +101,8 @@ def fetch_status(user, id) do end end - def follow(%User{} = follower, %{ "user_id" => followed_id }) do - with %User{} = followed <- Repo.get(User, followed_id), + def follow(%User{} = follower, params) do + with %User{} = followed <- get_user(params), { :ok, follower } <- User.follow(follower, followed), { :ok, activity } <- ActivityPub.insert(%{ "type" => "Follow", @@ -115,20 +115,6 @@ def follow(%User{} = follower, %{ "user_id" => followed_id }) do end end - def follow(%User{} = follower, %{ "screen_name" => followed_name }) do - with %User{} = followed <- Repo.get_by(User, nickname: followed_name), - { :ok, follower } <- User.follow(follower, followed), - { :ok, activity } <- ActivityPub.insert(%{ - "type" => "Follow", - "actor" => follower.ap_id, - "object" => followed.ap_id, - "published" => make_date() - }) - do - { :ok, follower, followed, activity } - end - end - def unfollow(%User{} = follower, followed_id) do with %User{} = followed <- Repo.get(User, followed_id), { :ok, follower } <- User.unfollow(follower, followed) @@ -202,4 +188,13 @@ defp activity_to_status(activity, opts) do defp make_date do DateTime.utc_now() |> DateTime.to_iso8601 end + + defp get_user(params) do + case params do + %{ "user_id" => user_id } -> + Repo.get(User, user_id) + %{ "screen_name" => nickname } -> + Repo.get_by(User, nickname: nickname) + end + end end diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 6d4172dfe..b5e52807e 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -44,18 +44,18 @@ def friends_timeline(%{assigns: %{user: user}} = conn, params) do end def follow(%{assigns: %{user: user}} = conn, params) do - { :ok, _user, follower, _activity } = TwitterAPI.follow(user, params) + { :ok, user, followed, _activity } = TwitterAPI.follow(user, params) - response = follower |> UserRepresenter.to_json(%{for: user}) + response = followed |> UserRepresenter.to_json(%{for: user}) 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) + { :ok, user, followed } = TwitterAPI.unfollow(user, followed_id) - response = follower |> UserRepresenter.to_json(%{for: user}) + response = followed |> UserRepresenter.to_json(%{for: user}) conn |> json_reply(200, response) From f6e632d25b726048a9a8ad3abfa1e4adef418966 Mon Sep 17 00:00:00 2001 From: dtluna Date: Sun, 16 Apr 2017 17:39:34 +0300 Subject: [PATCH 3/4] Remove unnecesary asserts --- test/web/twitter_api/twitter_api_test.exs | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 67b47163d..eda1c8c1c 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -122,7 +122,6 @@ test "Follow another user using user_id" do follow = Repo.get(Activity, activity.id) assert user.following == [User.ap_followers(following)] - assert follow == activity end test "Follow another user using screen_name" do @@ -135,7 +134,6 @@ test "Follow another user using screen_name" do follow = Repo.get(Activity, activity.id) assert user.following == [User.ap_followers(following)] - assert follow == activity end test "Unfollow another user" do From 369be708b2126e15fd870977810b3e71514d9979 Mon Sep 17 00:00:00 2001 From: dtluna Date: Sun, 16 Apr 2017 17:41:30 +0300 Subject: [PATCH 4/4] Remove unnecesary queries --- test/web/twitter_api/twitter_api_test.exs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index eda1c8c1c..4993da3ed 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -118,9 +118,6 @@ test "Follow another user using user_id" do {:ok, user, following, activity } = TwitterAPI.follow(user, %{"user_id" => following.id}) - user = Repo.get(User, user.id) - follow = Repo.get(Activity, activity.id) - assert user.following == [User.ap_followers(following)] end @@ -130,9 +127,6 @@ test "Follow another user using screen_name" do {:ok, user, following, activity } = TwitterAPI.follow(user, %{"screen_name" => following.nickname}) - user = Repo.get(User, user.id) - follow = Repo.get(Activity, activity.id) - assert user.following == [User.ap_followers(following)] end