The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

244 lines
7.2 KiB

  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(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', xmlns: TagManager::XMLNS).nil?
  12. UpdateRemoteProfileService.new.call(xml.at_xpath('/xmlns:feed', xmlns: TagManager::XMLNS), account, true)
  13. end
  14. def process_entries(xml, account)
  15. xml.xpath('//xmlns:entry', xmlns: TagManager::XMLNS).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. 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. Rails.logger.debug "Creating remote status #{id}"
  35. status = status_from_xml(@xml)
  36. if verb == :share
  37. original_status = status_from_xml(@xml.at_xpath('.//activity:object', activity: TagManager::AS_XMLNS))
  38. status.reblog = original_status
  39. if original_status.nil?
  40. status.destroy
  41. return nil
  42. elsif original_status.reblog?
  43. status.reblog = original_status.reblog
  44. end
  45. end
  46. status.save!
  47. NotifyService.new.call(status.reblog.account, status) if status.reblog? && status.reblog.account.local?
  48. Rails.logger.debug "Queuing remote status #{status.id} (#{id}) for distribution"
  49. DistributionWorker.perform_async(status.id)
  50. status
  51. end
  52. def delete_status
  53. Rails.logger.debug "Deleting remote status #{id}"
  54. status = Status.find_by(uri: id)
  55. RemoveStatusService.new.call(status) unless status.nil?
  56. nil
  57. end
  58. def skip_unsupported_type?
  59. !([:post, :share, :delete].include?(verb) && [:activity, :note, :comment].include?(type))
  60. end
  61. def status_from_xml(entry)
  62. # Return early if status already exists in db
  63. status = find_status(id(entry))
  64. return status unless status.nil?
  65. # If status embeds an author, find that author
  66. # If that author cannot be found, don't record the status (do not misattribute)
  67. if account?(entry)
  68. begin
  69. account = find_or_resolve_account(acct(entry))
  70. return nil if account.nil?
  71. rescue Goldfinger::Error
  72. return nil
  73. end
  74. else
  75. account = @account
  76. end
  77. status = Status.create!(
  78. uri: id(entry),
  79. url: url(entry),
  80. account: account,
  81. text: content(entry),
  82. created_at: published(entry)
  83. )
  84. if thread?(entry)
  85. Rails.logger.debug "Trying to attach #{status.id} (#{id(entry)}) to #{thread(entry).first}"
  86. status.thread = find_or_resolve_status(status, *thread(entry))
  87. end
  88. mentions_from_xml(status, entry)
  89. hashtags_from_xml(status, entry)
  90. media_from_xml(status, entry)
  91. status
  92. end
  93. def find_or_resolve_account(acct)
  94. FollowRemoteAccountService.new.call(acct)
  95. end
  96. def find_or_resolve_status(parent, uri, url)
  97. status = find_status(uri)
  98. ThreadResolveWorker.perform_async(parent.id, url) if status.nil?
  99. status
  100. end
  101. def find_status(uri)
  102. if TagManager.instance.local_id?(uri)
  103. local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status')
  104. return Status.find(local_id)
  105. end
  106. Status.find_by(uri: uri)
  107. end
  108. def mentions_from_xml(parent, xml)
  109. processed_account_ids = []
  110. public_visibility = false
  111. xml.xpath('./xmlns:link[@rel="mentioned"]', xmlns: TagManager::XMLNS).each do |link|
  112. if link['ostatus:object-type'] == TagManager::TYPES[:collection] && link['href'] == TagManager::COLLECTIONS[:public]
  113. public_visibility = true
  114. next
  115. elsif link['ostatus:object-type'] == TagManager::TYPES[:group]
  116. next
  117. end
  118. url = Addressable::URI.parse(link['href'])
  119. mentioned_account = if TagManager.instance.local_domain?(url.host)
  120. Account.find_local(url.path.gsub('/users/', ''))
  121. else
  122. Account.find_by(url: link['href']) || FetchRemoteAccountService.new.call(link['href'])
  123. end
  124. next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id)
  125. mention = mentioned_account.mentions.where(status: parent).first_or_create(status: parent)
  126. # Notify local user
  127. NotifyService.new.call(mentioned_account, mention) if mentioned_account.local?
  128. # So we can skip duplicate mentions
  129. processed_account_ids << mentioned_account.id
  130. end
  131. parent.visibility = public_visibility ? :public : :unlisted
  132. parent.save!
  133. end
  134. def hashtags_from_xml(parent, xml)
  135. tags = xml.xpath('./xmlns:category', xmlns: TagManager::XMLNS).map { |category| category['term'] }.select { |t| !t.blank? }
  136. ProcessHashtagsService.new.call(parent, tags)
  137. end
  138. def media_from_xml(parent, xml)
  139. xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: TagManager::XMLNS).each do |link|
  140. next unless link['href']
  141. media = MediaAttachment.where(status: parent, remote_url: link['href']).first_or_initialize(account: parent.account, status: parent, remote_url: link['href'])
  142. begin
  143. media.file_remote_url = link['href']
  144. media.save
  145. rescue OpenURI::HTTPError, Paperclip::Errors::NotIdentifiedByImageMagickError
  146. next
  147. end
  148. end
  149. end
  150. def id(xml = @xml)
  151. xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content
  152. end
  153. def verb(xml = @xml)
  154. raw = xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content
  155. TagManager::VERBS.key(raw)
  156. rescue
  157. :post
  158. end
  159. def type(xml = @xml)
  160. raw = xml.at_xpath('./activity:object-type', activity: TagManager::AS_XMLNS).content
  161. TagManager::TYPES.key(raw)
  162. rescue
  163. :activity
  164. end
  165. def url(xml = @xml)
  166. link = xml.at_xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS)
  167. link.nil? ? nil : link['href']
  168. end
  169. def content(xml = @xml)
  170. xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content
  171. end
  172. def published(xml = @xml)
  173. xml.at_xpath('./xmlns:published', xmlns: TagManager::XMLNS).content
  174. end
  175. def thread?(xml = @xml)
  176. !xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS).nil?
  177. end
  178. def thread(xml = @xml)
  179. thr = xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS)
  180. [thr['ref'], thr['href']]
  181. end
  182. def account?(xml = @xml)
  183. !xml.at_xpath('./xmlns:author', xmlns: TagManager::XMLNS).nil?
  184. end
  185. def acct(xml = @xml)
  186. username = xml.at_xpath('./xmlns:author/xmlns:name', xmlns: TagManager::XMLNS).content
  187. url = xml.at_xpath('./xmlns:author/xmlns:uri', xmlns: TagManager::XMLNS).content
  188. domain = Addressable::URI.parse(url).host
  189. "#{username}@#{domain}"
  190. end
  191. end
  192. end