From 5e3b243b67e4b8de034794f691a336300b4df6a7 Mon Sep 17 00:00:00 2001 From: albert Date: Tue, 20 Dec 2011 16:18:35 -0500 Subject: [PATCH] users with no negative feedback can now change their names --- app/controllers/users_controller.rb | 8 +++++++- app/logical/popular_post_explorer.rb | 3 +-- app/models/user.rb | 19 ++++++++++++++++++- app/views/users/edit.html.erb | 5 ++++- config/routes.rb | 3 ++- test/unit/user_test.rb | 16 ++++++++++++++++ 6 files changed, 48 insertions(+), 6 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index bd873c258..6d7a9d8c8 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,6 +1,6 @@ class UsersController < ApplicationController respond_to :html, :xml, :json - before_filter :member_only, :only => [:edit, :update] + before_filter :member_only, :only => [:edit, :update, :upgrade] rescue_from User::PrivilegeError, :with => "static/access_denied" def new @@ -53,6 +53,12 @@ class UsersController < ApplicationController redirect_to user_path(@user), :notice => "Email was sent" end + + def cache + @user = User.find(params[:id]) + @user.update_cache + render :nothing => true + end private def check_privilege(user) diff --git a/app/logical/popular_post_explorer.rb b/app/logical/popular_post_explorer.rb index 44bdf54fd..4f2350fc1 100644 --- a/app/logical/popular_post_explorer.rb +++ b/app/logical/popular_post_explorer.rb @@ -9,8 +9,7 @@ class PopularPostExplorer private def load_posts - # Post.tag_match("order:rank").where("image_width >= ?", Danbooru.config.medium_image_width).limit(5).offset(offset) - @posts = Post.where("image_width >= ?", Danbooru.config.medium_image_width).limit(50) + Post.tag_match("order:rank").where("image_width >= ?", Danbooru.config.medium_image_width).limit(5).offset(offset) end def sort_posts diff --git a/app/models/user.rb b/app/models/user.rb index 7695f9d43..f2ac0ec03 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -25,10 +25,12 @@ class User < ActiveRecord::Base validates_confirmation_of :password validates_presence_of :email, :if => lambda {|rec| rec.new_record? && Danbooru.config.enable_email_verification?} validate :validate_ip_addr_is_not_banned, :on => :create + validate :validate_feedback_on_name_change, :on => :update before_validation :normalize_blacklisted_tags before_create :encrypt_password_on_create before_update :encrypt_password_on_update after_save :update_cache + after_update :update_remote_cache before_create :promote_to_admin_if_first_user has_many :feedback, :class_name => "UserFeedback", :dependent => :destroy has_many :posts, :foreign_key => "uploader_id" @@ -71,7 +73,7 @@ class User < ActiveRecord::Base module ClassMethods def name_to_id(name) - Cache.get("uni:#{Cache.sanitize(name)}") do + Cache.get("uni:#{Cache.sanitize(name)}", 1.hour) do select_value_sql("SELECT id FROM users WHERE lower(name) = ?", name.downcase) end end @@ -98,6 +100,21 @@ class User < ActiveRecord::Base def update_cache Cache.put("uin:#{id}", name) end + + def update_remote_cache + if name_changed? + Danbooru.config.other_server_hosts.each do |server| + Net::HTTP.delete(URI.parse("http://#{server}/users/#{id}/cache")) + end + end + end + + def validate_feedback_on_name_change + if feedback.negative.count > 0 && name_changed? + self.errors[:base] << "You can not change your name if you have any negative feedback" + return false + end + end end module PasswordMethods diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index bfd5ea468..85f7b2488 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -4,11 +4,14 @@ <%= simple_form_for @user do |f| %>
+ <% if @user.feedback.negative.count == 0 %> + <%= f.input :name %> + <% end %> + <%= f.input :email, :required => Danbooru.config.enable_email_verification?, :hint => "Used for messages and for password resets", :as => :email %> <%= f.input :time_zone %> <%= f.input :receive_email_notifications %> <%= f.input :comment_threshold, :hint => "Comments below this score will be hidden by default" %> - <%= f.input :always_resize_images, :hint => "Automatically resize images to fit your browser window" %> <%= f.input :default_image_size, :hint => "Medium shows images resized to #{Danbooru.config.medium_image_width} pixels wide, large is #{Danbooru.config.large_image_width} pixels wide, and original is whatever the original image is", :collection => %w(medium large original), :include_blank => false %> <%= f.input :favorite_tags, :hint => "A list of whitespace delimited tags that show up in your profile", :input_html => {:size => "40x5"} %> <%= f.input :blacklisted_tags, :hint => "A list of newline delimited tags that you never want to see", :input_html => {:size => "40x5"} %> diff --git a/config/routes.rb b/config/routes.rb index 3beb5a90c..33bacc7e5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -158,6 +158,7 @@ Danbooru::Application.routes.draw do end member do + delete :cache post :upgrade end end @@ -171,7 +172,7 @@ Danbooru::Application.routes.draw do end end resources :wiki_page_versions, :only => [:index, :show] - + # aliases resources :wpages, :controller => "wiki_pages" resources :ftopics, :controller => "forum_topics" diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index e05923fcb..eec5c541b 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -31,6 +31,22 @@ class UserTest < ActiveSupport::TestCase assert_equal(User::Levels::MEMBER, @user.level) end end + + context "who has negeative feedback and is trying to change their name" do + setup do + @mod = Factory.create(:moderator_user) + + CurrentUser.scoped(@mod, "127.0.0.1") do + Factory.create(:user_feedback, :user => @user, :category => "negative") + end + end + + should "not validate" do + @user.reload + @user.update_attributes(:name => "fanfarlo") + assert_equal(["You can not change your name if you have any negative feedback"], @user.errors.full_messages) + end + end should "not validate if the originating ip address is banned" do Factory.create(:ip_ban)