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.

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