Add following using screen_name parameter
authordtluna <dtluna@openmailbox.org>
Mon, 10 Apr 2017 13:38:21 +0000 (16:38 +0300)
committerdtluna <dtluna@openmailbox.org>
Mon, 10 Apr 2017 13:38:21 +0000 (16:38 +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..f6793cc218b7e3f1e8a9bd068c1741529ff2b728 100644 (file)
@@ -101,7 +101,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI 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 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI 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)
index f2c893e96d3c5c08ef886471de614eb0a0de32c9..dc53e09ec31f6f070700e5d05c460403473a23b6 100644 (file)
@@ -43,8 +43,8 @@ defmodule Pleroma.Web.TwitterAPI.Controller 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})
 
index ad932131ac2522ccf153d158368f0562ba7b4aaa..65d7b0ca808c19186cbc8da8dc59df7cb4d9011d 100644 (file)
@@ -103,11 +103,24 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest 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)