From 4719a5ed1c497e4a14a1edc6121e9f6bd3120d16 Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 14 Jan 2021 20:01:23 -0600 Subject: [PATCH] users: set default settings in ruby instead of in database. Specify the default settings for new users inside the User model instead of inside the database. This makes it easier to change defaults, and it makes the code clearer. --- app/controllers/users_controller.rb | 1 + app/models/user.rb | 30 ++++++++++++++- ...210115015308_remove_defaults_from_users.rb | 22 +++++++++++ db/structure.sql | 38 +++++++++---------- test/factories/user.rb | 5 --- .../forum_topics_controller_test.rb | 2 +- 6 files changed, 71 insertions(+), 27 deletions(-) create mode 100644 db/migrate/20210115015308_remove_defaults_from_users.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 1ee303cbb..387037ea1 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -63,6 +63,7 @@ class UsersController < ApplicationController @user = authorize User.new( last_ip_addr: CurrentUser.ip_addr, + last_logged_in_at: Time.zone.now, requires_verification: user_verifier.requires_verification?, level: user_verifier.initial_level, name: params[:user][:name], diff --git a/app/models/user.rb b/app/models/user.rb index d0c9f7109..b042a1f6c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -69,7 +69,36 @@ class User < ApplicationRecord is_verified ) + DEFAULT_BLACKLIST = ["spoilers", "guro", "scat", "furry -rating:s"].join("\n") + + attribute :id + attribute :created_at + attribute :updated_at + attribute :name + attribute :level, default: Levels::MEMBER + attribute :bcrypt_password_hash + attribute :inviter_id + attribute :last_logged_in_at, default: -> { Time.zone.now } + attribute :last_forum_read_at, default: "1960-01-01 00:00:00" + attribute :last_ip_addr + attribute :comment_threshold, default: 0 + attribute :default_image_size, default: "large" + attribute :favorite_tags + attribute :blacklisted_tags, default: DEFAULT_BLACKLIST + attribute :time_zone, default: "Eastern Time (US & Canada)" + attribute :custom_style + attribute :post_upload_count, default: 0 + attribute :post_update_count, default: 0 + attribute :note_update_count, default: 0 + attribute :unread_dmail_count, default: 0 + attribute :favorite_count, default: 0 + attribute :per_page, default: 20 + attribute :theme, default: :light + attribute :upload_points, default: 1000 + attribute :bit_prefs, default: 0 + has_bit_flags BOOLEAN_ATTRIBUTES, :field => "bit_prefs" + enum theme: { light: 0, dark: 100 }, _suffix: true attr_reader :password @@ -122,7 +151,6 @@ class User < ApplicationRecord belongs_to :inviter, class_name: "User", optional: true accepts_nested_attributes_for :email_address, reject_if: :all_blank, allow_destroy: true - enum theme: { light: 0, dark: 100 }, _suffix: true # UserDeletion#rename renames deleted users to `user_<1234>~`. Tildes # are appended if the username is taken. diff --git a/db/migrate/20210115015308_remove_defaults_from_users.rb b/db/migrate/20210115015308_remove_defaults_from_users.rb new file mode 100644 index 000000000..c64af70a6 --- /dev/null +++ b/db/migrate/20210115015308_remove_defaults_from_users.rb @@ -0,0 +1,22 @@ +class RemoveDefaultsFromUsers < ActiveRecord::Migration[6.1] + def change + change_table(:users) do |t| + t.change_default :level, from: 20, to: nil + t.change_default :last_logged_in_at, from: "now()", to: nil + t.change_default :last_forum_read_at, from: "1960-01-01 00:00:00", to: nil + t.change_default :comment_threshold, from: 0, to: nil + t.change_default :default_image_size, from: "large", to: nil + t.change_default :blacklisted_tags, from: "spoilers\nguro\nscat\nfurry -rating:s", to: nil + t.change_default :time_zone, from: "Eastern Time (US & Canada)", to: nil + t.change_default :post_update_count, from: 0, to: nil + t.change_default :note_update_count, from: 0, to: nil + t.change_default :favorite_count, from: 0, to: nil + t.change_default :post_upload_count, from: 0, to: nil + t.change_default :per_page, from: 20, to: nil + t.change_default :bit_prefs, from: 0, to: nil + t.change_default :unread_dmail_count, from: 0, to: nil + t.change_default :theme, from: 0, to: nil + t.change_default :upload_points, from: 1000, to: nil + end + end +end diff --git a/db/structure.sql b/db/structure.sql index 04a414cc9..f3c8557c2 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2236,32 +2236,29 @@ ALTER TABLE ONLY public.posts ALTER COLUMN tag_index SET STATISTICS 2000; CREATE TABLE public.users ( id integer NOT NULL, name character varying NOT NULL, - level integer DEFAULT 20 NOT NULL, + level integer NOT NULL, inviter_id integer, created_at timestamp without time zone NOT NULL, - last_logged_in_at timestamp without time zone DEFAULT now(), - last_forum_read_at timestamp without time zone DEFAULT '1960-01-01 00:00:00'::timestamp without time zone, - comment_threshold integer DEFAULT 0 NOT NULL, + last_logged_in_at timestamp without time zone, + last_forum_read_at timestamp without time zone, + comment_threshold integer NOT NULL, updated_at timestamp without time zone, - default_image_size character varying DEFAULT 'large'::character varying NOT NULL, + default_image_size character varying NOT NULL, favorite_tags text, - blacklisted_tags text DEFAULT 'spoilers -guro -scat -furry -rating:s'::text, - time_zone character varying DEFAULT 'Eastern Time (US & Canada)'::character varying NOT NULL, - post_update_count integer DEFAULT 0 NOT NULL, - note_update_count integer DEFAULT 0 NOT NULL, - favorite_count integer DEFAULT 0 NOT NULL, - post_upload_count integer DEFAULT 0 NOT NULL, + blacklisted_tags text, + time_zone character varying NOT NULL, + post_update_count integer NOT NULL, + note_update_count integer NOT NULL, + favorite_count integer NOT NULL, + post_upload_count integer NOT NULL, bcrypt_password_hash text, - per_page integer DEFAULT 20 NOT NULL, + per_page integer NOT NULL, custom_style text, - bit_prefs bigint DEFAULT 0 NOT NULL, + bit_prefs bigint NOT NULL, last_ip_addr inet, - unread_dmail_count integer DEFAULT 0 NOT NULL, - theme integer DEFAULT 0 NOT NULL, - upload_points integer DEFAULT 1000 NOT NULL + unread_dmail_count integer NOT NULL, + theme integer NOT NULL, + upload_points integer NOT NULL ); @@ -7885,6 +7882,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20210108030723'), ('20210108030724'), ('20210110015410'), -('20210110090656'); +('20210110090656'), +('20210115015308'); diff --git a/test/factories/user.rb b/test/factories/user.rb index 15ae2acb6..dc6cf0942 100644 --- a/test/factories/user.rb +++ b/test/factories/user.rb @@ -2,13 +2,8 @@ FactoryBot.define do factory(:user, aliases: [:creator, :updater]) do name { SecureRandom.uuid } password {"password"} - default_image_size {"large"} level {20} - created_at {Time.now} last_logged_in_at {Time.now} - favorite_count {0} - bit_prefs {0} - last_forum_read_at {nil} factory(:banned_user) do transient { ban_duration {3} } diff --git a/test/functional/forum_topics_controller_test.rb b/test/functional/forum_topics_controller_test.rb index 06e7d0807..a138f76fa 100644 --- a/test/functional/forum_topics_controller_test.rb +++ b/test/functional/forum_topics_controller_test.rb @@ -72,7 +72,7 @@ class ForumTopicsControllerTest < ActionDispatch::IntegrationTest should "not record a topic visit for non-html requests" do get_auth forum_topic_path(@forum_topic), @user, params: {format: :json} @user.reload - assert_nil(@user.last_forum_read_at) + assert_equal(Time.zone.parse("1960-01-01"), @user.last_forum_read_at) end should "render for atom feed" do