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 %{
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
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
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
defmodule Pleroma.ActivityTest do
use Pleroma.DataCase
+ alias Pleroma.Activity
import Pleroma.Factory
test "returns an activity by it's AP id" 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
# 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
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
{: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
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