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.

201 lines
6.2 KiB

8 years ago
8 years ago
  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. # @option [Boolean] :with_rate_limit
  20. # @return [Status]
  21. def call(account, options = {})
  22. @account = account
  23. @options = options
  24. @text = @options[:text] || ''
  25. @in_reply_to = @options[:thread]
  26. return idempotency_duplicate if idempotency_given? && idempotency_duplicate?
  27. validate_media!
  28. preprocess_attributes!
  29. if scheduled?
  30. schedule_status!
  31. else
  32. process_status!
  33. postprocess_status!
  34. bump_potential_friendship!
  35. end
  36. redis.setex(idempotency_key, 3_600, @status.id) if idempotency_given?
  37. @status
  38. end
  39. private
  40. def preprocess_attributes!
  41. if @text.blank? && @options[:spoiler_text].present?
  42. @text = '.'
  43. if @media.find(&:video?) || @media.find(&:gifv?)
  44. @text = '📹'
  45. elsif @media.find(&:audio?)
  46. @text = '🎵'
  47. elsif @media.find(&:image?)
  48. @text = '🖼'
  49. end
  50. end
  51. @visibility = @options[:visibility] || @account.user&.setting_default_privacy
  52. @visibility = :unlisted if @visibility&.to_sym == :public && @account.silenced?
  53. @scheduled_at = @options[:scheduled_at]&.to_datetime
  54. @scheduled_at = nil if scheduled_in_the_past?
  55. rescue ArgumentError
  56. raise ActiveRecord::RecordInvalid
  57. end
  58. def process_status!
  59. # The following transaction block is needed to wrap the UPDATEs to
  60. # the media attachments when the status is created
  61. ApplicationRecord.transaction do
  62. @status = @account.statuses.create!(status_attributes)
  63. end
  64. process_hashtags_service.call(@status)
  65. process_mentions_service.call(@status)
  66. end
  67. def schedule_status!
  68. status_for_validation = @account.statuses.build(status_attributes)
  69. if status_for_validation.valid?
  70. status_for_validation.destroy
  71. # The following transaction block is needed to wrap the UPDATEs to
  72. # the media attachments when the scheduled status is created
  73. ApplicationRecord.transaction do
  74. @status = @account.scheduled_statuses.create!(scheduled_status_attributes)
  75. end
  76. else
  77. raise ActiveRecord::RecordInvalid
  78. end
  79. end
  80. def postprocess_status!
  81. LinkCrawlWorker.perform_async(@status.id) unless @status.spoiler_text?
  82. DistributionWorker.perform_async(@status.id)
  83. ActivityPub::DistributionWorker.perform_async(@status.id) unless @status.local_only?
  84. PollExpirationNotifyWorker.perform_at(@status.poll.expires_at, @status.poll.id) if @status.poll
  85. end
  86. def validate_media!
  87. return if @options[:media_ids].blank? || !@options[:media_ids].is_a?(Enumerable)
  88. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > 4 || @options[:poll].present?
  89. @media = @account.media_attachments.where(status_id: nil).where(id: @options[:media_ids].take(4).map(&:to_i))
  90. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && @media.find(&:audio_or_video?)
  91. end
  92. def language_from_option(str)
  93. ISO_639.find(str)&.alpha2
  94. end
  95. def process_mentions_service
  96. ProcessMentionsService.new
  97. end
  98. def process_hashtags_service
  99. ProcessHashtagsService.new
  100. end
  101. def scheduled?
  102. @scheduled_at.present?
  103. end
  104. def idempotency_key
  105. "idempotency:status:#{@account.id}:#{@options[:idempotency]}"
  106. end
  107. def idempotency_given?
  108. @options[:idempotency].present?
  109. end
  110. def idempotency_duplicate
  111. if scheduled?
  112. @account.schedule_statuses.find(@idempotency_duplicate)
  113. else
  114. @account.statuses.find(@idempotency_duplicate)
  115. end
  116. end
  117. def idempotency_duplicate?
  118. @idempotency_duplicate = redis.get(idempotency_key)
  119. end
  120. def scheduled_in_the_past?
  121. @scheduled_at.present? && @scheduled_at <= Time.now.utc + MIN_SCHEDULE_OFFSET
  122. end
  123. def bump_potential_friendship!
  124. return if !@status.reply? || @account.id == @status.in_reply_to_account_id
  125. ActivityTracker.increment('activity:interactions')
  126. return if @account.following?(@status.in_reply_to_account_id)
  127. PotentialFriendshipTracker.record(@account.id, @status.in_reply_to_account_id, :reply)
  128. end
  129. def status_attributes
  130. {
  131. text: @text,
  132. media_attachments: @media || [],
  133. thread: @in_reply_to,
  134. poll_attributes: poll_attributes,
  135. sensitive: (@options[:sensitive].nil? ? @account.user&.setting_default_sensitive : @options[:sensitive]) || @options[:spoiler_text].present?,
  136. spoiler_text: @options[:spoiler_text] || '',
  137. visibility: @visibility,
  138. language: language_from_option(@options[:language]) || @account.user&.setting_default_language&.presence || LanguageDetector.instance.detect(@text, @account),
  139. application: @options[:application],
  140. content_type: @options[:content_type] || @account.user&.setting_default_content_type,
  141. rate_limit: @options[:with_rate_limit],
  142. }.compact
  143. end
  144. def scheduled_status_attributes
  145. {
  146. scheduled_at: @scheduled_at,
  147. media_attachments: @media || [],
  148. params: scheduled_options,
  149. }
  150. end
  151. def poll_attributes
  152. return if @options[:poll].blank?
  153. @options[:poll].merge(account: @account, voters_count: 0)
  154. end
  155. def scheduled_options
  156. @options.tap do |options_hash|
  157. options_hash[:in_reply_to_id] = options_hash.delete(:thread)&.id
  158. options_hash[:application_id] = options_hash.delete(:application)&.id
  159. options_hash[:scheduled_at] = nil
  160. options_hash[:idempotency] = nil
  161. options_hash[:with_rate_limit] = false
  162. end
  163. end
  164. end