--- /dev/null
+defmodule Pleroma.Web.CommonAPI do
+ alias Pleroma.{Repo, Activity, Object}
+ alias Pleroma.Web.ActivityPub.ActivityPub
+
+ def delete(activity_id, user) do
+ with %Activity{data: %{"object" => %{"id" => object_id}}} <- Repo.get(Activity, activity_id),
+ %Object{} = object <- Object.get_by_ap_id(object_id),
+ true <- user.ap_id == object.data["actor"],
+ {:ok, delete} <- ActivityPub.delete(object) do
+ {:ok, delete}
+ end
+ end
+end
alias Pleroma.Web.MastodonAPI.{StatusView, AccountView}
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.TwitterAPI.TwitterAPI
+ alias Pleroma.Web.CommonAPI
def create_app(conn, params) do
with cs <- App.register_changeset(%App{}, params) |> IO.inspect,
render conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity}
end
end
+
+ def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
+ with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
+ json(conn, %{})
+ else
+ _e ->
+ conn
+ |> put_status(403)
+ |> json(%{error: "Can't delete this post"})
+ end
+ end
end
get "/timelines/home", MastodonAPIController, :home_timeline
post "/statuses", MastodonAPIController, :post_status
+ delete "/statuses/:id", MastodonAPIController, :delete_status
end
scope "/api", Pleroma.Web do
use Pleroma.Web, :controller
alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView}
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
+ alias Pleroma.Web.CommonAPI
alias Pleroma.{Repo, Activity, User, Object}
alias Pleroma.Web.ActivityPub.ActivityPub
alias Ecto.Changeset
end
def delete_post(%{assigns: %{user: user}} = conn, %{"id" => id}) do
- with %Activity{data: %{"object" => %{"id" => object_id}}} <- Repo.get(Activity, id),
- %Object{} = object <- Object.get_by_ap_id(object_id),
- true <- user.ap_id == object.data["actor"],
- {:ok, delete} <- ActivityPub.delete(object) |> IO.inspect do
+ with {:ok, delete} <- CommonAPI.delete(id, user) do
json = ActivityRepresenter.to_json(delete, %{user: user, for: user})
conn
|> json_reply(200, json)
assert %{"id" => id} = json_response(conn, 200)
assert id == activity.id
end
+
+ describe "deleting a status" do
+ test "when you created it", %{conn: conn} do
+ activity = insert(:note_activity)
+ author = User.get_by_ap_id(activity.data["actor"])
+
+ conn = conn
+ |> assign(:user, author)
+ |> delete("/api/v1/statuses/#{activity.id}")
+
+ assert %{} = json_response(conn, 200)
+
+ assert Repo.get(Activity, activity.id) == nil
+ end
+
+ test "when you didn't create it", %{conn: conn} do
+ activity = insert(:note_activity)
+ user = insert(:user)
+
+ conn = conn
+ |> assign(:user, user)
+ |> delete("/api/v1/statuses/#{activity.id}")
+
+ assert %{"error" => _} = json_response(conn, 403)
+
+ assert Repo.get(Activity, activity.id) == activity
+ end
+ end
end