Relay likes for comments

This commit is contained in:
Benjamin Neff 2017-09-23 04:42:42 +02:00
parent 5f5d8c5e13
commit 84b89d5594
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
5 changed files with 80 additions and 20 deletions

View file

@ -158,10 +158,10 @@ module Diaspora
when Person when Person
User.find(recipient_id).disconnected_by(object) User.find(recipient_id).disconnected_by(object)
when Diaspora::Relayable when Diaspora::Relayable
if object.parent.author.local? if object.root.author.local?
parent_author = object.parent.author.owner root_author = object.root.author.owner
retraction = Retraction.for(object) retraction = Retraction.for(object)
retraction.defer_dispatch(parent_author, false) retraction.defer_dispatch(root_author, false)
retraction.perform retraction.perform
else else
object.destroy! object.destroy!
@ -257,7 +257,7 @@ module Diaspora
yield.tap do |relayable| yield.tap do |relayable|
retract_if_author_ignored(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! relayable.save!
end end
end end
@ -272,18 +272,18 @@ module Diaspora
end end
private_class_method def self.retract_if_author_ignored(relayable) private_class_method def self.retract_if_author_ignored(relayable)
parent_author = relayable.parent.author.owner root_author = relayable.root.author.owner
return unless parent_author && parent_author.ignored_people.include?(relayable.author) return unless root_author && root_author.ignored_people.include?(relayable.author)
retraction = Retraction.for(relayable) 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 raise Diaspora::Federation::AuthorIgnored
end end
private_class_method def self.relay_relayable(relayable) private_class_method def self.relay_relayable(relayable)
parent_author = relayable.parent.author.owner root_author = relayable.root.author.owner
Diaspora::Federation::Dispatcher.defer_dispatch(parent_author, relayable) if parent_author Diaspora::Federation::Dispatcher.defer_dispatch(root_author, relayable) if root_author
end end
# check if the object already exists, otherwise save it. # check if the object already exists, otherwise save it.

View file

@ -17,9 +17,15 @@ module Diaspora
end end
end end
def root
@root ||= parent
@root = @root.parent while @root.is_a?(Relayable)
@root
end
def author_is_not_ignored def author_is_not_ignored
unless new_record? && parent.present? && parent.author.local? && unless new_record? && root.present? && root.author.local? &&
parent.author.owner.ignored_people.include?(author) root.author.owner.ignored_people.include?(author)
return return
end end
@ -28,19 +34,19 @@ module Diaspora
# @return [Array<Person>] # @return [Array<Person>]
def subscribers def subscribers
if parent.author.local? if root.author.local?
if author.local? if author.local?
parent.subscribers root.subscribers
else 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 end
else else
[parent.author, author] [root.author, author]
end end
end end
def sender_for_dispatch def sender_for_dispatch
parent.author.owner if parent.author.local? root.author.owner if root.author.local?
end end
# @abstract # @abstract

View file

@ -74,7 +74,7 @@ describe Diaspora::Federation::Receive do
let(:entity) { comment_entity } let(:entity) { comment_entity }
it_behaves_like "it ignores existing object received twice", Comment 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 it_behaves_like "it relays relayables", Comment
end end
@ -241,8 +241,49 @@ describe Diaspora::Federation::Receive do
let(:entity) { like_entity } let(:entity) { like_entity }
it_behaves_like "it ignores existing object received twice", Like 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 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 end
describe ".message" do describe ".message" do
@ -408,7 +449,7 @@ describe Diaspora::Federation::Receive do
let(:entity) { poll_participation_entity } let(:entity) { poll_participation_entity }
it_behaves_like "it ignores existing object received twice", PollParticipation 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 it_behaves_like "it relays relayables", PollParticipation
end end

View file

@ -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(:remote_object_on_local_parent) { FactoryGirl.create(:like, target: local_parent, author: remote_raphael) }
let(:relayable) { Like::Generator.new(alice, status).build } let(:relayable) { Like::Generator.new(alice, status).build }
end 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 end

View file

@ -15,7 +15,7 @@ shared_examples_for "it ignores existing object received twice" do |klass|
end end
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 it "saves the relayable if the author is not ignored" do
Diaspora::Federation::Receive.perform(entity) Diaspora::Federation::Receive.perform(entity)