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.

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