<commit_before>class ProcessFeedService < BaseService
  ACTIVITY_NS = 'http://activitystrea.ms/spec/1.0/'.freeze
  THREAD_NS   = 'http://purl.org/syndication/thread/1.0'.freeze

  # Create local statuses from an Atom feed
  # @param [String] body Atom feed
  # @param [Account] account Account this feed belongs to
  # @return [Enumerable] created statuses
  def call(body, account)
    xml = Nokogiri::XML(body)
    update_remote_profile_service.call(xml.at_xpath('/xmlns:feed/xmlns:author'), account) unless xml.at_xpath('/xmlns:feed').nil?
    xml.xpath('//xmlns:entry').reverse_each.map { |entry| process_entry(account, entry) }.compact
  end

  private

  def process_entry(account, entry)
    return unless [:note, :comment, :activity].include? object_type(entry)

    status = Status.find_by(uri: activity_id(entry))

    # If we already have a post and the verb is now "delete", we gotta delete it and move on!
    if !status.nil? && verb(entry) == :delete
      delete_post!(status)
      return
    end

    return unless status.nil?

    status = Status.new(uri: activity_id(entry), url: activity_link(entry), account: account, text: content(entry), created_at: published(entry), updated_at: updated(entry))

    if verb(entry) == :share
      add_reblog!(entry, status)
    elsif verb(entry) == :post
      if thread_id(entry).nil?
        add_post!(entry, status)
      else
        add_reply!(entry, status)
      end
    else
      return
    end

    # If we added a status, go through accounts it mentions and create respective relations
    # Also record all media attachments for the status and for the reblogged status if present
    unless status.new_record?
      record_remote_mentions(status, entry.xpath('./xmlns:link[@rel="mentioned"]'))
      record_remote_mentions(status.reblog, entry.at_xpath('./activity:object', activity: ACTIVITY_NS).xpath('./xmlns:link[@rel="mentioned"]')) if status.reblog?

      process_attachments(entry, status)
      process_attachments(entry.xpath('./activity:object', activity: ACTIVITY_NS), status.reblog) if status.reblog?

      DistributionWorker.perform_async(status.id)
      return status
    end
  end

  def record_remote_mentions(status, links)
    # Here we have to do a reverse lookup of local accounts by their URL!
    # It's not pretty at all! I really wish all these protocols sticked to
    # using acct:username@domain only! It would make things so much easier
    # and tidier

    links.each do |mention_link|
      href_val = mention_link.attribute('href').value

      next if href_val == 'http://activityschema.org/collection/public'

      href = Addressable::URI.parse(href_val)

      if TagManager.instance.local_domain?(href.host)
        # A local user is mentioned
        mentioned_account = Account.find_local(href.path.gsub('/users/', ''))

        unless mentioned_account.nil?
          mentioned_account.mentions.where(status: status).first_or_create(status: status)
          NotificationMailer.mention(mentioned_account, status).deliver_later unless mentioned_account.blocking?(status.account)
        end
      else
        # What to do about remote user?
        # This is kinda dodgy because URLs could change, we don't index them
        mentioned_account = Account.find_by(url: href.to_s)

        if mentioned_account.nil?
          mentioned_account = FetchRemoteAccountService.new.call(href)
        end

        unless mentioned_account.nil?
          mentioned_account.mentions.where(status: status).first_or_create(status: status)
        end
      end
    end
  end

  def process_attachments(entry, status)
    entry.xpath('./xmlns:link[@rel="enclosure"]').each do |enclosure_link|
      next if enclosure_link.attribute('href').nil?

      media = MediaAttachment.where(status: status, remote_url: enclosure_link.attribute('href').value).first

      next unless media.nil?

      begin
        media = MediaAttachment.new(account: status.account, status: status, remote_url: enclosure_link.attribute('href').value)
        media.file_remote_url = enclosure_link.attribute('href').value
        media.save
      rescue Paperclip::Errors::NotIdentifiedByImageMagickError
        Rails.logger.debug "Error saving attachment from #{enclosure_link.attribute('href').value}"
        next
      end
    end
  end

  def add_post!(_entry, status)
    status.save!
  end

  def add_reblog!(entry, status)
    status.reblog = find_original_status(entry, target_id(entry))

    if status.reblog.nil?
      status.reblog = fetch_remote_status(entry)
    end<commit_msg>Fix duplication of media attachments when a remote status reblogs a local one<commit_after>class ProcessFeedService < BaseService
  ACTIVITY_NS = 'http://activitystrea.ms/spec/1.0/'.freeze
  THREAD_NS   = 'http://purl.org/syndication/thread/1.0'.freeze

  # Create local statuses from an Atom feed
  # @param [String] body Atom feed
  # @param [Account] account Account this feed belongs to
  # @return [Enumerable] created statuses
  def call(body, account)
    xml = Nokogiri::XML(body)
    update_remote_profile_service.call(xml.at_xpath('/xmlns:feed/xmlns:author'), account) unless xml.at_xpath('/xmlns:feed').nil?
    xml.xpath('//xmlns:entry').reverse_each.map { |entry| process_entry(account, entry) }.compact
  end

  private

  def process_entry(account, entry)
    return unless [:note, :comment, :activity].include? object_type(entry)

    status = Status.find_by(uri: activity_id(entry))

    # If we already have a post and the verb is now "delete", we gotta delete it and move on!
    if !status.nil? && verb(entry) == :delete
      delete_post!(status)
      return
    end

    return unless status.nil?

    status = Status.new(uri: activity_id(entry), url: activity_link(entry), account: account, text: content(entry), created_at: published(entry), updated_at: updated(entry))

    if verb(entry) == :share
      add_reblog!(entry, status)
    elsif verb(entry) == :post
      if thread_id(entry).nil?
        add_post!(entry, status)
      else
        add_reply!(entry, status)
      end
    else
      return
    end

    # If we added a status, go through accounts it mentions and create respective relations
    # Also record all media attachments for the status and for the reblogged status if present
    unless status.new_record?
      record_remote_mentions(status, entry.xpath('./xmlns:link[@rel="mentioned"]'))
      record_remote_mentions(status.reblog, entry.at_xpath('./activity:object', activity: ACTIVITY_NS).xpath('./xmlns:link[@rel="mentioned"]')) if status.reblog?

      process_attachments(entry, status)
      process_attachments(entry.xpath('./activity:object', activity: ACTIVITY_NS), status.reblog) if status.reblog?

      DistributionWorker.perform_async(status.id)
      return status
    end
  end

  def record_remote_mentions(status, links)
    return if status.local?

    # Here we have to do a reverse lookup of local accounts by their URL!
    # It's not pretty at all! I really wish all these protocols sticked to
    # using acct:username@domain only! It would make things so much easier
    # and tidier

    links.each do |mention_link|
      href_val = mention_link.attribute('href').value

      next if href_val == 'http://activityschema.org/collection/public'

      href = Addressable::URI.parse(href_val)

      if TagManager.instance.local_domain?(href.host)
        # A local user is mentioned
        mentioned_account = Account.find_local(href.path.gsub('/users/', ''))

        unless mentioned_account.nil?
          mentioned_account.mentions.where(status: status).first_or_create(status: status)
          NotificationMailer.mention(mentioned_account, status).deliver_later unless mentioned_account.blocking?(status.account)
        end
      else
        # What to do about remote user?
        # This is kinda dodgy because URLs could change, we don't index them
        mentioned_account = Account.find_by(url: href.to_s)

        if mentioned_account.nil?
          mentioned_account = FetchRemoteAccountService.new.call(href)
        end

        unless mentioned_account.nil?
          mentioned_account.mentions.where(status: status).first_or_create(status: status)
        end
      end
    end
  end

  def process_attachments(entry, status)
    return if status.local?

    entry.xpath('./xmlns:link[@rel="enclosure"]').each do |enclosure_link|
      next if enclosure_link.attribute('href').nil?

      media = MediaAttachment.where(status: status, remote_url: enclosure_link.attribute('href').value).first

      next unless media.nil?

      begin
        media = MediaAttachment.new(account: status.account, status: status, remote_url: enclosure_link.attribute('href').value)
        media.file_remote_url = enclosure_link.attribute('href').value
        media.save
      rescue Paperclip::Errors::NotIdentifiedByImageMagickError
        Rails.logger.debug "Error saving attachment from #{enclosure_link.attribute('href').value}"
        next
      end
    end
  end

  def add_post!(_entry, status)
    status.save!
  end

  def add_reblog!(entry, status)
    status.reblog = find_original_status(entry, target_id(entry))

    if status.reblog.nil?
      status.reblog = fetch_remote_status(entry)
    end
{'fraction_non_alphanumeric': 0.10323974082073434, 'fraction_numerical': 0.0008639308855291577, 'mean_word_length': 3.5065693430656935, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 22}, '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': '5983908', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2685, 'n_words': 691}