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.

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