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.

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