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