object: add safe object deletion function
authorWilliam Pitcock <nenolod@dereferenced.org>
Thu, 1 Nov 2018 07:28:48 +0000 (07:28 +0000)
committerWilliam Pitcock <nenolod@dereferenced.org>
Thu, 1 Nov 2018 07:28:48 +0000 (07:28 +0000)
lib/pleroma/object.ex

index 1bcff5a7b092f5dbc1db4a23192f2d98b4e5cf99..8f96fd8fb31b624295347b0014c8b4deae8d6089 100644 (file)
@@ -1,6 +1,6 @@
 defmodule Pleroma.Object do
   use Ecto.Schema
-  alias Pleroma.{Repo, Object}
+  alias Pleroma.{Repo, Object, Activity}
   import Ecto.{Query, Changeset}
 
   schema "objects" do
@@ -52,4 +52,12 @@ defmodule Pleroma.Object do
   def context_mapping(context) do
     Object.change(%Object{}, %{data: %{"id" => context}})
   end
+
+  def delete(%Object{data: %{"id" => id}} = object) do
+    with Repo.delete(object),
+         Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
+         {:ok, true} <- Cachex.del(:user_cache, "object:#{id}") do
+      :ok
+    end
+  end
 end