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.

460 lines
14 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::Activity::Create < ActivityPub::Activity
  3. def perform
  4. return reject_payload! if unsupported_object_type? || invalid_origin?(@object['id']) || Tombstone.exists?(uri: @object['id']) || !related_to_local_activity?
  5. RedisLock.acquire(lock_options) do |lock|
  6. if lock.acquired?
  7. return if delete_arrived_first?(object_uri) || poll_vote?
  8. @status = find_existing_status
  9. if @status.nil?
  10. process_status
  11. elsif @options[:delivered_to_account_id].present?
  12. postprocess_audience_and_deliver
  13. end
  14. else
  15. raise Mastodon::RaceConditionError
  16. end
  17. end
  18. @status
  19. end
  20. private
  21. def audience_to
  22. @object['to'] || @json['to']
  23. end
  24. def audience_cc
  25. @object['cc'] || @json['cc']
  26. end
  27. def process_status
  28. @tags = []
  29. @mentions = []
  30. @params = {}
  31. process_status_params
  32. process_tags
  33. process_audience
  34. ApplicationRecord.transaction do
  35. @status = Status.create!(@params)
  36. attach_tags(@status)
  37. end
  38. resolve_thread(@status)
  39. fetch_replies(@status)
  40. check_for_spam
  41. distribute(@status)
  42. forward_for_reply if @status.distributable?
  43. end
  44. def find_existing_status
  45. status = status_from_uri(object_uri)
  46. status ||= Status.find_by(uri: @object['atomUri']) if @object['atomUri'].present?
  47. status
  48. end
  49. def process_status_params
  50. @params = begin
  51. {
  52. uri: @object['id'],
  53. url: object_url || @object['id'],
  54. account: @account,
  55. text: text_from_content || '',
  56. language: detected_language,
  57. spoiler_text: converted_object_type? ? '' : (text_from_summary || ''),
  58. created_at: @object['published'],
  59. override_timestamps: @options[:override_timestamps],
  60. reply: @object['inReplyTo'].present?,
  61. sensitive: @object['sensitive'] || false,
  62. visibility: visibility_from_audience,
  63. thread: replied_to_status,
  64. conversation: conversation_from_uri(@object['conversation']),
  65. media_attachment_ids: process_attachments.take(4).map(&:id),
  66. poll: process_poll,
  67. }
  68. end
  69. end
  70. def process_audience
  71. (as_array(audience_to) + as_array(audience_cc)).uniq.each do |audience|
  72. next if audience == ActivityPub::TagManager::COLLECTIONS[:public]
  73. # Unlike with tags, there is no point in resolving accounts we don't already
  74. # know here, because silent mentions would only be used for local access
  75. # control anyway
  76. account = account_from_uri(audience)
  77. next if account.nil? || @mentions.any? { |mention| mention.account_id == account.id }
  78. @mentions << Mention.new(account: account, silent: true)
  79. # If there is at least one silent mention, then the status can be considered
  80. # as a limited-audience status, and not strictly a direct message, but only
  81. # if we considered a direct message in the first place
  82. next unless @params[:visibility] == :direct
  83. @params[:visibility] = :limited
  84. end
  85. # If the payload was delivered to a specific inbox, the inbox owner must have
  86. # access to it, unless they already have access to it anyway
  87. return if @options[:delivered_to_account_id].nil? || @mentions.any? { |mention| mention.account_id == @options[:delivered_to_account_id] }
  88. @mentions << Mention.new(account_id: @options[:delivered_to_account_id], silent: true)
  89. return unless @params[:visibility] == :direct
  90. @params[:visibility] = :limited
  91. end
  92. def postprocess_audience_and_deliver
  93. return if @status.mentions.find_by(account_id: @options[:delivered_to_account_id])
  94. delivered_to_account = Account.find(@options[:delivered_to_account_id])
  95. @status.mentions.create(account: delivered_to_account, silent: true)
  96. @status.update(visibility: :limited) if @status.direct_visibility?
  97. return unless delivered_to_account.following?(@account)
  98. FeedInsertWorker.perform_async(@status.id, delivered_to_account.id, :home)
  99. end
  100. def attach_tags(status)
  101. @tags.each do |tag|
  102. status.tags << tag
  103. TrendingTags.record_use!(tag, status.account, status.created_at) if status.public_visibility?
  104. end
  105. @mentions.each do |mention|
  106. mention.status = status
  107. mention.save
  108. end
  109. end
  110. def process_tags
  111. return if @object['tag'].nil?
  112. as_array(@object['tag']).each do |tag|
  113. if equals_or_includes?(tag['type'], 'Hashtag')
  114. process_hashtag tag
  115. elsif equals_or_includes?(tag['type'], 'Mention')
  116. process_mention tag
  117. elsif equals_or_includes?(tag['type'], 'Emoji')
  118. process_emoji tag
  119. end
  120. end
  121. end
  122. def process_hashtag(tag)
  123. return if tag['name'].blank?
  124. Tag.find_or_create_by_names(tag['name']) do |hashtag|
  125. @tags << hashtag unless @tags.include?(hashtag) || !hashtag.valid?
  126. end
  127. rescue ActiveRecord::RecordInvalid
  128. nil
  129. end
  130. def process_mention(tag)
  131. return if tag['href'].blank?
  132. account = account_from_uri(tag['href'])
  133. account = ActivityPub::FetchRemoteAccountService.new.call(tag['href']) if account.nil?
  134. return if account.nil?
  135. @mentions << Mention.new(account: account, silent: false)
  136. end
  137. def process_emoji(tag)
  138. return if skip_download?
  139. return if tag['name'].blank? || tag['icon'].blank? || tag['icon']['url'].blank?
  140. shortcode = tag['name'].delete(':')
  141. image_url = tag['icon']['url']
  142. uri = tag['id']
  143. updated = tag['updated']
  144. emoji = CustomEmoji.find_by(shortcode: shortcode, domain: @account.domain)
  145. return unless emoji.nil? || image_url != emoji.image_remote_url || (updated && updated >= emoji.updated_at)
  146. emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: shortcode, uri: uri)
  147. emoji.image_remote_url = image_url
  148. emoji.save
  149. end
  150. def process_attachments
  151. return [] if @object['attachment'].nil?
  152. media_attachments = []
  153. as_array(@object['attachment']).each do |attachment|
  154. next if attachment['url'].blank? || media_attachments.size >= 4
  155. begin
  156. href = Addressable::URI.parse(attachment['url']).normalize.to_s
  157. media_attachment = MediaAttachment.create(account: @account, remote_url: href, description: attachment['name'].presence, focus: attachment['focalPoint'], blurhash: supported_blurhash?(attachment['blurhash']) ? attachment['blurhash'] : nil)
  158. media_attachments << media_attachment
  159. next if unsupported_media_type?(attachment['mediaType']) || skip_download?
  160. media_attachment.file_remote_url = href
  161. media_attachment.save
  162. rescue Mastodon::UnexpectedResponseError, HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError
  163. RedownloadMediaWorker.perform_in(rand(30..600).seconds, media_attachment.id)
  164. end
  165. end
  166. media_attachments
  167. rescue Addressable::URI::InvalidURIError => e
  168. Rails.logger.debug "Invalid URL in attachment: #{e}"
  169. media_attachments
  170. end
  171. def process_poll
  172. return unless @object['type'] == 'Question' && (@object['anyOf'].is_a?(Array) || @object['oneOf'].is_a?(Array))
  173. expires_at = begin
  174. if @object['closed'].is_a?(String)
  175. @object['closed']
  176. elsif !@object['closed'].nil? && !@object['closed'].is_a?(FalseClass)
  177. Time.now.utc
  178. else
  179. @object['endTime']
  180. end
  181. end
  182. if @object['anyOf'].is_a?(Array)
  183. multiple = true
  184. items = @object['anyOf']
  185. else
  186. multiple = false
  187. items = @object['oneOf']
  188. end
  189. voters_count = @object['votersCount']
  190. @account.polls.new(
  191. multiple: multiple,
  192. expires_at: expires_at,
  193. options: items.map { |item| item['name'].presence || item['content'] }.compact,
  194. cached_tallies: items.map { |item| item.dig('replies', 'totalItems') || 0 },
  195. voters_count: voters_count
  196. )
  197. end
  198. def poll_vote?
  199. return false if replied_to_status.nil? || replied_to_status.preloadable_poll.nil? || !replied_to_status.local? || !replied_to_status.preloadable_poll.options.include?(@object['name'])
  200. poll_vote! unless replied_to_status.preloadable_poll.expired?
  201. true
  202. end
  203. def poll_vote!
  204. poll = replied_to_status.preloadable_poll
  205. already_voted = true
  206. RedisLock.acquire(poll_lock_options) do |lock|
  207. if lock.acquired?
  208. already_voted = poll.votes.where(account: @account).exists?
  209. poll.votes.create!(account: @account, choice: poll.options.index(@object['name']), uri: @object['id'])
  210. else
  211. raise Mastodon::RaceConditionError
  212. end
  213. end
  214. increment_voters_count! unless already_voted
  215. ActivityPub::DistributePollUpdateWorker.perform_in(3.minutes, replied_to_status.id) unless replied_to_status.preloadable_poll.hide_totals?
  216. end
  217. def resolve_thread(status)
  218. return unless status.reply? && status.thread.nil? && Request.valid_url?(in_reply_to_uri)
  219. ThreadResolveWorker.perform_async(status.id, in_reply_to_uri)
  220. end
  221. def fetch_replies(status)
  222. collection = @object['replies']
  223. return if collection.nil?
  224. replies = ActivityPub::FetchRepliesService.new.call(status, collection, false)
  225. return unless replies.nil?
  226. uri = value_or_id(collection)
  227. ActivityPub::FetchRepliesWorker.perform_async(status.id, uri) unless uri.nil?
  228. end
  229. def conversation_from_uri(uri)
  230. return nil if uri.nil?
  231. return Conversation.find_by(id: OStatus::TagManager.instance.unique_tag_to_local_id(uri, 'Conversation')) if OStatus::TagManager.instance.local_id?(uri)
  232. begin
  233. Conversation.find_or_create_by!(uri: uri)
  234. rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
  235. retry
  236. end
  237. end
  238. def visibility_from_audience
  239. if equals_or_includes?(audience_to, ActivityPub::TagManager::COLLECTIONS[:public])
  240. :public
  241. elsif equals_or_includes?(audience_cc, ActivityPub::TagManager::COLLECTIONS[:public])
  242. :unlisted
  243. elsif equals_or_includes?(audience_to, @account.followers_url)
  244. :private
  245. else
  246. :direct
  247. end
  248. end
  249. def audience_includes?(account)
  250. uri = ActivityPub::TagManager.instance.uri_for(account)
  251. equals_or_includes?(audience_to, uri) || equals_or_includes?(audience_cc, uri)
  252. end
  253. def replied_to_status
  254. return @replied_to_status if defined?(@replied_to_status)
  255. if in_reply_to_uri.blank?
  256. @replied_to_status = nil
  257. else
  258. @replied_to_status = status_from_uri(in_reply_to_uri)
  259. @replied_to_status ||= status_from_uri(@object['inReplyToAtomUri']) if @object['inReplyToAtomUri'].present?
  260. @replied_to_status
  261. end
  262. end
  263. def in_reply_to_uri
  264. value_or_id(@object['inReplyTo'])
  265. end
  266. def text_from_content
  267. return Formatter.instance.linkify([[text_from_name, text_from_summary.presence].compact.join("\n\n"), object_url || @object['id']].join(' ')) if converted_object_type?
  268. if @object['content'].present?
  269. @object['content']
  270. elsif content_language_map?
  271. @object['contentMap'].values.first
  272. end
  273. end
  274. def text_from_summary
  275. if @object['summary'].present?
  276. @object['summary']
  277. elsif summary_language_map?
  278. @object['summaryMap'].values.first
  279. end
  280. end
  281. def text_from_name
  282. if @object['name'].present?
  283. @object['name']
  284. elsif name_language_map?
  285. @object['nameMap'].values.first
  286. end
  287. end
  288. def detected_language
  289. if content_language_map?
  290. @object['contentMap'].keys.first
  291. elsif name_language_map?
  292. @object['nameMap'].keys.first
  293. elsif summary_language_map?
  294. @object['summaryMap'].keys.first
  295. elsif supported_object_type?
  296. LanguageDetector.instance.detect(text_from_content, @account)
  297. end
  298. end
  299. def object_url
  300. return if @object['url'].blank?
  301. url_candidate = url_to_href(@object['url'], 'text/html')
  302. if invalid_origin?(url_candidate)
  303. nil
  304. else
  305. url_candidate
  306. end
  307. end
  308. def summary_language_map?
  309. @object['summaryMap'].is_a?(Hash) && !@object['summaryMap'].empty?
  310. end
  311. def content_language_map?
  312. @object['contentMap'].is_a?(Hash) && !@object['contentMap'].empty?
  313. end
  314. def name_language_map?
  315. @object['nameMap'].is_a?(Hash) && !@object['nameMap'].empty?
  316. end
  317. def unsupported_media_type?(mime_type)
  318. mime_type.present? && !MediaAttachment.supported_mime_types.include?(mime_type)
  319. end
  320. def supported_blurhash?(blurhash)
  321. components = blurhash.blank? ? nil : Blurhash.components(blurhash)
  322. components.present? && components.none? { |comp| comp > 5 }
  323. end
  324. def skip_download?
  325. return @skip_download if defined?(@skip_download)
  326. @skip_download ||= DomainBlock.reject_media?(@account.domain)
  327. end
  328. def reply_to_local?
  329. !replied_to_status.nil? && replied_to_status.account.local?
  330. end
  331. def related_to_local_activity?
  332. fetch? || followed_by_local_accounts? || requested_through_relay? ||
  333. responds_to_followed_account? || addresses_local_accounts?
  334. end
  335. def responds_to_followed_account?
  336. !replied_to_status.nil? && (replied_to_status.account.local? || replied_to_status.account.passive_relationships.exists?)
  337. end
  338. def addresses_local_accounts?
  339. return true if @options[:delivered_to_account_id]
  340. local_usernames = (as_array(audience_to) + as_array(audience_cc)).uniq.select { |uri| ActivityPub::TagManager.instance.local_uri?(uri) }.map { |uri| ActivityPub::TagManager.instance.uri_to_local_id(uri, :username) }
  341. return false if local_usernames.empty?
  342. Account.local.where(username: local_usernames).exists?
  343. end
  344. def check_for_spam
  345. SpamCheck.perform(@status)
  346. end
  347. def forward_for_reply
  348. return unless @json['signature'].present? && reply_to_local?
  349. ActivityPub::RawDistributionWorker.perform_async(Oj.dump(@json), replied_to_status.account_id, [@account.preferred_inbox_url])
  350. end
  351. def increment_voters_count!
  352. poll = replied_to_status.preloadable_poll
  353. unless poll.voters_count.nil?
  354. poll.voters_count = poll.voters_count + 1
  355. poll.save
  356. end
  357. rescue ActiveRecord::StaleObjectError
  358. poll.reload
  359. retry
  360. end
  361. def lock_options
  362. { redis: Redis.current, key: "create:#{@object['id']}" }
  363. end
  364. def poll_lock_options
  365. { redis: Redis.current, key: "vote:#{replied_to_status.poll_id}:#{@account.id}" }
  366. end
  367. end