Add user timeline fetching without credentials

This commit is contained in:
dtluna 2017-04-16 16:44:30 +03:00
parent 7a0046d127
commit 4b4ece2c74
4 changed files with 61 additions and 23 deletions

View File

@ -23,6 +23,7 @@ def user_fetcher(username) do
pipe_through :api
get "/statuses/public_timeline", TwitterAPI.Controller, :public_timeline
get "/statuses/public_and_external_timeline", TwitterAPI.Controller, :public_timeline
get "/statuses/user_timeline", TwitterAPI.Controller, :user_timeline
get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status
get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation
get "/statusnet/config", TwitterAPI.Controller, :config
@ -37,7 +38,6 @@ def user_fetcher(username) do
post "/statuses/update", TwitterAPI.Controller, :status_update
get "/statuses/home_timeline", TwitterAPI.Controller, :friends_timeline
get "/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline
get "/statuses/user_timeline", TwitterAPI.Controller, :user_timeline
post "/friendships/create", TwitterAPI.Controller, :follow
post "/friendships/destroy", TwitterAPI.Controller, :unfollow
post "/statusnet/media/upload", TwitterAPI.Controller, :upload

View File

@ -81,8 +81,7 @@ def fetch_public_statuses(user, opts \\ %{}) do
end
def fetch_user_statuses(user, opts \\ %{}) do
target = get_user(user, opts)
ActivityPub.fetch_activities([], Map.merge(opts, %{"actor_id" => target.ap_id}))
ActivityPub.fetch_activities([], opts)
|> activities_to_statuses(%{for: user})
end
@ -250,12 +249,37 @@ def register_user(params) do
{:ok, UserRepresenter.to_map(user)}
else
{:error, changeset} ->
errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} -> msg end)
|> Poison.encode!
errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
|> Poison.encode!
{:error, %{error: errors}}
end
end
def get_user(user, 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 of user_id"}
end
end
end
defp activities_to_statuses(activities, opts) do
Enum.map(activities, fn(activity) ->
activity_to_status(activity, opts)
@ -296,15 +320,4 @@ defp activity_to_status(activity, opts) do
defp make_date do
DateTime.utc_now() |> DateTime.to_iso8601
end
defp get_user(user, params) do
case params do
%{ "user_id" => user_id } ->
Repo.get(User, user_id)
%{ "screen_name" => nickname } ->
Repo.get_by!(User, nickname: nickname)
_ ->
user
end
end
end

View File

@ -45,9 +45,15 @@ def friends_timeline(%{assigns: %{user: user}} = conn, params) do
end
def user_timeline(%{assigns: %{user: user}} = conn, params) do
statuses = TwitterAPI.fetch_user_statuses(user, params)
conn
|> json_reply(200, statuses |> Poison.encode!)
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
@ -148,6 +154,11 @@ def register(conn, params) do
end
end
defp bad_request_reply(conn, error_message) do
json = Poison.encode!(%{"error" => error_message})
json_reply(conn, 400, json)
end
defp json_reply(conn, status, json) do
conn
|> put_resp_content_type("application/json")

View File

@ -102,19 +102,33 @@ test "fetch friends' statuses" do
assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: activity_user, mentioned: [user]})
end
test "get a user by params" do
user1_result = {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
{:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
assert {:error, "You need to specify screen_name of user_id"} == TwitterAPI.get_user(nil, nil)
assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.id})
assert user1_result == TwitterAPI.get_user(nil, %{"screen_name" => user1.nickname})
assert user1_result == TwitterAPI.get_user(user1, nil)
assert user1_result == TwitterAPI.get_user(user2, %{"user_id" => user1.id})
assert user1_result == TwitterAPI.get_user(user2, %{"screen_name" => user1.nickname})
assert {:error, "No user with such screen_name"} == TwitterAPI.get_user(nil, %{"screen_name" => "Satan"})
assert {:error, "No user with such user_id"} == TwitterAPI.get_user(nil, %{"user_id" => 666})
end
test "fetch user's statuses" do
{:ok, user1} = UserBuilder.insert(%{ap_id: "some id"})
{:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2"})
{:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
{:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
{:ok, status1} = ActivityBuilder.insert(%{"id" => 1}, %{user: user1})
{:ok, status2} = ActivityBuilder.insert(%{"id" => 2}, %{user: user2})
user1_statuses = TwitterAPI.fetch_user_statuses(user1, %{})
user1_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user1.ap_id})
assert length(user1_statuses) == 1
assert Enum.at(user1_statuses, 0) == ActivityRepresenter.to_map(status1, %{user: user1})
user2_statuses = TwitterAPI.fetch_user_statuses(user1, %{"screen_name" => user2.nickname })
user2_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user2.ap_id})
assert length(user2_statuses) == 1
assert Enum.at(user2_statuses, 0) == ActivityRepresenter.to_map(status2, %{user: user2})