From f226f46a097b6b87a87ce1ead07383e8f83ad8ea Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 1 Aug 2017 17:05:07 +0200 Subject: [PATCH] Add basic incoming deletions. --- lib/pleroma/activity.ex | 8 +++- .../web/ostatus/handlers/delete_handler.ex | 14 +++++++ lib/pleroma/web/ostatus/ostatus.ex | 4 +- test/fixtures/delete.xml | 39 +++++++++++++++++++ .../delete_handling_test.exs | 23 +++++++++++ 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 lib/pleroma/web/ostatus/handlers/delete_handler.ex create mode 100644 test/fixtures/delete.xml create mode 100644 test/web/ostatus/incoming_documents/delete_handling_test.exs diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex index 40e14005b..0b7188aba 100644 --- a/lib/pleroma/activity.ex +++ b/lib/pleroma/activity.ex @@ -15,9 +15,13 @@ def get_by_ap_id(ap_id) do where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))) end + def all_by_object_ap_id_q(ap_id) do + from activity in Activity, + where: fragment("(?)->'object'->>'id' = ?", activity.data, ^to_string(ap_id)) + end + def all_by_object_ap_id(ap_id) do - Repo.all(from activity in Activity, - where: fragment("(?)->'object'->>'id' = ?", activity.data, ^to_string(ap_id))) + Repo.all(all_by_object_ap_id_q(ap_id)) end def get_create_activity_by_object_ap_id(ap_id) do diff --git a/lib/pleroma/web/ostatus/handlers/delete_handler.ex b/lib/pleroma/web/ostatus/handlers/delete_handler.ex new file mode 100644 index 000000000..2e5f9469b --- /dev/null +++ b/lib/pleroma/web/ostatus/handlers/delete_handler.ex @@ -0,0 +1,14 @@ +defmodule Pleroma.Web.OStatus.DeleteHandler do + require Logger + alias Pleroma.Web.{XML, OStatus} + alias Pleroma.{Activity, Object, Repo} + + def handle_delete(entry, doc \\ nil) do + with id <- XML.string_from_xpath("//id", entry), + object when not is_nil(object) <- Object.get_by_ap_id(id) do + Repo.delete(object) + Repo.delete_all(Activity.all_by_object_ap_id_q(id)) + nil + end + end +end diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index d9a5924dc..469d4031c 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -9,7 +9,7 @@ defmodule Pleroma.Web.OStatus do alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.{WebFinger, Websub} - alias Pleroma.Web.OStatus.{FollowHandler, NoteHandler} + alias Pleroma.Web.OStatus.{FollowHandler, NoteHandler, DeleteHandler} def feed_path(user) do "#{user.ap_id}/feed.atom" @@ -34,6 +34,8 @@ def handle_incoming(xml_string) do try do case verb do + 'http://activitystrea.ms/schema/1.0/delete' -> + with {:ok, activity} <- DeleteHandler.handle_delete(entry, doc), do: activity 'http://activitystrea.ms/schema/1.0/follow' -> with {:ok, activity} <- FollowHandler.handle(entry, doc), do: activity 'http://activitystrea.ms/schema/1.0/share' -> diff --git a/test/fixtures/delete.xml b/test/fixtures/delete.xml new file mode 100644 index 000000000..731e1c204 --- /dev/null +++ b/test/fixtures/delete.xml @@ -0,0 +1,39 @@ + + + https://mastodon.sdf.org/users/snowdusk.atom + snowdusk + Amateur live performance DJ/radio DJ on SDF's underground Internet radio http://aNONradio.net (LIVE Sat Sun Mon Tue 23:00-24:00 UTC) - http://snowdusk.sdf.org + 2017-06-17T04:14:34Z + https://mastodon.sdf.org/system/accounts/avatars/000/000/002/original/405a7652d5f60449.jpg?1497672873 + + https://mastodon.sdf.org/users/snowdusk + http://activitystrea.ms/schema/1.0/person + https://mastodon.sdf.org/users/snowdusk + snowdusk + snowdusk@mastodon.sdf.org + <p>Amateur live performance DJ/radio DJ on SDF&apos;s underground Internet radio <a href="http://anonradio.net/" rel="nofollow noopener" target="_blank"><span class="invisible">http://</span><span class="">anonradio.net/</span><span class="invisible"></span></a> (LIVE Sat Sun Mon Tue 23:00-24:00 UTC) - <a href="http://snowdusk.sdf.org/" rel="nofollow noopener" target="_blank"><span class="invisible">http://</span><span class="">snowdusk.sdf.org/</span><span class="invisible"></span></a></p> + + + + snowdusk + snowdusk + Amateur live performance DJ/radio DJ on SDF's underground Internet radio http://aNONradio.net (LIVE Sat Sun Mon Tue 23:00-24:00 UTC) - http://snowdusk.sdf.org + public + + + + + + + + tag:mastodon.sdf.org,2017-06-10:objectId=310513:objectType=Status + 2017-06-10T22:02:31Z + 2017-06-10T22:02:31Z + snowdusk deleted status + http://activitystrea.ms/schema/1.0/activity + http://activitystrea.ms/schema/1.0/delete + Deleted status + + + + diff --git a/test/web/ostatus/incoming_documents/delete_handling_test.exs b/test/web/ostatus/incoming_documents/delete_handling_test.exs new file mode 100644 index 000000000..8cd6e295e --- /dev/null +++ b/test/web/ostatus/incoming_documents/delete_handling_test.exs @@ -0,0 +1,23 @@ +defmodule Pleroma.Web.OStatus.DeleteHandlingTest do + use Pleroma.DataCase + + import Pleroma.Factory + alias Pleroma.{Repo, Activity, Object} + alias Pleroma.Web.OStatus + + describe "deletions" do + test "it removes the mentioned activity" do + note = insert(:note_activity) + second_note = insert(:note_activity) + + incoming = File.read!("test/fixtures/delete.xml") + |> String.replace("tag:mastodon.sdf.org,2017-06-10:objectId=310513:objectType=Status", note.data["object"]["id"]) + {:ok, []} = OStatus.handle_incoming(incoming) + + refute Repo.get(Activity, note.id) + refute Object.get_by_ap_id(note.data["object"]["id"]) + assert Repo.get(Activity, second_note.id) + assert Object.get_by_ap_id(second_note.data["object"]["id"]) + end + end +end