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.

86 lines
2.9 KiB

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. ApplicationRecord.transaction do
  23. status = account.statuses.create!(text: text,
  24. thread: in_reply_to,
  25. sensitive: options[:sensitive],
  26. spoiler_text: options[:spoiler_text] || '',
  27. visibility: options[:visibility],
  28. language: detect_language_for(text, account),
  29. application: options[:application])
  30. attach_media(status, media)
  31. end
  32. process_mentions_service.call(status)
  33. process_hashtags_service.call(status)
  34. LinkCrawlWorker.perform_async(status.id) unless status.spoiler_text?
  35. DistributionWorker.perform_async(status.id)
  36. # match both with and without U+FE0F (the emoji variation selector)
  37. unless /[👁👁️]$/.match?(status.content)
  38. Pubsubhubbub::DistributionWorker.perform_async(status.stream_entry.id)
  39. end
  40. if options[:idempotency].present?
  41. redis.setex("idempotency:status:#{account.id}:#{options[:idempotency]}", 3_600, status.id)
  42. end
  43. status
  44. end
  45. private
  46. def validate_media!(media_ids)
  47. return if media_ids.blank? || !media_ids.is_a?(Enumerable)
  48. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if media_ids.size > 4
  49. media = MediaAttachment.where(status_id: nil).where(id: media_ids.take(4).map(&:to_i))
  50. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if media.size > 1 && media.find(&:video?)
  51. media
  52. end
  53. def attach_media(status, media)
  54. return if media.nil?
  55. media.update(status_id: status.id)
  56. end
  57. def detect_language_for(text, account)
  58. LanguageDetector.new(text, account).to_iso_s
  59. end
  60. def process_mentions_service
  61. @process_mentions_service ||= ProcessMentionsService.new
  62. end
  63. def process_hashtags_service
  64. @process_hashtags_service ||= ProcessHashtagsService.new
  65. end
  66. def redis
  67. Redis.current
  68. end
  69. end