From: dtluna Date: Thu, 20 Apr 2017 07:39:18 +0000 (+0300) Subject: Merge branch 'feature/follow-by-screen-name' of ssh.gitgud.io:dtluna/pleroma into... X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=d952b6cd7bd54f6d10d4f71e0ffb25b5f0e20582;p=akkoma Merge branch 'feature/follow-by-screen-name' of ssh.gitgud.io:dtluna/pleroma into feature/follow-unfollow-by-screen-name --- d952b6cd7bd54f6d10d4f71e0ffb25b5f0e20582 diff --cc lib/pleroma/web/twitter_api/twitter_api.ex index 9b9ee0bd1,de76822fc..e02c0766f --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@@ -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, @@@ -259,31 -250,6 +259,31 @@@ 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) diff --cc lib/pleroma/web/twitter_api/twitter_api_controller.ex index 8b5e6270b,429417884..92c20f97a --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@@ -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 diff --cc test/web/twitter_api/twitter_api_test.exs index 196822f99,4993da3ed..3a1745a1b --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@@ -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