Unverified Commit 84b89d55 authored by Benjamin Neff's avatar Benjamin Neff
Browse files

Relay likes for comments

parent 5f5d8c5e
Loading
Loading
Loading
Loading
+9 −9
Original line number Diff line number Diff line
@@ -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.
+13 −7
Original line number Diff line number Diff line
@@ -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<Person>]
    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
+44 −3
Original line number Diff line number Diff line
@@ -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

+13 −0
Original line number Diff line number Diff line
@@ -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
+1 −1
Original line number Diff line number Diff line
@@ -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)