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.

154 lines
4.3 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'],
  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 = ActivityPub::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. ActivityPub::ThreadResolveWorker.perform_async(status.id, @object['inReplyTo'])
  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 if @object['inReplyTo'].blank?
  97. @replied_to_status ||= status_from_uri(@object['inReplyTo'])
  98. end
  99. def text_from_content
  100. if @object['content'].present?
  101. @object['content']
  102. elsif language_map?
  103. @object['contentMap'].values.first
  104. end
  105. end
  106. def language_from_content
  107. return nil unless language_map?
  108. @object['contentMap'].keys.first
  109. end
  110. def language_map?
  111. @object['contentMap'].is_a?(Hash) && !@object['contentMap'].empty?
  112. end
  113. def unsupported_object_type?
  114. @object.is_a?(String) || !%w(Article Note).include?(@object['type'])
  115. end
  116. def unsupported_media_type?(mime_type)
  117. mime_type.present? && !(MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES).include?(mime_type)
  118. end
  119. def skip_download?
  120. return @skip_download if defined?(@skip_download)
  121. @skip_download ||= DomainBlock.find_by(domain: @account.domain)&.reject_media?
  122. end
  123. end