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.

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