mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-12-23 09:44:52 +00:00
Create tombstone instead of object deletion
This commit is contained in:
parent
61a88a6757
commit
0f412cf6e6
|
@ -1,7 +1,7 @@
|
|||
defmodule Pleroma.Activity do
|
||||
use Ecto.Schema
|
||||
alias Pleroma.{Repo, Activity, Notification}
|
||||
import Ecto.Query
|
||||
import Ecto.{Query, Changeset}
|
||||
|
||||
# https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
|
||||
@mastodon_notification_types %{
|
||||
|
@ -103,4 +103,22 @@ 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
|
||||
tombstone = get_tombstone(activity)
|
||||
|
||||
activity
|
||||
|> change(%{data: tombstone})
|
||||
|> Repo.update()
|
||||
end
|
||||
end
|
||||
|
|
|
@ -62,9 +62,27 @@ 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",
|
||||
deleted: deleted
|
||||
}
|
||||
end
|
||||
|
||||
def swap_data_with_tombstone(object) do
|
||||
tombstone = get_tombstone(object)
|
||||
|
||||
object
|
||||
|> Object.change(%{data: tombstone})
|
||||
|> Repo.update()
|
||||
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)),
|
||||
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),
|
||||
{:ok, true} <- Cachex.del(:object_cache, "object:#{id}") do
|
||||
{:ok, object}
|
||||
end
|
||||
|
|
|
@ -69,7 +69,12 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|
|||
mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
|
||||
|
||||
if inReplyTo do
|
||||
{Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
|
||||
to =
|
||||
[inReplyTo.data["actor"] | mentioned_users]
|
||||
|> Enum.uniq()
|
||||
|> Enum.reject(&is_nil/1)
|
||||
|
||||
{to, []}
|
||||
else
|
||||
{mentioned_users, []}
|
||||
end
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
defmodule Pleroma.ActivityTest do
|
||||
use Pleroma.DataCase
|
||||
alias Pleroma.Activity
|
||||
import Pleroma.Factory
|
||||
|
||||
test "returns an activity by it's AP id" do
|
||||
|
@ -24,4 +25,28 @@ defmodule Pleroma.ActivityTest do
|
|||
|
||||
assert activity == found_activity
|
||||
end
|
||||
|
||||
test "returns tombstone" do
|
||||
activity = insert(:note_activity)
|
||||
deleted = DateTime.utc_now()
|
||||
|
||||
assert Pleroma.Activity.get_tombstone(activity, deleted) == %{
|
||||
id: activity.data["object"]["id"],
|
||||
context: activity.data["context"],
|
||||
type: "tombstone",
|
||||
published: activity.data["published"],
|
||||
deleted: deleted
|
||||
}
|
||||
end
|
||||
|
||||
test "swaps data with tombstone" do
|
||||
activity = insert(:note_activity)
|
||||
|
||||
{:ok, deleted} = Pleroma.Activity.swap_data_with_tombstone(activity)
|
||||
assert deleted.data.type == "tombstone"
|
||||
|
||||
found_activity = Repo.get(Activity, activity.id)
|
||||
|
||||
assert deleted.data.type == found_activity.data["type"]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -625,7 +625,7 @@ defmodule Pleroma.UserTest do
|
|||
|
||||
# TODO: Remove favorites, repeats, delete activities.
|
||||
|
||||
refute Repo.get(Activity, activity.id)
|
||||
assert Repo.get(Activity, activity.id).data["type"] == "tombstone"
|
||||
end
|
||||
|
||||
test "get_public_key_for_ap_id fetches a user that's not in the db" do
|
||||
|
|
|
@ -478,7 +478,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
|
|||
|
||||
assert Repo.get(Activity, delete.id) != nil
|
||||
|
||||
assert Repo.get(Object, object.id) == nil
|
||||
assert Repo.get(Object, object.id).data["type"] == "tombstone"
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -363,7 +363,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
|
|||
|
||||
{:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
|
||||
|
||||
refute Repo.get(Activity, activity.id)
|
||||
assert Repo.get(Activity, activity.id).data["type"] == "tombstone"
|
||||
end
|
||||
|
||||
test "it fails for incoming deletes with spoofed origin" do
|
||||
|
|
|
@ -292,7 +292,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
|
|||
|
||||
assert %{} = json_response(conn, 200)
|
||||
|
||||
assert Repo.get(Activity, activity.id) == nil
|
||||
assert Repo.get(Activity, activity.id).data["type"] == "tombstone"
|
||||
end
|
||||
|
||||
test "when you didn't create it", %{conn: conn} do
|
||||
|
|
Loading…
Reference in a new issue