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.

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