post_status_service.rb 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. Pubsubhubbub::DistributionWorker.perform_async(@status.stream_entry.id)
  74. ActivityPub::DistributionWorker.perform_async(@status.id)
  75. PollExpirationNotifyWorker.perform_at(@status.poll.expires_at, @status.poll.id) if @status.poll
  76. end
  77. def validate_media!
  78. return if @options[:media_ids].blank? || !@options[:media_ids].is_a?(Enumerable)
  79. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > 4 || @options[:poll].present?
  80. @media = @account.media_attachments.where(status_id: nil).where(id: @options[:media_ids].take(4).map(&:to_i))
  81. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && @media.find(&:video?)
  82. end
  83. def language_from_option(str)
  84. ISO_639.find(str)&.alpha2
  85. end
  86. def process_mentions_service
  87. ProcessMentionsService.new
  88. end
  89. def process_hashtags_service
  90. ProcessHashtagsService.new
  91. end
  92. def scheduled?
  93. @scheduled_at.present?
  94. end
  95. def idempotency_key
  96. "idempotency:status:#{@account.id}:#{@options[:idempotency]}"
  97. end
  98. def idempotency_given?
  99. @options[:idempotency].present?
  100. end
  101. def idempotency_duplicate
  102. if scheduled?
  103. @account.schedule_statuses.find(@idempotency_duplicate)
  104. else
  105. @account.statuses.find(@idempotency_duplicate)
  106. end
  107. end
  108. def idempotency_duplicate?
  109. @idempotency_duplicate = redis.get(idempotency_key)
  110. end
  111. def scheduled_in_the_past?
  112. @scheduled_at.present? && @scheduled_at <= Time.now.utc + MIN_SCHEDULE_OFFSET
  113. end
  114. def bump_potential_friendship!
  115. return if !@status.reply? || @account.id == @status.in_reply_to_account_id
  116. ActivityTracker.increment('activity:interactions')
  117. return if @account.following?(@status.in_reply_to_account_id)
  118. PotentialFriendshipTracker.record(@account.id, @status.in_reply_to_account_id, :reply)
  119. end
  120. def status_attributes
  121. {
  122. text: @text,
  123. media_attachments: @media || [],
  124. thread: @in_reply_to,
  125. poll_attributes: poll_attributes,
  126. sensitive: (@options[:sensitive].nil? ? @account.user&.setting_default_sensitive : @options[:sensitive]) || @options[:spoiler_text].present?,
  127. spoiler_text: @options[:spoiler_text] || '',
  128. visibility: @visibility,
  129. language: language_from_option(@options[:language]) || @account.user&.setting_default_language&.presence || LanguageDetector.instance.detect(@text, @account),
  130. application: @options[:application],
  131. }.compact
  132. end
  133. def scheduled_status_attributes
  134. {
  135. scheduled_at: @scheduled_at,
  136. media_attachments: @media || [],
  137. params: scheduled_options,
  138. }
  139. end
  140. def poll_attributes
  141. return if @options[:poll].blank?
  142. @options[:poll].merge(account: @account)
  143. end
  144. def scheduled_options
  145. @options.tap do |options_hash|
  146. options_hash[:in_reply_to_id] = options_hash.delete(:thread)&.id
  147. options_hash[:application_id] = options_hash.delete(:application)&.id
  148. options_hash[:scheduled_at] = nil
  149. options_hash[:idempotency] = nil
  150. end
  151. end
  152. end