Add error messages for repeated follows and unfollows
This commit is contained in:
parent
b5e94bf930
commit
65ef18a715
|
@ -31,22 +31,31 @@ def follow_changeset(struct, params \\ %{}) do
|
||||||
|
|
||||||
def follow(%User{} = follower, %User{} = followed) do
|
def follow(%User{} = follower, %User{} = followed) do
|
||||||
ap_followers = User.ap_followers(followed)
|
ap_followers = User.ap_followers(followed)
|
||||||
following = [ap_followers | follower.following]
|
if following?(follower, followed) do
|
||||||
|> Enum.uniq
|
{ :error,
|
||||||
|
"Could not follow user: #{followed.nickname} is already on your list." }
|
||||||
|
else
|
||||||
|
following = [ap_followers | follower.following]
|
||||||
|
|> Enum.uniq
|
||||||
|
|
||||||
follower
|
follower
|
||||||
|> follow_changeset(%{following: following})
|
|> follow_changeset(%{following: following})
|
||||||
|> Repo.update
|
|> Repo.update
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def unfollow(%User{} = follower, %User{} = followed) do
|
def unfollow(%User{} = follower, %User{} = followed) do
|
||||||
ap_followers = User.ap_followers(followed)
|
ap_followers = User.ap_followers(followed)
|
||||||
following = follower.following
|
if following?(follower, followed) do
|
||||||
|> List.delete(ap_followers)
|
following = follower.following
|
||||||
|
|> List.delete(ap_followers)
|
||||||
|
|
||||||
follower
|
follower
|
||||||
|> follow_changeset(%{following: following})
|
|> follow_changeset(%{following: following})
|
||||||
|> Repo.update
|
|> Repo.update
|
||||||
|
else
|
||||||
|
{ :error, "Not subscribed!" }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def following?(%User{} = follower, %User{} = followed) do
|
def following?(%User{} = follower, %User{} = followed) do
|
||||||
|
|
|
@ -103,7 +103,7 @@ def fetch_status(user, id) do
|
||||||
|
|
||||||
def follow(%User{} = follower, followed_id) do
|
def follow(%User{} = follower, followed_id) do
|
||||||
with %User{} = followed <- Repo.get(User, followed_id),
|
with %User{} = followed <- Repo.get(User, followed_id),
|
||||||
{ :ok, follower } <- User.follow(follower, followed),
|
{ :ok, follower } <- User.follow(follower, followed),
|
||||||
{ :ok, activity } <- ActivityPub.insert(%{
|
{ :ok, activity } <- ActivityPub.insert(%{
|
||||||
"type" => "Follow",
|
"type" => "Follow",
|
||||||
"actor" => follower.ap_id,
|
"actor" => follower.ap_id,
|
||||||
|
@ -112,6 +112,8 @@ def follow(%User{} = follower, followed_id) do
|
||||||
})
|
})
|
||||||
do
|
do
|
||||||
{ :ok, follower, followed, activity }
|
{ :ok, follower, followed, activity }
|
||||||
|
else
|
||||||
|
err -> err
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -120,6 +122,8 @@ def unfollow(%User{} = follower, followed_id) do
|
||||||
{ :ok, follower } <- User.unfollow(follower, followed)
|
{ :ok, follower } <- User.unfollow(follower, followed)
|
||||||
do
|
do
|
||||||
{ :ok, follower, followed }
|
{ :ok, follower, followed }
|
||||||
|
else
|
||||||
|
err -> err
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -44,21 +44,24 @@ def friends_timeline(%{assigns: %{user: user}} = conn, params) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
|
def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
|
||||||
{ :ok, _user, follower, _activity } = TwitterAPI.follow(user, followed_id)
|
case TwitterAPI.follow(user, followed_id) 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
|
||||||
|
|
||||||
response = follower |> UserRepresenter.to_json(%{for: user})
|
|
||||||
|
|
||||||
conn
|
|
||||||
|> json_reply(200, response)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
|
def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
|
||||||
{ :ok, user, follower } = TwitterAPI.unfollow(user, followed_id)
|
case TwitterAPI.unfollow(user, followed_id) do
|
||||||
|
{ :ok, user, followed } ->
|
||||||
|
response = followed |> UserRepresenter.to_json(%{for: user})
|
||||||
|
|
||||||
response = follower |> UserRepresenter.to_json(%{for: user})
|
conn
|
||||||
|
|> json_reply(200, response)
|
||||||
conn
|
{ :error, msg } -> forbidden_json_reply(conn, msg)
|
||||||
|> json_reply(200, response)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
|
def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
|
||||||
|
@ -88,4 +91,11 @@ defp json_reply(conn, status, json) do
|
||||||
|> put_resp_content_type("application/json")
|
|> put_resp_content_type("application/json")
|
||||||
|> send_resp(status, json)
|
|> send_resp(status, json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp forbidden_json_reply(conn, error_message) do
|
||||||
|
json = %{"error" => error_message, "request" => conn.request_path}
|
||||||
|
|> Poison.encode!
|
||||||
|
|
||||||
|
json_reply(conn, 403, json)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -105,26 +105,31 @@ test "fetch a single status" do
|
||||||
|
|
||||||
test "Follow another user" do
|
test "Follow another user" do
|
||||||
{ :ok, user } = UserBuilder.insert
|
{ :ok, user } = UserBuilder.insert
|
||||||
{ :ok, following } = UserBuilder.insert(%{nickname: "guy"})
|
{ :ok, followed } = UserBuilder.insert(%{nickname: "guy"})
|
||||||
|
|
||||||
{:ok, user, following, activity } = TwitterAPI.follow(user, following.id)
|
{ :ok, user, followed, activity } = TwitterAPI.follow(user, followed.id)
|
||||||
|
|
||||||
user = Repo.get(User, user.id)
|
user = Repo.get(User, user.id)
|
||||||
follow = Repo.get(Activity, activity.id)
|
follow = Repo.get(Activity, activity.id)
|
||||||
|
|
||||||
assert user.following == [User.ap_followers(following)]
|
assert user.following == [User.ap_followers(followed)]
|
||||||
assert follow == activity
|
assert follow == activity
|
||||||
|
|
||||||
|
{ :error, msg } = TwitterAPI.follow(user, followed.id)
|
||||||
|
assert msg == "Could not follow user: #{followed.nickname} is already on your list."
|
||||||
end
|
end
|
||||||
|
|
||||||
test "Unfollow another user" do
|
test "Unfollow another user" do
|
||||||
{ :ok, following } = UserBuilder.insert(%{nickname: "guy"})
|
{ :ok, followed } = UserBuilder.insert(%{nickname: "guy"})
|
||||||
{ :ok, user } = UserBuilder.insert(%{following: [User.ap_followers(following)]})
|
{ :ok, user } = UserBuilder.insert(%{following: [User.ap_followers(followed)]})
|
||||||
|
|
||||||
{:ok, user, _following } = TwitterAPI.unfollow(user, following.id)
|
{ :ok, user, _followed } = TwitterAPI.unfollow(user, followed.id)
|
||||||
|
|
||||||
user = Repo.get(User, user.id)
|
user = Repo.get(User, user.id)
|
||||||
|
|
||||||
assert user.following == []
|
assert user.following == []
|
||||||
|
{ :error, msg } = TwitterAPI.unfollow(user, followed.id)
|
||||||
|
assert msg == "Not subscribed!"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "fetch statuses in a context using the conversation id" do
|
test "fetch statuses in a context using the conversation id" do
|
||||||
|
|
Loading…
Reference in New Issue