The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

428 lines
13 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::Activity::Create < ActivityPub::Activity
  3. def perform
  4. return reject_payload! if unsupported_object_type? || invalid_origin?(@object['id']) || Tombstone.exists?(uri: @object['id']) || !related_to_local_activity?
  5. RedisLock.acquire(lock_options) do |lock|
  6. if lock.acquired?
  7. return if delete_arrived_first?(object_uri) || poll_vote?
  8. @status = find_existing_status
  9. if @status.nil?
  10. process_status
  11. elsif @options[:delivered_to_account_id].present?
  12. postprocess_audience_and_deliver
  13. end
  14. else
  15. raise Mastodon::RaceConditionError
  16. end
  17. end
  18. @status
  19. end
  20. private
  21. def process_status
  22. @tags = []
  23. @mentions = []
  24. @params = {}
  25. process_status_params
  26. process_tags
  27. process_audience
  28. ApplicationRecord.transaction do
  29. @status = Status.create!(@params)
  30. attach_tags(@status)
  31. end
  32. resolve_thread(@status)
  33. fetch_replies(@status)
  34. check_for_spam
  35. distribute(@status)
  36. forward_for_reply if @status.distributable?
  37. end
  38. def find_existing_status
  39. status = status_from_uri(object_uri)
  40. status ||= Status.find_by(uri: @object['atomUri']) if @object['atomUri'].present?
  41. status
  42. end
  43. def process_status_params
  44. @params = begin
  45. {
  46. uri: @object['id'],
  47. url: object_url || @object['id'],
  48. account: @account,
  49. text: text_from_content || '',
  50. language: detected_language,
  51. spoiler_text: converted_object_type? ? '' : (text_from_summary || ''),
  52. created_at: @object['published'],
  53. override_timestamps: @options[:override_timestamps],
  54. reply: @object['inReplyTo'].present?,
  55. sensitive: @object['sensitive'] || false,
  56. visibility: visibility_from_audience,
  57. thread: replied_to_status,
  58. conversation: conversation_from_uri(@object['conversation']),
  59. media_attachment_ids: process_attachments.take(4).map(&:id),
  60. poll: process_poll,
  61. }
  62. end
  63. end
  64. def process_audience
  65. (as_array(@object['to']) + as_array(@object['cc'])).uniq.each do |audience|
  66. next if audience == ActivityPub::TagManager::COLLECTIONS[:public]
  67. # Unlike with tags, there is no point in resolving accounts we don't already
  68. # know here, because silent mentions would only be used for local access
  69. # control anyway
  70. account = account_from_uri(audience)
  71. next if account.nil? || @mentions.any? { |mention| mention.account_id == account.id }
  72. @mentions << Mention.new(account: account, silent: true)
  73. # If there is at least one silent mention, then the status can be considered
  74. # as a limited-audience status, and not strictly a direct message, but only
  75. # if we considered a direct message in the first place
  76. next unless @params[:visibility] == :direct
  77. @params[:visibility] = :limited
  78. end
  79. # If the payload was delivered to a specific inbox, the inbox owner must have
  80. # access to it, unless they already have access to it anyway
  81. return if @options[:delivered_to_account_id].nil? || @mentions.any? { |mention| mention.account_id == @options[:delivered_to_account_id] }
  82. @mentions << Mention.new(account_id: @options[:delivered_to_account_id], silent: true)
  83. return unless @params[:visibility] == :direct
  84. @params[:visibility] = :limited
  85. end
  86. def postprocess_audience_and_deliver
  87. return if @status.mentions.find_by(account_id: @options[:delivered_to_account_id])
  88. delivered_to_account = Account.find(@options[:delivered_to_account_id])
  89. @status.mentions.create(account: delivered_to_account, silent: true)
  90. @status.update(visibility: :limited) if @status.direct_visibility?
  91. return unless delivered_to_account.following?(@account)
  92. FeedInsertWorker.perform_async(@status.id, delivered_to_account.id, :home)
  93. end
  94. def attach_tags(status)
  95. @tags.each do |tag|
  96. status.tags << tag
  97. TrendingTags.record_use!(tag, status.account, status.created_at) if status.public_visibility?
  98. end
  99. @mentions.each do |mention|
  100. mention.status = status
  101. mention.save
  102. end
  103. end
  104. def process_tags
  105. return if @object['tag'].nil?
  106. as_array(@object['tag']).each do |tag|
  107. if equals_or_includes?(tag['type'], 'Hashtag')
  108. process_hashtag tag
  109. elsif equals_or_includes?(tag['type'], 'Mention')
  110. process_mention tag
  111. elsif equals_or_includes?(tag['type'], 'Emoji')
  112. process_emoji tag
  113. end
  114. end
  115. end
  116. def process_hashtag(tag)
  117. return if tag['name'].blank?
  118. Tag.find_or_create_by_names(tag['name']) do |hashtag|
  119. @tags << hashtag unless @tags.include?(hashtag)
  120. end
  121. rescue ActiveRecord::RecordInvalid
  122. nil
  123. end
  124. def process_mention(tag)
  125. return if tag['href'].blank?
  126. account = account_from_uri(tag['href'])
  127. account = ::FetchRemoteAccountService.new.call(tag['href']) if account.nil?
  128. return if account.nil?
  129. @mentions << Mention.new(account: account, silent: false)
  130. end
  131. def process_emoji(tag)
  132. return if skip_download?
  133. return if tag['name'].blank? || tag['icon'].blank? || tag['icon']['url'].blank?
  134. shortcode = tag['name'].delete(':')
  135. image_url = tag['icon']['url']
  136. uri = tag['id']
  137. updated = tag['updated']
  138. emoji = CustomEmoji.find_by(shortcode: shortcode, domain: @account.domain)
  139. return unless emoji.nil? || image_url != emoji.image_remote_url || (updated && updated >= emoji.updated_at)
  140. emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: shortcode, uri: uri)
  141. emoji.image_remote_url = image_url
  142. emoji.save
  143. end
  144. def process_attachments
  145. return [] if @object['attachment'].nil?
  146. media_attachments = []
  147. as_array(@object['attachment']).each do |attachment|
  148. next if attachment['url'].blank?
  149. href = Addressable::URI.parse(attachment['url']).normalize.to_s
  150. media_attachment = MediaAttachment.create(account: @account, remote_url: href, description: attachment['name'].presence, focus: attachment['focalPoint'], blurhash: supported_blurhash?(attachment['blurhash']) ? attachment['blurhash'] : nil)
  151. media_attachments << media_attachment
  152. next if unsupported_media_type?(attachment['mediaType']) || skip_download?
  153. media_attachment.file_remote_url = href
  154. media_attachment.save
  155. end
  156. media_attachments
  157. rescue Addressable::URI::InvalidURIError => e
  158. Rails.logger.debug e
  159. media_attachments
  160. end
  161. def process_poll
  162. return unless @object['type'] == 'Question' && (@object['anyOf'].is_a?(Array) || @object['oneOf'].is_a?(Array))
  163. expires_at = begin
  164. if @object['closed'].is_a?(String)
  165. @object['closed']
  166. elsif !@object['closed'].nil? && !@object['closed'].is_a?(FalseClass)
  167. Time.now.utc
  168. else
  169. @object['endTime']
  170. end
  171. end
  172. if @object['anyOf'].is_a?(Array)
  173. multiple = true
  174. items = @object['anyOf']
  175. else
  176. multiple = false
  177. items = @object['oneOf']
  178. end
  179. @account.polls.new(
  180. multiple: multiple,
  181. expires_at: expires_at,
  182. options: items.map { |item| item['name'].presence || item['content'] }.compact,
  183. cached_tallies: items.map { |item| item.dig('replies', 'totalItems') || 0 }
  184. )
  185. end
  186. def poll_vote?
  187. return false if replied_to_status.nil? || replied_to_status.preloadable_poll.nil? || !replied_to_status.local? || !replied_to_status.preloadable_poll.options.include?(@object['name'])
  188. unless replied_to_status.preloadable_poll.expired?
  189. replied_to_status.preloadable_poll.votes.create!(account: @account, choice: replied_to_status.preloadable_poll.options.index(@object['name']), uri: @object['id'])
  190. ActivityPub::DistributePollUpdateWorker.perform_in(3.minutes, replied_to_status.id) unless replied_to_status.preloadable_poll.hide_totals?
  191. end
  192. true
  193. end
  194. def resolve_thread(status)
  195. return unless status.reply? && status.thread.nil? && Request.valid_url?(in_reply_to_uri)
  196. ThreadResolveWorker.perform_async(status.id, in_reply_to_uri)
  197. end
  198. def fetch_replies(status)
  199. collection = @object['replies']
  200. return if collection.nil?
  201. replies = ActivityPub::FetchRepliesService.new.call(status, collection, false)
  202. return unless replies.nil?
  203. uri = value_or_id(collection)
  204. ActivityPub::FetchRepliesWorker.perform_async(status.id, uri) unless uri.nil?
  205. end
  206. def conversation_from_uri(uri)
  207. return nil if uri.nil?
  208. return Conversation.find_by(id: OStatus::TagManager.instance.unique_tag_to_local_id(uri, 'Conversation')) if OStatus::TagManager.instance.local_id?(uri)
  209. begin
  210. Conversation.find_or_create_by!(uri: uri)
  211. rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
  212. retry
  213. end
  214. end
  215. def visibility_from_audience
  216. if equals_or_includes?(@object['to'], ActivityPub::TagManager::COLLECTIONS[:public])
  217. :public
  218. elsif equals_or_includes?(@object['cc'], ActivityPub::TagManager::COLLECTIONS[:public])
  219. :unlisted
  220. elsif equals_or_includes?(@object['to'], @account.followers_url)
  221. :private
  222. else
  223. :direct
  224. end
  225. end
  226. def audience_includes?(account)
  227. uri = ActivityPub::TagManager.instance.uri_for(account)
  228. equals_or_includes?(@object['to'], uri) || equals_or_includes?(@object['cc'], uri)
  229. end
  230. def replied_to_status
  231. return @replied_to_status if defined?(@replied_to_status)
  232. if in_reply_to_uri.blank?
  233. @replied_to_status = nil
  234. else
  235. @replied_to_status = status_from_uri(in_reply_to_uri)
  236. @replied_to_status ||= status_from_uri(@object['inReplyToAtomUri']) if @object['inReplyToAtomUri'].present?
  237. @replied_to_status
  238. end
  239. end
  240. def in_reply_to_uri
  241. value_or_id(@object['inReplyTo'])
  242. end
  243. def text_from_content
  244. return Formatter.instance.linkify([[text_from_name, text_from_summary.presence].compact.join("\n\n"), object_url || @object['id']].join(' ')) if converted_object_type?
  245. if @object['content'].present?
  246. @object['content']
  247. elsif content_language_map?
  248. @object['contentMap'].values.first
  249. end
  250. end
  251. def text_from_summary
  252. if @object['summary'].present?
  253. @object['summary']
  254. elsif summary_language_map?
  255. @object['summaryMap'].values.first
  256. end
  257. end
  258. def text_from_name
  259. if @object['name'].present?
  260. @object['name']
  261. elsif name_language_map?
  262. @object['nameMap'].values.first
  263. end
  264. end
  265. def detected_language
  266. if content_language_map?
  267. @object['contentMap'].keys.first
  268. elsif name_language_map?
  269. @object['nameMap'].keys.first
  270. elsif summary_language_map?
  271. @object['summaryMap'].keys.first
  272. elsif supported_object_type?
  273. LanguageDetector.instance.detect(text_from_content, @account)
  274. end
  275. end
  276. def object_url
  277. return if @object['url'].blank?
  278. url_candidate = url_to_href(@object['url'], 'text/html')
  279. if invalid_origin?(url_candidate)
  280. nil
  281. else
  282. url_candidate
  283. end
  284. end
  285. def summary_language_map?
  286. @object['summaryMap'].is_a?(Hash) && !@object['summaryMap'].empty?
  287. end
  288. def content_language_map?
  289. @object['contentMap'].is_a?(Hash) && !@object['contentMap'].empty?
  290. end
  291. def name_language_map?
  292. @object['nameMap'].is_a?(Hash) && !@object['nameMap'].empty?
  293. end
  294. def unsupported_media_type?(mime_type)
  295. mime_type.present? && !MediaAttachment.supported_mime_types.include?(mime_type)
  296. end
  297. def supported_blurhash?(blurhash)
  298. components = blurhash.blank? ? nil : Blurhash.components(blurhash)
  299. components.present? && components.none? { |comp| comp > 5 }
  300. end
  301. def skip_download?
  302. return @skip_download if defined?(@skip_download)
  303. @skip_download ||= DomainBlock.reject_media?(@account.domain)
  304. end
  305. def reply_to_local?
  306. !replied_to_status.nil? && replied_to_status.account.local?
  307. end
  308. def related_to_local_activity?
  309. fetch? || followed_by_local_accounts? || requested_through_relay? ||
  310. responds_to_followed_account? || addresses_local_accounts?
  311. end
  312. def responds_to_followed_account?
  313. !replied_to_status.nil? && (replied_to_status.account.local? || replied_to_status.account.passive_relationships.exists?)
  314. end
  315. def addresses_local_accounts?
  316. return true if @options[:delivered_to_account_id]
  317. local_usernames = (as_array(@object['to']) + as_array(@object['cc'])).uniq.select { |uri| ActivityPub::TagManager.instance.local_uri?(uri) }.map { |uri| ActivityPub::TagManager.instance.uri_to_local_id(uri, :username) }
  318. return false if local_usernames.empty?
  319. Account.local.where(username: local_usernames).exists?
  320. end
  321. def check_for_spam
  322. spam_check = SpamCheck.new(@status)
  323. return if spam_check.skip?
  324. if spam_check.spam?
  325. spam_check.flag!
  326. else
  327. spam_check.remember!
  328. end
  329. end
  330. def forward_for_reply
  331. return unless @json['signature'].present? && reply_to_local?
  332. ActivityPub::RawDistributionWorker.perform_async(Oj.dump(@json), replied_to_status.account_id, [@account.preferred_inbox_url])
  333. end
  334. def lock_options
  335. { redis: Redis.current, key: "create:#{@object['id']}" }
  336. end
  337. end