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.

48 lines
1.7 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 [Enumerable] :media_ids Optional array of media IDs to attach
  11. # @option [Doorkeeper::Application] :application
  12. # @return [Status]
  13. def call(account, text, in_reply_to = nil, options = {})
  14. status = account.statuses.create!(text: text,
  15. thread: in_reply_to,
  16. sensitive: options[:sensitive],
  17. visibility: options[:visibility],
  18. application: options[:application])
  19. attach_media(status, options[:media_ids])
  20. process_mentions_service.call(status)
  21. process_hashtags_service.call(status)
  22. LinkCrawlWorker.perform_async(status.id)
  23. DistributionWorker.perform_async(status.id)
  24. Pubsubhubbub::DistributionWorker.perform_async(status.stream_entry.id)
  25. status
  26. end
  27. private
  28. def attach_media(status, media_ids)
  29. return if media_ids.nil? || !media_ids.is_a?(Enumerable)
  30. media = MediaAttachment.where(status_id: nil).where(id: media_ids.take(4).map(&:to_i))
  31. media.update(status_id: status.id)
  32. end
  33. def process_mentions_service
  34. @process_mentions_service ||= ProcessMentionsService.new
  35. end
  36. def process_hashtags_service
  37. @process_hashtags_service ||= ProcessHashtagsService.new
  38. end
  39. end