2016-02-24 06:57:29 -05: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 16:53:20 -05:00
|
|
|
def call(body, account)
|
|
|
|
xml = Nokogiri::XML(body)
|
|
|
|
|
2016-02-28 15:22:56 -05:00
|
|
|
# If we got a full feed, make sure the account's profile is up to date
|
2016-02-28 08:26:26 -05:00
|
|
|
unless xml.at_xpath('/xmlns:feed').nil?
|
|
|
|
update_remote_profile_service.(xml.at_xpath('/xmlns:feed/xmlns:author'), account)
|
|
|
|
end
|
|
|
|
|
2016-02-28 15:22:56 -05:00
|
|
|
# Process entries
|
2016-02-23 19:28:53 -05:00
|
|
|
xml.xpath('//xmlns:entry').each do |entry|
|
2016-02-28 09:46:29 -05:00
|
|
|
next unless [:note, :comment, :activity].include? object_type(entry)
|
2016-02-23 19:28:53 -05:00
|
|
|
|
|
|
|
status = Status.find_by(uri: activity_id(entry))
|
|
|
|
|
2016-03-16 05:46:15 -04:00
|
|
|
# If we already have a post and the verb is now "delete", we gotta delete it and move on!
|
|
|
|
if verb(entry) == :delete
|
|
|
|
delete_post!(status)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2016-02-23 19:28:53 -05:00
|
|
|
next unless status.nil?
|
|
|
|
|
2016-02-28 15:22:56 -05: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-23 19:28:53 -05:00
|
|
|
|
2016-03-16 05:46:15 -04:00
|
|
|
if object_type(entry) == :comment && verb(entry) == :post
|
2016-02-23 19:28:53 -05:00
|
|
|
add_reply!(entry, status)
|
|
|
|
elsif verb(entry) == :share
|
|
|
|
add_reblog!(entry, status)
|
2016-03-16 05:46:15 -04:00
|
|
|
elsif verb(entry) == :post
|
2016-02-23 19:28:53 -05:00
|
|
|
add_post!(entry, status)
|
|
|
|
end
|
2016-02-24 18:17:01 -05:00
|
|
|
|
2016-02-28 15:22:56 -05:00
|
|
|
# If we added a status, go through accounts it mentions and create respective relations
|
|
|
|
unless status.new_record?
|
|
|
|
entry.xpath('./xmlns:link[@rel="mentioned"]').each do |mention_link|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
href = Addressable::URI.parse(mention_link.attribute('href').value)
|
|
|
|
|
2016-02-29 14:06:39 -05:00
|
|
|
if href.host == Rails.configuration.x.local_domain
|
2016-03-18 18:23:19 -04:00
|
|
|
mentioned_account = Account.find_local(href.path.gsub('/users/', ''))
|
2016-02-28 15:22:56 -05:00
|
|
|
|
|
|
|
unless mentioned_account.nil?
|
2016-03-18 18:47:02 -04:00
|
|
|
mentioned_account.mentions.where(status: status).first_or_create(status: status)
|
2016-02-28 15:22:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-23 19:28:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-02-24 18:22:46 -05:00
|
|
|
def add_post!(_entry, status)
|
2016-02-23 19:28:53 -05:00
|
|
|
status.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_reblog!(entry, status)
|
|
|
|
status.reblog = find_original_status(entry, target_id(entry))
|
2016-02-24 11:23:59 -05:00
|
|
|
|
|
|
|
if status.reblog.nil?
|
|
|
|
status.reblog = fetch_remote_status(entry)
|
|
|
|
end
|
|
|
|
|
2016-02-23 21:05:40 -05:00
|
|
|
status.save! unless status.reblog.nil?
|
2016-02-23 19:28:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_reply!(entry, status)
|
|
|
|
status.thread = find_original_status(entry, thread_id(entry))
|
2016-02-24 11:23:59 -05:00
|
|
|
status.save!
|
2016-02-23 19:28:53 -05:00
|
|
|
end
|
2016-02-20 16:53:20 -05:00
|
|
|
|
2016-03-16 05:46:15 -04:00
|
|
|
def delete_post!(status)
|
|
|
|
status.destroy!
|
|
|
|
end
|
|
|
|
|
2016-02-24 18:22:46 -05:00
|
|
|
def find_original_status(_xml, id)
|
2016-02-23 19:28:53 -05:00
|
|
|
return nil if id.nil?
|
2016-02-20 16:53:20 -05:00
|
|
|
|
2016-02-23 19:28:53 -05:00
|
|
|
if local_id?(id)
|
|
|
|
Status.find(unique_tag_to_local_id(id, 'Status'))
|
|
|
|
else
|
2016-02-24 11:23:59 -05:00
|
|
|
Status.find_by(uri: id)
|
2016-02-20 16:53:20 -05:00
|
|
|
end
|
|
|
|
end
|
2016-02-23 19:28:53 -05:00
|
|
|
|
2016-02-24 11:23:59 -05: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 16:20:50 -04:00
|
|
|
account = follow_remote_account_service.("#{username}@#{domain}", false)
|
2016-02-24 11:23:59 -05:00
|
|
|
return nil if account.nil?
|
|
|
|
end
|
2016-02-23 19:28:53 -05:00
|
|
|
|
2016-02-24 11:23:59 -05:00
|
|
|
Status.new(account: account, uri: target_id(xml), text: target_content(xml), url: target_url(xml))
|
2016-02-23 19:28:53 -05: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 09:46:29 -05:00
|
|
|
xml.at_xpath('./thr:in-reply-to').attribute('ref').value
|
2016-02-23 19:28:53 -05:00
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_id(xml)
|
2016-02-24 11:23:59 -05:00
|
|
|
xml.at_xpath('.//activity:object/xmlns:id').content
|
2016-02-23 19:28:53 -05:00
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def activity_id(xml)
|
2016-02-24 18:22:46 -05:00
|
|
|
xml.at_xpath('./xmlns:id').content
|
2016-02-23 19:28:53 -05:00
|
|
|
end
|
|
|
|
|
2016-02-28 15:22:56 -05:00
|
|
|
def activity_link(xml)
|
|
|
|
xml.at_xpath('./xmlns:link[@rel="alternate"]').attribute('href').value
|
|
|
|
rescue
|
|
|
|
''
|
|
|
|
end
|
|
|
|
|
2016-02-24 11:23:59 -05:00
|
|
|
def target_content(xml)
|
|
|
|
xml.at_xpath('.//activity:object/xmlns:content').content
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_url(xml)
|
2016-02-28 15:22:56 -05:00
|
|
|
xml.at_xpath('.//activity:object/xmlns:link[@rel="alternate"]').attribute('href').value
|
2016-02-24 11:23:59 -05:00
|
|
|
end
|
|
|
|
|
2016-02-23 19:28:53 -05: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-23 21:05:40 -05:00
|
|
|
|
|
|
|
def follow_remote_account_service
|
2016-02-24 06:57:29 -05:00
|
|
|
@follow_remote_account_service ||= FollowRemoteAccountService.new
|
2016-02-23 21:05:40 -05:00
|
|
|
end
|
2016-02-24 18:17:01 -05:00
|
|
|
|
2016-02-28 08:26:26 -05:00
|
|
|
def update_remote_profile_service
|
|
|
|
@update_remote_profile_service ||= UpdateRemoteProfileService.new
|
|
|
|
end
|
2016-02-20 16:53:20 -05:00
|
|
|
end
|