create.rb 11 KB

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