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.

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