Feed processing except fetching remote statuses

shrike
Eugen Rochko 2016-02-24 01:28:53 +01:00
parent 79baf2fd99
commit 9411e8c35a
2 changed files with 95 additions and 14 deletions

View File

@ -1,24 +1,103 @@
class ProcessFeedService class ProcessFeedService
include ApplicationHelper
def call(body, account) def call(body, account)
xml = Nokogiri::XML(body) xml = Nokogiri::XML(body)
xml.xpath('/xmlns:feed/xmlns:entry').each do |entry| xml.xpath('//xmlns:entry').each do |entry|
uri = entry.at_xpath('./xmlns:id').content next unless [:note, :comment, :activity].includes? object_type(entry)
status = Status.find_by(uri: uri)
next if !status.nil? status = Status.find_by(uri: activity_id(entry))
status = Status.new next unless status.nil?
status.account = account
status.uri = uri
status.text = entry.at_xpath('./xmlns:content').content
status.created_at = entry.at_xpath('./xmlns:published').content
status.updated_at = entry.at_xpath('./xmlns:updated').content
status.save!
# todo: not everything is a status. there are follows, favourites status = Status.new(uri: activity_id(entry), account: account, text: content(entry), created_at: published(entry), updated_at: updated(entry))
# todo: RTs
# account.statuses.create!(reblog: status, uri: activity_uri(xml), url: activity_url(xml), text: content(xml)) if object_type(entry) == :comment
add_reply!(entry, status)
elsif verb(entry) == :share
add_reblog!(entry, status)
else
add_post!(entry, status)
end end
end end
end
private
def add_post!(entry, status)
status.save!
end
def add_reblog!(entry, status)
status.reblog = find_original_status(entry, target_id(entry))
end
def add_reply!(entry, status)
status.thread = find_original_status(entry, thread_id(entry))
end
def find_original_status(xml, id)
return nil if id.nil?
if local_id?(id)
Status.find(unique_tag_to_local_id(id, 'Status'))
else
status = Status.find_by(uri: id)
if status.nil?
status = fetch_remote_status(xml, id)
end
status
end
end
def fetch_remote_status(xml, id)
# todo
end
def local_id?(id)
id.start_with?("tag:#{LOCAL_DOMAIN}")
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)
xml.at_xpath('./activity:object/xmlns:id').content
rescue
nil
end
def activity_id(xml)
entry.at_xpath('./xmlns:id').content
end
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
end end

View File

@ -46,6 +46,8 @@ class ProcessInteractionService
def verb(xml) def verb(xml)
xml.at_xpath('//activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym xml.at_xpath('//activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
rescue
:post
end end
def follow!(account, target_account) def follow!(account, target_account)