forked from AkkomaGang/akkoma
Better error handling in TwitterApiController.
This commit is contained in:
parent
9d054d30bc
commit
0f70e83e8c
5 changed files with 54 additions and 14 deletions
|
@ -72,8 +72,10 @@ def create_activity_by_object_id_query(ap_ids) do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_create_activity_by_object_ap_id(ap_id) do
|
def get_create_activity_by_object_ap_id(ap_id) when is_binary(ap_id) do
|
||||||
create_activity_by_object_id_query([ap_id])
|
create_activity_by_object_id_query([ap_id])
|
||||||
|> Repo.one()
|
|> Repo.one()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_create_activity_by_object_ap_id(_), do: nil
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,6 +9,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|
||||||
def get_by_id_or_ap_id(id) do
|
def get_by_id_or_ap_id(id) do
|
||||||
activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
|
activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
|
||||||
|
|
||||||
|
activity &&
|
||||||
if activity.data["type"] == "Create" do
|
if activity.data["type"] == "Create" do
|
||||||
activity
|
activity
|
||||||
else
|
else
|
||||||
|
|
|
@ -64,7 +64,7 @@ def unblock(%User{} = blocker, params) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def repeat(%User{} = user, ap_id_or_id) do
|
def repeat(%User{} = user, ap_id_or_id) do
|
||||||
with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(ap_id_or_id, user),
|
with {:ok, _announce, %{data: %{"id" => id}}} <- CommonAPI.repeat(ap_id_or_id, user),
|
||||||
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
|
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
|
||||||
{:ok, activity}
|
{:ok, activity}
|
||||||
end
|
end
|
||||||
|
@ -77,14 +77,14 @@ defp unrepeat(%User{} = user, ap_id_or_id) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def fav(%User{} = user, ap_id_or_id) do
|
def fav(%User{} = user, ap_id_or_id) do
|
||||||
with {:ok, _fav, %{data: %{"id" => id}}} = CommonAPI.favorite(ap_id_or_id, user),
|
with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
|
||||||
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
|
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
|
||||||
{:ok, activity}
|
{:ok, activity}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def unfav(%User{} = user, ap_id_or_id) do
|
def unfav(%User{} = user, ap_id_or_id) do
|
||||||
with {:ok, _unfav, _fav, %{data: %{"id" => id}}} = CommonAPI.unfavorite(ap_id_or_id, user),
|
with {:ok, _unfav, _fav, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
|
||||||
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
|
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
|
||||||
{:ok, activity}
|
{:ok, activity}
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,6 +8,8 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
|
action_fallback(:errors)
|
||||||
|
|
||||||
def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
|
def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
|
||||||
token = Phoenix.Token.sign(conn, "user socket", user.id)
|
token = Phoenix.Token.sign(conn, "user socket", user.id)
|
||||||
render(conn, UserView, "show.json", %{user: user, token: token})
|
render(conn, UserView, "show.json", %{user: user, token: token})
|
||||||
|
@ -218,19 +220,22 @@ def get_by_id_or_ap_id(id) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||||
with {:ok, activity} <- TwitterAPI.fav(user, id) do
|
with {_, {:ok, id}} <- {:param_cast, Ecto.Type.cast(:integer, id)},
|
||||||
|
{:ok, activity} <- TwitterAPI.fav(user, id) do
|
||||||
render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
|
render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||||
with {:ok, activity} <- TwitterAPI.unfav(user, id) do
|
with {_, {:ok, id}} <- {:param_cast, Ecto.Type.cast(:integer, id)},
|
||||||
|
{:ok, activity} <- TwitterAPI.unfav(user, id) do
|
||||||
render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
|
render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||||
with {:ok, activity} <- TwitterAPI.repeat(user, id) do
|
with {_, {:ok, id}} <- {:param_cast, Ecto.Type.cast(:integer, id)},
|
||||||
|
{:ok, activity} <- TwitterAPI.repeat(user, id) do
|
||||||
render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
|
render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -389,4 +394,16 @@ defp forbidden_json_reply(conn, error_message) do
|
||||||
defp error_json(conn, error_message) do
|
defp error_json(conn, error_message) do
|
||||||
%{"error" => error_message, "request" => conn.request_path} |> Jason.encode!()
|
%{"error" => error_message, "request" => conn.request_path} |> Jason.encode!()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def errors(conn, {:param_cast, _}) do
|
||||||
|
conn
|
||||||
|
|> put_status(400)
|
||||||
|
|> json("Invalid parameters")
|
||||||
|
end
|
||||||
|
|
||||||
|
def errors(conn, _) do
|
||||||
|
conn
|
||||||
|
|> put_status(500)
|
||||||
|
|> json("Something went wrong")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -260,7 +260,7 @@ test "without valid credentials", %{conn: conn} do
|
||||||
test "with credentials", %{conn: conn, user: current_user} do
|
test "with credentials", %{conn: conn, user: current_user} do
|
||||||
other_user = insert(:user)
|
other_user = insert(:user)
|
||||||
|
|
||||||
{:ok, activity} =
|
{:ok, _activity} =
|
||||||
ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: other_user})
|
ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: other_user})
|
||||||
|
|
||||||
conn =
|
conn =
|
||||||
|
@ -510,6 +510,24 @@ test "with credentials", %{conn: conn, user: current_user} do
|
||||||
|
|
||||||
assert json_response(conn, 200)
|
assert json_response(conn, 200)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "with credentials, invalid param", %{conn: conn, user: current_user} do
|
||||||
|
conn =
|
||||||
|
conn
|
||||||
|
|> with_credentials(current_user.nickname, "test")
|
||||||
|
|> post("/api/favorites/create/wrong.json")
|
||||||
|
|
||||||
|
assert json_response(conn, 400)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "with credentials, invalid activity", %{conn: conn, user: current_user} do
|
||||||
|
conn =
|
||||||
|
conn
|
||||||
|
|> with_credentials(current_user.nickname, "test")
|
||||||
|
|> post("/api/favorites/create/1.json")
|
||||||
|
|
||||||
|
assert json_response(conn, 500)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "POST /api/favorites/destroy/:id" do
|
describe "POST /api/favorites/destroy/:id" do
|
||||||
|
@ -793,7 +811,7 @@ test "it returns the tags timeline", %{conn: conn} do
|
||||||
test "Convert newlines to <br> in bio", %{conn: conn} do
|
test "Convert newlines to <br> in bio", %{conn: conn} do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
||||||
conn =
|
_conn =
|
||||||
conn
|
conn
|
||||||
|> assign(:user, user)
|
|> assign(:user, user)
|
||||||
|> post("/api/account/update_profile.json", %{
|
|> post("/api/account/update_profile.json", %{
|
||||||
|
@ -904,6 +922,8 @@ test "with credentials and valid password", %{conn: conn, user: current_user} do
|
||||||
|> post("/api/pleroma/delete_account", %{"password" => "test"})
|
|> post("/api/pleroma/delete_account", %{"password" => "test"})
|
||||||
|
|
||||||
assert json_response(conn, 200) == %{"status" => "success"}
|
assert json_response(conn, 200) == %{"status" => "success"}
|
||||||
|
# Wait a second for the started task to end
|
||||||
|
:timer.sleep(1000)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue