start on accounts using site context, improved settings page, clean up space representation

This commit is contained in:
Kyle Drake 2014-10-07 02:22:08 +02:00
parent 8a36f1c6f5
commit d3c1546d45
17 changed files with 243 additions and 105 deletions

View file

@ -34,11 +34,8 @@ class Site < Sequel::Model
geojson csv tsv mf ico pdf asc key pgp xml mid midi
}
ONE_MEGABYTE_IN_BYTES = 1048576
FREE_MAXIMUM_IN_MEGABYTES = 20
SUPPORTER_MAXIMUM_IN_MEGABYTES = 1024
FREE_MAXIMUM_IN_BYTES = FREE_MAXIMUM_IN_MEGABYTES * ONE_MEGABYTE_IN_BYTES
SUPPORTER_MAXIMUM_IN_BYTES = SUPPORTER_MAXIMUM_IN_MEGABYTES * ONE_MEGABYTE_IN_BYTES
FREE_MAXIMUM = 20 * Numeric::ONE_MEGABYTE
SUPPORTER_MAXIMUM = 1000 * Numeric::ONE_MEGABYTE
MINIMUM_PASSWORD_LENGTH = 5
BAD_USERNAME_REGEX = /[^\w-]/i
@ -95,6 +92,7 @@ class Site < Sequel::Model
SUGGESTIONS_LIMIT = 32
SUGGESTIONS_VIEWS_MIN = 500
CHILD_SITES_MAX = 1000
PLAN_FEATURES[:catbus] = PLAN_FEATURES[:fatcat].merge(
name: 'Cat Bus',
@ -147,9 +145,33 @@ class Site < Sequel::Model
one_to_many :site_changes
many_to_one :parent, :key => :parent_site_id, :class => self
one_to_many :children, :key => :parent_site_id, :class => self
def account_sites
if parent?
sites = [self] + children
else
sites = [parent] + parent.children
end
sites.compact
end
def other_sites
if parent?
return children
else
sites = (parent + children)
sites.delete self
sites
end
end
class << self
def valid_login?(username, plaintext)
site = self[username: username]
def valid_login?(username_or_email, plaintext)
site = get_with_identifier username_or_email
return false if site.nil?
site.valid_password? plaintext
end
@ -161,6 +183,14 @@ class Site < Sequel::Model
def bcrypt_cost=(cost)
@bcrypt_cost = cost
end
def get_with_identifier(username_or_email)
if username_or_email =~ /@/
site = self.where(email: username_or_email).where(parent_site_id: nil).first
else
site = self[username: username_or_email]
end
end
end
def self.banned_ip?(ip)
@ -317,6 +347,12 @@ class Site < Sequel::Model
def commenting_allowed?
return true if commenting_allowed
if supporter?
set commenting_allowed: true
save_changes validate: false
return true
end
if events_dataset.exclude(site_change_id: nil).count >= COMMENTING_ALLOWED_UPDATED_COUNT &&
created_at < Time.now - 604800
set commenting_allowed: true
@ -554,6 +590,10 @@ class Site < Sequel::Model
super
end
def parent?
parent_site_id.nil?
end
# def after_destroy
# FileUtils.rm_rf files_path
# super
@ -576,7 +616,7 @@ class Site < Sequel::Model
end
# Check that email has been provided
if values[:email].empty?
if parent? && values[:email].empty?
errors.add :email, 'An email address is required.'
end
@ -586,12 +626,12 @@ class Site < Sequel::Model
email_check.exclude!(id: self.id) unless new?
email_check = email_check.first
if email_check && email_check.id != self.id
if parent? && email_check && email_check.id != self.id
errors.add :email, 'This email address already exists on Neocities, please use your existing account instead of creating a new one.'
end
end
unless values[:email] =~ EMAIL_SANITY_REGEX
if parent? && (values[:email] =~ EMAIL_SANITY_REGEX).nil?
errors.add :email, 'A valid email address is required.'
end
@ -604,7 +644,7 @@ class Site < Sequel::Model
end
end
if values[:password].nil? || (@password_length && @password_length < MINIMUM_PASSWORD_LENGTH)
if parent? && (values[:password].nil? || (@password_length && @password_length < MINIMUM_PASSWORD_LENGTH))
errors.add :password, "Password must be at least #{MINIMUM_PASSWORD_LENGTH} characters."
end
@ -622,6 +662,10 @@ class Site < Sequel::Model
if !site.nil? && site.id != self.id
errors.add :domain, "Domain provided is already being used by another site, please choose another."
end
if new? && !parent? && CHILD_SITE_MAX == children_dataset.count
errors.add :child_site_id, "Cannot add child site, exceeds #{CHILD_SITE_MAX} limit."
end
end
if @new_tags_string
@ -715,39 +759,33 @@ class Site < Sequel::Model
list.select {|f| f[:is_directory] == false}.sort_by{|f| f[:name].downcase}
end
def file_size_too_large?(size_in_bytes)
return true if size_in_bytes + used_space_in_bytes > maximum_space_in_bytes
def file_size_too_large?(size)
return true if size + used_space > maximum_space
false
end
def used_space_in_bytes
def used_space
space = Dir.glob(File.join(files_path, '*')).collect {|p| File.size(p)}.inject {|sum,x| sum += x}
space.nil? ? 0 : space
end
def used_space_in_megabytes
(used_space_in_bytes.to_f / self.class::ONE_MEGABYTE_IN_BYTES).round(2)
def total_used_space
total = 0
account_sites.each {|s| total += s.used_space}
total
end
def available_space_in_bytes
remaining = maximum_space_in_bytes - used_space_in_bytes
def remaining_space
remaining = maximum_space - total_used_space
remaining < 0 ? 0 : remaining
end
def available_space_in_megabytes
(available_space_in_bytes.to_f / self.class::ONE_MEGABYTE_IN_BYTES).round(2)
end
def maximum_space_in_bytes
supporter? ? self.class::SUPPORTER_MAXIMUM_IN_BYTES : self.class::FREE_MAXIMUM_IN_BYTES
end
def maximum_space_in_megabytes
supporter? ? self.class::SUPPORTER_MAXIMUM_IN_MEGABYTES : self.class::FREE_MAXIMUM_IN_MEGABYTES
def maximum_space
(parent? ? self : parent).supporter? ? SUPPORTER_MAXIMUM : FREE_MAXIMUM
end
def space_percentage_used
((used_space_in_bytes.to_f / maximum_space_in_bytes) * 100).round(1)
((total_used_space.to_f / maximum_space) * 100).round(1)
end
# This returns true even if they end their support plan.