diff --git a/app/workers/receive_base.rb b/app/workers/receive_base.rb index 7c280b550..1fbccefa6 100644 --- a/app/workers/receive_base.rb +++ b/app/workers/receive_base.rb @@ -19,6 +19,7 @@ module Workers DiasporaFederation::Salmon::InvalidSignature, DiasporaFederation::Salmon::InvalidAlgorithm, DiasporaFederation::Salmon::InvalidEncoding, + Diaspora::Federation::AuthorIgnored, # TODO: deprecated DiasporaFederation::Salmon::MissingMagicEnvelope, DiasporaFederation::Salmon::MissingAuthor, @@ -27,10 +28,7 @@ module Workers logger.warn "don't retry for error: #{e.class}" rescue ActiveRecord::RecordInvalid => e logger.warn "failed to save received object: #{e.record.errors.full_messages}" - raise e unless [ - "already been taken", - "is ignored by the post author" - ].any? {|reason| e.message.include? reason } + raise e unless e.message.include? "already been taken" end end end diff --git a/lib/diaspora/federation.rb b/lib/diaspora/federation.rb index f6ab41662..a3baae637 100644 --- a/lib/diaspora/federation.rb +++ b/lib/diaspora/federation.rb @@ -4,6 +4,10 @@ module Diaspora def self.xml(entity) DiasporaFederation::Salmon::XmlPayload.pack(entity) end + + # Raised, if author is ignored by the relayable parent author + class AuthorIgnored < RuntimeError + end end end diff --git a/lib/diaspora/federation/receive.rb b/lib/diaspora/federation/receive.rb index 4d7b42254..09a990cb2 100644 --- a/lib/diaspora/federation/receive.rb +++ b/lib/diaspora/federation/receive.rb @@ -15,10 +15,7 @@ module Diaspora created_at: entity.created_at, text: entity.text, commentable: Post.find_by(guid: entity.parent_guid) - ).tap do |comment| - comment.author_signature = entity.author_signature if comment.parent.author.local? - comment.save! - end + ).tap {|comment| save_relayable(comment, entity) } end def self.contact(entity) @@ -52,10 +49,7 @@ module Diaspora guid: entity.guid, positive: entity.positive, target: entity.parent_type.constantize.find_by(guid: entity.parent_guid) - ).tap do |like| - like.author_signature = entity.author_signature if like.parent.author.local? - like.save! - end + ).tap {|like| save_relayable(like, entity) } end def self.message(entity) @@ -85,8 +79,8 @@ module Diaspora poll: Poll.find_by(guid: entity.parent_guid) ).tap do |poll_participation| poll_participation.poll_answer_guid = entity.poll_answer_guid - poll_participation.author_signature = entity.author_signature if poll_participation.parent.author.local? - poll_participation.save! + + save_relayable(poll_participation, entity) end end @@ -170,6 +164,22 @@ module Diaspora end end end + + def self.save_relayable(relayable, entity) + retract_if_author_ignored(relayable) + + relayable.author_signature = entity.author_signature if relayable.parent.author.local? + relayable.save! + end + + def self.retract_if_author_ignored(relayable) + parent_author = relayable.parent.author + return unless parent_author.local? && parent_author.owner.ignored_people.include?(relayable.author) + + # TODO: send retraction + + raise Diaspora::Federation::AuthorIgnored + end end end end diff --git a/lib/diaspora/relayable.rb b/lib/diaspora/relayable.rb index 2c6aa7962..9419705b7 100644 --- a/lib/diaspora/relayable.rb +++ b/lib/diaspora/relayable.rb @@ -30,15 +30,12 @@ module Diaspora end def author_is_not_ignored - if self.new_record? && self.parent.present? - post_author = self.parent.author - relayable_author = self.author - - if post_author.local? && post_author.owner.ignored_people.include?(relayable_author) - self.errors.add(:author_id, 'This person is ignored by the post author') - #post_author.owner.retract(self) - end + unless new_record? && parent.present? && parent.author.local? && + parent.author.owner.ignored_people.include?(author) + return end + + errors.add(:author_id, "This relayable author is ignored by the post author") end # @return [Boolean] true