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.
 
 
 
 

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