<commit_before>module DiasporaFederation
  module Federation
    module Receiver
      # Common functionality for receivers
      class AbstractReceiver
        include Logging

        # Creates a new receiver
        # @param [MagicEnvelope] magic_envelope the received magic envelope
        # @param [Object] recipient_id the identifier of the recipient of a private message
        def initialize(magic_envelope, recipient_id=nil)
          @entity = magic_envelope.payload
          @sender = magic_envelope.sender
          @recipient_id = recipient_id
        end

        # Validates and receives the entity
        def receive
          validate_and_receive
        rescue => e # rubocop:disable Lint/RescueWithoutErrorClass
          logger.error "failed to receive #{entity}"
          raise e
        end

        private

        attr_reader :entity, :sender, :recipient_id

        def validate_and_receive
          validate
          DiasporaFederation.callbacks.trigger(:receive_entity, entity, sender, recipient_id)
          logger.info "successfully received #{entity} from person #{sender}#{" for #{recipient_id}" if recipient_id}"
          fetch_linked_entities_from_text
        end

        def validate
          raise InvalidSender, "invalid sender: #{sender}" unless sender_valid?
        end

        def sender_valid?
          if entity.respond_to?(:sender_valid?)
            entity.sender_valid?(sender)
          else
            sender == entity.author
          end
        end

        def fetch_linked_entities_from_text
          DiasporaUrlParser.fetch_linked_entities(entity.text) if entity.respond_to?(:text) && entity.text
        end
      end
    end
  end
end<commit_msg>Fetch linked entities before saving<commit_after>module DiasporaFederation
  module Federation
    module Receiver
      # Common functionality for receivers
      class AbstractReceiver
        include Logging

        # Creates a new receiver
        # @param [MagicEnvelope] magic_envelope the received magic envelope
        # @param [Object] recipient_id the identifier of the recipient of a private message
        def initialize(magic_envelope, recipient_id=nil)
          @entity = magic_envelope.payload
          @sender = magic_envelope.sender
          @recipient_id = recipient_id
        end

        # Validates and receives the entity
        def receive
          validate_and_receive
        rescue => e # rubocop:disable Lint/RescueWithoutErrorClass
          logger.error "failed to receive #{entity}"
          raise e
        end

        private

        attr_reader :entity, :sender, :recipient_id

        def validate_and_receive
          validate
          fetch_linked_entities_from_text
          DiasporaFederation.callbacks.trigger(:receive_entity, entity, sender, recipient_id)
          logger.info "successfully received #{entity} from person #{sender}#{" for #{recipient_id}" if recipient_id}"
        end

        def validate
          raise InvalidSender, "invalid sender: #{sender}" unless sender_valid?
        end

        def sender_valid?
          if entity.respond_to?(:sender_valid?)
            entity.sender_valid?(sender)
          else
            sender == entity.author
          end
        end

        def fetch_linked_entities_from_text
          DiasporaUrlParser.fetch_linked_entities(entity.text) if entity.respond_to?(:text) && entity.text
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.05841526894158473, 'fraction_numerical': 0.0, 'mean_word_length': 2.3100478468899523, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6987012', 'n_tokens_mistral': 901, 'n_tokens_neox': 864, 'n_words': 249}