use RelatedEntity as normal nested entity, but don't add it to xml

This commit is contained in:
Benjamin Neff 2016-06-05 04:13:55 +02:00
parent e5203182bf
commit 4bde03e2de
18 changed files with 59 additions and 60 deletions

View file

@ -23,6 +23,11 @@ module DiasporaFederation
# if the entity also have a parent (Comment or Like), +nil+ if it has no parent
# @return [RelatedEntity] parent entity
entity :parent, Entities::RelatedEntity, default: nil
# never add {RelatedEntity} to xml
def to_xml
nil
end
end
end
end

View file

@ -10,10 +10,6 @@ module DiasporaFederation
# digest instance used for signing
DIGEST = OpenSSL::Digest::SHA256.new
# parent entity
# @return [RelatedEntity] parent entity
attr_reader :parent
# order from the parsed xml for signature
# @return [Array] order from xml
attr_reader :xml_order
@ -49,20 +45,24 @@ module DiasporaFederation
# This signature is required only when federation from upstream (parent) post author to
# downstream subscribers. This is the case when the parent author has to resend a relayable
# received from one of his subscribers to all others.
#
# @return [String] parent author signature
#
# @param [Entity] entity the entity in which it is included
def self.included(entity)
entity.class_eval do
# @!attribute [r] parent
# meta information about the parent object
# @return [RelatedEntity] parent entity
#
# @param [Entity] klass the entity in which it is included
def self.included(klass)
klass.class_eval do
property :author, xml_name: :diaspora_handle
property :guid
property :parent_guid
property :author_signature, default: nil
property :parent_author_signature, default: nil
entity :parent, Entities::RelatedEntity
end
entity.extend ParseXML
klass.extend ParseXML
end
# Initializes a new relayable Entity with order and additional xml elements
@ -72,7 +72,6 @@ module DiasporaFederation
# @param [Hash] additional_xml_elements additional xml elements
# @see DiasporaFederation::Entity#initialize
def initialize(data, xml_order=nil, additional_xml_elements={})
@parent = data[:parent] if data
@xml_order = xml_order
@additional_xml_elements = additional_xml_elements

View file

@ -53,18 +53,10 @@ module DiasporaFederation
# @return [String] target author signature
property :target_author_signature, default: nil
# target entity
# @return [RelatedEntity] target entity
attr_reader :target
# Initializes a new relayable retraction entity
#
# @param [Hash] data entity data
# @see DiasporaFederation::Entity#initialize
def initialize(data)
@target = data[:target] if data
super(data)
end
# @!attribute [r] target
# target entity
# @return [RelatedEntity] target entity
entity :target, Entities::RelatedEntity
# use only {Retraction} for receive
# @return [Retraction] instance as normal retraction

View file

@ -20,18 +20,10 @@ module DiasporaFederation
# @return [String] target type
property :target_type, xml_name: :type
# target entity
# @return [RelatedEntity] target entity
attr_reader :target
# Initializes a new retraction entity
#
# @param [Hash] data entity data
# @see DiasporaFederation::Entity#initialize
def initialize(data)
@target = data[:target] if data
super(data)
end
# @!attribute [r] target
# target entity
# @return [RelatedEntity] target entity
entity :target, Entities::RelatedEntity
def sender_valid?(sender)
case target_type

View file

@ -30,18 +30,10 @@ module DiasporaFederation
# @return [String] author signature
property :target_author_signature, default: nil
# target entity
# @return [RelatedEntity] target entity
attr_reader :target
# Initializes a new signed retraction entity
#
# @param [Hash] data entity data
# @see DiasporaFederation::Entity#initialize
def initialize(data)
@target = data[:target] if data
super(data)
end
# @!attribute [r] target
# target entity
# @return [RelatedEntity] target entity
entity :target, Entities::RelatedEntity
# use only {Retraction} for receive
# @return [Retraction] instance as normal retraction

View file

@ -225,7 +225,8 @@ module DiasporaFederation
else
# call #to_xml for each item and append to root
[*value].compact.each do |item|
root_element << item.to_xml
child = item.to_xml
root_element << child if child
end
end
end

View file

@ -26,6 +26,11 @@ module DiasporaFederation
property :qwer, xml_name: :asdf
end
class TestEntityWithRelatedEntity < DiasporaFederation::Entity
property :test
entity :parent, RelatedEntity
end
class Entity < DiasporaFederation::Entity
property :test
end

View file

@ -21,7 +21,7 @@ XML
}
let(:string) { "Comment:#{data[:guid]}:#{parent.guid}" }
it_behaves_like "an Entity subclass", [:parent]
it_behaves_like "an Entity subclass"
it_behaves_like "an XML Entity", [:created_at]

View file

@ -27,7 +27,7 @@ XML
}
let(:string) { "Like:#{data[:guid]}:Post:#{parent.guid}" }
it_behaves_like "an Entity subclass", [:parent]
it_behaves_like "an Entity subclass"
it_behaves_like "an XML Entity"

View file

@ -23,7 +23,7 @@ XML
}
let(:string) { "Message:#{data[:guid]}:#{parent.guid}" }
it_behaves_like "an Entity subclass", [:parent]
it_behaves_like "an Entity subclass"
it_behaves_like "an XML Entity"

View file

@ -26,7 +26,7 @@ XML
}
let(:string) { "Participation:#{data[:guid]}:Post:#{parent.guid}" }
it_behaves_like "an Entity subclass", [:parent]
it_behaves_like "an Entity subclass"
it_behaves_like "an XML Entity"

View file

@ -25,7 +25,7 @@ XML
}
let(:string) { "PollParticipation:#{data[:guid]}:#{parent.guid}" }
it_behaves_like "an Entity subclass", [:parent]
it_behaves_like "an Entity subclass"
it_behaves_like "an XML Entity"

View file

@ -4,5 +4,11 @@ module DiasporaFederation
let(:string) { "RelatedEntity" }
it_behaves_like "an Entity subclass"
describe "#to_xml" do
it "returns nil" do
expect(described_class.new(data).to_xml).to be_nil
end
end
end
end

View file

@ -34,9 +34,9 @@ XML
}
let(:string) { "RelayableRetraction:#{data[:target_type]}:#{data[:target_guid]}" }
it_behaves_like "an Entity subclass", [:target]
it_behaves_like "an Entity subclass"
it_behaves_like "an XML Entity", %i(parent_author_signature target_author_signature)
it_behaves_like "an XML Entity", %i(parent_author_signature target_author_signature target)
it_behaves_like "a retraction"

View file

@ -22,7 +22,7 @@ XML
}
let(:string) { "Retraction:#{data[:target_type]}:#{data[:target_guid]}" }
it_behaves_like "an Entity subclass", [:target]
it_behaves_like "an Entity subclass"
it_behaves_like "an XML Entity"

View file

@ -24,7 +24,7 @@ XML
}
let(:string) { "SignedRetraction:#{data[:target_type]}:#{data[:target_guid]}" }
it_behaves_like "an Entity subclass", [:target]
it_behaves_like "an Entity subclass"
it_behaves_like "an XML Entity", [:target_author_signature]

View file

@ -282,6 +282,13 @@ XML
expect(xml.xpath("other_entity")).to have_exactly(2).items
end
it "is not added to xml if #to_xml returns nil" do
entity = Entities::TestEntityWithRelatedEntity.new(test: "test", parent: FactoryGirl.build(:related_entity))
xml = entity.to_xml
expect(xml.children).to have_exactly(1).items
xml.children.first.name = "test"
end
it "instantiates nested entities if provided as hash" do
entity = Entities::TestNestedEntity.new(nested_hash)

View file

@ -1,10 +1,10 @@
shared_examples "an Entity subclass" do |ignored_props=[]|
shared_examples "an Entity subclass" do
it "should be an Entity" do
expect(described_class).to be < DiasporaFederation::Entity
end
it "has its properties set" do
expect(described_class.class_props.keys).to include(*(data.keys - ignored_props))
expect(described_class.class_props.keys).to include(*data.keys)
end
context "behaviour" do
@ -26,7 +26,7 @@ shared_examples "an Entity subclass" do |ignored_props=[]|
describe "#to_h" do
it "should return a hash with nested data" do
expected_data = Hash[data.reject {|key, _| ignored_props.include?(key) }.map {|key, value|
expected_data = Hash[data.map {|key, value|
if [String, TrueClass, FalseClass, Fixnum, Time, NilClass].include?(value.class)
[key, value]
elsif value.instance_of?(Array)
@ -76,7 +76,7 @@ shared_examples "an XML Entity" do |ignored_props=[]|
if value.nil?
expect(parsed_value).to be_nil
elsif type == String
expect(parsed_value).to eq(value.to_s)
expect(parsed_value.to_s).to eq(value.to_s)
elsif type.instance_of?(Array)
value.each_with_index {|entity, index| check_entity(entity, parsed_value[index]) }
elsif type.ancestors.include?(DiasporaFederation::Entity)