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.

61 lines
2.1 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. # @return [Status]
  14. def call(account, text, in_reply_to = nil, options = {})
  15. media = validate_media!(options[:media_ids])
  16. status = account.statuses.create!(text: text,
  17. thread: in_reply_to,
  18. sensitive: options[:sensitive],
  19. spoiler_text: options[:spoiler_text] || '',
  20. visibility: options[:visibility],
  21. application: options[:application])
  22. attach_media(status, media)
  23. process_mentions_service.call(status)
  24. process_hashtags_service.call(status)
  25. LinkCrawlWorker.perform_async(status.id)
  26. DistributionWorker.perform_async(status.id)
  27. Pubsubhubbub::DistributionWorker.perform_async(status.stream_entry.id)
  28. status
  29. end
  30. private
  31. def validate_media!(media_ids)
  32. return if media_ids.nil? || !media_ids.is_a?(Enumerable)
  33. raise Mastodon::ValidationError, 'Cannot attach more than 4 files' if media_ids.size > 4
  34. media = MediaAttachment.where(status_id: nil).where(id: media_ids.take(4).map(&:to_i))
  35. raise Mastodon::ValidationError, 'Cannot attach a video to a toot that already contains images' if media.size > 1 && media.find(&:video?)
  36. media
  37. end
  38. def attach_media(status, media)
  39. return if media.nil?
  40. media.update(status_id: status.id)
  41. end
  42. def process_mentions_service
  43. @process_mentions_service ||= ProcessMentionsService.new
  44. end
  45. def process_hashtags_service
  46. @process_hashtags_service ||= ProcessHashtagsService.new
  47. end
  48. end