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.

305 lines
8.7 KiB

8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. # frozen_string_literal: true
  2. class ProcessFeedService < BaseService
  3. def call(body, account)
  4. xml = Nokogiri::XML(body)
  5. xml.encoding = 'utf-8'
  6. update_author(body, account)
  7. process_entries(xml, account)
  8. end
  9. private
  10. def update_author(body, account)
  11. RemoteProfileUpdateWorker.perform_async(account.id, body.force_encoding('UTF-8'), true)
  12. end
  13. def process_entries(xml, account)
  14. xml.xpath('//xmlns:entry', xmlns: TagManager::XMLNS).reverse_each.map { |entry| ProcessEntry.new.call(entry, account) }.compact
  15. end
  16. class ProcessEntry
  17. include AuthorExtractor
  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. rescue ActiveRecord::RecordInvalid => e
  29. Rails.logger.debug "Nothing was saved for #{id} because: #{e}"
  30. nil
  31. end
  32. private
  33. def create_status
  34. if redis.exists("delete_upon_arrival:#{id}")
  35. Rails.logger.debug "Delete for status #{id} was queued, ignoring"
  36. return
  37. end
  38. status, just_created = nil
  39. Rails.logger.debug "Creating remote status #{id}"
  40. if verb == :share
  41. original_status = shared_status_from_xml(@xml.at_xpath('.//activity:object', activity: TagManager::AS_XMLNS))
  42. return nil if original_status.nil?
  43. end
  44. ApplicationRecord.transaction do
  45. status, just_created = status_from_xml(@xml)
  46. return if status.nil?
  47. return status unless just_created
  48. if verb == :share
  49. status.reblog = original_status.reblog? ? original_status.reblog : original_status
  50. end
  51. status.thread = find_status(thread(@xml).first) if thread?(@xml)
  52. status.save!
  53. end
  54. if thread?(@xml) && status.thread.nil?
  55. Rails.logger.debug "Trying to attach #{status.id} (#{id(@xml)}) to #{thread(@xml).first}"
  56. ThreadResolveWorker.perform_async(status.id, thread(@xml).second)
  57. end
  58. notify_about_mentions!(status) unless status.reblog?
  59. notify_about_reblog!(status) if status.reblog? && status.reblog.account.local?
  60. Rails.logger.debug "Queuing remote status #{status.id} (#{id}) for distribution"
  61. LinkCrawlWorker.perform_async(status.id) unless status.spoiler_text?
  62. DistributionWorker.perform_async(status.id)
  63. status
  64. end
  65. def notify_about_mentions!(status)
  66. status.mentions.includes(:account).each do |mention|
  67. mentioned_account = mention.account
  68. next unless mentioned_account.local?
  69. NotifyService.new.call(mentioned_account, mention)
  70. end
  71. end
  72. def notify_about_reblog!(status)
  73. NotifyService.new.call(status.reblog.account, status)
  74. end
  75. def delete_status
  76. Rails.logger.debug "Deleting remote status #{id}"
  77. status = Status.find_by(uri: id)
  78. if status.nil?
  79. redis.setex("delete_upon_arrival:#{id}", 6 * 3_600, id)
  80. else
  81. RemoveStatusService.new.call(status)
  82. end
  83. nil
  84. end
  85. def skip_unsupported_type?
  86. !([:post, :share, :delete].include?(verb) && [:activity, :note, :comment].include?(type))
  87. end
  88. def shared_status_from_xml(entry)
  89. status = find_status(id(entry))
  90. return status unless status.nil?
  91. FetchRemoteStatusService.new.call(url(entry))
  92. end
  93. def status_from_xml(entry)
  94. # Return early if status already exists in db
  95. status = find_status(id(entry))
  96. return [status, false] unless status.nil?
  97. # If status embeds an author, find that author
  98. # If that author cannot be found, don't record the status (do not misattribute)
  99. if account?(entry)
  100. begin
  101. account = author_from_xml(entry)
  102. return [nil, false] if account.nil?
  103. rescue Goldfinger::Error
  104. return [nil, false]
  105. end
  106. else
  107. account = @account
  108. end
  109. return [nil, false] if account.suspended?
  110. status = Status.create!(
  111. uri: id(entry),
  112. url: url(entry),
  113. account: account,
  114. text: content(entry),
  115. spoiler_text: content_warning(entry),
  116. created_at: published(entry),
  117. reply: thread?(entry),
  118. language: content_language(entry),
  119. visibility: visibility_scope(entry),
  120. conversation: find_or_create_conversation(entry)
  121. )
  122. mentions_from_xml(status, entry)
  123. hashtags_from_xml(status, entry)
  124. media_from_xml(status, entry)
  125. [status, true]
  126. end
  127. def find_or_create_conversation(xml)
  128. uri = xml.at_xpath('./ostatus:conversation', ostatus: TagManager::OS_XMLNS)&.attribute('ref')&.content
  129. return if uri.nil?
  130. if TagManager.instance.local_id?(uri)
  131. local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Conversation')
  132. return Conversation.find_by(id: local_id)
  133. end
  134. Conversation.find_by(uri: uri)
  135. end
  136. def find_status(uri)
  137. if TagManager.instance.local_id?(uri)
  138. local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status')
  139. return Status.find_by(id: local_id)
  140. end
  141. Status.find_by(uri: uri)
  142. end
  143. def mentions_from_xml(parent, xml)
  144. processed_account_ids = []
  145. xml.xpath('./xmlns:link[@rel="mentioned"]', xmlns: TagManager::XMLNS).each do |link|
  146. next if [TagManager::TYPES[:group], TagManager::TYPES[:collection]].include? link['ostatus:object-type']
  147. mentioned_account = account_from_href(link['href'])
  148. next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id)
  149. mentioned_account.mentions.where(status: parent).first_or_create(status: parent)
  150. # So we can skip duplicate mentions
  151. processed_account_ids << mentioned_account.id
  152. end
  153. end
  154. def account_from_href(href)
  155. url = Addressable::URI.parse(href).normalize
  156. if TagManager.instance.web_domain?(url.host)
  157. Account.find_local(url.path.gsub('/users/', ''))
  158. else
  159. Account.where(uri: href).or(Account.where(url: href)).first || FetchRemoteAccountService.new.call(href)
  160. end
  161. end
  162. def hashtags_from_xml(parent, xml)
  163. tags = xml.xpath('./xmlns:category', xmlns: TagManager::XMLNS).map { |category| category['term'] }.select(&:present?)
  164. ProcessHashtagsService.new.call(parent, tags)
  165. end
  166. def media_from_xml(parent, xml)
  167. do_not_download = DomainBlock.find_by(domain: parent.account.domain)&.reject_media?
  168. xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: TagManager::XMLNS).each do |link|
  169. next unless link['href']
  170. media = MediaAttachment.where(status: parent, remote_url: link['href']).first_or_initialize(account: parent.account, status: parent, remote_url: link['href'])
  171. parsed_url = Addressable::URI.parse(link['href']).normalize
  172. next if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty?
  173. media.save
  174. next if do_not_download
  175. begin
  176. media.file_remote_url = link['href']
  177. media.save!
  178. rescue ActiveRecord::RecordInvalid
  179. next
  180. end
  181. end
  182. end
  183. def id(xml = @xml)
  184. xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content
  185. end
  186. def verb(xml = @xml)
  187. raw = xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content
  188. TagManager::VERBS.key(raw)
  189. rescue
  190. :post
  191. end
  192. def type(xml = @xml)
  193. raw = xml.at_xpath('./activity:object-type', activity: TagManager::AS_XMLNS).content
  194. TagManager::TYPES.key(raw)
  195. rescue
  196. :activity
  197. end
  198. def url(xml = @xml)
  199. link = xml.at_xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS)
  200. link.nil? ? nil : link['href']
  201. end
  202. def content(xml = @xml)
  203. xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content
  204. end
  205. def content_language(xml = @xml)
  206. xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS)['xml:lang']&.presence || 'en'
  207. end
  208. def content_warning(xml = @xml)
  209. xml.at_xpath('./xmlns:summary', xmlns: TagManager::XMLNS)&.content || ''
  210. end
  211. def visibility_scope(xml = @xml)
  212. xml.at_xpath('./mastodon:scope', mastodon: TagManager::MTDN_XMLNS)&.content&.to_sym || :public
  213. end
  214. def published(xml = @xml)
  215. xml.at_xpath('./xmlns:published', xmlns: TagManager::XMLNS).content
  216. end
  217. def thread?(xml = @xml)
  218. !xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS).nil?
  219. end
  220. def thread(xml = @xml)
  221. thr = xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS)
  222. [thr['ref'], thr['href']]
  223. end
  224. def account?(xml = @xml)
  225. !xml.at_xpath('./xmlns:author', xmlns: TagManager::XMLNS).nil?
  226. end
  227. def redis
  228. Redis.current
  229. end
  230. end
  231. end