2016-02-24 22:57:29 +11:00
|
|
|
class ProcessFeedService < BaseService
|
|
|
|
# Create local statuses from an Atom feed
|
|
|
|
# @param [String] body Atom feed
|
|
|
|
# @param [Account] account Account this feed belongs to
|
2016-02-21 08:53:20 +11:00
|
|
|
def call(body, account)
|
|
|
|
xml = Nokogiri::XML(body)
|
|
|
|
|
2016-02-24 11:28:53 +11:00
|
|
|
xml.xpath('//xmlns:entry').each do |entry|
|
|
|
|
next unless [:note, :comment, :activity].includes? object_type(entry)
|
|
|
|
|
|
|
|
status = Status.find_by(uri: activity_id(entry))
|
|
|
|
|
|
|
|
next unless status.nil?
|
|
|
|
|
|
|
|
status = Status.new(uri: activity_id(entry), account: account, text: content(entry), created_at: published(entry), updated_at: updated(entry))
|
|
|
|
|
|
|
|
if object_type(entry) == :comment
|
|
|
|
add_reply!(entry, status)
|
|
|
|
elsif verb(entry) == :share
|
|
|
|
add_reblog!(entry, status)
|
|
|
|
else
|
|
|
|
add_post!(entry, status)
|
|
|
|
end
|
2016-02-25 10:17:01 +11:00
|
|
|
|
|
|
|
process_mentions_service.(status) unless status.new_record?
|
2016-02-24 11:28:53 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-02-25 10:22:46 +11:00
|
|
|
def add_post!(_entry, status)
|
2016-02-24 11:28:53 +11:00
|
|
|
status.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_reblog!(entry, status)
|
|
|
|
status.reblog = find_original_status(entry, target_id(entry))
|
2016-02-25 03:23:59 +11:00
|
|
|
|
|
|
|
if status.reblog.nil?
|
|
|
|
status.reblog = fetch_remote_status(entry)
|
|
|
|
end
|
|
|
|
|
2016-02-24 13:05:40 +11:00
|
|
|
status.save! unless status.reblog.nil?
|
2016-02-24 11:28:53 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_reply!(entry, status)
|
|
|
|
status.thread = find_original_status(entry, thread_id(entry))
|
2016-02-25 03:23:59 +11:00
|
|
|
status.save!
|
2016-02-24 11:28:53 +11:00
|
|
|
end
|
2016-02-21 08:53:20 +11:00
|
|
|
|
2016-02-25 10:22:46 +11:00
|
|
|
def find_original_status(_xml, id)
|
2016-02-24 11:28:53 +11:00
|
|
|
return nil if id.nil?
|
2016-02-21 08:53:20 +11:00
|
|
|
|
2016-02-24 11:28:53 +11:00
|
|
|
if local_id?(id)
|
|
|
|
Status.find(unique_tag_to_local_id(id, 'Status'))
|
|
|
|
else
|
2016-02-25 03:23:59 +11:00
|
|
|
Status.find_by(uri: id)
|
2016-02-21 08:53:20 +11:00
|
|
|
end
|
|
|
|
end
|
2016-02-24 11:28:53 +11:00
|
|
|
|
2016-02-25 03:23:59 +11:00
|
|
|
def fetch_remote_status(xml)
|
|
|
|
username = xml.at_xpath('./activity:object/xmlns:author/xmlns:name').content
|
|
|
|
url = xml.at_xpath('./activity:object/xmlns:author/xmlns:uri').content
|
|
|
|
domain = Addressable::URI.parse(url).host
|
|
|
|
account = Account.find_by(username: username, domain: domain)
|
|
|
|
|
|
|
|
if account.nil?
|
|
|
|
account = follow_remote_account_service.("acct:#{username}@#{domain}", false)
|
|
|
|
return nil if account.nil?
|
|
|
|
end
|
2016-02-24 11:28:53 +11:00
|
|
|
|
2016-02-25 03:23:59 +11:00
|
|
|
Status.new(account: account, uri: target_id(xml), text: target_content(xml), url: target_url(xml))
|
2016-02-24 11:28:53 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def published(xml)
|
|
|
|
xml.at_xpath('./xmlns:published').content
|
|
|
|
end
|
|
|
|
|
|
|
|
def updated(xml)
|
|
|
|
xml.at_xpath('./xmlns:updated').content
|
|
|
|
end
|
|
|
|
|
|
|
|
def content(xml)
|
|
|
|
xml.at_xpath('./xmlns:content').content
|
|
|
|
end
|
|
|
|
|
|
|
|
def thread_id(xml)
|
|
|
|
xml.at_xpath('./thr:in-reply-to-id').attribute('ref').value
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_id(xml)
|
2016-02-25 03:23:59 +11:00
|
|
|
xml.at_xpath('.//activity:object/xmlns:id').content
|
2016-02-24 11:28:53 +11:00
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def activity_id(xml)
|
2016-02-25 10:22:46 +11:00
|
|
|
xml.at_xpath('./xmlns:id').content
|
2016-02-24 11:28:53 +11:00
|
|
|
end
|
|
|
|
|
2016-02-25 03:23:59 +11:00
|
|
|
def target_content(xml)
|
|
|
|
xml.at_xpath('.//activity:object/xmlns:content').content
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_url(xml)
|
|
|
|
xml.at_xpath('.//activity:object/xmlns:link[@rel=alternate]').attribute('href').value
|
|
|
|
end
|
|
|
|
|
2016-02-24 11:28:53 +11:00
|
|
|
def object_type(xml)
|
|
|
|
xml.at_xpath('./activity:object-type').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
|
|
|
|
rescue
|
|
|
|
:note
|
|
|
|
end
|
|
|
|
|
|
|
|
def verb(xml)
|
|
|
|
xml.at_xpath('./activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
|
|
|
|
rescue
|
|
|
|
:post
|
|
|
|
end
|
2016-02-24 13:05:40 +11:00
|
|
|
|
|
|
|
def follow_remote_account_service
|
2016-02-24 22:57:29 +11:00
|
|
|
@follow_remote_account_service ||= FollowRemoteAccountService.new
|
2016-02-24 13:05:40 +11:00
|
|
|
end
|
2016-02-25 10:17:01 +11:00
|
|
|
|
|
|
|
def process_mentions_service
|
|
|
|
@process_mentions_service ||= ProcessMentionsService.new
|
|
|
|
end
|
2016-02-21 08:53:20 +11:00
|
|
|
end
|