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.

186 lines
5.5 KiB

8 years ago
8 years ago
  1. # frozen_string_literal: true
  2. class PostStatusService < BaseService
  3. MIN_SCHEDULE_OFFSET = 5.minutes.freeze
  4. # Post a text status update, fetch and notify remote users mentioned
  5. # @param [Account] account Account from which to post
  6. # @param [Hash] options
  7. # @option [String] :text Message
  8. # @option [Status] :thread Optional status to reply to
  9. # @option [Boolean] :sensitive
  10. # @option [String] :visibility
  11. # @option [String] :spoiler_text
  12. # @option [String] :language
  13. # @option [String] :scheduled_at
  14. # @option [Enumerable] :media_ids Optional array of media IDs to attach
  15. # @option [Doorkeeper::Application] :application
  16. # @option [String] :idempotency Optional idempotency key
  17. # @return [Status]
  18. def call(account, options = {})
  19. @account = account
  20. @options = options
  21. @text = @options[:text] || ''
  22. @in_reply_to = @options[:thread]
  23. return idempotency_duplicate if idempotency_given? && idempotency_duplicate?
  24. validate_media!
  25. preprocess_attributes!
  26. if scheduled?
  27. schedule_status!
  28. else
  29. process_status!
  30. postprocess_status!
  31. bump_potential_friendship!
  32. end
  33. redis.setex(idempotency_key, 3_600, @status.id) if idempotency_given?
  34. @status
  35. end
  36. private
  37. def preprocess_attributes!
  38. if @text.blank? && @options[:spoiler_text].present?
  39. @text = '.'
  40. @text = @media.find(&:video?) ? '📹' : '🖼' if @media.size > 0
  41. end
  42. @visibility = @options[:visibility] || @account.user&.setting_default_privacy
  43. @visibility = :unlisted if @visibility == :public && @account.silenced
  44. @scheduled_at = @options[:scheduled_at]&.to_datetime
  45. @scheduled_at = nil if scheduled_in_the_past?
  46. rescue ArgumentError
  47. raise ActiveRecord::RecordInvalid
  48. end
  49. def process_status!
  50. # The following transaction block is needed to wrap the UPDATEs to
  51. # the media attachments when the status is created
  52. ApplicationRecord.transaction do
  53. @status = @account.statuses.create!(status_attributes)
  54. end
  55. process_hashtags_service.call(@status)
  56. process_mentions_service.call(@status)
  57. end
  58. def schedule_status!
  59. status_for_validation = @account.statuses.build(status_attributes)
  60. if status_for_validation.valid?
  61. status_for_validation.destroy
  62. # The following transaction block is needed to wrap the UPDATEs to
  63. # the media attachments when the scheduled status is created
  64. ApplicationRecord.transaction do
  65. @status = @account.scheduled_statuses.create!(scheduled_status_attributes)
  66. end
  67. else
  68. raise ActiveRecord::RecordInvalid
  69. end
  70. end
  71. def postprocess_status!
  72. LinkCrawlWorker.perform_async(@status.id) unless @status.spoiler_text?
  73. DistributionWorker.perform_async(@status.id)
  74. unless @status.local_only?
  75. Pubsubhubbub::DistributionWorker.perform_async(@status.stream_entry.id)
  76. ActivityPub::DistributionWorker.perform_async(@status.id)
  77. end
  78. end
  79. def validate_media!
  80. return if @options[:media_ids].blank? || !@options[:media_ids].is_a?(Enumerable)
  81. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > 4
  82. @media = MediaAttachment.where(status_id: nil).where(id: @options[:media_ids].take(4).map(&:to_i))
  83. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && @media.find(&:video?)
  84. end
  85. def language_from_option(str)
  86. ISO_639.find(str)&.alpha2
  87. end
  88. def process_mentions_service
  89. ProcessMentionsService.new
  90. end
  91. def process_hashtags_service
  92. ProcessHashtagsService.new
  93. end
  94. def redis
  95. Redis.current
  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. sensitive: (@options[:sensitive].nil? ? @account.user&.setting_default_sensitive : @options[:sensitive]) || @options[:spoiler_text].present?,
  131. spoiler_text: @options[:spoiler_text] || '',
  132. visibility: @visibility,
  133. language: language_from_option(@options[:language]) || @account.user&.setting_default_language&.presence || LanguageDetector.instance.detect(@text, @account),
  134. application: @options[:application],
  135. }
  136. end
  137. def scheduled_status_attributes
  138. {
  139. scheduled_at: @scheduled_at,
  140. media_attachments: @media || [],
  141. params: scheduled_options,
  142. }
  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