logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

post_status_service.rb (3087B)


  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. # @option [String] :idempotency Optional idempotency key
  14. # @return [Status]
  15. def call(account, text, in_reply_to = nil, **options)
  16. if options[:idempotency].present?
  17. existing_id = redis.get("idempotency:status:#{account.id}:#{options[:idempotency]}")
  18. return Status.find(existing_id) if existing_id
  19. end
  20. media = validate_media!(options[:media_ids])
  21. status = nil
  22. text = options.delete(:spoiler_text) if text.blank? && options[:spoiler_text].present?
  23. text = '.' if text.blank? && !media.empty?
  24. ApplicationRecord.transaction do
  25. status = account.statuses.create!(text: text,
  26. media_attachments: media || [],
  27. thread: in_reply_to,
  28. sensitive: (options[:sensitive].nil? ? account.user&.setting_default_sensitive : options[:sensitive]),
  29. spoiler_text: options[:spoiler_text] || '',
  30. visibility: options[:visibility] || account.user&.setting_default_privacy,
  31. language: LanguageDetector.instance.detect(text, account),
  32. application: options[:application])
  33. end
  34. process_mentions_service.call(status)
  35. process_hashtags_service.call(status)
  36. LinkCrawlWorker.perform_async(status.id) unless status.spoiler_text?
  37. DistributionWorker.perform_async(status.id)
  38. Pubsubhubbub::DistributionWorker.perform_async(status.stream_entry.id)
  39. ActivityPub::DistributionWorker.perform_async(status.id)
  40. ActivityPub::ReplyDistributionWorker.perform_async(status.id) if status.reply? && status.thread.account.local?
  41. if options[:idempotency].present?
  42. redis.setex("idempotency:status:#{account.id}:#{options[:idempotency]}", 3_600, status.id)
  43. end
  44. status
  45. end
  46. private
  47. def validate_media!(media_ids)
  48. return if media_ids.blank? || !media_ids.is_a?(Enumerable)
  49. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if media_ids.size > 4
  50. media = MediaAttachment.where(status_id: nil).where(id: media_ids.take(4).map(&:to_i))
  51. raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if media.size > 1 && media.find(&:video?)
  52. media
  53. end
  54. def process_mentions_service
  55. ProcessMentionsService.new
  56. end
  57. def process_hashtags_service
  58. ProcessHashtagsService.new
  59. end
  60. def redis
  61. Redis.current
  62. end
  63. end