create.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. # frozen_string_literal: true
  2. class ActivityPub::Activity::Create < ActivityPub::Activity
  3. include FormattingHelper
  4. def perform
  5. @account.schedule_refresh_if_stale!
  6. dereference_object!
  7. case @object['type']
  8. when 'EncryptedMessage'
  9. create_encrypted_message
  10. else
  11. create_status
  12. end
  13. end
  14. private
  15. def create_encrypted_message
  16. return reject_payload! if non_matching_uri_hosts?(@account.uri, object_uri) || @options[:delivered_to_account_id].blank?
  17. target_account = Account.find(@options[:delivered_to_account_id])
  18. target_device = target_account.devices.find_by(device_id: @object.dig('to', 'deviceId'))
  19. return if target_device.nil?
  20. target_device.encrypted_messages.create!(
  21. from_account: @account,
  22. from_device_id: @object.dig('attributedTo', 'deviceId'),
  23. type: @object['messageType'],
  24. body: @object['cipherText'],
  25. digest: @object.dig('digest', 'digestValue'),
  26. message_franking: message_franking.to_token
  27. )
  28. end
  29. def message_franking
  30. MessageFranking.new(
  31. hmac: @object.dig('digest', 'digestValue'),
  32. original_franking: @object['messageFranking'],
  33. source_account_id: @account.id,
  34. target_account_id: @options[:delivered_to_account_id],
  35. timestamp: Time.now.utc
  36. )
  37. end
  38. def create_status
  39. return reject_payload! if unsupported_object_type? || non_matching_uri_hosts?(@account.uri, object_uri) || tombstone_exists? || !related_to_local_activity?
  40. with_redis_lock("create:#{object_uri}") do
  41. return if delete_arrived_first?(object_uri) || poll_vote?
  42. @status = find_existing_status
  43. if @status.nil?
  44. process_status
  45. elsif @options[:delivered_to_account_id].present?
  46. postprocess_audience_and_deliver
  47. end
  48. end
  49. @status
  50. end
  51. def audience_to
  52. as_array(@object['to'] || @json['to']).map { |x| value_or_id(x) }
  53. end
  54. def audience_cc
  55. as_array(@object['cc'] || @json['cc']).map { |x| value_or_id(x) }
  56. end
  57. def process_status
  58. @tags = []
  59. @mentions = []
  60. @silenced_account_ids = []
  61. @params = {}
  62. process_status_params
  63. process_tags
  64. process_audience
  65. ApplicationRecord.transaction do
  66. @status = Status.create!(@params)
  67. attach_tags(@status)
  68. end
  69. resolve_thread(@status)
  70. fetch_replies(@status)
  71. distribute
  72. forward_for_reply
  73. end
  74. def distribute
  75. # Spread out crawling randomly to avoid DDoSing the link
  76. LinkCrawlWorker.perform_in(rand(1..59).seconds, @status.id)
  77. # Distribute into home and list feeds and notify mentioned accounts
  78. ::DistributionWorker.perform_async(@status.id, { 'silenced_account_ids' => @silenced_account_ids }) if @options[:override_timestamps] || @status.within_realtime_window?
  79. end
  80. def find_existing_status
  81. status = status_from_uri(object_uri)
  82. status ||= Status.find_by(uri: @object['atomUri']) if @object['atomUri'].present?
  83. status
  84. end
  85. def process_status_params
  86. @status_parser = ActivityPub::Parser::StatusParser.new(@json, followers_collection: @account.followers_url)
  87. @params = {
  88. uri: @status_parser.uri,
  89. url: @status_parser.url || @status_parser.uri,
  90. account: @account,
  91. text: converted_object_type? ? converted_text : (@status_parser.text || ''),
  92. language: @status_parser.language,
  93. spoiler_text: converted_object_type? ? '' : (@status_parser.spoiler_text || ''),
  94. created_at: @status_parser.created_at,
  95. edited_at: @status_parser.edited_at && @status_parser.edited_at != @status_parser.created_at ? @status_parser.edited_at : nil,
  96. override_timestamps: @options[:override_timestamps],
  97. reply: @status_parser.reply,
  98. sensitive: @account.sensitized? || @status_parser.sensitive || false,
  99. visibility: @status_parser.visibility,
  100. thread: replied_to_status,
  101. conversation: conversation_from_uri(@object['conversation']),
  102. media_attachment_ids: process_attachments.take(4).map(&:id),
  103. poll: process_poll,
  104. }
  105. end
  106. def process_audience
  107. # Unlike with tags, there is no point in resolving accounts we don't already
  108. # know here, because silent mentions would only be used for local access control anyway
  109. accounts_in_audience = (audience_to + audience_cc).uniq.filter_map do |audience|
  110. account_from_uri(audience) unless ActivityPub::TagManager.instance.public_collection?(audience)
  111. end
  112. # If the payload was delivered to a specific inbox, the inbox owner must have
  113. # access to it, unless they already have access to it anyway
  114. if @options[:delivered_to_account_id]
  115. accounts_in_audience << delivered_to_account
  116. accounts_in_audience.uniq!
  117. end
  118. accounts_in_audience.each do |account|
  119. # This runs after tags are processed, and those translate into non-silent
  120. # mentions, which take precedence
  121. next if @mentions.any? { |mention| mention.account_id == account.id }
  122. @mentions << Mention.new(account: account, silent: true)
  123. # If there is at least one silent mention, then the status can be considered
  124. # as a limited-audience status, and not strictly a direct message, but only
  125. # if we considered a direct message in the first place
  126. @params[:visibility] = :limited if @params[:visibility] == :direct
  127. end
  128. # Accounts that are tagged but are not in the audience are not
  129. # supposed to be notified explicitly
  130. @silenced_account_ids = @mentions.map(&:account_id) - accounts_in_audience.map(&:id)
  131. end
  132. def postprocess_audience_and_deliver
  133. return if @status.mentions.find_by(account_id: @options[:delivered_to_account_id])
  134. @status.mentions.create(account: delivered_to_account, silent: true)
  135. @status.update(visibility: :limited) if @status.direct_visibility?
  136. return unless delivered_to_account.following?(@account)
  137. FeedInsertWorker.perform_async(@status.id, delivered_to_account.id, 'home')
  138. end
  139. def delivered_to_account
  140. @delivered_to_account ||= Account.find(@options[:delivered_to_account_id])
  141. end
  142. def attach_tags(status)
  143. @tags.each do |tag|
  144. status.tags << tag
  145. tag.update(last_status_at: status.created_at) if tag.last_status_at.nil? || (tag.last_status_at < status.created_at && tag.last_status_at < 12.hours.ago)
  146. end
  147. # If we're processing an old status, this may register tags as being used now
  148. # as opposed to when the status was really published, but this is probably
  149. # not a big deal
  150. Trends.tags.register(status)
  151. @mentions.each do |mention|
  152. mention.status = status
  153. mention.save
  154. end
  155. end
  156. def process_tags
  157. return if @object['tag'].nil?
  158. as_array(@object['tag']).each do |tag|
  159. if equals_or_includes?(tag['type'], 'Hashtag')
  160. process_hashtag tag
  161. elsif equals_or_includes?(tag['type'], 'Mention')
  162. process_mention tag
  163. elsif equals_or_includes?(tag['type'], 'Emoji')
  164. process_emoji tag
  165. end
  166. end
  167. end
  168. def process_hashtag(tag)
  169. return if tag['name'].blank?
  170. Tag.find_or_create_by_names(tag['name']) do |hashtag|
  171. @tags << hashtag unless @tags.include?(hashtag) || !hashtag.valid?
  172. end
  173. rescue ActiveRecord::RecordInvalid
  174. nil
  175. end
  176. def process_mention(tag)
  177. return if tag['href'].blank?
  178. account = account_from_uri(tag['href'])
  179. account = ActivityPub::FetchRemoteAccountService.new.call(tag['href'], request_id: @options[:request_id]) if account.nil?
  180. return if account.nil?
  181. @mentions << Mention.new(account: account, silent: false)
  182. end
  183. def process_emoji(tag)
  184. return if skip_download?
  185. custom_emoji_parser = ActivityPub::Parser::CustomEmojiParser.new(tag)
  186. return if custom_emoji_parser.shortcode.blank? || custom_emoji_parser.image_remote_url.blank?
  187. emoji = CustomEmoji.find_by(shortcode: custom_emoji_parser.shortcode, domain: @account.domain)
  188. return unless emoji.nil? || custom_emoji_parser.image_remote_url != emoji.image_remote_url || (custom_emoji_parser.updated_at && custom_emoji_parser.updated_at >= emoji.updated_at)
  189. begin
  190. emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: custom_emoji_parser.shortcode, uri: custom_emoji_parser.uri)
  191. emoji.image_remote_url = custom_emoji_parser.image_remote_url
  192. emoji.save
  193. rescue Seahorse::Client::NetworkingError => e
  194. Rails.logger.warn "Error storing emoji: #{e}"
  195. end
  196. end
  197. def process_attachments
  198. return [] if @object['attachment'].nil?
  199. media_attachments = []
  200. as_array(@object['attachment']).each do |attachment|
  201. media_attachment_parser = ActivityPub::Parser::MediaAttachmentParser.new(attachment)
  202. next if media_attachment_parser.remote_url.blank? || media_attachments.size >= 4
  203. begin
  204. media_attachment = MediaAttachment.create(
  205. account: @account,
  206. remote_url: media_attachment_parser.remote_url,
  207. thumbnail_remote_url: media_attachment_parser.thumbnail_remote_url,
  208. description: media_attachment_parser.description,
  209. focus: media_attachment_parser.focus,
  210. blurhash: media_attachment_parser.blurhash
  211. )
  212. media_attachments << media_attachment
  213. next if unsupported_media_type?(media_attachment_parser.file_content_type) || skip_download?
  214. media_attachment.download_file!
  215. media_attachment.download_thumbnail!
  216. media_attachment.save
  217. rescue Mastodon::UnexpectedResponseError, HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError
  218. RedownloadMediaWorker.perform_in(rand(30..600).seconds, media_attachment.id)
  219. rescue Seahorse::Client::NetworkingError => e
  220. Rails.logger.warn "Error storing media attachment: #{e}"
  221. end
  222. end
  223. media_attachments
  224. rescue Addressable::URI::InvalidURIError => e
  225. Rails.logger.debug { "Invalid URL in attachment: #{e}" }
  226. media_attachments
  227. end
  228. def process_poll
  229. poll_parser = ActivityPub::Parser::PollParser.new(@object)
  230. return unless poll_parser.valid?
  231. @account.polls.new(
  232. multiple: poll_parser.multiple,
  233. expires_at: poll_parser.expires_at,
  234. options: poll_parser.options,
  235. cached_tallies: poll_parser.cached_tallies,
  236. voters_count: poll_parser.voters_count
  237. )
  238. end
  239. def poll_vote?
  240. 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'])
  241. poll_vote! unless replied_to_status.preloadable_poll.expired?
  242. true
  243. end
  244. def poll_vote!
  245. poll = replied_to_status.preloadable_poll
  246. already_voted = true
  247. with_redis_lock("vote:#{replied_to_status.poll_id}:#{@account.id}") do
  248. already_voted = poll.votes.where(account: @account).exists?
  249. poll.votes.create!(account: @account, choice: poll.options.index(@object['name']), uri: object_uri)
  250. end
  251. increment_voters_count! unless already_voted
  252. ActivityPub::DistributePollUpdateWorker.perform_in(3.minutes, replied_to_status.id) unless replied_to_status.preloadable_poll.hide_totals?
  253. end
  254. def resolve_thread(status)
  255. return unless status.reply? && status.thread.nil? && Request.valid_url?(in_reply_to_uri)
  256. ThreadResolveWorker.perform_async(status.id, in_reply_to_uri, { 'request_id' => @options[:request_id] })
  257. end
  258. def fetch_replies(status)
  259. collection = @object['replies']
  260. return if collection.nil?
  261. replies = ActivityPub::FetchRepliesService.new.call(status, collection, allow_synchronous_requests: false, request_id: @options[:request_id])
  262. return unless replies.nil?
  263. uri = value_or_id(collection)
  264. ActivityPub::FetchRepliesWorker.perform_async(status.id, uri, { 'request_id' => @options[:request_id] }) unless uri.nil?
  265. end
  266. def conversation_from_uri(uri)
  267. return nil if uri.nil?
  268. return Conversation.find_by(id: OStatus::TagManager.instance.unique_tag_to_local_id(uri, 'Conversation')) if OStatus::TagManager.instance.local_id?(uri)
  269. begin
  270. Conversation.find_or_create_by!(uri: uri)
  271. rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
  272. retry
  273. end
  274. end
  275. def replied_to_status
  276. return @replied_to_status if defined?(@replied_to_status)
  277. if in_reply_to_uri.blank?
  278. @replied_to_status = nil
  279. else
  280. @replied_to_status = status_from_uri(in_reply_to_uri)
  281. @replied_to_status ||= status_from_uri(@object['inReplyToAtomUri']) if @object['inReplyToAtomUri'].present?
  282. @replied_to_status
  283. end
  284. end
  285. def in_reply_to_uri
  286. value_or_id(@object['inReplyTo'])
  287. end
  288. def converted_text
  289. linkify([@status_parser.title.presence, @status_parser.spoiler_text.presence, @status_parser.url || @status_parser.uri].compact.join("\n\n"))
  290. end
  291. def unsupported_media_type?(mime_type)
  292. mime_type.present? && !MediaAttachment.supported_mime_types.include?(mime_type)
  293. end
  294. def skip_download?
  295. return @skip_download if defined?(@skip_download)
  296. @skip_download ||= DomainBlock.reject_media?(@account.domain)
  297. end
  298. def reply_to_local?
  299. !replied_to_status.nil? && replied_to_status.account.local?
  300. end
  301. def related_to_local_activity?
  302. fetch? || followed_by_local_accounts? || requested_through_relay? ||
  303. responds_to_followed_account? || addresses_local_accounts?
  304. end
  305. def responds_to_followed_account?
  306. !replied_to_status.nil? && (replied_to_status.account.local? || replied_to_status.account.passive_relationships.exists?)
  307. end
  308. def addresses_local_accounts?
  309. return true if @options[:delivered_to_account_id]
  310. local_usernames = (audience_to + audience_cc).uniq.select { |uri| ActivityPub::TagManager.instance.local_uri?(uri) }.map { |uri| ActivityPub::TagManager.instance.uri_to_local_id(uri, :username) }
  311. return false if local_usernames.empty?
  312. Account.local.where(username: local_usernames).exists?
  313. end
  314. def tombstone_exists?
  315. Tombstone.exists?(uri: object_uri)
  316. end
  317. def forward_for_reply
  318. return unless @status.distributable? && @json['signature'].present? && reply_to_local?
  319. ActivityPub::RawDistributionWorker.perform_async(Oj.dump(@json), replied_to_status.account_id, [@account.preferred_inbox_url])
  320. end
  321. def increment_voters_count!
  322. poll = replied_to_status.preloadable_poll
  323. unless poll.voters_count.nil?
  324. poll.voters_count = poll.voters_count + 1
  325. poll.save
  326. end
  327. rescue ActiveRecord::StaleObjectError
  328. poll.reload
  329. retry
  330. end
  331. end