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.

290 lines
8.2 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).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. status_params = process_status_params
  20. ApplicationRecord.transaction do
  21. @status = Status.create!(status_params)
  22. process_tags(@status)
  23. end
  24. resolve_thread(@status)
  25. distribute(@status)
  26. forward_for_reply if @status.public_visibility? || @status.unlisted_visibility?
  27. end
  28. def find_existing_status
  29. status = status_from_uri(object_uri)
  30. status ||= Status.find_by(uri: @object['atomUri']) if @object['atomUri'].present?
  31. status
  32. end
  33. def process_status_params
  34. {
  35. uri: @object['id'],
  36. url: object_url || @object['id'],
  37. account: @account,
  38. text: text_from_content || '',
  39. language: detected_language,
  40. spoiler_text: text_from_summary || '',
  41. created_at: @object['published'],
  42. override_timestamps: @options[:override_timestamps],
  43. reply: @object['inReplyTo'].present?,
  44. sensitive: @object['sensitive'] || false,
  45. visibility: visibility_from_audience,
  46. thread: replied_to_status,
  47. conversation: conversation_from_uri(@object['conversation']),
  48. media_attachment_ids: process_attachments.take(4).map(&:id),
  49. }
  50. end
  51. def process_tags(status)
  52. return if @object['tag'].nil?
  53. as_array(@object['tag']).each do |tag|
  54. if equals_or_includes?(tag['type'], 'Hashtag')
  55. process_hashtag tag, status
  56. elsif equals_or_includes?(tag['type'], 'Mention')
  57. process_mention tag, status
  58. elsif equals_or_includes?(tag['type'], 'Emoji')
  59. process_emoji tag, status
  60. end
  61. end
  62. end
  63. def process_hashtag(tag, status)
  64. return if tag['name'].blank?
  65. hashtag = tag['name'].gsub(/\A#/, '').mb_chars.downcase
  66. hashtag = Tag.where(name: hashtag).first_or_create(name: hashtag)
  67. return if status.tags.include?(hashtag)
  68. status.tags << hashtag
  69. TrendingTags.record_use!(hashtag, status.account, status.created_at) if status.public_visibility?
  70. rescue ActiveRecord::RecordInvalid
  71. nil
  72. end
  73. def process_mention(tag, status)
  74. return if tag['href'].blank?
  75. account = account_from_uri(tag['href'])
  76. account = FetchRemoteAccountService.new.call(tag['href'], id: false) if account.nil?
  77. return if account.nil?
  78. account.mentions.create(status: status)
  79. end
  80. def process_emoji(tag, _status)
  81. return if skip_download?
  82. return if tag['name'].blank? || tag['icon'].blank? || tag['icon']['url'].blank?
  83. shortcode = tag['name'].delete(':')
  84. image_url = tag['icon']['url']
  85. uri = tag['id']
  86. updated = tag['updated']
  87. emoji = CustomEmoji.find_by(shortcode: shortcode, domain: @account.domain)
  88. return unless emoji.nil? || image_url != emoji.image_remote_url || (updated && emoji.updated_at >= updated)
  89. emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: shortcode, uri: uri)
  90. emoji.image_remote_url = image_url
  91. emoji.save
  92. end
  93. def process_attachments
  94. return [] if @object['attachment'].nil?
  95. media_attachments = []
  96. as_array(@object['attachment']).each do |attachment|
  97. next if attachment['url'].blank?
  98. href = Addressable::URI.parse(attachment['url']).normalize.to_s
  99. media_attachment = MediaAttachment.create(account: @account, remote_url: href, description: attachment['name'].presence, focus: attachment['focalPoint'])
  100. media_attachments << media_attachment
  101. next if unsupported_media_type?(attachment['mediaType']) || skip_download?
  102. media_attachment.file_remote_url = href
  103. media_attachment.save
  104. end
  105. media_attachments
  106. rescue Addressable::URI::InvalidURIError => e
  107. Rails.logger.debug e
  108. media_attachments
  109. end
  110. def resolve_thread(status)
  111. return unless status.reply? && status.thread.nil?
  112. ThreadResolveWorker.perform_async(status.id, in_reply_to_uri)
  113. end
  114. def conversation_from_uri(uri)
  115. return nil if uri.nil?
  116. return Conversation.find_by(id: OStatus::TagManager.instance.unique_tag_to_local_id(uri, 'Conversation')) if OStatus::TagManager.instance.local_id?(uri)
  117. Conversation.find_by(uri: uri) || Conversation.create(uri: uri)
  118. end
  119. def visibility_from_audience
  120. if equals_or_includes?(@object['to'], ActivityPub::TagManager::COLLECTIONS[:public])
  121. :public
  122. elsif equals_or_includes?(@object['cc'], ActivityPub::TagManager::COLLECTIONS[:public])
  123. :unlisted
  124. elsif equals_or_includes?(@object['to'], @account.followers_url)
  125. :private
  126. else
  127. :direct
  128. end
  129. end
  130. def audience_includes?(account)
  131. uri = ActivityPub::TagManager.instance.uri_for(account)
  132. equals_or_includes?(@object['to'], uri) || equals_or_includes?(@object['cc'], uri)
  133. end
  134. def replied_to_status
  135. return @replied_to_status if defined?(@replied_to_status)
  136. if in_reply_to_uri.blank?
  137. @replied_to_status = nil
  138. else
  139. @replied_to_status = status_from_uri(in_reply_to_uri)
  140. @replied_to_status ||= status_from_uri(@object['inReplyToAtomUri']) if @object['inReplyToAtomUri'].present?
  141. @replied_to_status
  142. end
  143. end
  144. def in_reply_to_uri
  145. value_or_id(@object['inReplyTo'])
  146. end
  147. def text_from_content
  148. return Formatter.instance.linkify([text_from_name, object_url || @object['id']].join(' ')) if converted_object_type?
  149. if @object['content'].present?
  150. @object['content']
  151. elsif content_language_map?
  152. @object['contentMap'].values.first
  153. end
  154. end
  155. def text_from_summary
  156. if @object['summary'].present?
  157. @object['summary']
  158. elsif summary_language_map?
  159. @object['summaryMap'].values.first
  160. end
  161. end
  162. def text_from_name
  163. if @object['name'].present?
  164. @object['name']
  165. elsif name_language_map?
  166. @object['nameMap'].values.first
  167. end
  168. end
  169. def detected_language
  170. if content_language_map?
  171. @object['contentMap'].keys.first
  172. elsif name_language_map?
  173. @object['nameMap'].keys.first
  174. elsif summary_language_map?
  175. @object['summaryMap'].keys.first
  176. elsif supported_object_type?
  177. LanguageDetector.instance.detect(text_from_content, @account)
  178. end
  179. end
  180. def object_url
  181. return if @object['url'].blank?
  182. url_candidate = url_to_href(@object['url'], 'text/html')
  183. if invalid_origin?(url_candidate)
  184. nil
  185. else
  186. url_candidate
  187. end
  188. end
  189. def summary_language_map?
  190. @object['summaryMap'].is_a?(Hash) && !@object['summaryMap'].empty?
  191. end
  192. def content_language_map?
  193. @object['contentMap'].is_a?(Hash) && !@object['contentMap'].empty?
  194. end
  195. def name_language_map?
  196. @object['nameMap'].is_a?(Hash) && !@object['nameMap'].empty?
  197. end
  198. def unsupported_object_type?
  199. @object.is_a?(String) || !(supported_object_type? || converted_object_type?)
  200. end
  201. def unsupported_media_type?(mime_type)
  202. mime_type.present? && !(MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES).include?(mime_type)
  203. end
  204. def supported_object_type?
  205. equals_or_includes_any?(@object['type'], SUPPORTED_TYPES)
  206. end
  207. def converted_object_type?
  208. equals_or_includes_any?(@object['type'], CONVERTED_TYPES)
  209. end
  210. def skip_download?
  211. return @skip_download if defined?(@skip_download)
  212. @skip_download ||= DomainBlock.find_by(domain: @account.domain)&.reject_media?
  213. end
  214. def invalid_origin?(url)
  215. return true if unsupported_uri_scheme?(url)
  216. needle = Addressable::URI.parse(url).host
  217. haystack = Addressable::URI.parse(@account.uri).host
  218. !haystack.casecmp(needle).zero?
  219. end
  220. def reply_to_local?
  221. !replied_to_status.nil? && replied_to_status.account.local?
  222. end
  223. def forward_for_reply
  224. return unless @json['signature'].present? && reply_to_local?
  225. ActivityPub::RawDistributionWorker.perform_async(Oj.dump(@json), replied_to_status.account_id, [@account.preferred_inbox_url])
  226. end
  227. def lock_options
  228. { redis: Redis.current, key: "create:#{@object['id']}" }
  229. end
  230. end