create.rb 6.2 KB

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