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.

97 lines
3.6 KiB

8 years ago
8 years ago
8 years ago
  1. # frozen_string_literal: true
  2. class PostStatusService < BaseService
  3. # Post a text status update, fetch and notify remote users mentioned
  4. # @param [Account] account Account from which to post
  5. # @param [String] text Message
  6. # @param [Status] in_reply_to Optional status to reply to
  7. # @param [Hash] options
  8. # @option [Boolean] :sensitive
  9. # @option [String] :visibility
  10. # @option [String] :spoiler_text
  11. # @option [Enumerable] :media_ids Optional array of media IDs to attach
  12. # @option [Doorkeeper::Application] :application
  13. # @option [String] :idempotency Optional idempotency key
  14. # @return [Status]
  15. def call(account, text, in_reply_to = nil, **options)
  16. if options[:idempotency].present?
  17. existing_id = redis.get("idempotency:status:#{account.id}:#{options[:idempotency]}")
  18. return Status.find(existing_id) if existing_id
  19. end
  20. media = validate_media!(options[:media_ids])
  21. status = nil
  22. if text.blank? && options[:spoiler_text].present?
  23. text = '.'
  24. text = media.find(&:video?) ? '📹' : '🖼' if media.size > 0
  25. end
  26. ApplicationRecord.transaction do
  27. status = account.statuses.create!(text: text,
  28. media_attachments: media || [],
  29. thread: in_reply_to,
  30. sensitive: (options[:sensitive].nil? ? account.user&.setting_default_sensitive : options[:sensitive]) || options[:spoiler_text].present?,
  31. spoiler_text: options[:spoiler_text] || '',
  32. visibility: options[:visibility] || account.user&.setting_default_privacy,
  33. language: language_from_option(options[:language]) || account.user&.setting_default_language&.presence || LanguageDetector.instance.detect(text, account),
  34. application: options[:application])
  35. end
  36. process_hashtags_service.call(status)
  37. process_mentions_service.call(status)
  38. LinkCrawlWorker.perform_async(status.id) unless status.spoiler_text?
  39. DistributionWorker.perform_async(status.id)
  40. unless status.local_only?
  41. Pubsubhubbub::DistributionWorker.perform_async(status.stream_entry.id)
  42. ActivityPub::DistributionWorker.perform_async(status.id)
  43. ActivityPub::ReplyDistributionWorker.perform_async(status.id) if status.reply? && status.thread.account.local?
  44. end
  45. if options[:idempotency].present?
  46. redis.setex("idempotency:status:#{account.id}:#{options[:idempotency]}", 3_600, status.id)
  47. end
  48. bump_potential_friendship(account, status)
  49. status
  50. end
  51. private
  52. def validate_media!(media_ids)
  53. return if media_ids.blank? || !media_ids.is_a?(Enumerable)
  54. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if media_ids.size > 4
  55. media = MediaAttachment.where(status_id: nil).where(id: media_ids.take(4).map(&:to_i))
  56. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if media.size > 1 && media.find(&:video?)
  57. media
  58. end
  59. def language_from_option(str)
  60. ISO_639.find(str)&.alpha2
  61. end
  62. def process_mentions_service
  63. ProcessMentionsService.new
  64. end
  65. def process_hashtags_service
  66. ProcessHashtagsService.new
  67. end
  68. def redis
  69. Redis.current
  70. end
  71. def bump_potential_friendship(account, status)
  72. return if !status.reply? || account.id == status.in_reply_to_account_id
  73. ActivityTracker.increment('activity:interactions')
  74. return if account.following?(status.in_reply_to_account_id)
  75. PotentialFriendshipTracker.record(account.id, status.in_reply_to_account_id, :reply)
  76. end
  77. end