post_status_service.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # frozen_string_literal: true
  2. class PostStatusService < BaseService
  3. include Redisable
  4. MIN_SCHEDULE_OFFSET = 5.minutes.freeze
  5. # Post a text status update, fetch and notify remote users mentioned
  6. # @param [Account] account Account from which to post
  7. # @param [Hash] options
  8. # @option [String] :text Message
  9. # @option [Status] :thread Optional status to reply to
  10. # @option [Boolean] :sensitive
  11. # @option [String] :visibility
  12. # @option [String] :spoiler_text
  13. # @option [String] :language
  14. # @option [String] :scheduled_at
  15. # @option [Hash] :poll Optional poll to attach
  16. # @option [Enumerable] :media_ids Optional array of media IDs to attach
  17. # @option [Doorkeeper::Application] :application
  18. # @option [String] :idempotency Optional idempotency key
  19. # @return [Status]
  20. def call(account, options = {})
  21. @account = account
  22. @options = options
  23. @text = @options[:text] || ''
  24. @in_reply_to = @options[:thread]
  25. return idempotency_duplicate if idempotency_given? && idempotency_duplicate?
  26. validate_media!
  27. preprocess_attributes!
  28. if scheduled?
  29. schedule_status!
  30. else
  31. process_status!
  32. postprocess_status!
  33. bump_potential_friendship!
  34. end
  35. redis.setex(idempotency_key, 3_600, @status.id) if idempotency_given?
  36. @status
  37. end
  38. private
  39. def preprocess_attributes!
  40. @text = @options.delete(:spoiler_text) if @text.blank? && @options[:spoiler_text].present?
  41. @visibility = @options[:visibility] || @account.user&.setting_default_privacy
  42. @visibility = :unlisted if @visibility == :public && @account.silenced?
  43. @scheduled_at = @options[:scheduled_at]&.to_datetime
  44. @scheduled_at = nil if scheduled_in_the_past?
  45. rescue ArgumentError
  46. raise ActiveRecord::RecordInvalid
  47. end
  48. def process_status!
  49. # The following transaction block is needed to wrap the UPDATEs to
  50. # the media attachments when the status is created
  51. ApplicationRecord.transaction do
  52. @status = @account.statuses.create!(status_attributes)
  53. end
  54. process_hashtags_service.call(@status)
  55. process_mentions_service.call(@status)
  56. end
  57. def schedule_status!
  58. status_for_validation = @account.statuses.build(status_attributes)
  59. if status_for_validation.valid?
  60. status_for_validation.destroy
  61. # The following transaction block is needed to wrap the UPDATEs to
  62. # the media attachments when the scheduled status is created
  63. ApplicationRecord.transaction do
  64. @status = @account.scheduled_statuses.create!(scheduled_status_attributes)
  65. end
  66. else
  67. raise ActiveRecord::RecordInvalid
  68. end
  69. end
  70. def postprocess_status!
  71. LinkCrawlWorker.perform_async(@status.id) unless @status.spoiler_text?
  72. DistributionWorker.perform_async(@status.id)
  73. ActivityPub::DistributionWorker.perform_async(@status.id)
  74. PollExpirationNotifyWorker.perform_at(@status.poll.expires_at, @status.poll.id) if @status.poll
  75. end
  76. def validate_media!
  77. return if @options[:media_ids].blank? || !@options[:media_ids].is_a?(Enumerable)
  78. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > 4 || @options[:poll].present?
  79. @media = @account.media_attachments.where(status_id: nil).where(id: @options[:media_ids].take(4).map(&:to_i))
  80. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && @media.find(&:audio_or_video?)
  81. end
  82. def language_from_option(str)
  83. ISO_639.find(str)&.alpha2
  84. end
  85. def process_mentions_service
  86. ProcessMentionsService.new
  87. end
  88. def process_hashtags_service
  89. ProcessHashtagsService.new
  90. end
  91. def scheduled?
  92. @scheduled_at.present?
  93. end
  94. def idempotency_key
  95. "idempotency:status:#{@account.id}:#{@options[:idempotency]}"
  96. end
  97. def idempotency_given?
  98. @options[:idempotency].present?
  99. end
  100. def idempotency_duplicate
  101. if scheduled?
  102. @account.schedule_statuses.find(@idempotency_duplicate)
  103. else
  104. @account.statuses.find(@idempotency_duplicate)
  105. end
  106. end
  107. def idempotency_duplicate?
  108. @idempotency_duplicate = redis.get(idempotency_key)
  109. end
  110. def scheduled_in_the_past?
  111. @scheduled_at.present? && @scheduled_at <= Time.now.utc + MIN_SCHEDULE_OFFSET
  112. end
  113. def bump_potential_friendship!
  114. return if !@status.reply? || @account.id == @status.in_reply_to_account_id
  115. ActivityTracker.increment('activity:interactions')
  116. return if @account.following?(@status.in_reply_to_account_id)
  117. PotentialFriendshipTracker.record(@account.id, @status.in_reply_to_account_id, :reply)
  118. end
  119. def status_attributes
  120. {
  121. text: @text,
  122. media_attachments: @media || [],
  123. thread: @in_reply_to,
  124. poll_attributes: poll_attributes,
  125. sensitive: (@options[:sensitive].nil? ? @account.user&.setting_default_sensitive : @options[:sensitive]) || @options[:spoiler_text].present?,
  126. spoiler_text: @options[:spoiler_text] || '',
  127. visibility: @visibility,
  128. language: language_from_option(@options[:language]) || @account.user&.setting_default_language&.presence || LanguageDetector.instance.detect(@text, @account),
  129. application: @options[:application],
  130. }.compact
  131. end
  132. def scheduled_status_attributes
  133. {
  134. scheduled_at: @scheduled_at,
  135. media_attachments: @media || [],
  136. params: scheduled_options,
  137. }
  138. end
  139. def poll_attributes
  140. return if @options[:poll].blank?
  141. @options[:poll].merge(account: @account)
  142. end
  143. def scheduled_options
  144. @options.tap do |options_hash|
  145. options_hash[:in_reply_to_id] = options_hash.delete(:thread)&.id
  146. options_hash[:application_id] = options_hash.delete(:application)&.id
  147. options_hash[:scheduled_at] = nil
  148. options_hash[:idempotency] = nil
  149. end
  150. end
  151. end