From d42b5c128e80e783e3118a1784b76efb74c98161 Mon Sep 17 00:00:00 2001
From: Benjamin Neff <benjamin@coding4coffee.ch>
Date: Mon, 8 Jun 2015 01:41:27 +0200
Subject: [PATCH] update photos when received over public path

closes #6082
---
 Changelog.md                        |  2 ++
 lib/diaspora/federated/shareable.rb | 10 ++++++++++
 lib/postzord/receiver/public.rb     |  6 +++++-
 spec/models/photo_spec.rb           | 15 +++++++++++++++
 spec/models/post_spec.rb            | 16 ++++++++++++++++
 5 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/Changelog.md b/Changelog.md
index 961ca4906e..92d8b40a90 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -28,6 +28,7 @@
 * Improved logging source [#6041](https://github.com/diaspora/diaspora/pull/6041)
 * Gracefully handle duplicate entry while receiving share-visibility in parallel [#6068](https://github.com/diaspora/diaspora/pull/6068)
 * Update twitter gem to get rid of deprecation warnings [#6083](https://github.com/diaspora/diaspora/pull/6083)
+* Refactor photos federation to get rid of some hacks [#6082](https://github.com/diaspora/diaspora/pull/6082)
 
 ## Bug fixes
 * Disable auto follow back on aspect deletion [#5846](https://github.com/diaspora/diaspora/pull/5846)
@@ -60,6 +61,7 @@
 * Only strip text direction codepoints around hashtags [#6067](https://github.com/diaspora/diaspora/issues/6067)
 * Fix selected week on admin weekly stats page [#6079](https://github.com/diaspora/diaspora/pull/6079)
 * Fix that some unread conversations may be hidden [#6060](https://github.com/diaspora/diaspora/pull/6060)
+* Fix photo links in the mobile interface [#6082](https://github.com/diaspora/diaspora/pull/6082)
 
 ## Features
 * Hide post title of limited post in comment notification email [#5843](https://github.com/diaspora/diaspora/pull/5843)
diff --git a/lib/diaspora/federated/shareable.rb b/lib/diaspora/federated/shareable.rb
index 26dad7db81..0b29da8d8c 100644
--- a/lib/diaspora/federated/shareable.rb
+++ b/lib/diaspora/federated/shareable.rb
@@ -44,6 +44,16 @@ module Diaspora
           end
         end
 
+        # @return [void]
+        def receive_public
+          local_shareable = persisted_shareable
+          if local_shareable
+            update_existing_sharable(local_shareable) if verify_persisted_shareable(local_shareable)
+          else
+            save!
+          end
+        end
+
         # The list of people that should receive this Shareable.
         #
         # @param [User] user The context, or dispatching user.
diff --git a/lib/postzord/receiver/public.rb b/lib/postzord/receiver/public.rb
index 29ba0491b3..c96f0d3406 100644
--- a/lib/postzord/receiver/public.rb
+++ b/lib/postzord/receiver/public.rb
@@ -61,7 +61,11 @@ class Postzord::Receiver::Public < Postzord::Receiver
 
   # @return [void]
   def receive_object
-    @object.save! if @object.respond_to?(:save!)
+    if @object.respond_to?(:receive_public)
+      @object.receive_public
+    elsif @object.respond_to?(:save!)
+      @object.save!
+    end
   end
 
   # @return [Array<Integer>] User ids
diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb
index 72bab80e90..7f4902c0d5 100644
--- a/spec/models/photo_spec.rb
+++ b/spec/models/photo_spec.rb
@@ -277,4 +277,19 @@ describe Photo, :type => :model do
       }.to_not change(StatusMessage, :count)
     end
   end
+
+  describe "#receive_public" do
+    it "updates the photo if it is already persisted" do
+      allow(@photo).to receive(:persisted_shareable).and_return(@photo2)
+      expect(@photo2).to receive(:update_attributes)
+      @photo.receive_public
+    end
+
+    it "does not update the photo if the author mismatches" do
+      @photo.author = bob.person
+      allow(@photo).to receive(:persisted_shareable).and_return(@photo2)
+      expect(@photo).not_to receive(:update_existing_sharable)
+      @photo.receive_public
+    end
+  end
 end
diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index eeee9ffa0f..00fbe98e2d 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -336,6 +336,22 @@ describe Post, :type => :model do
     end
   end
 
+  describe "#receive_public" do
+    it "saves the post if the post is unknown" do
+      @post = FactoryGirl.create(:status_message, author: bob.person)
+      allow(@post).to receive(:persisted_shareable).and_return(nil)
+      expect(@post).to receive(:save!)
+      @post.receive_public
+    end
+
+    it "does not update the post because not mutable" do
+      @post = FactoryGirl.create(:status_message, author: bob.person)
+      expect(@post).to receive(:update_existing_sharable).and_call_original
+      expect(@post).not_to receive(:update_attributes)
+      @post.receive_public
+    end
+  end
+
   describe '#reshares_count' do
     before :each do
       @post = @user.post :status_message, :text => "hello", :to => @aspect.id, :public => true
-- 
GitLab