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.

181 lines
5.5 KiB

7 years ago
7 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. @text = @options.delete(:spoiler_text) if @text.blank? && @options[:spoiler_text].present?
  39. @visibility = @options[:visibility] || @account.user&.setting_default_privacy
  40. @visibility = :unlisted if @visibility == :public && @account.silenced
  41. @scheduled_at = @options[:scheduled_at]&.to_datetime
  42. @scheduled_at = nil if scheduled_in_the_past?
  43. rescue ArgumentError
  44. raise ActiveRecord::RecordInvalid
  45. end
  46. def process_status!
  47. # The following transaction block is needed to wrap the UPDATEs to
  48. # the media attachments when the status is created
  49. ApplicationRecord.transaction do
  50. @status = @account.statuses.create!(status_attributes)
  51. end
  52. process_hashtags_service.call(@status)
  53. process_mentions_service.call(@status)
  54. end
  55. def schedule_status!
  56. status_for_validation = @account.statuses.build(status_attributes)
  57. if status_for_validation.valid?
  58. status_for_validation.destroy
  59. # The following transaction block is needed to wrap the UPDATEs to
  60. # the media attachments when the scheduled status is created
  61. ApplicationRecord.transaction do
  62. @status = @account.scheduled_statuses.create!(scheduled_status_attributes)
  63. end
  64. else
  65. raise ActiveRecord::RecordInvalid
  66. end
  67. end
  68. def postprocess_status!
  69. LinkCrawlWorker.perform_async(@status.id) unless @status.spoiler_text?
  70. DistributionWorker.perform_async(@status.id)
  71. Pubsubhubbub::DistributionWorker.perform_async(@status.stream_entry.id)
  72. ActivityPub::DistributionWorker.perform_async(@status.id)
  73. end
  74. def validate_media!
  75. return if @options[:media_ids].blank? || !@options[:media_ids].is_a?(Enumerable)
  76. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > 4
  77. @media = @account.media_attachments.where(status_id: nil).where(id: @options[:media_ids].take(4).map(&:to_i))
  78. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && @media.find(&:video?)
  79. end
  80. def language_from_option(str)
  81. ISO_639.find(str)&.alpha2
  82. end
  83. def process_mentions_service
  84. ProcessMentionsService.new
  85. end
  86. def process_hashtags_service
  87. ProcessHashtagsService.new
  88. end
  89. def redis
  90. Redis.current
  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. sensitive: (@options[:sensitive].nil? ? @account.user&.setting_default_sensitive : @options[:sensitive]) || @options[:spoiler_text].present?,
  126. spoiler_text: @options[:spoiler_text] || '',
  127. visibility: @visibility,
  128. language: language_from_option(@options[:language]) || @account.user&.setting_default_language&.presence || LanguageDetector.instance.detect(@text, @account),
  129. application: @options[:application],
  130. }
  131. end
  132. def scheduled_status_attributes
  133. {
  134. scheduled_at: @scheduled_at,
  135. media_attachments: @media || [],
  136. params: scheduled_options,
  137. }
  138. end
  139. def scheduled_options
  140. @options.tap do |options_hash|
  141. options_hash[:in_reply_to_id] = options_hash.delete(:thread)&.id
  142. options_hash[:application_id] = options_hash.delete(:application)&.id
  143. options_hash[:scheduled_at] = nil
  144. options_hash[:idempotency] = nil
  145. end
  146. end
  147. end