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

29 lines
695 B

  1. # frozen_string_literal: true
  2. class Subscription < ApplicationRecord
  3. MIN_EXPIRATION = 3600 * 24 * 7
  4. MAX_EXPIRATION = 3600 * 24 * 30
  5. belongs_to :account, required: true
  6. validates :callback_url, presence: true
  7. validates :callback_url, uniqueness: { scope: :account_id }
  8. scope :active, -> { where(confirmed: true).where('expires_at > ?', Time.now.utc) }
  9. def lease_seconds=(str)
  10. self.expires_at = Time.now.utc + [[MIN_EXPIRATION, str.to_i].max, MAX_EXPIRATION].min.seconds
  11. end
  12. def lease_seconds
  13. (expires_at - Time.now.utc).to_i
  14. end
  15. before_validation :set_min_expiration
  16. private
  17. def set_min_expiration
  18. self.lease_seconds = 0 unless expires_at
  19. end
  20. end