Merge branch 'feature/follow-by-screen-name' of ssh.gitgud.io:dtluna/pleroma into feature/follow-unfollow-by-screen-name

This commit is contained in:
dtluna 2017-04-20 10:39:18 +03:00
commit d952b6cd7b
3 changed files with 20 additions and 14 deletions

View file

@ -108,8 +108,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
end end
end end
def follow(%User{} = follower, followed_id) do def follow(%User{} = follower, params) do
with %User{} = followed <- Repo.get(User, followed_id), with { :ok, %User{} = followed } <- get_user(params),
{ :ok, follower } <- User.follow(follower, followed), { :ok, follower } <- User.follow(follower, followed),
{ :ok, activity } <- ActivityPub.insert(%{ { :ok, activity } <- ActivityPub.insert(%{
"type" => "Follow", "type" => "Follow",
@ -259,7 +259,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
end end
end end
def get_user(user, params) do def get_user(user \\ nil, params) do
case params do case params do
%{ "user_id" => user_id } -> %{ "user_id" => user_id } ->
case target = Repo.get(User, user_id) do case target = Repo.get(User, user_id) do

View file

@ -57,8 +57,8 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
end end
end end
def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do def follow(%{assigns: %{user: user}} = conn, params) do
case TwitterAPI.follow(user, followed_id) do case TwitterAPI.follow(user, params) do
{ :ok, user, followed, _activity } -> { :ok, user, followed, _activity } ->
response = followed |> UserRepresenter.to_json(%{for: user}) response = followed |> UserRepresenter.to_json(%{for: user})
conn conn

View file

@ -144,19 +144,25 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
assert status == ActivityRepresenter.to_map(activity, %{for: user, user: actor}) assert status == ActivityRepresenter.to_map(activity, %{for: user, user: actor})
end end
test "Follow another user" do test "Follow another user using user_id" do
user = insert(:user) user = insert(:user)
followed = insert(:user) followed = insert(:user)
{ :ok, user, followed, activity } = TwitterAPI.follow(user, followed.id) {:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"user_id" => followed.id})
user = Repo.get(User, user.id)
follow = Repo.get(Activity, activity.id)
assert user.following == [User.ap_followers(followed)] assert user.following == [User.ap_followers(followed)]
assert follow == activity
{ :error, msg } = TwitterAPI.follow(user, followed.id) { :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)
followed = insert(:user)
{:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
assert user.following == [User.ap_followers(followed)]
{ :error, msg } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
assert msg == "Could not follow user: #{followed.nickname} is already on your list." assert msg == "Could not follow user: #{followed.nickname} is already on your list."
end end