Revert Activity tombstones, add ObjectTombstone struct

This commit is contained in:
Maxim Filippov 2018-12-25 03:00:06 +03:00
commit f75f707f6c
9 changed files with 43 additions and 62 deletions

View file

@ -1,7 +1,7 @@
defmodule Pleroma.Activity do
use Ecto.Schema
alias Pleroma.{Repo, Activity, Notification}
import Ecto.{Query, Changeset}
import Ecto.Query
# https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
@mastodon_notification_types %{
@ -103,25 +103,4 @@ defmodule Pleroma.Activity do
end
def mastodon_notification_type(%Activity{}), do: nil
def get_tombstone(%Activity{data: data}, deleted \\ DateTime.utc_now()) do
%{
id: data["id"],
context: data["context"],
type: "Tombstone",
published: data["published"],
deleted: deleted
}
end
def swap_data_with_tombstone(activity) do
with tombstone = get_tombstone(activity),
Notification.clear(activity),
{:ok, changed_activity} =
activity
|> change(%{data: tombstone})
|> Repo.update() do
{:ok, changed_activity}
end
end
end

View file

@ -1,6 +1,6 @@
defmodule Pleroma.Object do
use Ecto.Schema
alias Pleroma.{Repo, Object, User, Activity}
alias Pleroma.{Repo, Object, User, Activity, ObjectTombstone}
import Ecto.{Query, Changeset}
schema "objects" do
@ -62,16 +62,17 @@ defmodule Pleroma.Object do
Object.change(%Object{}, %{data: %{"id" => context}})
end
def get_tombstone(%Object{data: data}, deleted \\ DateTime.utc_now()) do
%{
id: data["id"],
type: "Tombstone",
def make_tombstone(%Object{data: %{"id" => id, "type" => type}}, deleted \\ DateTime.utc_now()) do
%ObjectTombstone{
id: id,
formerType: type,
deleted: deleted
}
|> Map.from_struct()
end
def swap_data_with_tombstone(object) do
tombstone = get_tombstone(object)
def swap_object_with_tombstone(object) do
tombstone = make_tombstone(object)
object
|> Object.change(%{data: tombstone})
@ -79,10 +80,8 @@ defmodule Pleroma.Object do
end
def delete(%Object{data: %{"id" => id}} = object) do
with swap_data_with_tombstone(object),
Activity.all_non_create_by_object_ap_id_q(id)
|> Repo.all()
|> Enum.each(&Activity.swap_data_with_tombstone/1),
with {:ok, _obj} = swap_object_with_tombstone(object),
Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
{:ok, true} <- Cachex.del(:object_cache, "object:#{id}") do
{:ok, object}
end

View file

@ -0,0 +1,4 @@
defmodule Pleroma.ObjectTombstone do
@enforce_keys [:id, :formerType, :deleted]
defstruct [:id, :formerType, :deleted, type: "Tombstone"]
end