2019-11-15 18:36:47 +01:00
|
|
|
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
|
2020-08-27 11:53:24 +02:00
|
|
|
require Ecto.Query
|
2019-11-15 18:36:47 +01:00
|
|
|
|
|
|
|
@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)
|
2019-12-03 11:29:51 +01:00
|
|
|
|> validate_required(@required_attrs)
|
2019-11-15 18:36:47 +01:00
|
|
|
end
|
|
|
|
|
2020-01-22 02:14:42 +01:00
|
|
|
@spec create_tombstone(map) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
|
2019-11-15 18:36:47 +01:00
|
|
|
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
|
2020-09-02 08:59:59 +02:00
|
|
|
__MODULE__
|
|
|
|
|> Ecto.Query.where([t], t.uri == ^uri)
|
|
|
|
|> Ecto.Query.preload([t], [:actor])
|
|
|
|
|> Repo.one()
|
2019-11-15 18:36:47 +01:00
|
|
|
end
|
2020-08-27 11:53:24 +02:00
|
|
|
|
|
|
|
@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
|
2019-11-15 18:36:47 +01:00
|
|
|
end
|