mobilizon.chapril.org-mobil.../lib/mobilizon/tombstone.ex
Thomas Citharel 1984f71cbf
Add group admin profiles
And other fixes

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2020-08-27 12:12:54 +02:00

60 lines
1.4 KiB
Elixir

defmodule Mobilizon.Tombstone do
@moduledoc """
Represent tombstones for deleted objects. Saves only URI
"""
use Ecto.Schema
import Ecto.Changeset
alias Mobilizon.Actors.Actor
alias Mobilizon.Storage.Repo
require Ecto.Query
@type t :: %__MODULE__{
uri: String.t(),
actor: Actor.t()
}
@required_attrs [:uri, :actor_id]
@optional_attrs []
@attrs @required_attrs ++ @optional_attrs
schema "tombstones" do
field(:uri, :string)
belongs_to(:actor, Actor)
timestamps()
end
@doc false
def changeset(%__MODULE__{} = tombstone, attrs) do
tombstone
|> cast(attrs, @attrs)
|> validate_required(@required_attrs)
end
@spec create_tombstone(map) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
def create_tombstone(attrs) do
%__MODULE__{}
|> changeset(attrs)
|> Repo.insert(on_conflict: :replace_all, conflict_target: :uri)
end
@spec find_tombstone(String.t()) :: Ecto.Schema.t() | nil
def find_tombstone(uri) do
Repo.get_by(__MODULE__, uri: uri)
end
@spec delete_actor_tombstones(String.t() | integer()) :: {integer(), nil}
def delete_actor_tombstones(actorId) do
__MODULE__
|> Ecto.Query.where(actor_id: ^actorId)
|> Repo.delete_all()
end
def delete_uri_tombstone(uri) do
__MODULE__
|> Ecto.Query.where(uri: ^uri)
|> Repo.delete_all()
end
end