From ad892dbc0c377d82b2cae349f189a41708a1e6af Mon Sep 17 00:00:00 2001
From: Eugen Rochko <eugen@zeonfederated.com>
Date: Thu, 17 Aug 2017 21:35:00 +0200
Subject: [PATCH] Add _:atomUri property for deduplicating OStatus/ActivityPub
 legacy records (#4593)

---
 app/lib/activitypub/activity/create.rb         |  8 +++++++-
 app/serializers/activitypub/note_serializer.rb | 10 ++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/app/lib/activitypub/activity/create.rb b/app/lib/activitypub/activity/create.rb
index 4c4049bc69..de9c47d5c9 100644
--- a/app/lib/activitypub/activity/create.rb
+++ b/app/lib/activitypub/activity/create.rb
@@ -4,7 +4,7 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
   def perform
     return if delete_arrived_first?(object_uri) || unsupported_object_type?
 
-    status = Status.find_by(uri: object_uri)
+    status = find_existing_status
 
     return status unless status.nil?
 
@@ -23,6 +23,12 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
 
   private
 
+  def find_existing_status
+    status   = Status.find_by(uri: object_uri)
+    status ||= Status.find_by(uri: @object['_:atomUri']) if @object['_:atomUri'].present?
+    status
+  end
+
   def status_params
     {
       uri: @object['id'],
diff --git a/app/serializers/activitypub/note_serializer.rb b/app/serializers/activitypub/note_serializer.rb
index 4c13f8e596..bc8eb8a356 100644
--- a/app/serializers/activitypub/note_serializer.rb
+++ b/app/serializers/activitypub/note_serializer.rb
@@ -8,6 +8,8 @@ class ActivityPub::NoteSerializer < ActiveModel::Serializer
   has_many :media_attachments, key: :attachment
   has_many :virtual_tags, key: :tag
 
+  attribute :atom_uri, key: '_:atomUri', if: :local?
+
   def id
     ActivityPub::TagManager.instance.uri_for(object)
   end
@@ -52,6 +54,14 @@ class ActivityPub::NoteSerializer < ActiveModel::Serializer
     object.mentions + object.tags
   end
 
+  def atom_uri
+    ::TagManager.instance.uri_for(object)
+  end
+
+  def local?
+    object.account.local?
+  end
+
   class MediaAttachmentSerializer < ActiveModel::Serializer
     include RoutingHelper
 
-- 
GitLab