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.

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