extract mappings between diaspora and the federation gem

This commit is contained in:
Benjamin Neff 2016-06-20 06:15:41 +02:00
parent 7bdf4921fd
commit 65f03b2d08
9 changed files with 184 additions and 140 deletions

View file

@ -74,7 +74,7 @@ DiasporaFederation.configure do |config|
end end
on :fetch_related_entity do |entity_type, guid| on :fetch_related_entity do |entity_type, guid|
entity = entity_type.constantize.find_by(guid: guid) entity = Diaspora::Federation::Mappings.model_class_for(entity_type).find_by(guid: guid)
Diaspora::Federation::Entities.related_entity(entity) if entity Diaspora::Federation::Entities.related_entity(entity) if entity
end end
@ -97,39 +97,13 @@ DiasporaFederation.configure do |config|
when DiasporaFederation::Entities::Retraction when DiasporaFederation::Entities::Retraction
Diaspora::Federation::Receive.retraction(entity, recipient_id) Diaspora::Federation::Receive.retraction(entity, recipient_id)
else else
persisted = case entity persisted = Diaspora::Federation::Receive.perform(entity)
when DiasporaFederation::Entities::Comment
Diaspora::Federation::Receive.comment(entity)
when DiasporaFederation::Entities::Contact
Diaspora::Federation::Receive.contact(entity)
when DiasporaFederation::Entities::Conversation
Diaspora::Federation::Receive.conversation(entity)
when DiasporaFederation::Entities::Like
Diaspora::Federation::Receive.like(entity)
when DiasporaFederation::Entities::Message
Diaspora::Federation::Receive.message(entity)
when DiasporaFederation::Entities::Participation
Diaspora::Federation::Receive.participation(entity)
when DiasporaFederation::Entities::Photo
Diaspora::Federation::Receive.photo(entity)
when DiasporaFederation::Entities::PollParticipation
Diaspora::Federation::Receive.poll_participation(entity)
when DiasporaFederation::Entities::Profile
Diaspora::Federation::Receive.profile(entity)
when DiasporaFederation::Entities::Reshare
Diaspora::Federation::Receive.reshare(entity)
when DiasporaFederation::Entities::StatusMessage
Diaspora::Federation::Receive.status_message(entity)
else
raise DiasporaFederation::Entity::UnknownEntity, "unknown entity: #{entity.class}"
end
Workers::ReceiveLocal.perform_async(persisted.class.to_s, persisted.id, [recipient_id].compact) if persisted Workers::ReceiveLocal.perform_async(persisted.class.to_s, persisted.id, [recipient_id].compact) if persisted
end end
end end
on :fetch_public_entity do |entity_type, guid| on :fetch_public_entity do |entity_type, guid|
entity = entity_type.constantize.find_by(guid: guid, public: true) entity = Diaspora::Federation::Mappings.model_class_for(entity_type).find_by(guid: guid, public: true)
Diaspora::Federation::Entities.post(entity) if entity.is_a? Post Diaspora::Federation::Entities.post(entity) if entity.is_a? Post
end end

View file

@ -12,4 +12,5 @@ end
require "diaspora/federation/dispatcher" require "diaspora/federation/dispatcher"
require "diaspora/federation/entities" require "diaspora/federation/entities"
require "diaspora/federation/mappings"
require "diaspora/federation/receive" require "diaspora/federation/receive"

View file

@ -2,23 +2,7 @@ module Diaspora
module Federation module Federation
module Entities module Entities
def self.build(entity) def self.build(entity)
case entity public_send(Mappings.builder_for(entity.class), entity)
when AccountDeletion then account_deletion(entity)
when Comment then comment(entity)
when Contact then contact(entity)
when Conversation then conversation(entity)
when Like then like(entity)
when Message then message(entity)
when Participation then participation(entity)
when Photo then photo(entity)
when PollParticipation then poll_participation(entity)
when Profile then profile(entity)
when Reshare then reshare(entity)
when Retraction then build_retraction(entity)
when StatusMessage then status_message(entity)
else
raise DiasporaFederation::Entity::UnknownEntity, "unknown entity: #{entity.class}"
end
end end
def self.build_retraction(retraction) def self.build_retraction(retraction)
@ -85,7 +69,7 @@ module Diaspora
guid: like.guid, guid: like.guid,
parent_guid: like.target.guid, parent_guid: like.target.guid,
positive: like.positive, positive: like.positive,
parent_type: like.target.class.base_class.to_s, parent_type: Mappings.entity_name_for(like.target),
author_signature: like.author_signature, author_signature: like.author_signature,
parent: related_entity(like.target) parent: related_entity(like.target)
) )
@ -117,7 +101,7 @@ module Diaspora
author: participation.diaspora_handle, author: participation.diaspora_handle,
guid: participation.guid, guid: participation.guid,
parent_guid: participation.target.guid, parent_guid: participation.target.guid,
parent_type: participation.target.class.base_class.to_s, parent_type: Mappings.entity_name_for(participation.target),
parent: related_entity(participation.target) parent: related_entity(participation.target)
) )
end end
@ -185,7 +169,7 @@ module Diaspora
def self.relayable_retraction(target, sender) def self.relayable_retraction(target, sender)
DiasporaFederation::Entities::RelayableRetraction.new( DiasporaFederation::Entities::RelayableRetraction.new(
target_guid: target.guid, target_guid: target.guid,
target_type: target.class.base_class.to_s, target_type: Mappings.entity_name_for(target),
target: related_entity(target), target: related_entity(target),
author: sender.diaspora_handle author: sender.diaspora_handle
) )
@ -206,17 +190,18 @@ module Diaspora
def self.retraction(target) def self.retraction(target)
case target case target
when Contact when Contact
# TODO: deprecated
author = target.user.diaspora_handle author = target.user.diaspora_handle
DiasporaFederation::Entities::Retraction.new( DiasporaFederation::Entities::Retraction.new(
target_guid: target.user.guid, target_guid: target.user.guid,
target_type: Person.to_s, target_type: "Person",
target: DiasporaFederation::Entities::RelatedEntity.new(author: author, local: true), target: DiasporaFederation::Entities::RelatedEntity.new(author: author, local: true),
author: author author: author
) )
else else
DiasporaFederation::Entities::Retraction.new( DiasporaFederation::Entities::Retraction.new(
target_guid: target.guid, target_guid: target.guid,
target_type: target.class.base_class.to_s, target_type: Mappings.entity_name_for(target),
target: related_entity(target), target: related_entity(target),
author: target.diaspora_handle author: target.diaspora_handle
) )
@ -227,7 +212,7 @@ module Diaspora
def self.signed_retraction(target, sender) def self.signed_retraction(target, sender)
DiasporaFederation::Entities::SignedRetraction.new( DiasporaFederation::Entities::SignedRetraction.new(
target_guid: target.guid, target_guid: target.guid,
target_type: target.class.base_class.to_s, target_type: Mappings.entity_name_for(target),
target: related_entity(target), target: related_entity(target),
author: sender.diaspora_handle author: sender.diaspora_handle
) )

View file

@ -0,0 +1,81 @@
module Diaspora
module Federation
module Mappings
# used in Diaspora::Federation::Receive
def self.receiver_for(federation_class)
fetch_from(ENTITY_RECEIVERS, federation_class)
end
# used in Diaspora::Federation::Entities
def self.builder_for(diaspora_class)
fetch_from(ENTITY_BUILDERS, diaspora_class)
end
def self.model_class_for(entity_name)
fetch_from(ENTITY_MODELS, entity_name)
end
def self.entity_name_for(model)
fetch_from(ENTITY_NAMES, model.class.base_class)
end
private_class_method def self.fetch_from(mapping, key)
mapping.fetch(key) { raise DiasporaFederation::Entity::UnknownEntity, "unknown entity: #{key}" }
end
ENTITY_RECEIVERS = {
DiasporaFederation::Entities::Comment => :comment,
DiasporaFederation::Entities::Contact => :contact,
DiasporaFederation::Entities::Conversation => :conversation,
DiasporaFederation::Entities::Like => :like,
DiasporaFederation::Entities::Message => :message,
DiasporaFederation::Entities::Participation => :participation,
DiasporaFederation::Entities::Photo => :photo,
DiasporaFederation::Entities::PollParticipation => :poll_participation,
DiasporaFederation::Entities::Profile => :profile,
DiasporaFederation::Entities::Reshare => :reshare,
DiasporaFederation::Entities::StatusMessage => :status_message
}.freeze
ENTITY_BUILDERS = {
AccountDeletion => :account_deletion,
Comment => :comment,
Contact => :contact,
Conversation => :conversation,
Like => :like,
Message => :message,
Participation => :participation,
Photo => :photo,
PollParticipation => :poll_participation,
Profile => :profile,
Reshare => :reshare,
Retraction => :build_retraction,
StatusMessage => :status_message
}.freeze
ENTITY_MODELS = {
"Comment" => Comment,
"Conversation" => Conversation,
"Like" => Like,
"Participation" => Participation,
"PollParticipation" => PollParticipation,
"Photo" => Photo,
"Poll" => Poll,
"Post" => Post,
# TODO: deprecated
"Person" => Person,
"Reshare" => Post,
"StatusMessage" => Post
}.freeze
ENTITY_NAMES = {
Comment => "Comment",
Like => "Like",
Participation => "Participation",
PollParticipation => "PollParticipation",
Photo => "Photo",
Post => "Post"
}.freeze
end
end
end

View file

@ -3,6 +3,10 @@ module Diaspora
module Receive module Receive
extend Diaspora::Logging extend Diaspora::Logging
def self.perform(entity)
public_send(Mappings.receiver_for(entity.class), entity)
end
def self.account_deletion(entity) def self.account_deletion(entity)
AccountDeletion.create!(person: author_of(entity), diaspora_handle: entity.author) AccountDeletion.create!(person: author_of(entity), diaspora_handle: entity.author)
end end
@ -49,7 +53,7 @@ module Diaspora
author: author_of(entity), author: author_of(entity),
guid: entity.guid, guid: entity.guid,
positive: entity.positive, positive: entity.positive,
target: entity.parent_type.constantize.find_by(guid: entity.parent_guid) target: Mappings.model_class_for(entity.parent_type).find_by(guid: entity.parent_guid)
) )
end end
end end
@ -61,7 +65,7 @@ module Diaspora
def self.participation(entity) def self.participation(entity)
author = author_of(entity) author = author_of(entity)
ignore_existing_guid(Participation, entity.guid, author) do ignore_existing_guid(Participation, entity.guid, author) do
parent = entity.parent_type.constantize.find_by(guid: entity.parent_guid) parent = Mappings.model_class_for(entity.parent_type).find_by(guid: entity.parent_guid)
Participation.create!(author: author, guid: entity.guid, target: parent) if parent.author.local? Participation.create!(author: author, guid: entity.guid, target: parent) if parent.author.local?
end end
@ -134,7 +138,8 @@ module Diaspora
end end
def self.retraction(entity, recipient_id) def self.retraction(entity, recipient_id)
object = entity.target_type.constantize.where(guid: entity.target_guid).take! model_class = Diaspora::Federation::Mappings.model_class_for(entity.target_type)
object = model_class.where(guid: entity.target_guid).take!
case object case object
when Person when Person

View file

@ -346,38 +346,35 @@ describe "diaspora federation callbacks" do
DiasporaFederation.callbacks.trigger(:receive_entity, retraction, 42) DiasporaFederation.callbacks.trigger(:receive_entity, retraction, 42)
end end
%i(comment contact conversation like message participation photo it "receives a entity" do
poll_participation profile reshare status_message).each do |entity| received = FactoryGirl.build(:status_message_entity)
it "receives a #{entity}" do persisted = FactoryGirl.create(:status_message)
received = FactoryGirl.build("#{entity}_entity")
persisted = FactoryGirl.create(entity)
expect(Diaspora::Federation::Receive).to receive(entity).with(received).and_return(persisted) expect(Diaspora::Federation::Receive).to receive(:perform).with(received).and_return(persisted)
expect(Workers::ReceiveLocal).to receive(:perform_async).with(persisted.class.to_s, persisted.id, []) expect(Workers::ReceiveLocal).to receive(:perform_async).with(persisted.class.to_s, persisted.id, [])
DiasporaFederation.callbacks.trigger(:receive_entity, received, nil) DiasporaFederation.callbacks.trigger(:receive_entity, received, nil)
end end
it "receives a #{entity} for a recipient" do it "receives a entity for a recipient" do
received = FactoryGirl.build("#{entity}_entity") received = FactoryGirl.build(:status_message_entity)
persisted = FactoryGirl.create(entity) persisted = FactoryGirl.create(:status_message)
expect(Diaspora::Federation::Receive).to receive(entity).with(received).and_return(persisted) expect(Diaspora::Federation::Receive).to receive(:perform).with(received).and_return(persisted)
expect(Workers::ReceiveLocal).to receive(:perform_async).with(persisted.class.to_s, persisted.id, [42]) expect(Workers::ReceiveLocal).to receive(:perform_async).with(persisted.class.to_s, persisted.id, [42])
DiasporaFederation.callbacks.trigger(:receive_entity, received, 42) DiasporaFederation.callbacks.trigger(:receive_entity, received, 42)
end end
it "does not trigger a ReceiveLocal job if Receive.#{entity} returned nil" do it "does not trigger a ReceiveLocal job if Receive.perform returned nil" do
received = FactoryGirl.build("#{entity}_entity") received = FactoryGirl.build(:status_message_entity)
expect(Diaspora::Federation::Receive).to receive(entity).with(received).and_return(nil) expect(Diaspora::Federation::Receive).to receive(:perform).with(received).and_return(nil)
expect(Workers::ReceiveLocal).not_to receive(:perform_async) expect(Workers::ReceiveLocal).not_to receive(:perform_async)
DiasporaFederation.callbacks.trigger(:receive_entity, received, nil) DiasporaFederation.callbacks.trigger(:receive_entity, received, nil)
end end
end end
end
describe ":fetch_public_entity" do describe ":fetch_public_entity" do
it "fetches a Post" do it "fetches a Post" do

View file

@ -59,9 +59,10 @@ describe "attack vectors", type: :request do
it "should not receive contact retractions from another person" do it "should not receive contact retractions from another person" do
# we are banking on bob being friends with alice and eve # we are banking on bob being friends with alice and eve
# here, alice is trying to disconnect bob and eve # here, alice is trying to disconnect bob and eve
expect(bob.contacts(true).find_by(person_id: eve.person.id)).to be_sharing contact = bob.contacts(true).find_by(person_id: eve.person.id)
expect(contact).to be_sharing
post_message(generate_xml(Diaspora::Federation::Entities.retraction(eve.person), alice, bob), bob) post_message(generate_xml(Diaspora::Federation::Entities.retraction(contact), alice, bob), bob)
expect(bob.contacts(true).find_by(person_id: eve.person.id)).to be_sharing expect(bob.contacts(true).find_by(person_id: eve.person.id)).to be_sharing
end end

View file

@ -20,7 +20,7 @@ describe Diaspora::Federation::Receive do
let(:comment_entity) { FactoryGirl.build(:comment_entity, author: sender.diaspora_handle, parent_guid: post.guid) } let(:comment_entity) { FactoryGirl.build(:comment_entity, author: sender.diaspora_handle, parent_guid: post.guid) }
it "saves the comment" do it "saves the comment" do
received = Diaspora::Federation::Receive.comment(comment_entity) received = Diaspora::Federation::Receive.perform(comment_entity)
comment = Comment.find_by!(guid: comment_entity.guid) comment = Comment.find_by!(guid: comment_entity.guid)
@ -31,7 +31,7 @@ describe Diaspora::Federation::Receive do
end end
it "attaches the comment to the post" do it "attaches the comment to the post" do
Diaspora::Federation::Receive.comment(comment_entity) Diaspora::Federation::Receive.perform(comment_entity)
comment = Comment.find_by!(guid: comment_entity.guid) comment = Comment.find_by!(guid: comment_entity.guid)
@ -40,9 +40,9 @@ describe Diaspora::Federation::Receive do
end end
let(:entity) { comment_entity } let(:entity) { comment_entity }
it_behaves_like "it ignores existing object received twice", Comment, :comment it_behaves_like "it ignores existing object received twice", Comment
it_behaves_like "it rejects if the parent author ignores the author", Comment, :comment it_behaves_like "it rejects if the parent author ignores the author", Comment
it_behaves_like "it relays relayables", Comment, :comment it_behaves_like "it relays relayables", Comment
end end
describe ".contact" do describe ".contact" do
@ -51,7 +51,7 @@ describe Diaspora::Federation::Receive do
} }
it "creates the contact if it doesn't exist" do it "creates the contact if it doesn't exist" do
received = Diaspora::Federation::Receive.contact(contact_entity) received = Diaspora::Federation::Receive.perform(contact_entity)
contact = alice.contacts.find_by!(person_id: sender.id) contact = alice.contacts.find_by!(person_id: sender.id)
@ -62,7 +62,7 @@ describe Diaspora::Federation::Receive do
it "updates the contact if it exists" do it "updates the contact if it exists" do
alice.contacts.find_or_initialize_by(person_id: sender.id, receiving: true, sharing: false).save! alice.contacts.find_or_initialize_by(person_id: sender.id, receiving: true, sharing: false).save!
received = Diaspora::Federation::Receive.contact(contact_entity) received = Diaspora::Federation::Receive.perform(contact_entity)
contact = alice.contacts.find_by!(person_id: sender.id) contact = alice.contacts.find_by!(person_id: sender.id)
@ -75,7 +75,7 @@ describe Diaspora::Federation::Receive do
expect_any_instance_of(Contact).not_to receive(:save!) expect_any_instance_of(Contact).not_to receive(:save!)
expect(Diaspora::Federation::Receive.contact(contact_entity)).to be_nil expect(Diaspora::Federation::Receive.perform(contact_entity)).to be_nil
end end
context "sharing=false" do context "sharing=false" do
@ -91,7 +91,7 @@ describe Diaspora::Federation::Receive do
it "disconnects, if currently connected" do it "disconnects, if currently connected" do
alice.contacts.find_or_initialize_by(person_id: sender.id, receiving: true, sharing: true).save! alice.contacts.find_or_initialize_by(person_id: sender.id, receiving: true, sharing: true).save!
received = Diaspora::Federation::Receive.contact(unshare_contact_entity) received = Diaspora::Federation::Receive.perform(unshare_contact_entity)
expect(received).to be_nil expect(received).to be_nil
contact = alice.contacts.find_by!(person_id: sender.id) contact = alice.contacts.find_by!(person_id: sender.id)
@ -101,7 +101,7 @@ describe Diaspora::Federation::Receive do
end end
it "does nothing, if already disconnected" do it "does nothing, if already disconnected" do
received = Diaspora::Federation::Receive.contact(unshare_contact_entity) received = Diaspora::Federation::Receive.perform(unshare_contact_entity)
expect(received).to be_nil expect(received).to be_nil
expect(alice.contacts.find_by(person_id: sender.id)).to be_nil expect(alice.contacts.find_by(person_id: sender.id)).to be_nil
end end
@ -129,7 +129,7 @@ describe Diaspora::Federation::Receive do
} }
it "saves the conversation" do it "saves the conversation" do
received = Diaspora::Federation::Receive.conversation(conversation_entity) received = Diaspora::Federation::Receive.perform(conversation_entity)
conv = Conversation.find_by!(guid: conversation_entity.guid) conv = Conversation.find_by!(guid: conversation_entity.guid)
@ -139,7 +139,7 @@ describe Diaspora::Federation::Receive do
end end
it "saves the message" do it "saves the message" do
Diaspora::Federation::Receive.conversation(conversation_entity) Diaspora::Federation::Receive.perform(conversation_entity)
conv = Conversation.find_by!(guid: conversation_entity.guid) conv = Conversation.find_by!(guid: conversation_entity.guid)
@ -150,7 +150,7 @@ describe Diaspora::Federation::Receive do
end end
it "creates appropriate visibilities" do it "creates appropriate visibilities" do
Diaspora::Federation::Receive.conversation(conversation_entity) Diaspora::Federation::Receive.perform(conversation_entity)
conv = Conversation.find_by!(guid: conversation_entity.guid) conv = Conversation.find_by!(guid: conversation_entity.guid)
@ -158,7 +158,7 @@ describe Diaspora::Federation::Receive do
expect(conv.participants).to include(alice.person, bob.person) expect(conv.participants).to include(alice.person, bob.person)
end end
it_behaves_like "it ignores existing object received twice", Conversation, :conversation do it_behaves_like "it ignores existing object received twice", Conversation do
let(:entity) { conversation_entity } let(:entity) { conversation_entity }
end end
end end
@ -167,7 +167,7 @@ describe Diaspora::Federation::Receive do
let(:like_entity) { FactoryGirl.build(:like_entity, author: sender.diaspora_handle, parent_guid: post.guid) } let(:like_entity) { FactoryGirl.build(:like_entity, author: sender.diaspora_handle, parent_guid: post.guid) }
it "saves the like" do it "saves the like" do
received = Diaspora::Federation::Receive.like(like_entity) received = Diaspora::Federation::Receive.perform(like_entity)
like = Like.find_by!(guid: like_entity.guid) like = Like.find_by!(guid: like_entity.guid)
@ -177,7 +177,7 @@ describe Diaspora::Federation::Receive do
end end
it "attaches the like to the post" do it "attaches the like to the post" do
Diaspora::Federation::Receive.like(like_entity) Diaspora::Federation::Receive.perform(like_entity)
like = Like.find_by!(guid: like_entity.guid) like = Like.find_by!(guid: like_entity.guid)
@ -186,9 +186,9 @@ describe Diaspora::Federation::Receive do
end end
let(:entity) { like_entity } let(:entity) { like_entity }
it_behaves_like "it ignores existing object received twice", Like, :like it_behaves_like "it ignores existing object received twice", Like
it_behaves_like "it rejects if the parent author ignores the author", Like, :like it_behaves_like "it rejects if the parent author ignores the author", Like
it_behaves_like "it relays relayables", Like, :like it_behaves_like "it relays relayables", Like
end end
describe ".message" do describe ".message" do
@ -208,7 +208,7 @@ describe Diaspora::Federation::Receive do
} }
it "saves the message" do it "saves the message" do
received = Diaspora::Federation::Receive.message(message_entity) received = Diaspora::Federation::Receive.perform(message_entity)
msg = Message.find_by!(guid: message_entity.guid) msg = Message.find_by!(guid: message_entity.guid)
@ -219,7 +219,7 @@ describe Diaspora::Federation::Receive do
end end
it "attaches the message to the conversation" do it "attaches the message to the conversation" do
msg = Diaspora::Federation::Receive.message(message_entity) msg = Diaspora::Federation::Receive.perform(message_entity)
conv = Conversation.find_by!(guid: conversation.guid) conv = Conversation.find_by!(guid: conversation.guid)
@ -228,8 +228,8 @@ describe Diaspora::Federation::Receive do
end end
let(:entity) { message_entity } let(:entity) { message_entity }
it_behaves_like "it ignores existing object received twice", Message, :message it_behaves_like "it ignores existing object received twice", Message
it_behaves_like "it relays relayables", Message, :message it_behaves_like "it relays relayables", Message
end end
describe ".participation" do describe ".participation" do
@ -238,7 +238,7 @@ describe Diaspora::Federation::Receive do
} }
it "saves the participation" do it "saves the participation" do
received = Diaspora::Federation::Receive.participation(participation_entity) received = Diaspora::Federation::Receive.perform(participation_entity)
participation = Participation.find_by!(guid: participation_entity.guid) participation = Participation.find_by!(guid: participation_entity.guid)
@ -247,7 +247,7 @@ describe Diaspora::Federation::Receive do
end end
it "attaches the participation to the post" do it "attaches the participation to the post" do
Diaspora::Federation::Receive.participation(participation_entity) Diaspora::Federation::Receive.perform(participation_entity)
participation = Participation.find_by!(guid: participation_entity.guid) participation = Participation.find_by!(guid: participation_entity.guid)
@ -263,12 +263,12 @@ describe Diaspora::Federation::Receive do
parent_guid: remote_post.guid parent_guid: remote_post.guid
) )
expect(Diaspora::Federation::Receive.participation(remote_participation)).to be_nil expect(Diaspora::Federation::Receive.perform(remote_participation)).to be_nil
expect(Participation.exists?(guid: remote_participation.guid)).to be_falsey expect(Participation.exists?(guid: remote_participation.guid)).to be_falsey
end end
it_behaves_like "it ignores existing object received twice", Participation, :participation do it_behaves_like "it ignores existing object received twice", Participation do
let(:entity) { participation_entity } let(:entity) { participation_entity }
end end
end end
@ -277,7 +277,7 @@ describe Diaspora::Federation::Receive do
let(:photo_entity) { FactoryGirl.build(:photo_entity, author: sender.diaspora_handle) } let(:photo_entity) { FactoryGirl.build(:photo_entity, author: sender.diaspora_handle) }
it "saves the photo if it does not already exist" do it "saves the photo if it does not already exist" do
received = Diaspora::Federation::Receive.photo(photo_entity) received = Diaspora::Federation::Receive.perform(photo_entity)
photo = Photo.find_by!(guid: photo_entity.guid) photo = Photo.find_by!(guid: photo_entity.guid)
@ -288,13 +288,13 @@ describe Diaspora::Federation::Receive do
end end
it "updates the photo if it is already persisted" do it "updates the photo if it is already persisted" do
Diaspora::Federation::Receive.photo(photo_entity) Diaspora::Federation::Receive.perform(photo_entity)
photo = Photo.find_by!(guid: photo_entity.guid) photo = Photo.find_by!(guid: photo_entity.guid)
photo.remote_photo_name = "foobar.jpg" photo.remote_photo_name = "foobar.jpg"
photo.save photo.save
received = Diaspora::Federation::Receive.photo(photo_entity) received = Diaspora::Federation::Receive.perform(photo_entity)
photo.reload photo.reload
expect(received).to eq(photo) expect(received).to eq(photo)
@ -303,7 +303,7 @@ describe Diaspora::Federation::Receive do
end end
it "does not update the photo if the author mismatches" do it "does not update the photo if the author mismatches" do
Diaspora::Federation::Receive.photo(photo_entity) Diaspora::Federation::Receive.perform(photo_entity)
photo = Photo.find_by!(guid: photo_entity.guid) photo = Photo.find_by!(guid: photo_entity.guid)
photo.remote_photo_name = "foobar.jpg" photo.remote_photo_name = "foobar.jpg"
@ -311,7 +311,7 @@ describe Diaspora::Federation::Receive do
photo.save photo.save
expect { expect {
Diaspora::Federation::Receive.photo(photo_entity) Diaspora::Federation::Receive.perform(photo_entity)
}.to raise_error Diaspora::Federation::InvalidAuthor }.to raise_error Diaspora::Federation::InvalidAuthor
photo.reload photo.reload
@ -333,7 +333,7 @@ describe Diaspora::Federation::Receive do
} }
it "saves the poll participation" do it "saves the poll participation" do
received = Diaspora::Federation::Receive.poll_participation(poll_participation_entity) received = Diaspora::Federation::Receive.perform(poll_participation_entity)
poll_participation = PollParticipation.find_by!(guid: poll_participation_entity.guid) poll_participation = PollParticipation.find_by!(guid: poll_participation_entity.guid)
@ -343,7 +343,7 @@ describe Diaspora::Federation::Receive do
end end
it "attaches the poll participation to the poll" do it "attaches the poll participation to the poll" do
Diaspora::Federation::Receive.poll_participation(poll_participation_entity) Diaspora::Federation::Receive.perform(poll_participation_entity)
poll_participation = PollParticipation.find_by!(guid: poll_participation_entity.guid) poll_participation = PollParticipation.find_by!(guid: poll_participation_entity.guid)
@ -352,16 +352,16 @@ describe Diaspora::Federation::Receive do
end end
let(:entity) { poll_participation_entity } let(:entity) { poll_participation_entity }
it_behaves_like "it ignores existing object received twice", PollParticipation, :poll_participation it_behaves_like "it ignores existing object received twice", PollParticipation
it_behaves_like "it rejects if the parent author ignores the author", PollParticipation, :poll_participation it_behaves_like "it rejects if the parent author ignores the author", PollParticipation
it_behaves_like "it relays relayables", PollParticipation, :poll_participation it_behaves_like "it relays relayables", PollParticipation
end end
describe ".profile" do describe ".profile" do
let(:profile_entity) { FactoryGirl.build(:profile_entity, author: sender.diaspora_handle) } let(:profile_entity) { FactoryGirl.build(:profile_entity, author: sender.diaspora_handle) }
it "updates the profile of the person" do it "updates the profile of the person" do
received = Diaspora::Federation::Receive.profile(profile_entity) received = Diaspora::Federation::Receive.perform(profile_entity)
profile = Profile.find(sender.profile.id) profile = Profile.find(sender.profile.id)
@ -381,7 +381,7 @@ describe Diaspora::Federation::Receive do
let(:reshare_entity) { FactoryGirl.build(:reshare_entity, author: sender.diaspora_handle, root_guid: post.guid) } let(:reshare_entity) { FactoryGirl.build(:reshare_entity, author: sender.diaspora_handle, root_guid: post.guid) }
it "saves the reshare" do it "saves the reshare" do
received = Diaspora::Federation::Receive.reshare(reshare_entity) received = Diaspora::Federation::Receive.perform(reshare_entity)
reshare = Reshare.find_by!(guid: reshare_entity.guid) reshare = Reshare.find_by!(guid: reshare_entity.guid)
@ -390,7 +390,7 @@ describe Diaspora::Federation::Receive do
end end
it "attaches the reshare to the post" do it "attaches the reshare to the post" do
Diaspora::Federation::Receive.reshare(reshare_entity) Diaspora::Federation::Receive.perform(reshare_entity)
reshare = Reshare.find_by!(guid: reshare_entity.guid) reshare = Reshare.find_by!(guid: reshare_entity.guid)
@ -399,7 +399,7 @@ describe Diaspora::Federation::Receive do
expect(reshare.created_at.iso8601).to eq(reshare_entity.created_at.iso8601) expect(reshare.created_at.iso8601).to eq(reshare_entity.created_at.iso8601)
end end
it_behaves_like "it ignores existing object received twice", Reshare, :reshare do it_behaves_like "it ignores existing object received twice", Reshare do
let(:entity) { reshare_entity } let(:entity) { reshare_entity }
end end
end end
@ -502,7 +502,7 @@ describe Diaspora::Federation::Receive do
let(:status_message_entity) { FactoryGirl.build(:status_message_entity, author: sender.diaspora_handle) } let(:status_message_entity) { FactoryGirl.build(:status_message_entity, author: sender.diaspora_handle) }
it "saves the status message" do it "saves the status message" do
received = Diaspora::Federation::Receive.status_message(status_message_entity) received = Diaspora::Federation::Receive.perform(status_message_entity)
status_message = StatusMessage.find_by!(guid: status_message_entity.guid) status_message = StatusMessage.find_by!(guid: status_message_entity.guid)
@ -519,19 +519,19 @@ describe Diaspora::Federation::Receive do
end end
it "returns the status message if it already exists" do it "returns the status message if it already exists" do
first = Diaspora::Federation::Receive.status_message(status_message_entity) first = Diaspora::Federation::Receive.perform(status_message_entity)
second = Diaspora::Federation::Receive.status_message(status_message_entity) second = Diaspora::Federation::Receive.perform(status_message_entity)
expect(second).not_to be_nil expect(second).not_to be_nil
expect(first).to eq(second) expect(first).to eq(second)
end end
it "does not change anything if the status message already exists" do it "does not change anything if the status message already exists" do
Diaspora::Federation::Receive.status_message(status_message_entity) Diaspora::Federation::Receive.perform(status_message_entity)
expect_any_instance_of(StatusMessage).not_to receive(:create_or_update) expect_any_instance_of(StatusMessage).not_to receive(:create_or_update)
Diaspora::Federation::Receive.status_message(status_message_entity) Diaspora::Federation::Receive.perform(status_message_entity)
end end
end end
@ -542,7 +542,7 @@ describe Diaspora::Federation::Receive do
} }
it "saves the status message" do it "saves the status message" do
received = Diaspora::Federation::Receive.status_message(status_message_entity) received = Diaspora::Federation::Receive.perform(status_message_entity)
status_message = StatusMessage.find_by!(guid: status_message_entity.guid) status_message = StatusMessage.find_by!(guid: status_message_entity.guid)
@ -563,7 +563,7 @@ describe Diaspora::Federation::Receive do
} }
it "saves the status message" do it "saves the status message" do
received = Diaspora::Federation::Receive.status_message(status_message_entity) received = Diaspora::Federation::Receive.perform(status_message_entity)
status_message = StatusMessage.find_by!(guid: status_message_entity.guid) status_message = StatusMessage.find_by!(guid: status_message_entity.guid)
@ -594,7 +594,7 @@ describe Diaspora::Federation::Receive do
} }
it "saves the status message and photos" do it "saves the status message and photos" do
received = Diaspora::Federation::Receive.status_message(status_message_entity) received = Diaspora::Federation::Receive.perform(status_message_entity)
status_message = StatusMessage.find_by!(guid: status_message_entity.guid) status_message = StatusMessage.find_by!(guid: status_message_entity.guid)
@ -609,7 +609,7 @@ describe Diaspora::Federation::Receive do
received_photo.text = "foobar" received_photo.text = "foobar"
received_photo.save! received_photo.save!
received = Diaspora::Federation::Receive.status_message(status_message_entity) received = Diaspora::Federation::Receive.perform(status_message_entity)
status_message = StatusMessage.find_by!(guid: status_message_entity.guid) status_message = StatusMessage.find_by!(guid: status_message_entity.guid)

View file

@ -1,23 +1,23 @@
require "spec_helper" require "spec_helper"
shared_examples_for "it ignores existing object received twice" do |klass, method| shared_examples_for "it ignores existing object received twice" do |klass|
it "return nil if the #{klass} already exists" do it "return nil if the #{klass} already exists" do
expect(Diaspora::Federation::Receive.public_send(method, entity)).not_to be_nil expect(Diaspora::Federation::Receive.public_send(:perform, entity)).not_to be_nil
expect(Diaspora::Federation::Receive.public_send(method, entity)).to be_nil expect(Diaspora::Federation::Receive.public_send(:perform, entity)).to be_nil
end end
it "does not change anything if the #{klass} already exists" do it "does not change anything if the #{klass} already exists" do
Diaspora::Federation::Receive.public_send(method, entity) Diaspora::Federation::Receive.public_send(:perform, entity)
expect_any_instance_of(klass).not_to receive(:create_or_update) expect_any_instance_of(klass).not_to receive(:create_or_update)
Diaspora::Federation::Receive.public_send(method, entity) Diaspora::Federation::Receive.public_send(:perform, entity)
end end
end end
shared_examples_for "it rejects if the parent author ignores the author" do |klass, method| shared_examples_for "it rejects if the parent 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.public_send(method, entity) Diaspora::Federation::Receive.public_send(:perform, entity)
expect(klass.find_by!(guid: entity.guid)).to be_instance_of(klass) expect(klass.find_by!(guid: entity.guid)).to be_instance_of(klass)
end end
@ -29,7 +29,7 @@ shared_examples_for "it rejects if the parent author ignores the author" do |kla
it "raises an error and does not save the relayable" do it "raises an error and does not save the relayable" do
expect { expect {
Diaspora::Federation::Receive.public_send(method, entity) Diaspora::Federation::Receive.public_send(:perform, entity)
}.to raise_error Diaspora::Federation::AuthorIgnored }.to raise_error Diaspora::Federation::AuthorIgnored
expect(klass.find_by(guid: entity.guid)).to be_nil expect(klass.find_by(guid: entity.guid)).to be_nil
@ -47,13 +47,13 @@ shared_examples_for "it rejects if the parent author ignores the author" do |kla
expect(dispatcher).to receive(:dispatch) expect(dispatcher).to receive(:dispatch)
expect { expect {
Diaspora::Federation::Receive.public_send(method, entity) Diaspora::Federation::Receive.public_send(:perform, entity)
}.to raise_error Diaspora::Federation::AuthorIgnored }.to raise_error Diaspora::Federation::AuthorIgnored
end end
end end
end end
shared_examples_for "it relays relayables" do |klass, method| shared_examples_for "it relays relayables" do |klass|
it "dispatches the received relayable" do it "dispatches the received relayable" do
expect(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch) do |parent_author, relayable| expect(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch) do |parent_author, relayable|
expect(parent_author).to eq(alice) expect(parent_author).to eq(alice)
@ -61,6 +61,6 @@ shared_examples_for "it relays relayables" do |klass, method|
expect(relayable.guid).to eq(entity.guid) expect(relayable.guid).to eq(entity.guid)
end end
Diaspora::Federation::Receive.public_send(method, entity) Diaspora::Federation::Receive.public_send(:perform, entity)
end end
end end