1
0

scheduled_status.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: scheduled_statuses
  5. #
  6. # id :bigint(8) not null, primary key
  7. # account_id :bigint(8)
  8. # scheduled_at :datetime
  9. # params :jsonb
  10. #
  11. class ScheduledStatus < ApplicationRecord
  12. include Paginable
  13. TOTAL_LIMIT = 300
  14. DAILY_LIMIT = 25
  15. belongs_to :account, inverse_of: :scheduled_statuses
  16. has_many :media_attachments, inverse_of: :scheduled_status, dependent: :nullify
  17. validate :validate_future_date
  18. validate :validate_total_limit
  19. validate :validate_daily_limit
  20. private
  21. def validate_future_date
  22. errors.add(:scheduled_at, I18n.t('scheduled_statuses.too_soon')) if scheduled_at.present? && scheduled_at <= Time.now.utc + PostStatusService::MIN_SCHEDULE_OFFSET
  23. end
  24. def validate_total_limit
  25. errors.add(:base, I18n.t('scheduled_statuses.over_total_limit', limit: TOTAL_LIMIT)) if account.scheduled_statuses.count >= TOTAL_LIMIT
  26. end
  27. def validate_daily_limit
  28. errors.add(:base, I18n.t('scheduled_statuses.over_daily_limit', limit: DAILY_LIMIT)) if account.scheduled_statuses.where('scheduled_at::date = ?::date', scheduled_at).count >= DAILY_LIMIT
  29. end
  30. end