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.

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