From 1f10f39d1e77eb2dd51b7970a9d8161b0b8e2942 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Fri, 28 Feb 2014 16:40:04 -0800 Subject: [PATCH 01/14] fixes #1227 --- .../specific/meta_searches.css.scss | 5 + app/controllers/meta_searches_controller.rb | 6 + app/controllers/tag_aliases_controller.rb | 8 -- app/helpers/application_helper.rb | 2 +- app/logical/meta_searches/tag.rb | 30 ++++ app/views/meta_searches/tags.html.erb | 129 ++++++++++++++++++ .../tag_aliases/_secondary_links.html.erb | 1 + app/views/tag_aliases/index.html.erb | 7 +- .../_secondary_links.html.erb | 1 + app/views/tag_implications/index.html.erb | 7 +- app/views/tags/_secondary_links.html.erb | 3 +- config/routes.rb | 4 +- test/unit/meta_searches/tag_test.rb | 36 +++++ 13 files changed, 217 insertions(+), 22 deletions(-) create mode 100644 app/assets/stylesheets/specific/meta_searches.css.scss create mode 100644 app/controllers/meta_searches_controller.rb create mode 100644 app/logical/meta_searches/tag.rb create mode 100644 app/views/meta_searches/tags.html.erb create mode 100644 test/unit/meta_searches/tag_test.rb diff --git a/app/assets/stylesheets/specific/meta_searches.css.scss b/app/assets/stylesheets/specific/meta_searches.css.scss new file mode 100644 index 000000000..98133734d --- /dev/null +++ b/app/assets/stylesheets/specific/meta_searches.css.scss @@ -0,0 +1,5 @@ +#c-meta-searches { + section { + margin-bottom: 2em; + } +} \ No newline at end of file diff --git a/app/controllers/meta_searches_controller.rb b/app/controllers/meta_searches_controller.rb new file mode 100644 index 000000000..c9fd25126 --- /dev/null +++ b/app/controllers/meta_searches_controller.rb @@ -0,0 +1,6 @@ +class MetaSearchesController < ApplicationController + def tags + @meta_search = MetaSearches::Tag.new(params) + @meta_search.load_all + end +end diff --git a/app/controllers/tag_aliases_controller.rb b/app/controllers/tag_aliases_controller.rb index 67106a213..96a55ff8f 100644 --- a/app/controllers/tag_aliases_controller.rb +++ b/app/controllers/tag_aliases_controller.rb @@ -12,14 +12,6 @@ class TagAliasesController < ApplicationController respond_with(@tag_alias) end - def general_search - if params[:commit] == "Search Aliases" - redirect_to tag_aliases_path(:search => {:name_matches => params[:query]}) - else - redirect_to tag_implications_path(:search => {:name_matches => params[:query]}) - end - end - def index @search = TagAlias.search(params[:search]) @tag_aliases = @search.order("(case status when 'pending' then 0 when 'queued' then 1 else 2 end), antecedent_name, consequent_name").paginate(params[:page], :limit => params[:limit]) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 2bbb33d59..c9083bc78 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -139,7 +139,7 @@ protected when "artists", "artist_versions" /^\/artist/ - when "tags" + when "tags", "meta_searches" /^\/tags/ when "pools", "pool_versions" diff --git a/app/logical/meta_searches/tag.rb b/app/logical/meta_searches/tag.rb new file mode 100644 index 000000000..46eec97d3 --- /dev/null +++ b/app/logical/meta_searches/tag.rb @@ -0,0 +1,30 @@ +class MetaSearches::Tag + MAX_RESULTS = 25 + attr_reader :search_params, :tags, :tag_aliases, :tag_implications + + def initialize(search_params) + @search_params = search_params + end + + def load_all + load_tags + load_tag_aliases + load_tag_implications + end + + def load_tags + @tags = ::Tag.name_matches(name_param).limit(MAX_RESULTS) + end + + def load_tag_aliases + @tag_aliases = TagAlias.name_matches(name_param).limit(MAX_RESULTS) + end + + def load_tag_implications + @tag_implications = TagImplication.name_matches(name_param).limit(MAX_RESULTS) + end + + def name_param + search_params[:name] || "" + end +end diff --git a/app/views/meta_searches/tags.html.erb b/app/views/meta_searches/tags.html.erb new file mode 100644 index 000000000..21a3bffb3 --- /dev/null +++ b/app/views/meta_searches/tags.html.erb @@ -0,0 +1,129 @@ +
+
+

MetaSearch Tags

+ +
+ <%= form_tag(meta_searches_tags_path, :method => :get) do %> + <%= text_field_tag "name", params[:name] %> + <%= submit_tag "Go" %> + <% end %> +
+ +
+

Tags

+ + <% if @meta_search.tags.empty? %> +

No results

+ <% else %> + + + + + + + + + + + <% @meta_search.tags.each do |tag| %> + + + + + + <% end %> + +
NameCount
<%= tag.name %><%= tag.post_count %> + <% if tag.editable_by?(CurrentUser.user) %> + <%= link_to "edit", edit_tag_path(tag) %> + <% end %> + <% if CurrentUser.is_builder? %> + | <%= link_to "fix", new_tag_correction_path(:tag_id => tag.id) %> + <% end %> +
+ <% end %> +
+ + +
+

Tag Aliases

+ <% if @meta_search.tag_aliases.empty? %> +

No results

+ <% else %> + + + + + + + + + + + <% @meta_search.tag_aliases.each do |tag_alias| %> + + + + + + <% end %> + +
FromTo
<%= tag_alias.antecedent_name %><%= tag_alias.consequent_name %> + <%= link_to "Show", tag_alias_path(tag_alias) %> + <% if tag_alias.deletable_by?(CurrentUser.user) %> + | <%= link_to "Delete", tag_alias_path(tag_alias), :remote => true, :method => :delete, :confirm => "Are you sure you want to delete this alias?" %> + <% end %> + + <% if CurrentUser.is_admin? && tag_alias.is_pending? %> + | <%= link_to "Approve", approve_tag_alias_path(tag_alias), :remote => true, :method => :post %> + <% end %> + + <% if CurrentUser.is_janitor? %> + | <%= link_to "Fix", tag_alias_correction_path(:tag_alias_id => tag_alias.id) %> + <% end %> +
+ <% end %> +
+ +
+

Tag Implications

+ <% if @meta_search.tag_implications.empty? %> +

No results

+ <% else %> + + + + + + + + + + + <% @meta_search.tag_implications.each do |tag_implication| %> + + + + + + <% end %> + +
FromTo
<%= tag_implication.antecedent_name %><%= tag_implication.consequent_name %> + <%= link_to "Show", tag_implication_path(tag_implication) %> + <% if tag_implication.deletable_by?(CurrentUser.user) %> + | <%= link_to "Delete", tag_implication_path(tag_implication), :remote => true, :method => :delete, :confirm => "Are you sure you want to delete this implication?" %> + <% end %> + <% if CurrentUser.user.is_admin? && tag_implication.is_pending? %> + | <%= link_to "Approve", approve_tag_implication_path(tag_implication), :remote => true, :method => :post %> + <% end %> +
+ <% end %> +
+
+
+ +<%= render "tags/secondary_links" %> + +<% content_for(:page_title) do %> + MetaSearch - Tags - <%= Danbooru.config.app_name %> +<% end %> \ No newline at end of file diff --git a/app/views/tag_aliases/_secondary_links.html.erb b/app/views/tag_aliases/_secondary_links.html.erb index b7fc8f59c..663069a99 100644 --- a/app/views/tag_aliases/_secondary_links.html.erb +++ b/app/views/tag_aliases/_secondary_links.html.erb @@ -1,6 +1,7 @@ <% content_for(:secondary_links) do %>
  • <%= link_to "Listing", tag_aliases_path %>
  • +
  • <%= link_to "MetaSearch", meta_searches_tags_path %>
  • <% if CurrentUser.is_admin? %>
  • <%= link_to "New", new_tag_alias_path %>
  • <% else %> diff --git a/app/views/tag_aliases/index.html.erb b/app/views/tag_aliases/index.html.erb index 10f98ca82..6e74c4ad6 100644 --- a/app/views/tag_aliases/index.html.erb +++ b/app/views/tag_aliases/index.html.erb @@ -1,10 +1,9 @@
    diff --git a/app/views/tag_implications/_secondary_links.html.erb b/app/views/tag_implications/_secondary_links.html.erb index 77351b538..aafe29969 100644 --- a/app/views/tag_implications/_secondary_links.html.erb +++ b/app/views/tag_implications/_secondary_links.html.erb @@ -1,6 +1,7 @@ <% content_for(:secondary_links) do %>
  • <%= link_to "Listing", tag_implications_path %>
  • +
  • <%= link_to "MetaSearch", meta_searches_tags_path %>
  • <% if CurrentUser.is_admin? %>
  • <%= link_to "New", new_tag_implication_path %>
  • <% else %> diff --git a/app/views/tag_implications/index.html.erb b/app/views/tag_implications/index.html.erb index 6c018da22..46e54abe5 100644 --- a/app/views/tag_implications/index.html.erb +++ b/app/views/tag_implications/index.html.erb @@ -1,10 +1,9 @@
    diff --git a/app/views/tags/_secondary_links.html.erb b/app/views/tags/_secondary_links.html.erb index 672a295a4..854f4eff6 100644 --- a/app/views/tags/_secondary_links.html.erb +++ b/app/views/tags/_secondary_links.html.erb @@ -2,8 +2,7 @@
  • <%= render "tags/quick_search" %>
  • <%= link_to "Listing", tags_path %>
  • -
  • <%= link_to "Aliases", tag_aliases_path %>
  • -
  • <%= link_to "Implications", tag_implications_path %>
  • +
  • <%= link_to "MetaSearch", meta_searches_tags_path %>
  • <%= link_to "Cheatsheet", wiki_pages_path(:search => {:title => "help:cheatsheet"}) %>
  • <%= link_to "Help", wiki_pages_path(:search => {:title => "help:tags"}) %>
  • <% if @tag %> diff --git a/config/routes.rb b/config/routes.rb index e2cb274d9..dc85c2382 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -202,9 +202,6 @@ Danbooru::Application.routes.draw do member do post :approve end - collection do - get :general_search - end end resource :tag_alias_request, :only => [:new, :create] resources :tag_implications do @@ -370,6 +367,7 @@ Danbooru::Application.routes.draw do match "/static/contact" => "static#contact", :as => "contact" match "/static/benchmark" => "static#benchmark" match "/static/name_change" => "static#name_change", :as => "name_change" + match "/meta_searches/tags" => "meta_searches#tags", :as => "meta_searches_tags" root :to => "posts#index" end diff --git a/test/unit/meta_searches/tag_test.rb b/test/unit/meta_searches/tag_test.rb new file mode 100644 index 000000000..fdb5b8a02 --- /dev/null +++ b/test/unit/meta_searches/tag_test.rb @@ -0,0 +1,36 @@ +require "test_helper" + +module MetaSearches + class TagTest < ActionMailer::TestCase + context "The tag metasearcg" do + setup do + CurrentUser.user = FactoryGirl.create(:user) + CurrentUser.ip_addr = "127.0.0.1" + FactoryGirl.create(:tag, :name => "xxx") + FactoryGirl.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb") + FactoryGirl.create(:tag_implication, :antecedent_name => "ccc", :consequent_name => "ddd") + end + + should "find the tag" do + meta_search = Tag.new(:name => "xxx") + meta_search.load_all + assert_equal(1, meta_search.tags.size) + assert_equal("xxx", meta_search.tags.first.name) + end + + should "find the alias" do + meta_search = Tag.new(:name => "aaa") + meta_search.load_all + assert_equal(1, meta_search.tag_aliases.size) + assert_equal("aaa", meta_search.tag_aliases.first.antecedent_name) + end + + should "find the implication" do + meta_search = Tag.new(:name => "ccc") + meta_search.load_all + assert_equal(1, meta_search.tag_implications.size) + assert_equal("ccc", meta_search.tag_implications.first.antecedent_name) + end + end + end +end From 1b958c6abab2d6d582c5f4641cba97fc99123db8 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Fri, 28 Feb 2014 17:10:59 -0800 Subject: [PATCH 02/14] usability improvements for #1237 --- app/assets/javascripts/post_mode_menu.js | 7 ++++++- app/views/posts/partials/index/_mode_menu.html.erb | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/post_mode_menu.js b/app/assets/javascripts/post_mode_menu.js index 9efd7c45b..fd391421c 100644 --- a/app/assets/javascripts/post_mode_menu.js +++ b/app/assets/javascripts/post_mode_menu.js @@ -15,6 +15,10 @@ Danbooru.PostModeMenu.initialize_shortcuts = function() { $(document).bind("keypress", "1 2 3 4 5 6 7 8 9 0", Danbooru.PostModeMenu.change_tag_script); } + + Danbooru.PostModeMenu.show_notice = function(i) { + Danbooru.notice("Switched to tag script #" + i + ". To switch tag scripts, use the number keys."); + } Danbooru.PostModeMenu.change_tag_script = function(e) { if ($("#mode-box select").val() === "tag-script") { @@ -27,7 +31,7 @@ $("#tag-script-field").val(new_tag_script); Danbooru.Cookie.put("current_tag_script_id", new_tag_script_id); if (old_tag_script_id != new_tag_script_id) { - Danbooru.notice("Switched to tag script #" + new_tag_script_id + ". To switch tag scripts, use the number keys."); + Danbooru.PostModeMenu.show_notice(new_tag_script_id); } e.preventDefault(); @@ -121,6 +125,7 @@ var script = Danbooru.Cookie.get("tag-script-" + current_script_id); $("#tag-script-field").val(script).show(); + Danbooru.PostModeMenu.show_notice(1); } else { $("#tag-script-field").hide(); } diff --git a/app/views/posts/partials/index/_mode_menu.html.erb b/app/views/posts/partials/index/_mode_menu.html.erb index a16e63c21..a2edbc453 100644 --- a/app/views/posts/partials/index/_mode_menu.html.erb +++ b/app/views/posts/partials/index/_mode_menu.html.erb @@ -22,6 +22,6 @@ <% end %> + - <% end %> From c85f61577399185df6686f65fe55a26613a038b1 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Fri, 28 Feb 2014 17:11:46 -0800 Subject: [PATCH 03/14] usability improvements for #1237 --- app/assets/javascripts/post_mode_menu.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/post_mode_menu.js b/app/assets/javascripts/post_mode_menu.js index fd391421c..246d2d61b 100644 --- a/app/assets/javascripts/post_mode_menu.js +++ b/app/assets/javascripts/post_mode_menu.js @@ -125,7 +125,7 @@ var script = Danbooru.Cookie.get("tag-script-" + current_script_id); $("#tag-script-field").val(script).show(); - Danbooru.PostModeMenu.show_notice(1); + Danbooru.PostModeMenu.show_notice(current_script_id); } else { $("#tag-script-field").hide(); } From b523ed471cb80646916d6a7b23490a0dd02d541e Mon Sep 17 00:00:00 2001 From: r888888888 Date: Fri, 28 Feb 2014 17:57:33 -0800 Subject: [PATCH 04/14] fixes #1249 --- app/helpers/posts_helper.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/helpers/posts_helper.rb b/app/helpers/posts_helper.rb index 2abf9b765..5b65c939d 100644 --- a/app/helpers/posts_helper.rb +++ b/app/helpers/posts_helper.rb @@ -17,13 +17,17 @@ module PostsHelper end end + def wordbreakify(string) + string.gsub(/(.{15})/, "\\1") + end + def post_source_tag(post) if post.source =~ %r!http://img\d+\.pixiv\.net/img/([^\/]+)/! - text = "pixiv/#{$1}" + text = "pixiv/#{wordbreakify($1)}" source_link = link_to(text, post.normalized_source) source_search = "source:#{text}/" elsif post.source =~ %r!http://i\d\.pixiv\.net/img\d+/img/([^\/]+)/! - text = "pixiv/#{$1}" + text = "pixiv/#{wordbreakify($1)}" source_link = link_to(text, post.normalized_source) source_search = "source:#{text}/" elsif post.source =~ %r{\Ahttps?://} From 0ef37c0538150335d6587def60a57115760f9534 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Fri, 28 Feb 2014 18:10:36 -0800 Subject: [PATCH 05/14] fixes #1242 --- app/helpers/application_helper.rb | 4 ++++ app/helpers/post_versions_helper.rb | 6 +++--- app/helpers/posts_helper.rb | 4 ---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c9083bc78..ac05ecbaa 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,8 @@ module ApplicationHelper + def wordbreakify(string) + raw(string.gsub(/(.{10})/, "\\1")) + end + def nav_link_to(text, url, options = nil) if nav_link_match(params[:controller], url) klass = "current" diff --git a/app/helpers/post_versions_helper.rb b/app/helpers/post_versions_helper.rb index aa9716d58..a344ed09c 100644 --- a/app/helpers/post_versions_helper.rb +++ b/app/helpers/post_versions_helper.rb @@ -4,14 +4,14 @@ module PostVersionsHelper html = [] diff[:added_tags].each do |tag| prefix = diff[:obsolete_added_tags].include?(tag) ? '+' : '+' - html << prefix + link_to(tag, posts_path(:tags => tag)) + '' + html << prefix + link_to(wordbreakify(tag), posts_path(:tags => tag)) + '' end diff[:removed_tags].each do |tag| prefix = diff[:obsolete_removed_tags].include?(tag) ? '-' : '-' - html << prefix + link_to(tag, posts_path(:tags => tag)) + '' + html << prefix + link_to(wordbreakify(tag), posts_path(:tags => tag)) + '' end diff[:unchanged_tags].each do |tag| - html << '' + link_to(tag, posts_path(:tags => tag)) + '' + html << '' + link_to(wordbreakify(tag), posts_path(:tags => tag)) + '' end return html.join(" ").html_safe end diff --git a/app/helpers/posts_helper.rb b/app/helpers/posts_helper.rb index 5b65c939d..5c753f7f3 100644 --- a/app/helpers/posts_helper.rb +++ b/app/helpers/posts_helper.rb @@ -17,10 +17,6 @@ module PostsHelper end end - def wordbreakify(string) - string.gsub(/(.{15})/, "\\1") - end - def post_source_tag(post) if post.source =~ %r!http://img\d+\.pixiv\.net/img/([^\/]+)/! text = "pixiv/#{wordbreakify($1)}" From c58874cdfcfa591dbccc32e6b53d5ee982bd990a Mon Sep 17 00:00:00 2001 From: r888888888 Date: Fri, 28 Feb 2014 18:17:48 -0800 Subject: [PATCH 06/14] addresses #1255 --- app/models/post.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/post.rb b/app/models/post.rb index fdc70ee56..3a9dfe6bd 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -624,7 +624,7 @@ class Post < ActiveRecord::Base module FavoriteMethods def clean_fav_string? - rand(100) < [Math.log(fav_string.size, 2), 5].min + rand(100) < 50 end def clean_fav_string! From 093d2fc78356e8226730d90cd58f45c76727fc68 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Tue, 4 Mar 2014 17:12:12 -0800 Subject: [PATCH 07/14] fix for #1261 --- app/controllers/user_name_change_requests_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/user_name_change_requests_controller.rb b/app/controllers/user_name_change_requests_controller.rb index 2d9ff126a..2f62cf54e 100644 --- a/app/controllers/user_name_change_requests_controller.rb +++ b/app/controllers/user_name_change_requests_controller.rb @@ -51,7 +51,7 @@ class UserNameChangeRequestsController < ApplicationController private def check_privileges!(change_request) - return if CurrentUser.is_janitor? + return if CurrentUser.is_admin? raise User::PrivilegeError if change_request.user_id != CurrentUser.user.id end end From 14dd0430cb14642c5337a4c155c91b2c3ce2fa6b Mon Sep 17 00:00:00 2001 From: r888888888 Date: Wed, 5 Mar 2014 17:33:57 -0800 Subject: [PATCH 08/14] fixes #1257 --- app/controllers/admin/users_controller.rb | 5 +---- app/models/user.rb | 19 +++++++++++++++++-- test/unit/user_test.rb | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 64a317543..b19f11142 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -10,10 +10,7 @@ module Admin def update @user = User.find(params[:id]) sanitize_params! - @user.level = params[:user][:level] - @user.inviter_id = CurrentUser.id - TransactionLogItem.record_account_upgrade(@user) - @user.save + @user.promote_to!(params[:user][:level]) redirect_to edit_admin_user_path(@user), :notice => "User updated" end diff --git a/app/models/user.rb b/app/models/user.rb index b37409996..1957b04a0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -242,8 +242,23 @@ class User < ActiveRecord::Base end end - def promote_to(level) - update_attributes({:level => level}, :as => :admin) + def promote_to!(new_level) + self.level = new_level + self.inviter_id = CurrentUser.user.id + TransactionLogItem.record_account_upgrade(self) + if level > level_was + body_prefix = "Promoted" + elsif level < level_was + body_prefix = "Demoted" + else + body_prefix = "" + end + + feedback.create( + :category => "neutral", + :body => "#{body_prefix} by #{inviter.name} from #{level_string(level_was)} to #{level_string(level)}" + ) + save end def promote_to_admin_if_first_user diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 26255e5fb..dddb9741b 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -13,6 +13,26 @@ class UserTest < ActiveSupport::TestCase CurrentUser.user = nil CurrentUser.ip_addr = nil end + + context "promoting a user" do + setup do + CurrentUser.user = FactoryGirl.create(:moderator_user) + end + + should "create a transaction log item" do + assert_difference("TransactionLogItem.count") do + @user.promote_to!(User::Levels::GOLD) + end + end + + should "create a neutral feedback" do + assert_difference("UserFeedback.count") do + @user.promote_to!(User::Levels::GOLD) + end + + assert_equal("Promoted by #{CurrentUser.user.name} from Member to Gold", @user.feedback.last.body) + end + end context "favoriting a post" do setup do From 56ae78f96377b3e29d1de2c6fbbda8e17915b3eb Mon Sep 17 00:00:00 2001 From: r888888888 Date: Wed, 5 Mar 2014 17:44:07 -0800 Subject: [PATCH 09/14] refactored user promotion code, related to #1257 --- app/controllers/admin/users_controller.rb | 13 ------ app/logical/user_promotion.rb | 52 +++++++++++++++++++++++ app/models/user.rb | 23 +++------- 3 files changed, 58 insertions(+), 30 deletions(-) create mode 100644 app/logical/user_promotion.rb diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index b19f11142..fdb721cd4 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -9,21 +9,8 @@ module Admin def update @user = User.find(params[:id]) - sanitize_params! @user.promote_to!(params[:user][:level]) redirect_to edit_admin_user_path(@user), :notice => "User updated" end - - protected - def sanitize_params! - # admins can do anything - return if CurrentUser.is_admin? - - # can't promote/demote moderators - raise User::PrivilegeError if @user.is_moderator? - - # can't promote to admin - raise User::PrivilegeError if params[:user] && params[:user][:level].to_i >= User::Levels::ADMIN - end end end diff --git a/app/logical/user_promotion.rb b/app/logical/user_promotion.rb new file mode 100644 index 000000000..a37ad1a33 --- /dev/null +++ b/app/logical/user_promotion.rb @@ -0,0 +1,52 @@ +class UserPromotion + attr_reader :user, :promoter, :new_level + + def initialize(user, promoter, new_level) + @user = user + @promoter = promoter + @new_level = new_level + end + + def promote! + user.level = new_level + user.inviter_id = promoter.id + + validate + create_transaction_log_item + create_user_feedback + + user.save + end + +private + + def validate + # admins can do anything + return if promoter.is_admin? + + # can't promote/demote moderators + raise User::PrivilegeError if user.is_moderator? + + # can't promote to admin + raise User::PrivilegeError if new_level.to_i >= User::Levels::ADMIN + end + + def create_transaction_log_item + TransactionLogItem.record_account_upgrade(user) + end + + def create_user_feedback + if user.level > user.level_was + body_prefix = "Promoted" + elsif user.level < user.level_was + body_prefix = "Demoted" + else + body_prefix = "Updated" + end + + user.feedback.create( + :category => "neutral", + :body => "#{body_prefix} by #{promoter.name} from #{user.level_string_was} to #{user.level_string}" + ) + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 1957b04a0..99b181b48 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -243,22 +243,7 @@ class User < ActiveRecord::Base end def promote_to!(new_level) - self.level = new_level - self.inviter_id = CurrentUser.user.id - TransactionLogItem.record_account_upgrade(self) - if level > level_was - body_prefix = "Promoted" - elsif level < level_was - body_prefix = "Demoted" - else - body_prefix = "" - end - - feedback.create( - :category => "neutral", - :body => "#{body_prefix} by #{inviter.name} from #{level_string(level_was)} to #{level_string(level)}" - ) - save + UserPromotion.new(self, CurrentUser.user, new_level).promote! end def promote_to_admin_if_first_user @@ -296,6 +281,10 @@ class User < ActiveRecord::Base end end + def level_string_was + level_string(level_was) + end + def level_string(value = nil) case (value || level) when Levels::BLOCKED @@ -372,7 +361,7 @@ class User < ActiveRecord::Base def create_mod_action if level_changed? - ModAction.create(:description => %{"#{name}":/users/#{id} level changed #{level_string(level_was)} -> #{level_string}}) + ModAction.create(:description => %{"#{name}":/users/#{id} level changed #{level_string_was} -> #{level_string}}) end end From 37d75e4a8830863ac355f96480060752d285330b Mon Sep 17 00:00:00 2001 From: r888888888 Date: Wed, 5 Mar 2014 17:47:40 -0800 Subject: [PATCH 10/14] Fixes #1266 --- app/logical/user_promotion.rb | 28 +++++++++++++++++++++++++++- app/models/user_feedback.rb | 9 ++++++--- test/unit/user_test.rb | 6 ++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/app/logical/user_promotion.rb b/app/logical/user_promotion.rb index a37ad1a33..d6a2d0e83 100644 --- a/app/logical/user_promotion.rb +++ b/app/logical/user_promotion.rb @@ -14,6 +14,7 @@ class UserPromotion validate create_transaction_log_item create_user_feedback + create_dmail user.save end @@ -46,7 +47,32 @@ private user.feedback.create( :category => "neutral", - :body => "#{body_prefix} by #{promoter.name} from #{user.level_string_was} to #{user.level_string}" + :body => "#{body_prefix} by #{promoter.name} from #{user.level_string_was} to #{user.level_string}", + :disable_dmail_notification => true + ) + end + + def create_dmail + if user.level >= user.level_was + create_promotion_dmail + else + create_demotion_dmail + end + end + + def create_promotion_dmail + Dmail.create_split( + :to_id => user.id, + :title => "You have been promoted", + :body => "You have been promoted to a #{user.level_string} level account." + ) + end + + def create_demotion_dmail + Dmail.create_split( + :to_id => user.id, + :title => "You have been demoted", + :body => "You have been demoted to a #{user.level_string} level account." ) end end diff --git a/app/models/user_feedback.rb b/app/models/user_feedback.rb index ed8f5e7ff..70933353b 100644 --- a/app/models/user_feedback.rb +++ b/app/models/user_feedback.rb @@ -3,7 +3,8 @@ class UserFeedback < ActiveRecord::Base belongs_to :user belongs_to :creator, :class_name => "User" before_validation :initialize_creator, :on => :create - attr_accessible :body, :user_id, :category, :user_name + attr_accessor :disable_dmail_notification + attr_accessible :body, :user_id, :category, :user_name, :disable_dmail_notification validates_presence_of :user, :creator, :body, :category validates_inclusion_of :category, :in => %w(positive negative neutral) validate :creator_is_gold @@ -74,8 +75,10 @@ class UserFeedback < ActiveRecord::Base end def create_dmail - body = %{#{creator_name} created a "#{category} record":/user_feedbacks?search[user_id]=#{user_id} for your account. #{body}} - Dmail.create_split(:to_id => user_id, :title => "Your user record has been updated", :body => body) + unless disable_dmail_notification + body = %{#{creator_name} created a "#{category} record":/user_feedbacks?search[user_id]=#{user_id} for your account. #{body}} + Dmail.create_split(:to_id => user_id, :title => "Your user record has been updated", :body => body) + end end def creator_is_gold diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index dddb9741b..297f09d08 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -32,6 +32,12 @@ class UserTest < ActiveSupport::TestCase assert_equal("Promoted by #{CurrentUser.user.name} from Member to Gold", @user.feedback.last.body) end + + should "create a dmail" do + assert_difference("Dmail.count", 2) do + @user.promote_to!(User::Levels::GOLD) + end + end end context "favoriting a post" do From abe0fa5febdf8d8f9c3588a940456d299c129b70 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Wed, 5 Mar 2014 17:59:46 -0800 Subject: [PATCH 11/14] fixes #1276 --- app/views/users/_post_summary.html.erb | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/app/views/users/_post_summary.html.erb b/app/views/users/_post_summary.html.erb index 6675133a9..e3093232b 100644 --- a/app/views/users/_post_summary.html.erb +++ b/app/views/users/_post_summary.html.erb @@ -1,28 +1,24 @@ -
    -

    <%= link_to "Uploads", posts_path(:tags => "user:#{user.name}") %>

    - <% if presenter.has_uploads? %> +<% if presenter.has_uploads? %> +
    +

    <%= link_to "Uploads", posts_path(:tags => "user:#{user.name}") %>

    <% presenter.uploads.each do |post| %> <%= PostPresenter.preview(post, :tags => "user:#{user.name}") %> <% end %>
    - <% else %> -

    None

    - <% end %> -
    +
    +<% end %> -
    -

    <%= link_to "Favorites", favorites_path(:user_id => user.id) %>

    - <% if presenter.has_favorites? %> +<% if presenter.has_favorites? %> +
    +

    <%= link_to "Favorites", favorites_path(:user_id => user.id) %>

    <% presenter.favorites.each do |post| %> <%= PostPresenter.preview(post, :tags => "fav:#{user.name}") %> <% end %>
    - <% else %> -

    None

    - <% end %> -
    +
    +<% end %> <% presenter.subscriptions.each do |subscription| %>
    From 42915c2f8eb42ac0c473cebfc476c75836ec685e Mon Sep 17 00:00:00 2001 From: r888888888 Date: Thu, 6 Mar 2014 18:04:18 -0800 Subject: [PATCH 12/14] fixes #1367 --- app/logical/api_cache_generator.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/logical/api_cache_generator.rb b/app/logical/api_cache_generator.rb index b871520ee..e0aa15f1e 100644 --- a/app/logical/api_cache_generator.rb +++ b/app/logical/api_cache_generator.rb @@ -1,7 +1,8 @@ class ApiCacheGenerator def generate_tag_cache - FileUtils.mkdir_p("/var/www/danbooru2/shared/system/cache") - File.open("/var/www/danbooru2/shared/system/cache/tags.json", "w") do |f| + path = File.expand_path(File.join(Rails.root, "..", "shared")) + FileUtils.mkdir_p("#{path}/system/cache") + File.open("#{path}/system/cache/tags.json", "w") do |f| f.print("[") Tag.without_timeout do Tag.find_each do |tag| @@ -20,11 +21,11 @@ class ApiCacheGenerator f.seek(-2, IO::SEEK_END) f.print("]\n") end - Zlib::GzipWriter.open("/var/www/danbooru2/shared/system/cache/tags.json.gz") do |gz| - gz.write(IO.binread("/var/www/danbooru2/shared/system/cache/tags.json")) + Zlib::GzipWriter.open("#{path}/system/cache/tags.json.gz") do |gz| + gz.write(IO.binread("#{path}/system/cache/tags.json")) gz.close end - RemoteFileManager.new("/var/www/danbooru2/shared/system/cache/tags.json").distribute - RemoteFileManager.new("/var/www/danbooru2/shared/system/cache/tags.json.gz").distribute + RemoteFileManager.new("#{path}/system/cache/tags.json").distribute + RemoteFileManager.new("#{path}/shared/system/cache/tags.json.gz").distribute end end From 03e8906b09ff1085d9b8d4bf7e96cbb7ad2ba1c6 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Thu, 6 Mar 2014 18:18:18 -0800 Subject: [PATCH 13/14] fixes #1366 --- config/deploy.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/config/deploy.rb b/config/deploy.rb index 8be25577b..cb37b7251 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -14,6 +14,10 @@ set :whenever_command, "bundle exec whenever" set :whenever_environment, defer {stage} require 'whenever/capistrano' +require 'securerandom' +set :secret_1, SecureRandom.base64(32) +set :secret_2, SecureRandom.base64(32) + set :application, "danbooru" set :repository, "git://github.com/r888888888/danbooru.git" set :scm, :git @@ -30,6 +34,15 @@ namespace :local_config do run "mkdir -p #{deploy_to}/shared/config" end + desc "Initialize the secrets" + task :setup_secrets do + run "mkdir -p ~/.danbooru" + run "if [[ ! -e ~/.danbooru/session_secret_key ]] ; then echo '#{secret_1}' > ~/.danbooru/session_secret_key ; fi" + run "if [[ ! -e ~/.danbooru/secret_token ]] ; then echo '#{secret_2}' > ~/.danbooru/secret_token ; fi" + run "chmod -R 600 ~/.danbooru" + run "chown -R #{user}:#{user} ~/.danbooru" + end + desc "Initialize local config files" task :setup_local_files do run "curl -s https://raw.github.com/r888888888/danbooru/master/script/install/danbooru_local_config.rb.templ > #{deploy_to}/shared/config/danbooru_local_config.rb" @@ -138,6 +151,7 @@ after "deploy:setup", "reset_ownership_of_common_directory" after "deploy:setup", "local_config:setup_shared_directory" after "deploy:setup", "local_config:setup_local_files" after "deploy:setup", "data:setup_directories" +after "deploy:setup", "local_config:setup_secrets" after "deploy:create_symlink", "local_config:link_local_files" after "deploy:create_symlink", "data:link_directories" after "deploy:start", "delayed_job:start" From 159c297306013cba377dd328a5e809f510c009fe Mon Sep 17 00:00:00 2001 From: r888888888 Date: Thu, 6 Mar 2014 18:35:46 -0800 Subject: [PATCH 14/14] fixes #1414 --- app/logical/daily_maintenance.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/logical/daily_maintenance.rb b/app/logical/daily_maintenance.rb index c127e09ec..c648a944a 100644 --- a/app/logical/daily_maintenance.rb +++ b/app/logical/daily_maintenance.rb @@ -7,6 +7,7 @@ class DailyMaintenance ModAction.delete_all(['created_at < ?', 3.days.ago]) Delayed::Job.delete_all(['created_at < ?', 7.days.ago]) PostVote.delete_all(['created_at < ?', 1.month.ago]) + CommentVote.delete_all(['created_at < ?', 1.month.ago]) TagSubscription.process_all ApiCacheGenerator.new.generate_tag_cache end