Refactor follow API
authordtluna <dtluna@openmailbox.org>
Thu, 13 Apr 2017 12:32:13 +0000 (15:32 +0300)
committerdtluna <dtluna@openmailbox.org>
Thu, 13 Apr 2017 12:32:13 +0000 (15:32 +0300)
lib/pleroma/web/twitter_api/twitter_api.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex

index 425ff4ad2b846d7b6d4768037c0646e3224d6c30..1456aea0bccf9fe37150a3462146180cb1684c8a 100644 (file)
@@ -101,8 +101,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI 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 @@ 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)
@@ -202,4 +188,13 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI 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
index 6d4172dfe841310a91911f1b5ca2dcd09938d132..b5e52807e910fe00aba23ed0fdcdf9ba55a5f344 100644 (file)
@@ -44,18 +44,18 @@ defmodule Pleroma.Web.TwitterAPI.Controller 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)