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

47 lines
1.3 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: subscriptions
  5. #
  6. # id :integer not null, primary key
  7. # callback_url :string default(""), not null
  8. # secret :string
  9. # expires_at :datetime
  10. # confirmed :boolean default(FALSE), not null
  11. # account_id :integer not null
  12. # created_at :datetime not null
  13. # updated_at :datetime not null
  14. # last_successful_delivery_at :datetime
  15. #
  16. class Subscription < ApplicationRecord
  17. MIN_EXPIRATION = 3600 * 24 * 7
  18. MAX_EXPIRATION = 3600 * 24 * 30
  19. belongs_to :account, required: true
  20. validates :callback_url, presence: true
  21. validates :callback_url, uniqueness: { scope: :account_id }
  22. scope :active, -> { where(confirmed: true).where('expires_at > ?', Time.now.utc) }
  23. def lease_seconds=(str)
  24. self.expires_at = Time.now.utc + [[MIN_EXPIRATION, str.to_i].max, MAX_EXPIRATION].min.seconds
  25. end
  26. def lease_seconds
  27. (expires_at - Time.now.utc).to_i
  28. end
  29. def expired?
  30. Time.now.utc > expires_at
  31. end
  32. before_validation :set_min_expiration
  33. private
  34. def set_min_expiration
  35. self.lease_seconds = 0 unless expires_at
  36. end
  37. end