ProcessFeedService refactor

th-downstream
Eugen Rochko 8 years ago
parent 8f0869876b
commit 434c3fb2f7

@ -2,257 +2,209 @@ class ProcessFeedService < BaseService
ACTIVITY_NS = 'http://activitystrea.ms/spec/1.0/'.freeze ACTIVITY_NS = 'http://activitystrea.ms/spec/1.0/'.freeze
THREAD_NS = 'http://purl.org/syndication/thread/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) def call(body, account)
xml = Nokogiri::XML(body) 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 update_author(xml, account)
process_entries(xml, account)
end end
private private
def process_entry(account, entry) def update_author(xml, account)
return unless [:note, :comment, :activity].include? object_type(entry) return if xml.at_xpath('/xmlns:feed').nil?
UpdateRemoteProfileService.new.call(xml.at_xpath('/xmlns:feed/xmlns:author'), account)
status = Status.find_by(uri: activity_id(entry)) end
# If we already have a post and the verb is now "delete", we gotta delete it and move on! def process_entries(xml, account)
if !status.nil? && verb(entry) == :delete xml.xpath('//xmlns:entry').reverse_each.map { |entry| ProcessEntry.new.call(entry, account) }.compact
delete_post!(status) end
return
end
return unless status.nil? class ProcessEntry
def call(xml, account)
@account = account
@xml = xml
status = Status.new(uri: activity_id(entry), url: activity_link(entry), account: account, text: content(entry), created_at: published(entry), updated_at: updated(entry)) return if skip_unsupported_type?
if verb(entry) == :share case verb
add_reblog!(entry, status) when :post, :share
elsif verb(entry) == :post return create_status
if thread_id(entry).nil? when :delete
add_post!(entry, status) return delete_status
else
add_reply!(entry, status)
end end
else
return
end end
# If we added a status, go through accounts it mentions and create respective relations private
# 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?
if status.reblog? def create_status
ProcessHashtagsService.new.call(status.reblog, entry.at_xpath('./activity:object', activity: ACTIVITY_NS).xpath('./xmlns:category').map { |category| category['term'] }) Rails.logger.debug "Creating remote status #{id}"
else status = status_from_xml(@xml)
ProcessHashtagsService.new.call(status, entry.xpath('./xmlns:category').map { |category| category['term'] })
end
process_attachments(entry, status) if verb == :share
process_attachments(entry.xpath('./activity:object', activity: ACTIVITY_NS), status.reblog) if status.reblog? original_status = status_from_xml(xml.at_xpath('.//activity:object', activity: ACTIVITY_NS))
status.reblog = original_status
end
Rails.logger.debug "Queuing remote status #{status.id} for distribution" status.save!
Rails.logger.debug "Queuing remote status #{status.id} (#{id}) for distribution"
DistributionWorker.perform_async(status.id) DistributionWorker.perform_async(status.id)
return status status
end end
end
def record_remote_mentions(status, links) def delete_status
return if status.local? Rails.logger.debug "Deleting remote status #{id}"
status = Status.find_by(uri: id)
RemoveStatusService.new.call(status) unless status.nil?
nil
end
# Here we have to do a reverse lookup of local accounts by their URL! def skip_unsupported_type?
# It's not pretty at all! I really wish all these protocols sticked to !([:post, :share, :delete].include?(verb) && [:activity, :note, :comment].include?(type))
# using acct:username@domain only! It would make things so much easier end
# and tidier
links.each do |mention_link| def status_from_xml(entry)
href_val = mention_link.attribute('href').value # Return early if status already exists in db
status = find_status(id(entry))
return status unless status.nil?
status = Status.create!({
uri: id(entry),
url: url(entry),
account: account?(entry) ? find_or_resolve_account(acct(entry)) : @account,
text: content(entry),
created_at: published(entry),
})
if thread?(entry)
status.thread = find_or_resolve_status(status, *thread(entry))
end
next if href_val == 'http://activityschema.org/collection/public' mentions_from_xml(status, entry)
hashtags_from_xml(status, entry)
media_from_xml(status, entry)
href = Addressable::URI.parse(href_val) status
end
if TagManager.instance.local_domain?(href.host) def find_or_resolve_account(acct)
# A local user is mentioned FollowRemoteAccountService.new.call(acct)
mentioned_account = Account.find_local(href.path.gsub('/users/', '')) end
unless mentioned_account.nil? def find_or_resolve_status(parent, uri, url)
mentioned_account.mentions.where(status: status).first_or_create(status: status) status = find_status(uri)
NotificationMailer.mention(mentioned_account, status).deliver_later unless mentioned_account.blocking?(status.account) ThreadResolveWorker.perform_async(parent.id, url) if status.nil?
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? status
mentioned_account = FetchRemoteAccountService.new.call(href) end
end
unless mentioned_account.nil? def find_status(uri)
mentioned_account.mentions.where(status: status).first_or_create(status: status) if TagManager.instance.local_id?(uri)
end local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status')
return Status.find(local_id)
end end
Status.find_by(uri: uri)
end end
end
def process_attachments(entry, status) def mentions_from_xml(parent, xml)
return if status.local? processed_account_ids = []
entry.xpath('./xmlns:link[@rel="enclosure"]').each do |enclosure_link| xml.xpath('./xmlns:link[@rel="mentioned"]').each do |link|
next if enclosure_link.attribute('href').nil? next if link['href'] == 'http://activityschema.org/collection/public'
media = MediaAttachment.where(status: status, remote_url: enclosure_link.attribute('href').value).first url = Addressable::URI.parse(link['href'])
next unless media.nil? mentioned_account = if TagManager.instance.local_domain?(url.host)
Account.find_local(url.path.gsub('/users/', ''))
else
Account.find_by(url: link['href']) || FetchRemoteAccountService.new.call(link['href'])
end
begin next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id)
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) if mentioned_account.local?
status.save! # Send notifications
end NotificationMailer.mention(mentioned_account, parent).deliver_later unless mentioned_account.blocking?(parent.account)
end
def add_reblog!(entry, status) mentioned_account.mentions.where(status: parent).first_or_create(status: parent)
status.reblog = find_original_status(entry, target_id(entry))
if status.reblog.nil? # So we can skip duplicate mentions
status.reblog = fetch_remote_status(entry) processed_account_ids << mentioned_account.id
end end
if !status.reblog.nil?
status.save!
NotificationMailer.reblog(status.reblog, status.account).deliver_later if status.reblog.local? && !status.reblog.account.blocking?(status.account)
end end
end
def add_reply!(entry, status) def hashtags_from_xml(parent, xml)
status.thread = find_original_status(entry, thread_id(entry)) tags = xml.xpath('./xmlns:category').map { |category| category['term'] }
status.save! ProcessHashtagsService.new.call(parent, tags)
if status.thread.nil? && !thread_href(entry).nil?
ThreadResolveWorker.perform_async(status.id, thread_href(entry))
end end
end
def delete_post!(status) def media_from_xml(parent, xml)
remove_status_service.call(status) xml.xpath('./xmlns:link[@rel="enclosure"]').each do |link|
end next unless link['href']
def find_original_status(_xml, id) media = MediaAttachment.where(status: parent, remote_url: link['href']).first_or_initialize(account: parent.account, status: parent, remote_url: link['href'])
return nil if id.nil?
if TagManager.instance.local_id?(id) begin
Status.find(TagManager.instance.unique_tag_to_local_id(id, 'Status')) media.file_remote_url = link['href']
else media.save
Status.find_by(uri: id) rescue Paperclip::Errors::NotIdentifiedByImageMagickError
next
end
end
end end
end
def fetch_remote_status(xml)
username = xml.at_xpath('./activity:object', activity: ACTIVITY_NS).at_xpath('./xmlns:author/xmlns:name').content
url = xml.at_xpath('./activity:object', activity: ACTIVITY_NS).at_xpath('./xmlns:author/xmlns:uri').content
domain = Addressable::URI.parse(url).host
account = Account.find_remote(username, domain)
if account.nil? def id(xml = @xml)
account = follow_remote_account_service.call("#{username}@#{domain}") xml.at_xpath('./xmlns:id').content
end end
status = Status.new(account: account, uri: target_id(xml), text: target_content(xml), url: target_url(xml), created_at: published(xml), updated_at: updated(xml)) def verb(xml = @xml)
status.thread = find_original_status(xml, thread_id(xml)) raw = xml.at_xpath('./activity:verb', activity: ACTIVITY_NS).content
raw.gsub('http://activitystrea.ms/schema/1.0/', '').gsub('http://ostatus.org/schema/1.0/', '').to_sym
if status.save && status.thread.nil? && !thread_href(xml).nil? rescue
ThreadResolveWorker.perform_async(status.id, thread_href(xml)) :post
end end
status def type(xml = @xml)
rescue Goldfinger::Error, HTTP::Error raw = xml.at_xpath('./activity:object-type', activity: ACTIVITY_NS).content
nil raw.gsub('http://activitystrea.ms/schema/1.0/', '').gsub('http://ostatus.org/schema/1.0/', '').to_sym
end rescue
:activity
def published(xml) end
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').try(:content)
end
def thread_id(xml)
xml.at_xpath('./thr:in-reply-to', thr: THREAD_NS).attribute('ref').value
rescue
nil
end
def thread_href(xml)
xml.at_xpath('./thr:in-reply-to', thr: THREAD_NS).attribute('href').value
rescue
nil
end
def target_id(xml)
xml.at_xpath('.//activity:object', activity: ACTIVITY_NS).at_xpath('./xmlns:id').content
rescue
nil
end
def activity_id(xml)
xml.at_xpath('./xmlns:id').content
end
def activity_link(xml) def url(xml = @xml)
xml.at_xpath('./xmlns:link[@rel="alternate"]').attribute('href').value link = xml.at_xpath('./xmlns:link[@rel="alternate"]')
rescue link['href']
'' end
end
def target_content(xml) def content(xml = @xml)
xml.at_xpath('.//activity:object', activity: ACTIVITY_NS).at_xpath('./xmlns:content').content xml.at_xpath('./xmlns:content').content
end end
def target_url(xml) def published(xml = @xml)
xml.at_xpath('.//activity:object', activity: ACTIVITY_NS).at_xpath('./xmlns:link[@rel="alternate"]').attribute('href').value xml.at_xpath('./xmlns:published').content
end end
def object_type(xml) def thread?(xml = @xml)
xml.at_xpath('./activity:object-type', activity: ACTIVITY_NS).content.gsub('http://activitystrea.ms/schema/1.0/', '').gsub('http://ostatus.org/schema/1.0/', '').to_sym !xml.at_xpath('./thr:in-reply-to', thr: THREAD_NS).nil?
rescue end
:activity
end
def verb(xml) def thread(xml = @xml)
xml.at_xpath('./activity:verb', activity: ACTIVITY_NS).content.gsub('http://activitystrea.ms/schema/1.0/', '').gsub('http://ostatus.org/schema/1.0/', '').to_sym thr = xml.at_xpath('./thr:in-reply-to', thr: THREAD_NS)
rescue [thr['ref'], thr['href']]
:post end
end
def follow_remote_account_service def account?(xml = @xml)
@follow_remote_account_service ||= FollowRemoteAccountService.new !xml.at_xpath('./xmlns:author').nil?
end end
def update_remote_profile_service def acct(xml = @xml)
@update_remote_profile_service ||= UpdateRemoteProfileService.new username = xml.at_xpath('./xmlns:author/xmlns:name').content
end url = xml.at_xpath('./xmlns:author/xmlns:uri').content
domain = Addressable::URI.parse(url).host
def remove_status_service "#{username}@#{domain}"
@remove_status_service ||= RemoveStatusService.new end
end end
end end

Loading…
Cancel
Save