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.

175 lines
4.9 KiB

  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. status = find_existing_status
  6. return status unless status.nil?
  7. ApplicationRecord.transaction do
  8. status = Status.create!(status_params)
  9. process_tags(status)
  10. process_attachments(status)
  11. end
  12. resolve_thread(status)
  13. distribute(status)
  14. forward_for_reply if status.public_visibility? || status.unlisted_visibility?
  15. status
  16. end
  17. private
  18. def find_existing_status
  19. status = Status.find_by(uri: object_uri)
  20. status ||= Status.find_by(uri: @object['_:atomUri']) if @object['_:atomUri'].present?
  21. status
  22. end
  23. def status_params
  24. {
  25. uri: @object['id'],
  26. url: @object['url'] || @object['id'],
  27. account: @account,
  28. text: text_from_content || '',
  29. language: language_from_content,
  30. spoiler_text: @object['summary'] || '',
  31. created_at: @object['published'] || Time.now.utc,
  32. reply: @object['inReplyTo'].present?,
  33. sensitive: @object['sensitive'] || false,
  34. visibility: visibility_from_audience,
  35. thread: replied_to_status,
  36. conversation: conversation_from_uri(@object['_:conversation']),
  37. }
  38. end
  39. def process_tags(status)
  40. return unless @object['tag'].is_a?(Array)
  41. @object['tag'].each do |tag|
  42. case tag['type']
  43. when 'Hashtag'
  44. process_hashtag tag, status
  45. when 'Mention'
  46. process_mention tag, status
  47. end
  48. end
  49. end
  50. def process_hashtag(tag, status)
  51. hashtag = tag['name'].gsub(/\A#/, '').mb_chars.downcase
  52. hashtag = Tag.where(name: hashtag).first_or_initialize(name: hashtag)
  53. status.tags << hashtag
  54. end
  55. def process_mention(tag, status)
  56. account = account_from_uri(tag['href'])
  57. account = FetchRemoteAccountService.new.call(tag['href']) if account.nil?
  58. return if account.nil?
  59. account.mentions.create(status: status)
  60. end
  61. def process_attachments(status)
  62. return unless @object['attachment'].is_a?(Array)
  63. @object['attachment'].each do |attachment|
  64. next if unsupported_media_type?(attachment['mediaType'])
  65. href = Addressable::URI.parse(attachment['url']).normalize.to_s
  66. media_attachment = MediaAttachment.create(status: status, account: status.account, remote_url: href)
  67. next if skip_download?
  68. media_attachment.file_remote_url = href
  69. media_attachment.save
  70. end
  71. end
  72. def resolve_thread(status)
  73. return unless status.reply? && status.thread.nil?
  74. ThreadResolveWorker.perform_async(status.id, in_reply_to_uri)
  75. end
  76. def conversation_from_uri(uri)
  77. return nil if uri.nil?
  78. return Conversation.find_by(id: TagManager.instance.unique_tag_to_local_id(uri, 'Conversation')) if TagManager.instance.local_id?(uri)
  79. Conversation.find_by(uri: uri) || Conversation.create!(uri: uri)
  80. end
  81. def visibility_from_audience
  82. if equals_or_includes?(@object['to'], ActivityPub::TagManager::COLLECTIONS[:public])
  83. :public
  84. elsif equals_or_includes?(@object['cc'], ActivityPub::TagManager::COLLECTIONS[:public])
  85. :unlisted
  86. elsif equals_or_includes?(@object['to'], @account.followers_url)
  87. :private
  88. else
  89. :direct
  90. end
  91. end
  92. def audience_includes?(account)
  93. uri = ActivityPub::TagManager.instance.uri_for(account)
  94. equals_or_includes?(@object['to'], uri) || equals_or_includes?(@object['cc'], uri)
  95. end
  96. def replied_to_status
  97. return @replied_to_status if defined?(@replied_to_status)
  98. if in_reply_to_uri.blank?
  99. @replied_to_status = nil
  100. else
  101. @replied_to_status = status_from_uri(in_reply_to_uri)
  102. @replied_to_status ||= status_from_uri(@object['_:inReplyToAtomUri']) if @object['_:inReplyToAtomUri'].present?
  103. @replied_to_status
  104. end
  105. end
  106. def in_reply_to_uri
  107. value_or_id(@object['inReplyTo'])
  108. end
  109. def text_from_content
  110. if @object['content'].present?
  111. @object['content']
  112. elsif language_map?
  113. @object['contentMap'].values.first
  114. end
  115. end
  116. def language_from_content
  117. return nil unless language_map?
  118. @object['contentMap'].keys.first
  119. end
  120. def language_map?
  121. @object['contentMap'].is_a?(Hash) && !@object['contentMap'].empty?
  122. end
  123. def unsupported_object_type?
  124. @object.is_a?(String) || !%w(Article Note).include?(@object['type'])
  125. end
  126. def unsupported_media_type?(mime_type)
  127. mime_type.present? && !(MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES).include?(mime_type)
  128. end
  129. def skip_download?
  130. return @skip_download if defined?(@skip_download)
  131. @skip_download ||= DomainBlock.find_by(domain: @account.domain)&.reject_media?
  132. end
  133. def reply_to_local?
  134. !replied_to_status.nil? && replied_to_status.account.local?
  135. end
  136. def forward_for_reply
  137. return unless @json['signature'].present? && reply_to_local?
  138. ActivityPub::RawDistributionWorker.perform_async(Oj.dump(@json), replied_to_status.account_id)
  139. end
  140. end