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)
|> 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
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