You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.6 KiB

8 years ago
8 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. class ProcessFeedService < BaseService
  2. ACTIVITY_NS = 'http://activitystrea.ms/spec/1.0/'.freeze
  3. THREAD_NS = 'http://purl.org/syndication/thread/1.0'.freeze
  4. def call(body, account)
  5. xml = Nokogiri::XML(body)
  6. update_author(xml, account)
  7. process_entries(xml, account)
  8. end
  9. private
  10. def update_author(xml, account)
  11. return if xml.at_xpath('/xmlns:feed').nil?
  12. UpdateRemoteProfileService.new.call(xml.at_xpath('/xmlns:feed/xmlns:author'), account)
  13. end
  14. def process_entries(xml, account)
  15. xml.xpath('//xmlns:entry').reverse_each.map { |entry| ProcessEntry.new.call(entry, account) }.compact
  16. end
  17. class ProcessEntry
  18. def call(xml, account)
  19. @account = account
  20. @xml = xml
  21. return if skip_unsupported_type?
  22. case verb
  23. when :post, :share
  24. return create_status
  25. when :delete
  26. return delete_status
  27. end
  28. end
  29. private
  30. def create_status
  31. Rails.logger.debug "Creating remote status #{id}"
  32. status = status_from_xml(@xml)
  33. if verb == :share
  34. original_status = status_from_xml(@xml.at_xpath('.//activity:object', activity: ACTIVITY_NS))
  35. status.reblog = original_status
  36. if original_status.nil?
  37. status.destroy
  38. return nil
  39. elsif original_status.reblog?
  40. status.reblog = original_status.reblog
  41. end
  42. end
  43. status.save!
  44. Rails.logger.debug "Queuing remote status #{status.id} (#{id}) for distribution"
  45. DistributionWorker.perform_async(status.id)
  46. status
  47. end
  48. def delete_status
  49. Rails.logger.debug "Deleting remote status #{id}"
  50. status = Status.find_by(uri: id)
  51. RemoveStatusService.new.call(status) unless status.nil?
  52. nil
  53. end
  54. def skip_unsupported_type?
  55. !([:post, :share, :delete].include?(verb) && [:activity, :note, :comment].include?(type))
  56. end
  57. def status_from_xml(entry)
  58. # Return early if status already exists in db
  59. status = find_status(id(entry))
  60. return status unless status.nil?
  61. # If status embeds an author, find that author
  62. # If that author cannot be found, don't record the status (do not misattribute)
  63. if account?(entry)
  64. begin
  65. account = find_or_resolve_account(acct(entry))
  66. return nil if account.nil?
  67. rescue Goldfinger::Error
  68. return nil
  69. end
  70. else
  71. account = @account
  72. end
  73. status = Status.create!({
  74. uri: id(entry),
  75. url: url(entry),
  76. account: account,
  77. text: content(entry),
  78. created_at: published(entry),
  79. })
  80. if thread?(entry)
  81. Rails.logger.debug "Trying to attach #{status.id} (#{id(entry)}) to #{thread(entry).first}"
  82. status.thread = find_or_resolve_status(status, *thread(entry))
  83. end
  84. mentions_from_xml(status, entry)
  85. hashtags_from_xml(status, entry)
  86. media_from_xml(status, entry)
  87. status
  88. end
  89. def find_or_resolve_account(acct)
  90. FollowRemoteAccountService.new.call(acct)
  91. end
  92. def find_or_resolve_status(parent, uri, url)
  93. status = find_status(uri)
  94. ThreadResolveWorker.perform_async(parent.id, url) if status.nil?
  95. status
  96. end
  97. def find_status(uri)
  98. if TagManager.instance.local_id?(uri)
  99. local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status')
  100. return Status.find(local_id)
  101. end
  102. Status.find_by(uri: uri)
  103. end
  104. def mentions_from_xml(parent, xml)
  105. processed_account_ids = []
  106. xml.xpath('./xmlns:link[@rel="mentioned"]').each do |link|
  107. next if link['href'] == 'http://activityschema.org/collection/public'
  108. url = Addressable::URI.parse(link['href'])
  109. mentioned_account = if TagManager.instance.local_domain?(url.host)
  110. Account.find_local(url.path.gsub('/users/', ''))
  111. else
  112. Account.find_by(url: link['href']) || FetchRemoteAccountService.new.call(link['href'])
  113. end
  114. next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id)
  115. if mentioned_account.local?
  116. # Send notifications
  117. NotificationMailer.mention(mentioned_account, parent).deliver_later unless mentioned_account.blocking?(parent.account)
  118. end
  119. mentioned_account.mentions.where(status: parent).first_or_create(status: parent)
  120. # So we can skip duplicate mentions
  121. processed_account_ids << mentioned_account.id
  122. end
  123. end
  124. def hashtags_from_xml(parent, xml)
  125. tags = xml.xpath('./xmlns:category').map { |category| category['term'] }.select { |t| !t.blank? }
  126. ProcessHashtagsService.new.call(parent, tags)
  127. end
  128. def media_from_xml(parent, xml)
  129. xml.xpath('./xmlns:link[@rel="enclosure"]').each do |link|
  130. next unless link['href']
  131. media = MediaAttachment.where(status: parent, remote_url: link['href']).first_or_initialize(account: parent.account, status: parent, remote_url: link['href'])
  132. begin
  133. media.file_remote_url = link['href']
  134. media.save
  135. rescue Paperclip::Errors::NotIdentifiedByImageMagickError
  136. next
  137. end
  138. end
  139. end
  140. def id(xml = @xml)
  141. xml.at_xpath('./xmlns:id').content
  142. end
  143. def verb(xml = @xml)
  144. raw = xml.at_xpath('./activity:verb', activity: ACTIVITY_NS).content
  145. raw.gsub('http://activitystrea.ms/schema/1.0/', '').gsub('http://ostatus.org/schema/1.0/', '').to_sym
  146. rescue
  147. :post
  148. end
  149. def type(xml = @xml)
  150. raw = xml.at_xpath('./activity:object-type', activity: ACTIVITY_NS).content
  151. raw.gsub('http://activitystrea.ms/schema/1.0/', '').gsub('http://ostatus.org/schema/1.0/', '').to_sym
  152. rescue
  153. :activity
  154. end
  155. def url(xml = @xml)
  156. link = xml.at_xpath('./xmlns:link[@rel="alternate"]')
  157. link.nil? ? nil : link['href']
  158. end
  159. def content(xml = @xml)
  160. xml.at_xpath('./xmlns:content').content
  161. end
  162. def published(xml = @xml)
  163. xml.at_xpath('./xmlns:published').content
  164. end
  165. def thread?(xml = @xml)
  166. !xml.at_xpath('./thr:in-reply-to', thr: THREAD_NS).nil?
  167. end
  168. def thread(xml = @xml)
  169. thr = xml.at_xpath('./thr:in-reply-to', thr: THREAD_NS)
  170. [thr['ref'], thr['href']]
  171. end
  172. def account?(xml = @xml)
  173. !xml.at_xpath('./xmlns:author').nil?
  174. end
  175. def acct(xml = @xml)
  176. username = xml.at_xpath('./xmlns:author/xmlns:name').content
  177. url = xml.at_xpath('./xmlns:author/xmlns:uri').content
  178. domain = Addressable::URI.parse(url).host
  179. "#{username}@#{domain}"
  180. end
  181. end
  182. end