mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-12-20 16:32:27 +00:00
Add user timeline fetching without credentials
This commit is contained in:
parent
7a0046d127
commit
4b4ece2c74
|
@ -23,6 +23,7 @@ defmodule Pleroma.Web.Router 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 @@ defmodule Pleroma.Web.Router 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
|
||||
|
|
|
@ -81,8 +81,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI 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 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||
{:ok, UserRepresenter.to_map(user)}
|
||||
else
|
||||
{:error, changeset} ->
|
||||
errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} -> msg end)
|
||||
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 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI 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
|
||||
|
|
|
@ -45,9 +45,15 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
|||
end
|
||||
|
||||
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
|
||||
|
@ -148,6 +154,11 @@ defmodule Pleroma.Web.TwitterAPI.Controller 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")
|
||||
|
|
|
@ -102,19 +102,33 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest 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})
|
||||
|
|
Loading…
Reference in a new issue