闭社主体 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.

41 lines
1.4 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[:unlisted] ? :unlisted : :public)
  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. HubPingWorker.perform_async(account.id)
  18. Pubsubhubbub::DistributionWorker.perform_async(status.stream_entry.id)
  19. status
  20. end
  21. private
  22. def attach_media(status, media_ids)
  23. return if media_ids.nil? || !media_ids.is_a?(Enumerable)
  24. media = MediaAttachment.where(status_id: nil).where(id: media_ids.take(4).map(&:to_i))
  25. media.update(status_id: status.id)
  26. end
  27. def process_mentions_service
  28. @process_mentions_service ||= ProcessMentionsService.new
  29. end
  30. def process_hashtags_service
  31. @process_hashtags_service ||= ProcessHashtagsService.new
  32. end
  33. end