object: add safe object deletion function

This commit is contained in:
William Pitcock 2018-11-01 07:28:48 +00:00
parent 33587f5cb4
commit c4e43da63e

View file

@ -1,6 +1,6 @@
defmodule Pleroma.Object do defmodule Pleroma.Object do
use Ecto.Schema use Ecto.Schema
alias Pleroma.{Repo, Object} alias Pleroma.{Repo, Object, Activity}
import Ecto.{Query, Changeset} import Ecto.{Query, Changeset}
schema "objects" do schema "objects" do
@ -52,4 +52,12 @@ defmodule Pleroma.Object do
def context_mapping(context) do def context_mapping(context) do
Object.change(%Object{}, %{data: %{"id" => context}}) Object.change(%Object{}, %{data: %{"id" => context}})
end 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 end