2017-11-27 15:07:59 +00:00
|
|
|
# frozen_string_literal: true
|
2023-02-20 05:58:28 +00:00
|
|
|
|
2017-11-27 15:07:59 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: invites
|
|
|
|
#
|
2018-04-23 09:29:17 +00:00
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# user_id :bigint(8) not null
|
2017-11-27 15:07:59 +00:00
|
|
|
# code :string default(""), not null
|
|
|
|
# expires_at :datetime
|
|
|
|
# max_uses :integer
|
|
|
|
# uses :integer default(0), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2018-06-15 16:00:23 +00:00
|
|
|
# autofollow :boolean default(FALSE), not null
|
2019-08-19 09:40:42 +00:00
|
|
|
# comment :text
|
2017-11-27 15:07:59 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
class Invite < ApplicationRecord
|
2018-06-29 13:34:36 +00:00
|
|
|
include Expireable
|
|
|
|
|
2023-07-05 07:14:00 +00:00
|
|
|
# FIXME: make this a rails cfg key or whatev?
|
|
|
|
TH_USE_INVITE_QUOTA = !!ENV['TH_USE_INVITE_QUOTA']
|
2023-07-05 08:45:04 +00:00
|
|
|
TH_INVITE_MAX_USES = ENV.fetch('TH_INVITE_MAX_USES', 25).to_i
|
|
|
|
TH_ACTIVE_INVITE_SLOT_QUOTA = ENV.fetch('TH_ACTIVE_INVITE_SLOT_QUOTA', 40).to_i
|
2023-07-05 07:14:00 +00:00
|
|
|
|
2019-07-26 16:55:33 +00:00
|
|
|
belongs_to :user, inverse_of: :invites
|
2017-11-27 15:07:59 +00:00
|
|
|
has_many :users, inverse_of: :invite
|
|
|
|
|
2017-12-01 15:40:02 +00:00
|
|
|
scope :available, -> { where(expires_at: nil).or(where('expires_at >= ?', Time.now.utc)) }
|
|
|
|
|
2019-08-19 09:40:42 +00:00
|
|
|
validates :comment, length: { maximum: 420 }
|
|
|
|
|
2023-07-05 07:14:00 +00:00
|
|
|
with_options if: :th_use_invite_quota?, unless: :created_by_moderator? do |invite|
|
|
|
|
invite.validates :expires_at, presence: true
|
|
|
|
invite.validate :expires_in_at_most_one_week?
|
|
|
|
invite.validates :max_uses, presence: true
|
|
|
|
# In Rails 6.1, numericality doesn't support :in
|
|
|
|
invite.validates :max_uses, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: TH_INVITE_MAX_USES }
|
|
|
|
invite.validate :reasonable_outstanding_invite_count?
|
|
|
|
end
|
|
|
|
|
2017-11-27 15:07:59 +00:00
|
|
|
before_validation :set_code
|
|
|
|
|
|
|
|
def valid_for_use?
|
2020-09-15 12:37:58 +00:00
|
|
|
(max_uses.nil? || uses < max_uses) && !expired? && user&.functional?
|
2017-11-27 15:07:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_code
|
|
|
|
loop do
|
|
|
|
self.code = ([*('a'..'z'), *('A'..'Z'), *('0'..'9')] - %w(0 1 I l O)).sample(8).join
|
|
|
|
break if Invite.find_by(code: code).nil?
|
|
|
|
end
|
|
|
|
end
|
2023-07-05 07:14:00 +00:00
|
|
|
|
|
|
|
def created_by_moderator?
|
|
|
|
self.user.moderator
|
|
|
|
end
|
|
|
|
|
|
|
|
def th_use_invite_quota?
|
|
|
|
TH_USE_INVITE_QUOTA
|
|
|
|
end
|
|
|
|
|
|
|
|
def expires_in_at_most_one_week?
|
|
|
|
return if self.expires_in.to_i.seconds <= 1.week
|
|
|
|
# FIXME: Localize this
|
|
|
|
errors.add(:expires_in, 'must expire within one week')
|
|
|
|
end
|
|
|
|
|
|
|
|
def reasonable_outstanding_invite_count?
|
|
|
|
valid_invites = self.user.invites.filter { |i| i.valid_for_use? }
|
|
|
|
count = valid_invites.sum do |i|
|
|
|
|
next i.max_uses unless i.max_uses.nil?
|
|
|
|
|
|
|
|
errors.add(:max_uses, 'must not have any active unlimited-use invites')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
return if count + max_uses <= TH_ACTIVE_INVITE_SLOT_QUOTA
|
|
|
|
errors.add(:max_uses, "must not exceed active invite slot quota of #{TH_ACTIVE_INVITE_SLOT_QUOTA}")
|
|
|
|
end
|
2017-11-27 15:07:59 +00:00
|
|
|
end
|