use RelatedEntity as target for all Retractions

This commit is contained in:
Benjamin Neff 2016-03-28 21:13:34 +02:00
parent 01d45e225d
commit f88a3abb3d
9 changed files with 90 additions and 30 deletions

View file

@ -69,7 +69,7 @@ module DiasporaFederation
# use only {Retraction} for receive
# @return [Retraction] instance as normal retraction
def to_retraction
Retraction.new(author: author, target_guid: target_guid, target_type: target_type)
Retraction.new(author: author, target_guid: target_guid, target_type: target_type, target: target)
end
private
@ -77,22 +77,12 @@ module DiasporaFederation
# @param [Nokogiri::XML::Element] root_node xml nodes
# @return [Retraction] instance
def self.populate_entity(root_node)
entity_data = Hash[class_props.map {|name, type|
[name, parse_element_from_node(name, type, root_node)]
}]
entity_data[:target] = fetch_target(entity_data[:target_type], entity_data[:target_guid])
entity_data = entity_data(root_node)
entity_data[:target] = Retraction.send(:fetch_target, entity_data[:target_type], entity_data[:target_guid])
new(entity_data).to_retraction
end
private_class_method :populate_entity
def self.fetch_target(target_type, target_guid)
DiasporaFederation.callbacks.trigger(:fetch_related_entity, target_type, target_guid).tap do |target|
raise TargetNotFound unless target
end
end
private_class_method :fetch_target
# It updates also the signatures with the keys of the author and the parent
# if the signatures are not there yet and if the keys are available.
#
@ -114,10 +104,6 @@ module DiasporaFederation
hash[:parent_author_signature] = SignedRetraction.sign_with_key(privkey, self)
end
end
# Raised, if the target of the {Retraction} was not found.
class TargetNotFound < RuntimeError
end
end
end
end

View file

@ -19,6 +19,39 @@ module DiasporaFederation
# A string describing the type of the target.
# @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
# @param [Nokogiri::XML::Element] root_node xml nodes
# @return [Retraction] instance
def self.populate_entity(root_node)
entity_data = entity_data(root_node)
entity_data[:target] = fetch_target(entity_data[:target_type], entity_data[:target_guid])
new(entity_data)
end
private_class_method :populate_entity
def self.fetch_target(target_type, target_guid)
DiasporaFederation.callbacks.trigger(:fetch_related_entity, target_type, target_guid).tap do |target|
raise TargetNotFound unless target
end
end
private_class_method :fetch_target
# Raised, if the target of the {Retraction} was not found.
class TargetNotFound < RuntimeError
end
end
end
end

View file

@ -30,10 +30,23 @@ 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
# use only {Retraction} for receive
# @return [Retraction] instance as normal retraction
def to_retraction
Retraction.new(author: author, target_guid: target_guid, target_type: target_type)
Retraction.new(author: author, target_guid: target_guid, target_type: target_type, target: target)
end
# Create signature for a retraction
@ -49,7 +62,9 @@ module DiasporaFederation
# @param [Nokogiri::XML::Element] root_node xml nodes
# @return [Retraction] instance
def self.populate_entity(root_node)
super(root_node).to_retraction
entity_data = entity_data(root_node)
entity_data[:target] = Retraction.send(:fetch_target, entity_data[:target_type], entity_data[:target_guid])
new(entity_data).to_retraction
end
private_class_method :populate_entity

View file

@ -199,14 +199,19 @@ module DiasporaFederation
# @param [Nokogiri::XML::Element] root_node xml nodes
# @return [Entity] instance
def self.populate_entity(root_node)
entity_data = Hash[class_props.map {|name, type|
[name, parse_element_from_node(name, type, root_node)]
}]
new(entity_data)
new(entity_data(root_node))
end
private_class_method :populate_entity
# @param [Nokogiri::XML::Element] root_node xml nodes
# @return [Hash] entity data
def self.entity_data(root_node)
Hash[class_props.map {|name, type|
[name, parse_element_from_node(name, type, root_node)]
}]
end
private_class_method :entity_data
# @param [String] name property name to parse
# @param [Class] type target type to parse
# @param [Nokogiri::XML::Element] root_node XML node to parse

View file

@ -172,12 +172,14 @@ module DiasporaFederation
author { generate(:diaspora_id) }
target_guid { generate(:guid) }
target_type "Post"
target { FactoryGirl.build(:related_entity, author: author) }
end
factory :signed_retraction_entity, class: DiasporaFederation::Entities::SignedRetraction do
author { generate(:diaspora_id) }
target_guid { generate(:guid) }
target_type "Post"
target { FactoryGirl.build(:related_entity, author: author) }
end
factory :poll_answer_entity, class: DiasporaFederation::Entities::PollAnswer do

View file

@ -7,8 +7,8 @@ module DiasporaFederation
rule :author, %i(not_empty diaspora_id)
rule :target_guid, :guid
rule :target_type, :not_empty
rule :target, :not_nil
end
end
end

View file

@ -6,8 +6,8 @@ module DiasporaFederation
include Validation
rule :target_guid, :guid
rule :target_type, :not_empty
rule :target, :not_nil
rule :author, %i(not_empty diaspora_id)
end

View file

@ -1,6 +1,15 @@
module DiasporaFederation
describe Entities::Retraction do
let(:data) { FactoryGirl.attributes_for(:retraction_entity) }
let(:target) { FactoryGirl.create(:post, author: bob) }
let(:target_entity) { FactoryGirl.build(:related_entity, author: bob.diaspora_id) }
let(:data) {
FactoryGirl.attributes_for(
:retraction_entity,
target_guid: target.guid,
target_type: target.entity_type,
target: target_entity
)
}
let(:xml) {
<<-XML
@ -12,7 +21,7 @@ module DiasporaFederation
XML
}
it_behaves_like "an Entity subclass"
it_behaves_like "an Entity subclass", [:target]
it_behaves_like "an XML Entity"
end

View file

@ -1,6 +1,16 @@
module DiasporaFederation
describe Entities::SignedRetraction do
let(:data) { FactoryGirl.build(:signed_retraction_entity, author: alice.diaspora_id).send(:xml_elements) }
let(:target) { FactoryGirl.create(:post, author: alice) }
let(:target_entity) { FactoryGirl.build(:related_entity, author: alice.diaspora_id) }
let(:data) {
FactoryGirl.build(
:signed_retraction_entity,
author: alice.diaspora_id,
target_guid: target.guid,
target_type: target.entity_type,
target: target_entity
).send(:xml_elements).merge(target: target_entity)
}
let(:xml) {
<<-XML
@ -13,7 +23,7 @@ module DiasporaFederation
XML
}
it_behaves_like "an Entity subclass"
it_behaves_like "an Entity subclass", [:target]
it_behaves_like "an XML Entity", [:target_author_signature]