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.

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