2019-01-05 12:43:28 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: scheduled_statuses
|
|
|
|
#
|
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# params :jsonb
|
2024-12-14 01:16:03 +01:00
|
|
|
# scheduled_at :datetime
|
|
|
|
# account_id :bigint(8) not null
|
2019-01-05 12:43:28 +01:00
|
|
|
#
|
|
|
|
|
|
|
|
class ScheduledStatus < ApplicationRecord
|
|
|
|
include Paginable
|
|
|
|
|
|
|
|
TOTAL_LIMIT = 300
|
|
|
|
DAILY_LIMIT = 25
|
2024-11-07 11:28:59 +01:00
|
|
|
MINIMUM_OFFSET = 5.minutes.freeze
|
2019-01-05 12:43:28 +01:00
|
|
|
|
|
|
|
belongs_to :account, inverse_of: :scheduled_statuses
|
2019-01-06 16:38:40 +01:00
|
|
|
has_many :media_attachments, inverse_of: :scheduled_status, dependent: :nullify
|
2019-01-05 12:43:28 +01:00
|
|
|
|
|
|
|
validate :validate_future_date
|
|
|
|
validate :validate_total_limit
|
|
|
|
validate :validate_daily_limit
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def validate_future_date
|
2024-11-07 11:28:59 +01:00
|
|
|
errors.add(:scheduled_at, I18n.t('scheduled_statuses.too_soon')) if scheduled_at.present? && scheduled_at <= Time.now.utc + MINIMUM_OFFSET
|
2019-01-05 12:43:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate_total_limit
|
|
|
|
errors.add(:base, I18n.t('scheduled_statuses.over_total_limit', limit: TOTAL_LIMIT)) if account.scheduled_statuses.count >= TOTAL_LIMIT
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_daily_limit
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|