From 86990f353535983b01a6f99ec2f9871ceb5deb5a Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Fri, 12 Sep 2014 14:54:53 -0700 Subject: [PATCH] initial plan structure, and some misc fixes --- Gemfile | 1 + Gemfile.lock | 2 + Rakefile | 8 ++++ app.rb | 13 +++--- migrations/044_add_plan_type.rb | 9 ++++ models/site.rb | 82 +++++++++++++++++++++++++++------ models/tag.rb | 2 +- tests/site_file_tests.rb | 5 +- tests/site_tests.rb | 29 ++++++++++++ views/layout.erb | 2 +- 10 files changed, 129 insertions(+), 24 deletions(-) create mode 100644 migrations/044_add_plan_type.rb create mode 100644 tests/site_tests.rb diff --git a/Gemfile b/Gemfile index 1d941b34..ebfd3aba 100644 --- a/Gemfile +++ b/Gemfile @@ -22,6 +22,7 @@ gem 'cocaine' gem 'zipruby' gem 'sass', require: nil gem 'dav4rack' +gem 'filesize' platform :mri do gem 'magic' # sudo apt-get install file, For OSX: brew install libmagic diff --git a/Gemfile.lock b/Gemfile.lock index 837b1390..f36849d6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -57,6 +57,7 @@ GEM faraday (0.9.0) multipart-post (>= 1.2, < 3) ffi (1.9.3) + filesize (0.0.3) google-api-client (0.7.1) addressable (>= 2.3.2) autoparse (>= 0.3.3) @@ -215,6 +216,7 @@ DEPENDENCIES erubis fabrication faker + filesize google-api-client hiredis jdbc-postgres diff --git a/Rakefile b/Rakefile index 896222f2..eda7207d 100644 --- a/Rakefile +++ b/Rakefile @@ -116,3 +116,11 @@ task :buildssl => [:environment] do ar.add_buffer 'sslsites.json', payload.to_json end end + +desc 'Set existing stripe customers to internal supporter plan' +task :primenewstriperunonlyonce => [:environment] do + Site.exclude(stripe_customer_id: nil).all.each do |site| + site.plan_type = 'supporter' + site.save_changes validate: false + end +end \ No newline at end of file diff --git a/app.rb b/app.rb index 23fe780c..bcbe773b 100644 --- a/app.rb +++ b/app.rb @@ -348,7 +348,7 @@ post '/tags/add' do if current_site.valid? current_site.save else - flash[:errors] = current_site.errors[:tags].first + flash[:errors] = current_site.errors.first end redirect request.referer @@ -713,15 +713,16 @@ end post '/change_email' do require_login - current_site.email = params[:email] - current_site.email_confirmation_token = SecureRandom.hex 3 - current_site.email_confirmed = false - + if params[:email] == current_site.email current_site.errors.add :email, 'You are already using this email address for this account.' halt erb(:settings) end + current_site.email = params[:email] + current_site.email_confirmation_token = SecureRandom.hex 3 + current_site.email_confirmed = false + if current_site.valid? current_site.save_changes send_confirmation_email @@ -919,7 +920,7 @@ post %r{\/site_files\/save\/(.+)} do tempfile.close if current_site.file_size_too_large? tempfile.size - halt 'File is too large to fit in your space, it has NOT been saved. Please make a local copy and then try to reduce the size.' + halt 'File is too large to fit in your space, it has NOT been saved. You will need to reduce the size or upgrade to a new plan.' end current_site.store_file filename, tempfile diff --git a/migrations/044_add_plan_type.rb b/migrations/044_add_plan_type.rb new file mode 100644 index 00000000..6613ce2b --- /dev/null +++ b/migrations/044_add_plan_type.rb @@ -0,0 +1,9 @@ +Sequel.migration do + up { + DB.add_column :sites, :plan_type, :text + } + + down { + DB.drop_column :sites, :plan_type + } +end \ No newline at end of file diff --git a/models/site.rb b/models/site.rb index cfe2799f..7da54a82 100644 --- a/models/site.rb +++ b/models/site.rb @@ -78,6 +78,46 @@ class Site < Sequel::Model COMMENTING_ALLOWED_UPDATED_COUNT = 2 + PLAN_FEATURES = { + fatcat: { + name: 'Fat Cat', + space: Filesize.from('100GB').to_i, + bandwidth: Filesize.from('2TB').to_i, + custom_domains: true, + custom_ssl_certificates: true, + global_cdn: true, + ddos_mitigation: true, + unlimited_site_creation: true, + site_mounting: true, + no_file_restrictions: true + } + } + + PLAN_FEATURES[:catbus] = PLAN_FEATURES[:fatcat].merge( + name: 'Cat Bus', + space: Filesize.from('10GB').to_i, + bandwidth: Filesize.from('500GB').to_i + ) + + PLAN_FEATURES[:supporter] = PLAN_FEATURES[:catbus].merge( + name: 'Supporter', + space: Filesize.from('1GB').to_i, + bandwidth: Filesize.from('100GB').to_i, + unlimited_site_creation: false, + custom_ssl_certificates: false, + no_file_restrictions: false + ) + + PLAN_FEATURES[:free] = PLAN_FEATURES[:supporter].merge( + name: 'Free', + space: Filesize.from('30MB').to_i, + bandwidth: Filesize.from('10GB').to_i, + custom_domains: false, + global_cdn: false, + ddos_mitigation: false, + site_mounting: false + ) + many_to_one :server many_to_many :tags @@ -383,8 +423,11 @@ class Site < Sequel::Model if new_title.length < TITLE_MAX self.title = new_title - save_changes(validate: false) end + + self.site_changed = true + + save_changes(validate: false) end dirname = pathname.dirname.to_s @@ -408,11 +451,6 @@ class Site < Sequel::Model SiteChange.record self, relative_path - if self.site_changed != true - self.site_changed = true - save_changes(validate: false) - end - true end @@ -519,6 +557,11 @@ class Site < Sequel::Model super end + def email=(email) + @original_email = values[:email] unless new? + super + end + # def after_destroy # FileUtils.rm_rf files_path # super @@ -549,10 +592,15 @@ class Site < Sequel::Model errors.add :email, 'An email address is required.' end - # Check for existing email - email_check = self.class.select(:id).filter(email: values[:email]).first - if 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.' + # Check for existing email if new or changing email. + if new? || @original_email + email_check = self.class.select(:id).filter(email: values[:email]) + email_check.exclude!(id: self.id) unless new? + email_check = email_check.first + + if 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 @@ -676,7 +724,7 @@ class Site < Sequel::Model end list.select {|f| f[:is_directory]}.sort_by {|f| f[:name]} + - list.select {|f| f[:is_directory] == false}.sort_by{|f| f[:name]} + list.select {|f| f[:is_directory] == false}.sort_by{|f| f[:name].downcase} end def file_size_too_large?(size_in_bytes) @@ -719,14 +767,18 @@ class Site < Sequel::Model !values[:stripe_customer_id].nil? end - # This will return false if they have ended their support plan. + # This will return false if they have ended their plan. def ended_supporter? - values[:ended_plan] + values[:plan_ended] end def plan_name - return 'Free Plan' if !supporter? || (supporter? && ended_supporter?) - 'Supporter Plan' + PLAN_FEATURES[plan_type.to_sym][:name] + end + + def plan_type + return 'free' if values[:plan_type].nil? + values[:plan_type] end def latest_events(current_page=1, limit=10) diff --git a/models/tag.rb b/models/tag.rb index 2a7f5a63..25b200ab 100644 --- a/models/tag.rb +++ b/models/tag.rb @@ -13,7 +13,7 @@ class Tag < Sequel::Model end def self.suggestions(name, limit=3) - Tag.filter(name: /^#{name}/). + Tag.filter(name: /^#{name}/i). order(:name). limit(limit). all diff --git a/tests/site_file_tests.rb b/tests/site_file_tests.rb index 520c3639..a6c8fe77 100644 --- a/tests/site_file_tests.rb +++ b/tests/site_file_tests.rb @@ -9,9 +9,9 @@ end describe 'site_files' do describe 'upload' do - it 'succeeds with index.html file' do site = Fabricate :site + site.site_changed.must_equal false PurgeCacheWorker.jobs.clear ScreenshotWorker.jobs.clear @@ -25,6 +25,7 @@ describe 'site_files' do args = ScreenshotWorker.jobs.first['args'] args.first.must_equal site.username args.last.must_equal 'index.html' + site.reload.site_changed.must_equal true end it 'succeeds with valid file' do @@ -48,6 +49,8 @@ describe 'site_files' do Site::THUMBNAIL_RESOLUTIONS.each do |resolution| File.exists?(site.thumbnail_path('test.jpg', resolution)).must_equal true end + + site.site_changed.must_equal false end it 'works with directory path' do diff --git a/tests/site_tests.rb b/tests/site_tests.rb new file mode 100644 index 00000000..9a47819f --- /dev/null +++ b/tests/site_tests.rb @@ -0,0 +1,29 @@ +require_relative './environment.rb' +require 'rack/test' + +include Rack::Test::Methods + +def app + Sinatra::Application +end + +describe 'site' do + describe 'plan_name' do + it 'should set to free for missing stripe_customer_id' do + site = Fabricate :site + site.reload.plan_type.must_equal 'free' + end + + it 'should be free for no plan_type entry' do + site = Fabricate :site, stripe_customer_id: 'cust_derp' + site.plan_type.must_equal 'free' + end + + it 'should match plan_type' do + %w{supporter neko catbus fatcat}.each do |plan_type| + site = Fabricate :site, plan_type: plan_type + site.plan_type.must_equal plan_type + end + end + end +end \ No newline at end of file diff --git a/views/layout.erb b/views/layout.erb index f6a623f3..dcd9d509 100644 --- a/views/layout.erb +++ b/views/layout.erb @@ -58,7 +58,7 @@ $('#shareButton').popover({html: true}) $('.typeahead').typeahead({ - minLength: 3, + minLength: 2, highlight: true }, { name: 'tags',