1
0

scheduled_status.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. MINIMUM_OFFSET = 5.minutes.freeze
  16. belongs_to :account, inverse_of: :scheduled_statuses
  17. has_many :media_attachments, inverse_of: :scheduled_status, dependent: :nullify
  18. validate :validate_future_date
  19. validate :validate_total_limit
  20. validate :validate_daily_limit
  21. private
  22. def validate_future_date
  23. errors.add(:scheduled_at, I18n.t('scheduled_statuses.too_soon')) if scheduled_at.present? && scheduled_at <= Time.now.utc + MINIMUM_OFFSET
  24. end
  25. def validate_total_limit
  26. errors.add(:base, I18n.t('scheduled_statuses.over_total_limit', limit: TOTAL_LIMIT)) if account.scheduled_statuses.count >= TOTAL_LIMIT
  27. end
  28. def validate_daily_limit
  29. 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
  30. end
  31. end