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.

244 lines
6.9 KiB

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