2016-02-24 12:57:29 +01: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-20 22:53:20 +01:00
|
|
|
def call(body, account)
|
|
|
|
xml = Nokogiri::XML(body)
|
2016-03-25 02:13:30 +01:00
|
|
|
update_remote_profile_service.(xml.at_xpath('/xmlns:feed/xmlns:author'), account) unless xml.at_xpath('/xmlns:feed').nil?
|
|
|
|
xml.xpath('//xmlns:entry').each { |entry| process_entry(account, entry) }
|
|
|
|
end
|
2016-02-20 22:53:20 +01:00
|
|
|
|
2016-03-25 02:13:30 +01:00
|
|
|
private
|
2016-02-28 14:26:26 +01:00
|
|
|
|
2016-03-25 02:13:30 +01:00
|
|
|
def process_entry(account, entry)
|
|
|
|
return unless [:note, :comment, :activity].include? object_type(entry)
|
2016-02-24 01:28:53 +01:00
|
|
|
|
2016-03-25 02:13:30 +01:00
|
|
|
status = Status.find_by(uri: activity_id(entry))
|
2016-02-24 01:28:53 +01:00
|
|
|
|
2016-03-25 02:13:30 +01:00
|
|
|
# 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
|
2016-03-16 10:46:15 +01:00
|
|
|
|
2016-03-25 02:13:30 +01:00
|
|
|
return unless status.nil?
|
2016-02-24 01:28:53 +01:00
|
|
|
|
2016-03-25 02:13:30 +01:00
|
|
|
status = Status.new(uri: activity_id(entry), url: activity_link(entry), account: account, text: content(entry), created_at: published(entry), updated_at: updated(entry))
|
2016-02-24 01:28:53 +01:00
|
|
|
|
2016-03-25 02:13:30 +01:00
|
|
|
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)
|
2016-02-24 01:28:53 +01:00
|
|
|
end
|
2016-03-25 02:13:30 +01:00
|
|
|
end
|
2016-02-25 00:17:01 +01:00
|
|
|
|
2016-03-25 02:13:30 +01:00
|
|
|
# If we added a status, go through accounts it mentions and create respective relations
|
2016-09-06 12:30:15 +02:00
|
|
|
# Also record all media attachments for the status and for the reblogged status if present
|
2016-03-25 02:13:30 +01:00
|
|
|
unless status.new_record?
|
|
|
|
record_remote_mentions(status, entry.xpath('./xmlns:link[@rel="mentioned"]'))
|
2016-09-09 20:04:34 +02:00
|
|
|
|
2016-09-05 18:39:53 +02:00
|
|
|
process_attachments(entry, status)
|
2016-09-06 12:30:15 +02:00
|
|
|
process_attachments(entry.xpath('./activity:object'), status.reblog) if status.reblog?
|
2016-09-09 20:04:34 +02:00
|
|
|
|
2016-03-25 03:22:26 +01:00
|
|
|
DistributionWorker.perform_async(status.id)
|
2016-03-25 02:13:30 +01:00
|
|
|
end
|
|
|
|
end
|
2016-02-28 21:22:56 +01:00
|
|
|
|
2016-03-25 02:13:30 +01:00
|
|
|
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
|
2016-02-28 21:22:56 +01:00
|
|
|
|
2016-03-25 02:13:30 +01:00
|
|
|
links.each do |mention_link|
|
|
|
|
href = Addressable::URI.parse(mention_link.attribute('href').value)
|
2016-02-28 21:22:56 +01:00
|
|
|
|
2016-03-25 02:13:30 +01:00
|
|
|
if href.host == Rails.configuration.x.local_domain
|
|
|
|
# A local user is mentioned
|
|
|
|
mentioned_account = Account.find_local(href.path.gsub('/users/', ''))
|
2016-03-19 00:41:29 +01:00
|
|
|
|
2016-03-25 02:13:30 +01:00
|
|
|
unless mentioned_account.nil?
|
|
|
|
mentioned_account.mentions.where(status: status).first_or_create(status: status)
|
|
|
|
NotificationMailer.mention(mentioned_account, status).deliver_later
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# What to do about remote user?
|
2016-03-26 01:34:12 +01:00
|
|
|
# Are we supposed to do a search in the database by URL?
|
|
|
|
# We could technically open the URL, look for LRDD tags, get webfinger that way,
|
|
|
|
# finally acquire the acct:username@domain form, and then check DB
|
2016-02-28 21:22:56 +01:00
|
|
|
end
|
2016-02-24 01:28:53 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-05 18:39:53 +02:00
|
|
|
def process_attachments(entry, status)
|
|
|
|
entry.xpath('./xmlns:link[@rel="enclosure"]').each do |enclosure_link|
|
|
|
|
next if enclosure_link.attribute('href').nil?
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-25 00:22:46 +01:00
|
|
|
def add_post!(_entry, status)
|
2016-02-24 01:28:53 +01:00
|
|
|
status.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_reblog!(entry, status)
|
|
|
|
status.reblog = find_original_status(entry, target_id(entry))
|
2016-02-24 17:23:59 +01:00
|
|
|
|
|
|
|
if status.reblog.nil?
|
|
|
|
status.reblog = fetch_remote_status(entry)
|
|
|
|
end
|
|
|
|
|
2016-03-19 19:20:07 +01:00
|
|
|
if !status.reblog.nil?
|
|
|
|
status.save!
|
2016-03-24 13:25:33 +01:00
|
|
|
NotificationMailer.reblog(status.reblog, status.account).deliver_later if status.reblog.local?
|
2016-03-19 19:20:07 +01:00
|
|
|
end
|
2016-02-24 01:28:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_reply!(entry, status)
|
|
|
|
status.thread = find_original_status(entry, thread_id(entry))
|
2016-02-24 17:23:59 +01:00
|
|
|
status.save!
|
2016-02-24 01:28:53 +01:00
|
|
|
end
|
2016-02-20 22:53:20 +01:00
|
|
|
|
2016-03-16 10:46:15 +01:00
|
|
|
def delete_post!(status)
|
2016-09-04 14:43:00 +02:00
|
|
|
RemoveStatusService.new.(status)
|
2016-03-16 10:46:15 +01:00
|
|
|
end
|
|
|
|
|
2016-02-25 00:22:46 +01:00
|
|
|
def find_original_status(_xml, id)
|
2016-02-24 01:28:53 +01:00
|
|
|
return nil if id.nil?
|
2016-02-20 22:53:20 +01:00
|
|
|
|
2016-09-09 20:04:34 +02:00
|
|
|
if TagManager.instance.local_id?(id)
|
|
|
|
Status.find(TagManager.instance.unique_tag_to_local_id(id, 'Status'))
|
2016-02-24 01:28:53 +01:00
|
|
|
else
|
2016-02-24 17:23:59 +01:00
|
|
|
Status.find_by(uri: id)
|
2016-02-20 22:53:20 +01:00
|
|
|
end
|
|
|
|
end
|
2016-02-24 01:28:53 +01:00
|
|
|
|
2016-02-24 17:23:59 +01: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?
|
2016-03-16 21:20:50 +01:00
|
|
|
account = follow_remote_account_service.("#{username}@#{domain}", false)
|
2016-02-24 17:23:59 +01:00
|
|
|
end
|
2016-02-24 01:28:53 +01:00
|
|
|
|
2016-02-24 17:23:59 +01:00
|
|
|
Status.new(account: account, uri: target_id(xml), text: target_content(xml), url: target_url(xml))
|
2016-09-17 17:07:45 +02:00
|
|
|
rescue Goldfinger::Error, HTTP::Error
|
|
|
|
nil
|
2016-02-24 01:28:53 +01: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)
|
2016-02-28 15:46:29 +01:00
|
|
|
xml.at_xpath('./thr:in-reply-to').attribute('ref').value
|
2016-02-24 01:28:53 +01:00
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_id(xml)
|
2016-02-24 17:23:59 +01:00
|
|
|
xml.at_xpath('.//activity:object/xmlns:id').content
|
2016-02-24 01:28:53 +01:00
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def activity_id(xml)
|
2016-02-25 00:22:46 +01:00
|
|
|
xml.at_xpath('./xmlns:id').content
|
2016-02-24 01:28:53 +01:00
|
|
|
end
|
|
|
|
|
2016-02-28 21:22:56 +01:00
|
|
|
def activity_link(xml)
|
|
|
|
xml.at_xpath('./xmlns:link[@rel="alternate"]').attribute('href').value
|
|
|
|
rescue
|
|
|
|
''
|
|
|
|
end
|
|
|
|
|
2016-02-24 17:23:59 +01:00
|
|
|
def target_content(xml)
|
|
|
|
xml.at_xpath('.//activity:object/xmlns:content').content
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_url(xml)
|
2016-02-28 21:22:56 +01:00
|
|
|
xml.at_xpath('.//activity:object/xmlns:link[@rel="alternate"]').attribute('href').value
|
2016-02-24 17:23:59 +01:00
|
|
|
end
|
|
|
|
|
2016-02-24 01:28:53 +01: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 03:05:40 +01:00
|
|
|
|
|
|
|
def follow_remote_account_service
|
2016-02-24 12:57:29 +01:00
|
|
|
@follow_remote_account_service ||= FollowRemoteAccountService.new
|
2016-02-24 03:05:40 +01:00
|
|
|
end
|
2016-02-25 00:17:01 +01:00
|
|
|
|
2016-02-28 14:26:26 +01:00
|
|
|
def update_remote_profile_service
|
|
|
|
@update_remote_profile_service ||= UpdateRemoteProfileService.new
|
|
|
|
end
|
2016-02-20 22:53:20 +01:00
|
|
|
end
|