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.

342 lines
9.8 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::Activity::Create < ActivityPub::Activity
  3. SUPPORTED_TYPES = %w(Note).freeze
  4. CONVERTED_TYPES = %w(Image Video Article Page).freeze
  5. def perform
  6. return if delete_arrived_first?(object_uri) || unsupported_object_type? || invalid_origin?(@object['id'])
  7. RedisLock.acquire(lock_options) do |lock|
  8. if lock.acquired?
  9. @status = find_existing_status
  10. process_status if @status.nil?
  11. else
  12. raise Mastodon::RaceConditionError
  13. end
  14. end
  15. @status
  16. end
  17. private
  18. def process_status
  19. @tags = []
  20. @mentions = []
  21. @params = {}
  22. process_status_params
  23. process_tags
  24. process_audience
  25. ApplicationRecord.transaction do
  26. @status = Status.create!(@params)
  27. attach_tags(@status)
  28. end
  29. resolve_thread(@status)
  30. distribute(@status)
  31. forward_for_reply if @status.public_visibility? || @status.unlisted_visibility?
  32. end
  33. def find_existing_status
  34. status = status_from_uri(object_uri)
  35. status ||= Status.find_by(uri: @object['atomUri']) if @object['atomUri'].present?
  36. status
  37. end
  38. def process_status_params
  39. @params = begin
  40. {
  41. uri: @object['id'],
  42. url: object_url || @object['id'],
  43. account: @account,
  44. text: text_from_content || '',
  45. language: detected_language,
  46. spoiler_text: text_from_summary || '',
  47. created_at: @object['published'],
  48. override_timestamps: @options[:override_timestamps],
  49. reply: @object['inReplyTo'].present?,
  50. sensitive: @object['sensitive'] || false,
  51. visibility: visibility_from_audience,
  52. thread: replied_to_status,
  53. conversation: conversation_from_uri(@object['conversation']),
  54. media_attachment_ids: process_attachments.take(4).map(&:id),
  55. }
  56. end
  57. end
  58. def process_audience
  59. (as_array(@object['to']) + as_array(@object['cc'])).uniq.each do |audience|
  60. next if audience == ActivityPub::TagManager::COLLECTIONS[:public]
  61. # Unlike with tags, there is no point in resolving accounts we don't already
  62. # know here, because silent mentions would only be used for local access
  63. # control anyway
  64. account = account_from_uri(audience)
  65. next if account.nil? || @mentions.any? { |mention| mention.account_id == account.id }
  66. @mentions << Mention.new(account: account, silent: true)
  67. # If there is at least one silent mention, then the status can be considered
  68. # as a limited-audience status, and not strictly a direct message, but only
  69. # if we considered a direct message in the first place
  70. next unless @params[:visibility] == :direct
  71. @params[:visibility] = :limited
  72. end
  73. # If the payload was delivered to a specific inbox, the inbox owner must have
  74. # access to it, unless they already have access to it anyway
  75. return if @options[:delivered_to_account_id].nil? || @mentions.any? { |mention| mention.account_id == @options[:delivered_to_account_id] }
  76. @mentions << Mention.new(account_id: @options[:delivered_to_account_id], silent: true)
  77. return unless @params[:visibility] == :direct
  78. @params[:visibility] = :limited
  79. end
  80. def attach_tags(status)
  81. @tags.each do |tag|
  82. status.tags << tag
  83. TrendingTags.record_use!(tag, status.account, status.created_at) if status.public_visibility?
  84. end
  85. @mentions.each do |mention|
  86. mention.status = status
  87. mention.save
  88. end
  89. end
  90. def process_tags
  91. return if @object['tag'].nil?
  92. as_array(@object['tag']).each do |tag|
  93. if equals_or_includes?(tag['type'], 'Hashtag')
  94. process_hashtag tag
  95. elsif equals_or_includes?(tag['type'], 'Mention')
  96. process_mention tag
  97. elsif equals_or_includes?(tag['type'], 'Emoji')
  98. process_emoji tag
  99. end
  100. end
  101. end
  102. def process_hashtag(tag)
  103. return if tag['name'].blank?
  104. hashtag = tag['name'].gsub(/\A#/, '').mb_chars.downcase
  105. hashtag = Tag.where(name: hashtag).first_or_create!(name: hashtag)
  106. return if @tags.include?(hashtag)
  107. @tags << hashtag
  108. rescue ActiveRecord::RecordInvalid
  109. nil
  110. end
  111. def process_mention(tag)
  112. return if tag['href'].blank?
  113. account = account_from_uri(tag['href'])
  114. account = ::FetchRemoteAccountService.new.call(tag['href'], id: false) if account.nil?
  115. return if account.nil?
  116. @mentions << Mention.new(account: account, silent: false)
  117. end
  118. def process_emoji(tag)
  119. return if skip_download?
  120. return if tag['name'].blank? || tag['icon'].blank? || tag['icon']['url'].blank?
  121. shortcode = tag['name'].delete(':')
  122. image_url = tag['icon']['url']
  123. uri = tag['id']
  124. updated = tag['updated']
  125. emoji = CustomEmoji.find_by(shortcode: shortcode, domain: @account.domain)
  126. return unless emoji.nil? || image_url != emoji.image_remote_url || (updated && emoji.updated_at >= updated)
  127. emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: shortcode, uri: uri)
  128. emoji.image_remote_url = image_url
  129. emoji.save
  130. end
  131. def process_attachments
  132. return [] if @object['attachment'].nil?
  133. media_attachments = []
  134. as_array(@object['attachment']).each do |attachment|
  135. next if attachment['url'].blank?
  136. href = Addressable::URI.parse(attachment['url']).normalize.to_s
  137. media_attachment = MediaAttachment.create(account: @account, remote_url: href, description: attachment['name'].presence, focus: attachment['focalPoint'])
  138. media_attachments << media_attachment
  139. next if unsupported_media_type?(attachment['mediaType']) || skip_download?
  140. media_attachment.file_remote_url = href
  141. media_attachment.save
  142. end
  143. media_attachments
  144. rescue Addressable::URI::InvalidURIError => e
  145. Rails.logger.debug e
  146. media_attachments
  147. end
  148. def resolve_thread(status)
  149. return unless status.reply? && status.thread.nil?
  150. ThreadResolveWorker.perform_async(status.id, in_reply_to_uri)
  151. end
  152. def conversation_from_uri(uri)
  153. return nil if uri.nil?
  154. return Conversation.find_by(id: OStatus::TagManager.instance.unique_tag_to_local_id(uri, 'Conversation')) if OStatus::TagManager.instance.local_id?(uri)
  155. Conversation.find_by(uri: uri) || Conversation.create(uri: uri)
  156. end
  157. def visibility_from_audience
  158. if equals_or_includes?(@object['to'], ActivityPub::TagManager::COLLECTIONS[:public])
  159. :public
  160. elsif equals_or_includes?(@object['cc'], ActivityPub::TagManager::COLLECTIONS[:public])
  161. :unlisted
  162. elsif equals_or_includes?(@object['to'], @account.followers_url)
  163. :private
  164. else
  165. :direct
  166. end
  167. end
  168. def audience_includes?(account)
  169. uri = ActivityPub::TagManager.instance.uri_for(account)
  170. equals_or_includes?(@object['to'], uri) || equals_or_includes?(@object['cc'], uri)
  171. end
  172. def replied_to_status
  173. return @replied_to_status if defined?(@replied_to_status)
  174. if in_reply_to_uri.blank?
  175. @replied_to_status = nil
  176. else
  177. @replied_to_status = status_from_uri(in_reply_to_uri)
  178. @replied_to_status ||= status_from_uri(@object['inReplyToAtomUri']) if @object['inReplyToAtomUri'].present?
  179. @replied_to_status
  180. end
  181. end
  182. def in_reply_to_uri
  183. value_or_id(@object['inReplyTo'])
  184. end
  185. def text_from_content
  186. return Formatter.instance.linkify([text_from_name, object_url || @object['id']].join(' ')) if converted_object_type?
  187. if @object['content'].present?
  188. @object['content']
  189. elsif content_language_map?
  190. @object['contentMap'].values.first
  191. end
  192. end
  193. def text_from_summary
  194. if @object['summary'].present?
  195. @object['summary']
  196. elsif summary_language_map?
  197. @object['summaryMap'].values.first
  198. end
  199. end
  200. def text_from_name
  201. if @object['name'].present?
  202. @object['name']
  203. elsif name_language_map?
  204. @object['nameMap'].values.first
  205. end
  206. end
  207. def detected_language
  208. if content_language_map?
  209. @object['contentMap'].keys.first
  210. elsif name_language_map?
  211. @object['nameMap'].keys.first
  212. elsif summary_language_map?
  213. @object['summaryMap'].keys.first
  214. elsif supported_object_type?
  215. LanguageDetector.instance.detect(text_from_content, @account)
  216. end
  217. end
  218. def object_url
  219. return if @object['url'].blank?
  220. url_candidate = url_to_href(@object['url'], 'text/html')
  221. if invalid_origin?(url_candidate)
  222. nil
  223. else
  224. url_candidate
  225. end
  226. end
  227. def summary_language_map?
  228. @object['summaryMap'].is_a?(Hash) && !@object['summaryMap'].empty?
  229. end
  230. def content_language_map?
  231. @object['contentMap'].is_a?(Hash) && !@object['contentMap'].empty?
  232. end
  233. def name_language_map?
  234. @object['nameMap'].is_a?(Hash) && !@object['nameMap'].empty?
  235. end
  236. def unsupported_object_type?
  237. @object.is_a?(String) || !(supported_object_type? || converted_object_type?)
  238. end
  239. def unsupported_media_type?(mime_type)
  240. mime_type.present? && !(MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES).include?(mime_type)
  241. end
  242. def supported_object_type?
  243. equals_or_includes_any?(@object['type'], SUPPORTED_TYPES)
  244. end
  245. def converted_object_type?
  246. equals_or_includes_any?(@object['type'], CONVERTED_TYPES)
  247. end
  248. def skip_download?
  249. return @skip_download if defined?(@skip_download)
  250. @skip_download ||= DomainBlock.find_by(domain: @account.domain)&.reject_media?
  251. end
  252. def invalid_origin?(url)
  253. return true if unsupported_uri_scheme?(url)
  254. needle = Addressable::URI.parse(url).host
  255. haystack = Addressable::URI.parse(@account.uri).host
  256. !haystack.casecmp(needle).zero?
  257. end
  258. def reply_to_local?
  259. !replied_to_status.nil? && replied_to_status.account.local?
  260. end
  261. def forward_for_reply
  262. return unless @json['signature'].present? && reply_to_local?
  263. ActivityPub::RawDistributionWorker.perform_async(Oj.dump(@json), replied_to_status.account_id, [@account.preferred_inbox_url])
  264. end
  265. def lock_options
  266. { redis: Redis.current, key: "create:#{@object['id']}" }
  267. end
  268. end