Merge branch 'feature/follow-by-screen-name' of ssh.gitgud.io:dtluna/pleroma into...
authordtluna <dtluna@openmailbox.org>
Thu, 20 Apr 2017 07:39:18 +0000 (10:39 +0300)
committerdtluna <dtluna@openmailbox.org>
Thu, 20 Apr 2017 07:39:18 +0000 (10:39 +0300)
1  2 
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 9b9ee0bd1f922b52b4457a24f6e803bc90a42bd6,de76822fc25a51be68506d02a15bf0bac6cce9cd..e02c0766fbc8ba9225ee5026bc37195c058657ed
@@@ -108,9 -103,9 +108,9 @@@ defmodule Pleroma.Web.TwitterAPI.Twitte
      end
    end
  
-   def follow(%User{} = follower, followed_id) do
-     with %User{} = followed <- Repo.get(User, followed_id),
-     { :ok, follower } <- User.follow(follower, followed),
+   def follow(%User{} = follower, params) do
 -    with %User{} = followed <- get_user(params),
++    with { :ok, %User{} = followed } <- get_user(params),
+          { :ok, follower } <- User.follow(follower, followed),
           { :ok, activity } <- ActivityPub.insert(%{
             "type" => "Follow",
             "actor" => follower.ap_id,
      end
    end
  
-   def get_user(user, params) do
++  def get_user(user \\ nil, params) do
 +    case params do
 +      %{ "user_id" => user_id } ->
 +        case target = Repo.get(User, user_id) do
 +          nil ->
 +            {:error, "No user with such user_id"}
 +          _ ->
 +            {:ok, target}
 +        end
 +      %{ "screen_name" => nickname } ->
 +        case target = Repo.get_by(User, nickname: nickname) do
 +          nil ->
 +            {:error, "No user with such screen_name"}
 +          _ ->
 +            {:ok, target}
 +        end
 +      _ ->
 +        if user do
 +          {:ok, user}
 +        else
 +          {:error, "You need to specify screen_name or user_id"}
 +        end
 +    end
 +  end
 +
    defp activities_to_statuses(activities, opts) do
      Enum.map(activities, fn(activity) ->
        activity_to_status(activity, opts)
index 8b5e6270bc95fc15d9b654011927d1a31fc64ca4,429417884b071c5c6185c81e16f41a2c434ddec1..92c20f97a9fb42243d8519cb2bcd5a86c6fd8e8c
@@@ -45,26 -45,13 +45,26 @@@ defmodule Pleroma.Web.TwitterAPI.Contro
      |> json_reply(200, json)
    end
  
 -  def follow(%{assigns: %{user: user}} = conn, params) do
 -    { :ok, user, followed, _activity } = TwitterAPI.follow(user, params)
 -
 -    response = followed |> UserRepresenter.to_json(%{for: user})
 +  def user_timeline(%{assigns: %{user: user}} = conn, params) do
 +    case TwitterAPI.get_user(user, params) do
 +      {:ok, target_user} ->
 +        params = Map.merge(params, %{"actor_id" => target_user.ap_id})
 +        statuses  = TwitterAPI.fetch_user_statuses(user, params)
 +        conn
 +        |> json_reply(200, statuses |> Poison.encode!)
 +      {:error, msg} ->
 +        bad_request_reply(conn, msg)
 +    end
 +  end
  
-   def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
-     case TwitterAPI.follow(user, followed_id) do
 -    conn
 -    |> json_reply(200, response)
++  def follow(%{assigns: %{user: user}} = conn, params) do
++    case TwitterAPI.follow(user, params) do
 +      { :ok, user, followed, _activity } ->
 +        response = followed |> UserRepresenter.to_json(%{for: user})
 +        conn
 +        |> json_reply(200, response)
 +      { :error, msg } -> forbidden_json_reply(conn, msg)
 +    end
    end
  
    def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
index 196822f99284bd1fd0ea5bb8ae30297e06410527,4993da3edee9a6be4d2ae1381a462362d264f009..3a1745a1bc671a9892d18f81718531a738628fed
@@@ -144,20 -112,22 +144,26 @@@ defmodule Pleroma.Web.TwitterAPI.Twitte
      assert status == ActivityRepresenter.to_map(activity, %{for: user, user: actor})
    end
  
-   test "Follow another user" do
+   test "Follow another user using user_id" do
      user = insert(:user)
 -    following = insert(:user)
 +    followed = insert(:user)
  
-     { :ok, user, followed, activity } = TwitterAPI.follow(user, followed.id)
 -    {:ok, user, following, activity } = TwitterAPI.follow(user, %{"user_id" => following.id})
++    {:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"user_id" => followed.id})
++    assert user.following == [User.ap_followers(followed)]
  
-     user = Repo.get(User, user.id)
-     follow = Repo.get(Activity, activity.id)
 -    assert user.following == [User.ap_followers(following)]
++    { :error, msg } = TwitterAPI.follow(user, %{"user_id" => followed.id})
++    assert msg == "Could not follow user: #{followed.nickname} is already on your list."
+   end
+   test "Follow another user using screen_name" do
+     user = insert(:user)
 -    following = insert(:user)
++    followed = insert(:user)
  
 -    {:ok, user, following, activity } = TwitterAPI.follow(user, %{"screen_name" => following.nickname})
++    {:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
 +    assert user.following == [User.ap_followers(followed)]
-     assert follow == activity
  
-     { :error, msg } = TwitterAPI.follow(user, followed.id)
 -    assert user.following == [User.ap_followers(following)]
++    { :error, msg } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
 +    assert msg == "Could not follow user: #{followed.nickname} is already on your list."
    end
  
    test "Unfollow another user" do