Add delete to ActivityPub.
[akkoma] / lib / pleroma / web / activity_pub / activity_pub.ex
index 69a2d8f4ef23c18d5d59eff3b17e1b43af602ea7..146888c98eab2ea68ea866f32ad419cf5c5f7033 100644 (file)
@@ -75,6 +75,23 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     end
   end
 
+  def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
+    user = User.get_cached_by_ap_id(actor)
+    data = %{
+      "type" => "Delete",
+      "actor" => actor,
+      "object" => id,
+      "to" => [user.follower_address, "https://www.w3.org/ns/activitystreams#Public"]
+    }
+    with {:ok, activity} <- insert(data, local),
+         :ok <- maybe_federate(activity) do
+      Repo.delete(object)
+      Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id))
+      Repo.delete_all(Activity.all_by_object_ap_id_q(id))
+      {:ok, activity}
+    end
+  end
+
   def fetch_activities_for_context(context) do
     query = from activity in Activity,
       where: fragment("? @> ?", activity.data, ^%{ type: "Create", context: context }),
@@ -119,7 +136,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   def fetch_activities(recipients, opts \\ %{}) do
     base_query = from activity in Activity,
       limit: 20,
-      order_by: [desc: :inserted_at]
+      order_by: [desc: :id]
 
     base_query
     |> restrict_recipients(recipients)