The code powering m.abunchtell.com https://m.abunchtell.com
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

180 рядки
5.4 KiB

  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. @text = @options.delete(:spoiler_text) if @text.blank? && @options[:spoiler_text].present?
  40. @visibility = @options[:visibility] || @account.user&.setting_default_privacy
  41. @visibility = :unlisted if @visibility == :public && @account.silenced
  42. @scheduled_at = @options[:scheduled_at]&.to_datetime
  43. @scheduled_at = nil if scheduled_in_the_past?
  44. rescue ArgumentError
  45. raise ActiveRecord::RecordInvalid
  46. end
  47. def process_status!
  48. # The following transaction block is needed to wrap the UPDATEs to
  49. # the media attachments when the status is created
  50. ApplicationRecord.transaction do
  51. @status = @account.statuses.create!(status_attributes)
  52. end
  53. process_hashtags_service.call(@status)
  54. process_mentions_service.call(@status)
  55. end
  56. def schedule_status!
  57. status_for_validation = @account.statuses.build(status_attributes)
  58. if status_for_validation.valid?
  59. status_for_validation.destroy
  60. # The following transaction block is needed to wrap the UPDATEs to
  61. # the media attachments when the scheduled status is created
  62. ApplicationRecord.transaction do
  63. @status = @account.scheduled_statuses.create!(scheduled_status_attributes)
  64. end
  65. else
  66. raise ActiveRecord::RecordInvalid
  67. end
  68. end
  69. def postprocess_status!
  70. LinkCrawlWorker.perform_async(@status.id) unless @status.spoiler_text?
  71. DistributionWorker.perform_async(@status.id)
  72. Pubsubhubbub::DistributionWorker.perform_async(@status.stream_entry.id)
  73. ActivityPub::DistributionWorker.perform_async(@status.id)
  74. end
  75. def validate_media!
  76. return if @options[:media_ids].blank? || !@options[:media_ids].is_a?(Enumerable)
  77. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > 4
  78. @media = @account.media_attachments.where(status_id: nil).where(id: @options[:media_ids].take(4).map(&:to_i))
  79. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && @media.find(&:video?)
  80. end
  81. def language_from_option(str)
  82. ISO_639.find(str)&.alpha2
  83. end
  84. def process_mentions_service
  85. ProcessMentionsService.new
  86. end
  87. def process_hashtags_service
  88. ProcessHashtagsService.new
  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