闭社主体 forked from https://github.com/tootsuite/mastodon
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.

50 lines
1.8 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. status = account.statuses.create!(text: text,
  16. thread: in_reply_to,
  17. sensitive: options[:sensitive],
  18. spoiler_text: options[:spoiler_text] || '',
  19. visibility: options[:visibility],
  20. application: options[:application])
  21. attach_media(status, options[:media_ids])
  22. process_mentions_service.call(status)
  23. process_hashtags_service.call(status)
  24. LinkCrawlWorker.perform_async(status.id)
  25. DistributionWorker.perform_async(status.id)
  26. Pubsubhubbub::DistributionWorker.perform_async(status.stream_entry.id)
  27. status
  28. end
  29. private
  30. def attach_media(status, media_ids)
  31. return if media_ids.nil? || !media_ids.is_a?(Enumerable)
  32. media = MediaAttachment.where(status_id: nil).where(id: media_ids.take(4).map(&:to_i))
  33. media.update(status_id: status.id)
  34. end
  35. def process_mentions_service
  36. @process_mentions_service ||= ProcessMentionsService.new
  37. end
  38. def process_hashtags_service
  39. @process_hashtags_service ||= ProcessHashtagsService.new
  40. end
  41. end