闭社主体 forked from https://github.com/tootsuite/mastodon
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.

276 lines
7.8 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::Activity::Create < ActivityPub::Activity
  3. SUPPORTED_TYPES = %w(Note).freeze
  4. CONVERTED_TYPES = %w(Image Video Article).freeze
  5. def perform
  6. return if delete_arrived_first?(object_uri) || unsupported_object_type? || invalid_origin?(@object['id'])
  7. RedisLock.acquire(lock_options) do |lock|
  8. if lock.acquired?
  9. @status = find_existing_status
  10. process_status if @status.nil?
  11. else
  12. raise Mastodon::RaceConditionError
  13. end
  14. end
  15. @status
  16. end
  17. private
  18. def process_status
  19. status_params = process_status_params
  20. ApplicationRecord.transaction do
  21. @status = Status.create!(status_params)
  22. process_tags(@status)
  23. end
  24. resolve_thread(@status)
  25. distribute(@status)
  26. forward_for_reply if @status.public_visibility? || @status.unlisted_visibility?
  27. end
  28. def find_existing_status
  29. status = status_from_uri(object_uri)
  30. status ||= Status.find_by(uri: @object['atomUri']) if @object['atomUri'].present?
  31. status
  32. end
  33. def process_status_params
  34. {
  35. uri: @object['id'],
  36. url: object_url || @object['id'],
  37. account: @account,
  38. text: text_from_content || '',
  39. language: detected_language,
  40. spoiler_text: @object['summary'] || '',
  41. created_at: @object['published'],
  42. override_timestamps: @options[:override_timestamps],
  43. reply: @object['inReplyTo'].present?,
  44. sensitive: @object['sensitive'] || false,
  45. visibility: visibility_from_audience,
  46. thread: replied_to_status,
  47. conversation: conversation_from_uri(@object['conversation']),
  48. media_attachment_ids: process_attachments.take(4).map(&:id),
  49. }
  50. end
  51. def process_tags(status)
  52. return if @object['tag'].nil?
  53. as_array(@object['tag']).each do |tag|
  54. if equals_or_includes?(tag['type'], 'Hashtag')
  55. process_hashtag tag, status
  56. elsif equals_or_includes?(tag['type'], 'Mention')
  57. process_mention tag, status
  58. elsif equals_or_includes?(tag['type'], 'Emoji')
  59. process_emoji tag, status
  60. end
  61. end
  62. end
  63. def process_hashtag(tag, status)
  64. return if tag['name'].blank?
  65. hashtag = tag['name'].gsub(/\A#/, '').mb_chars.downcase
  66. hashtag = Tag.where(name: hashtag).first_or_create(name: hashtag)
  67. return if status.tags.include?(hashtag)
  68. status.tags << hashtag
  69. TrendingTags.record_use!(hashtag, status.account, status.created_at) if status.public_visibility?
  70. rescue ActiveRecord::RecordInvalid
  71. nil
  72. end
  73. def process_mention(tag, status)
  74. return if tag['href'].blank?
  75. account = account_from_uri(tag['href'])
  76. account = FetchRemoteAccountService.new.call(tag['href'], id: false) if account.nil?
  77. return if account.nil?
  78. account.mentions.create(status: status)
  79. end
  80. def process_emoji(tag, _status)
  81. return if skip_download?
  82. return if tag['name'].blank? || tag['icon'].blank? || tag['icon']['url'].blank?
  83. shortcode = tag['name'].delete(':')
  84. image_url = tag['icon']['url']
  85. uri = tag['id']
  86. updated = tag['updated']
  87. emoji = CustomEmoji.find_by(shortcode: shortcode, domain: @account.domain)
  88. return unless emoji.nil? || emoji.updated_at >= updated
  89. emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: shortcode, uri: uri)
  90. emoji.image_remote_url = image_url
  91. emoji.save
  92. end
  93. def process_attachments
  94. return [] if @object['attachment'].nil?
  95. media_attachments = []
  96. as_array(@object['attachment']).each do |attachment|
  97. next if attachment['url'].blank?
  98. href = Addressable::URI.parse(attachment['url']).normalize.to_s
  99. media_attachment = MediaAttachment.create(account: @account, remote_url: href, description: attachment['name'].presence, focus: attachment['focalPoint'])
  100. media_attachments << media_attachment
  101. next if unsupported_media_type?(attachment['mediaType']) || skip_download?
  102. media_attachment.file_remote_url = href
  103. media_attachment.save
  104. end
  105. media_attachments
  106. rescue Addressable::URI::InvalidURIError => e
  107. Rails.logger.debug e
  108. media_attachments
  109. end
  110. def resolve_thread(status)
  111. return unless status.reply? && status.thread.nil?
  112. ThreadResolveWorker.perform_async(status.id, in_reply_to_uri)
  113. end
  114. def conversation_from_uri(uri)
  115. return nil if uri.nil?
  116. return Conversation.find_by(id: OStatus::TagManager.instance.unique_tag_to_local_id(uri, 'Conversation')) if OStatus::TagManager.instance.local_id?(uri)
  117. Conversation.find_by(uri: uri) || Conversation.create(uri: uri)
  118. end
  119. def visibility_from_audience
  120. if equals_or_includes?(@object['to'], ActivityPub::TagManager::COLLECTIONS[:public])
  121. :public
  122. elsif equals_or_includes?(@object['cc'], ActivityPub::TagManager::COLLECTIONS[:public])
  123. :unlisted
  124. elsif equals_or_includes?(@object['to'], @account.followers_url)
  125. :private
  126. else
  127. :direct
  128. end
  129. end
  130. def audience_includes?(account)
  131. uri = ActivityPub::TagManager.instance.uri_for(account)
  132. equals_or_includes?(@object['to'], uri) || equals_or_includes?(@object['cc'], uri)
  133. end
  134. def replied_to_status
  135. return @replied_to_status if defined?(@replied_to_status)
  136. if in_reply_to_uri.blank?
  137. @replied_to_status = nil
  138. else
  139. @replied_to_status = status_from_uri(in_reply_to_uri)
  140. @replied_to_status ||= status_from_uri(@object['inReplyToAtomUri']) if @object['inReplyToAtomUri'].present?
  141. @replied_to_status
  142. end
  143. end
  144. def in_reply_to_uri
  145. value_or_id(@object['inReplyTo'])
  146. end
  147. def text_from_content
  148. return Formatter.instance.linkify([text_from_name, object_url || @object['id']].join(' ')) if converted_object_type?
  149. if @object['content'].present?
  150. @object['content']
  151. elsif content_language_map?
  152. @object['contentMap'].values.first
  153. end
  154. end
  155. def text_from_name
  156. if @object['name'].present?
  157. @object['name']
  158. elsif name_language_map?
  159. @object['nameMap'].values.first
  160. end
  161. end
  162. def detected_language
  163. if content_language_map?
  164. @object['contentMap'].keys.first
  165. elsif name_language_map?
  166. @object['nameMap'].keys.first
  167. elsif supported_object_type?
  168. LanguageDetector.instance.detect(text_from_content, @account)
  169. end
  170. end
  171. def object_url
  172. return if @object['url'].blank?
  173. url_candidate = url_to_href(@object['url'], 'text/html')
  174. if invalid_origin?(url_candidate)
  175. nil
  176. else
  177. url_candidate
  178. end
  179. end
  180. def content_language_map?
  181. @object['contentMap'].is_a?(Hash) && !@object['contentMap'].empty?
  182. end
  183. def name_language_map?
  184. @object['nameMap'].is_a?(Hash) && !@object['nameMap'].empty?
  185. end
  186. def unsupported_object_type?
  187. @object.is_a?(String) || !(supported_object_type? || converted_object_type?)
  188. end
  189. def unsupported_media_type?(mime_type)
  190. mime_type.present? && !(MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES).include?(mime_type)
  191. end
  192. def supported_object_type?
  193. equals_or_includes_any?(@object['type'], SUPPORTED_TYPES)
  194. end
  195. def converted_object_type?
  196. equals_or_includes_any?(@object['type'], CONVERTED_TYPES)
  197. end
  198. def skip_download?
  199. return @skip_download if defined?(@skip_download)
  200. @skip_download ||= DomainBlock.find_by(domain: @account.domain)&.reject_media?
  201. end
  202. def invalid_origin?(url)
  203. return true if unsupported_uri_scheme?(url)
  204. needle = Addressable::URI.parse(url).host
  205. haystack = Addressable::URI.parse(@account.uri).host
  206. !haystack.casecmp(needle).zero?
  207. end
  208. def reply_to_local?
  209. !replied_to_status.nil? && replied_to_status.account.local?
  210. end
  211. def forward_for_reply
  212. return unless @json['signature'].present? && reply_to_local?
  213. ActivityPub::RawDistributionWorker.perform_async(Oj.dump(@json), replied_to_status.account_id, [@account.preferred_inbox_url])
  214. end
  215. def lock_options
  216. { redis: Redis.current, key: "create:#{@object['id']}" }
  217. end
  218. end