The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

177 lines
5.3 KiB

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