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.

516 lines
16 KiB

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