From 84b89d5594ae0625992590d0a64d49e77a45b4d7 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sat, 23 Sep 2017 04:42:42 +0200 Subject: [PATCH] Relay likes for comments --- lib/diaspora/federation/receive.rb | 18 ++++---- lib/diaspora/relayable.rb | 20 ++++++--- spec/lib/diaspora/federation/receive_spec.rb | 47 ++++++++++++++++++-- spec/models/like_spec.rb | 13 ++++++ spec/shared_behaviors/receiving.rb | 2 +- 5 files changed, 80 insertions(+), 20 deletions(-) diff --git a/lib/diaspora/federation/receive.rb b/lib/diaspora/federation/receive.rb index 148160d95..adddf4000 100644 --- a/lib/diaspora/federation/receive.rb +++ b/lib/diaspora/federation/receive.rb @@ -158,10 +158,10 @@ module Diaspora when Person User.find(recipient_id).disconnected_by(object) when Diaspora::Relayable - if object.parent.author.local? - parent_author = object.parent.author.owner + if object.root.author.local? + root_author = object.root.author.owner retraction = Retraction.for(object) - retraction.defer_dispatch(parent_author, false) + retraction.defer_dispatch(root_author, false) retraction.perform else object.destroy! @@ -257,7 +257,7 @@ module Diaspora yield.tap do |relayable| retract_if_author_ignored(relayable) - relayable.signature = build_signature(klass, entity) if relayable.parent.author.local? + relayable.signature = build_signature(klass, entity) if relayable.root.author.local? relayable.save! end end @@ -272,18 +272,18 @@ module Diaspora end private_class_method def self.retract_if_author_ignored(relayable) - parent_author = relayable.parent.author.owner - return unless parent_author && parent_author.ignored_people.include?(relayable.author) + root_author = relayable.root.author.owner + return unless root_author && root_author.ignored_people.include?(relayable.author) retraction = Retraction.for(relayable) - Diaspora::Federation::Dispatcher.build(parent_author, retraction, subscribers: [relayable.author]).dispatch + Diaspora::Federation::Dispatcher.build(root_author, retraction, subscribers: [relayable.author]).dispatch raise Diaspora::Federation::AuthorIgnored end private_class_method def self.relay_relayable(relayable) - parent_author = relayable.parent.author.owner - Diaspora::Federation::Dispatcher.defer_dispatch(parent_author, relayable) if parent_author + root_author = relayable.root.author.owner + Diaspora::Federation::Dispatcher.defer_dispatch(root_author, relayable) if root_author end # check if the object already exists, otherwise save it. diff --git a/lib/diaspora/relayable.rb b/lib/diaspora/relayable.rb index b8cb9da91..55e3d8ff4 100644 --- a/lib/diaspora/relayable.rb +++ b/lib/diaspora/relayable.rb @@ -17,9 +17,15 @@ module Diaspora end end + def root + @root ||= parent + @root = @root.parent while @root.is_a?(Relayable) + @root + end + def author_is_not_ignored - unless new_record? && parent.present? && parent.author.local? && - parent.author.owner.ignored_people.include?(author) + unless new_record? && root.present? && root.author.local? && + root.author.owner.ignored_people.include?(author) return end @@ -28,19 +34,19 @@ module Diaspora # @return [Array] def subscribers - if parent.author.local? + if root.author.local? if author.local? - parent.subscribers + root.subscribers else - parent.subscribers.select(&:remote?).reject {|person| person.pod_id == author.pod_id } + root.subscribers.select(&:remote?).reject {|person| person.pod_id == author.pod_id } end else - [parent.author, author] + [root.author, author] end end def sender_for_dispatch - parent.author.owner if parent.author.local? + root.author.owner if root.author.local? end # @abstract diff --git a/spec/lib/diaspora/federation/receive_spec.rb b/spec/lib/diaspora/federation/receive_spec.rb index 9a7a8ca21..5f1fa632b 100644 --- a/spec/lib/diaspora/federation/receive_spec.rb +++ b/spec/lib/diaspora/federation/receive_spec.rb @@ -74,7 +74,7 @@ describe Diaspora::Federation::Receive do let(:entity) { comment_entity } it_behaves_like "it ignores existing object received twice", Comment - it_behaves_like "it rejects if the parent author ignores the author", Comment + it_behaves_like "it rejects if the root author ignores the author", Comment it_behaves_like "it relays relayables", Comment end @@ -241,8 +241,49 @@ describe Diaspora::Federation::Receive do let(:entity) { like_entity } it_behaves_like "it ignores existing object received twice", Like - it_behaves_like "it rejects if the parent author ignores the author", Like + it_behaves_like "it rejects if the root author ignores the author", Like it_behaves_like "it relays relayables", Like + + context "like for a comment" do + let(:comment) { FactoryGirl.create(:comment, post: post) } + let(:like_entity) { + build_relayable_federation_entity( + :like, + { + author: sender.diaspora_handle, + parent_guid: comment.guid, + parent_type: "Comment", + author_signature: "aa" + }, + "new_property" => "data" + ) + } + + it "attaches the like to the comment" do + Diaspora::Federation::Receive.perform(like_entity) + + like = Like.find_by!(guid: like_entity.guid) + + expect(comment.likes).to include(like) + expect(like.target).to eq(comment) + end + + it "saves the signature data" do + Diaspora::Federation::Receive.perform(like_entity) + + like = Like.find_by!(guid: like_entity.guid) + + expect(like.signature).not_to be_nil + expect(like.signature.author_signature).to eq("aa") + expect(like.signature.additional_data).to eq("new_property" => "data") + expect(like.signature.order).to eq(like_entity.signature_order.map(&:to_s)) + end + + let(:entity) { like_entity } + it_behaves_like "it ignores existing object received twice", Like + it_behaves_like "it rejects if the root author ignores the author", Like + it_behaves_like "it relays relayables", Like + end end describe ".message" do @@ -408,7 +449,7 @@ describe Diaspora::Federation::Receive do let(:entity) { poll_participation_entity } it_behaves_like "it ignores existing object received twice", PollParticipation - it_behaves_like "it rejects if the parent author ignores the author", PollParticipation + it_behaves_like "it rejects if the root author ignores the author", PollParticipation it_behaves_like "it relays relayables", PollParticipation end diff --git a/spec/models/like_spec.rb b/spec/models/like_spec.rb index 2701d88dc..d96121d78 100644 --- a/spec/models/like_spec.rb +++ b/spec/models/like_spec.rb @@ -62,4 +62,17 @@ describe Like, type: :model do let(:remote_object_on_local_parent) { FactoryGirl.create(:like, target: local_parent, author: remote_raphael) } let(:relayable) { Like::Generator.new(alice, status).build } end + + context "like for a comment" do + it_behaves_like "it is relayable" do + let(:local_parent) { local_luke.post(:status_message, text: "hi", to: local_luke.aspects.first) } + let(:remote_parent) { FactoryGirl.create(:status_message, author: remote_raphael) } + let(:comment_on_local_parent) { FactoryGirl.create(:comment, post: local_parent) } + let(:comment_on_remote_parent) { FactoryGirl.create(:comment, post: remote_parent) } + let(:object_on_local_parent) { local_luke.like!(comment_on_local_parent) } + let(:object_on_remote_parent) { local_luke.like!(comment_on_remote_parent) } + let(:remote_object_on_local_parent) { FactoryGirl.create(:like, target: local_parent, author: remote_raphael) } + let(:relayable) { Like::Generator.new(alice, status).build } + end + end end diff --git a/spec/shared_behaviors/receiving.rb b/spec/shared_behaviors/receiving.rb index 930d71aea..6a563e237 100644 --- a/spec/shared_behaviors/receiving.rb +++ b/spec/shared_behaviors/receiving.rb @@ -15,7 +15,7 @@ shared_examples_for "it ignores existing object received twice" do |klass| end end -shared_examples_for "it rejects if the parent author ignores the author" do |klass| +shared_examples_for "it rejects if the root author ignores the author" do |klass| it "saves the relayable if the author is not ignored" do Diaspora::Federation::Receive.perform(entity)