about summary refs log tree commit diff
path: root/app/services/process_feed_service.rb
blob: 31191a818f83e89dfbc68b15d0e48cb51c756673 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true

class ProcessFeedService < BaseService
  def call(body, account)
    xml = Nokogiri::XML(body)
    xml.encoding = 'utf-8'

    update_author(body, account)
    process_entries(xml, account)
  end

  private

  def update_author(body, account)
    RemoteProfileUpdateWorker.perform_async(account.id, body.force_encoding('UTF-8'), true)
  end

  def process_entries(xml, account)
    xml.xpath('//xmlns:entry', xmlns: TagManager::XMLNS).reverse_each.map { |entry| process_entry(entry, account) }.compact
  end

  def process_entry(xml, account)
    activity = OStatus::Activity::General.new(xml, account)
    activity.specialize&.perform if activity.status?
  rescue ActiveRecord::RecordInvalid => e
    Rails.logger.debug "Nothing was saved for #{activity.id} because: #{e}"
    nil
  end
end