tests for /web/mastodon_api/mastodon_api.ex
This commit is contained in:
parent
984d7be1a4
commit
04da1166db
4 changed files with 243 additions and 65 deletions
|
|
@ -13,10 +13,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPI do
|
|||
alias Pleroma.User
|
||||
alias Pleroma.Web.CommonAPI
|
||||
|
||||
@spec follow(User.t(), User.t(), map) :: {:ok, User.t()} | {:error, String.t()}
|
||||
def follow(follower, followed, params \\ %{}) do
|
||||
options = cast_params(params)
|
||||
reblogs = options[:reblogs]
|
||||
|
||||
result =
|
||||
if not User.following?(follower, followed) do
|
||||
CommonAPI.follow(follower, followed)
|
||||
|
|
@ -24,19 +22,25 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPI do
|
|||
{:ok, follower, followed, nil}
|
||||
end
|
||||
|
||||
with {:ok, follower, followed, _} <- result do
|
||||
reblogs
|
||||
|> case do
|
||||
false -> CommonAPI.hide_reblogs(follower, followed)
|
||||
_ -> CommonAPI.show_reblogs(follower, followed)
|
||||
end
|
||||
|> case do
|
||||
with {:ok, follower, _followed, _} <- result do
|
||||
options = cast_params(params)
|
||||
|
||||
case reblogs_visibility(options[:reblogs], result) do
|
||||
{:ok, follower} -> {:ok, follower}
|
||||
_ -> {:ok, follower}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp reblogs_visibility(false, {:ok, follower, followed, _}) do
|
||||
CommonAPI.hide_reblogs(follower, followed)
|
||||
end
|
||||
|
||||
defp reblogs_visibility(_, {:ok, follower, followed, _}) do
|
||||
CommonAPI.show_reblogs(follower, followed)
|
||||
end
|
||||
|
||||
@spec get_followers(User.t(), map()) :: list(User.t())
|
||||
def get_followers(user, params \\ %{}) do
|
||||
user
|
||||
|> User.get_followers_query()
|
||||
|
|
|
|||
|
|
@ -536,8 +536,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|
|||
|> put_view(StatusView)
|
||||
|> try_render("poll.json", %{object: object, for: user})
|
||||
else
|
||||
nil -> render_error(conn, :not_found, "Record not found")
|
||||
false -> render_error(conn, :not_found, "Record not found")
|
||||
error when is_nil(error) or error == false ->
|
||||
render_error(conn, :not_found, "Record not found")
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -1690,45 +1690,35 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|
|||
|> String.replace("{{user}}", user)
|
||||
|
||||
with {:ok, %{status: 200, body: body}} <-
|
||||
HTTP.get(
|
||||
url,
|
||||
[],
|
||||
adapter: [
|
||||
recv_timeout: timeout,
|
||||
pool: :default
|
||||
]
|
||||
),
|
||||
HTTP.get(url, [], adapter: [recv_timeout: timeout, pool: :default]),
|
||||
{:ok, data} <- Jason.decode(body) do
|
||||
data =
|
||||
data
|
||||
|> Enum.slice(0, limit)
|
||||
|> Enum.map(fn x ->
|
||||
Map.put(
|
||||
x,
|
||||
"id",
|
||||
case User.get_or_fetch(x["acct"]) do
|
||||
{:ok, %User{id: id}} -> id
|
||||
_ -> 0
|
||||
end
|
||||
)
|
||||
end)
|
||||
|> Enum.map(fn x ->
|
||||
Map.put(x, "avatar", MediaProxy.url(x["avatar"]))
|
||||
end)
|
||||
|> Enum.map(fn x ->
|
||||
Map.put(x, "avatar_static", MediaProxy.url(x["avatar_static"]))
|
||||
x
|
||||
|> Map.put("id", fetch_suggestion_id(x))
|
||||
|> Map.put("avatar", MediaProxy.url(x["avatar"]))
|
||||
|> Map.put("avatar_static", MediaProxy.url(x["avatar_static"]))
|
||||
end)
|
||||
|
||||
conn
|
||||
|> json(data)
|
||||
json(conn, data)
|
||||
else
|
||||
e -> Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}")
|
||||
e ->
|
||||
Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}")
|
||||
end
|
||||
else
|
||||
json(conn, [])
|
||||
end
|
||||
end
|
||||
|
||||
defp fetch_suggestion_id(attrs) do
|
||||
case User.get_or_fetch(attrs["acct"]) do
|
||||
{:ok, %User{id: id}} -> id
|
||||
_ -> 0
|
||||
end
|
||||
end
|
||||
|
||||
def status_card(%{assigns: %{user: user}} = conn, %{"id" => status_id}) do
|
||||
with %Activity{} = activity <- Activity.get_by_id(status_id),
|
||||
true <- Visibility.visible_for_user?(activity, user) do
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue