From f226f46a097b6b87a87ce1ead07383e8f83ad8ea Mon Sep 17 00:00:00 2001
From: Roger Braun <roger@rogerbraun.net>
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 @@
+<?xml version="1.0"?>
+<feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:media="http://purl.org/syndication/atommedia" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:mastodon="http://mastodon.social/schema/1.0">
+  <id>https://mastodon.sdf.org/users/snowdusk.atom</id>
+  <title>snowdusk</title>
+  <subtitle>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</subtitle>
+  <updated>2017-06-17T04:14:34Z</updated>
+  <logo>https://mastodon.sdf.org/system/accounts/avatars/000/000/002/original/405a7652d5f60449.jpg?1497672873</logo>
+  <author>
+    <id>https://mastodon.sdf.org/users/snowdusk</id>
+    <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
+    <uri>https://mastodon.sdf.org/users/snowdusk</uri>
+    <name>snowdusk</name>
+    <email>snowdusk@mastodon.sdf.org</email>
+    <summary type="html">&lt;p&gt;Amateur live performance DJ/radio DJ on SDF&amp;apos;s underground Internet radio &lt;a href="http://anonradio.net/" rel="nofollow noopener" target="_blank"&gt;&lt;span class="invisible"&gt;http://&lt;/span&gt;&lt;span class=""&gt;anonradio.net/&lt;/span&gt;&lt;span class="invisible"&gt;&lt;/span&gt;&lt;/a&gt; (LIVE Sat Sun Mon Tue 23:00-24:00 UTC) - &lt;a href="http://snowdusk.sdf.org/" rel="nofollow noopener" target="_blank"&gt;&lt;span class="invisible"&gt;http://&lt;/span&gt;&lt;span class=""&gt;snowdusk.sdf.org/&lt;/span&gt;&lt;span class="invisible"&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;</summary>
+    <link rel="alternate" type="text/html" href="https://mastodon.sdf.org/@snowdusk"/>
+    <link rel="avatar" type="image/jpeg" media:width="120" media:height="120" href="https://mastodon.sdf.org/system/accounts/avatars/000/000/002/original/405a7652d5f60449.jpg?1497672873"/>
+    <link rel="header" type="image/jpeg" media:width="700" media:height="335" href="https://mastodon.sdf.org/system/accounts/headers/000/000/002/original/f1e9b0fb21b4e5a0.jpeg?1495793472"/>
+    <poco:preferredUsername>snowdusk</poco:preferredUsername>
+    <poco:displayName>snowdusk</poco:displayName>
+    <poco:note>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</poco:note>
+    <mastodon:scope>public</mastodon:scope>
+  </author>
+  <link rel="alternate" type="text/html" href="https://mastodon.sdf.org/@snowdusk"/>
+  <link rel="self" type="application/atom+xml" href="https://mastodon.sdf.org/users/snowdusk.atom"/>
+  <link rel="next" type="application/atom+xml" href="https://mastodon.sdf.org/users/snowdusk.atom?max_id=7592"/>
+  <link rel="hub" href="https://mastodon.sdf.org/api/push"/>
+  <link rel="salmon" href="https://mastodon.sdf.org/api/salmon/2"/>
+  <entry>
+    <id>tag:mastodon.sdf.org,2017-06-10:objectId=310513:objectType=Status</id>
+    <published>2017-06-10T22:02:31Z</published>
+    <updated>2017-06-10T22:02:31Z</updated>
+    <title>snowdusk deleted status</title>
+    <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
+    <activity:verb>http://activitystrea.ms/schema/1.0/delete</activity:verb>
+    <content>Deleted status</content>
+    <link rel="alternate" type="text/html" href="https://mastodon.sdf.org/users/snowdusk/updates/7782"/>
+    <link rel="self" type="application/atom+xml" href="https://mastodon.sdf.org/users/snowdusk/updates/7782.atom"/>
+  </entry>
+</feed>
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
-- 
GitLab