From 66fc05e30be1fc45428b6cf2f2f568ba5ecca1b0 Mon Sep 17 00:00:00 2001 From: albert Date: Wed, 20 Feb 2013 00:02:43 -0500 Subject: [PATCH] change privileged accounts to gold accounts, add platinum accounts, add favorite and tag query limiting based on level --- app/controllers/favorites_controller.rb | 6 ++++- app/helpers/admin/users_helper.rb | 4 ++- app/logical/anonymous_user.rb | 14 +++++++++- app/logical/post_query_builder.rb | 8 +++--- app/models/tag.rb | 2 +- app/models/user.rb | 30 +++++++++++++++++++++- app/presenters/post_set_presenters/post.rb | 2 +- app/views/favorites/create.js.erb | 8 ++++-- test/factories/user.rb | 1 + 9 files changed, 63 insertions(+), 12 deletions(-) diff --git a/app/controllers/favorites_controller.rb b/app/controllers/favorites_controller.rb index 5c7dbb519..eeb4b270f 100644 --- a/app/controllers/favorites_controller.rb +++ b/app/controllers/favorites_controller.rb @@ -10,7 +10,11 @@ class FavoritesController < ApplicationController end def create - Post.find(params[:post_id]).add_favorite!(CurrentUser.user) + if CurrentUser.favorite_limit.nil? || CurrentUser.favorite_count < CurrentUser.favorite_limit + Post.find(params[:post_id]).add_favorite!(CurrentUser.user) + else + @error_msg = "You can only keep up to #{CurrentUser.favorite_limit} favorites. Upgrade your account to save more." + end end def destroy diff --git a/app/helpers/admin/users_helper.rb b/app/helpers/admin/users_helper.rb index 21aad0e82..31e844bcc 100644 --- a/app/helpers/admin/users_helper.rb +++ b/app/helpers/admin/users_helper.rb @@ -2,7 +2,9 @@ module Admin::UsersHelper def user_level_select(object, field) options = [ ["Member", User::Levels::MEMBER], - ["Privileged", User::Levels::PRIVILEGED], + ["Gold", User::Levels::PRIVILEGED], + ["Platinum", User::Levels::PLATINUM], + ["Builder", User::Levels::BUILDER], ["Contributor", User::Levels::CONTRIBUTOR], ["Janitor", User::Levels::JANITOR], ["Moderator", User::Levels::MODERATOR], diff --git a/app/logical/anonymous_user.rb b/app/logical/anonymous_user.rb index 9021493f3..352fccf05 100644 --- a/app/logical/anonymous_user.rb +++ b/app/logical/anonymous_user.rb @@ -133,7 +133,19 @@ class AnonymousUser :anonymous end - %w(member banned privileged contributor janitor moderator admin).each do |name| + def tag_query_limit + 2 + end + + def favorite_limit + 0 + end + + def favorite_count + 0 + end + + %w(member banned privileged platinum contributor janitor moderator admin).each do |name| define_method("is_#{name}?") do false end diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index ed98a70dd..fc013c192 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -49,24 +49,24 @@ class PostQueryBuilder "''" + escaped_token + "''" end end - + def add_tag_string_search_relation(tags, relation) tag_query_sql = [] if tags[:include].any? - raise ::Post::SearchError.new("You cannot search for more than #{Danbooru.config.tag_query_limit} tags at a time") if tags[:include].size > Danbooru.config.tag_query_limit + raise ::Post::SearchError.new("You cannot search for more than #{CurrentUser.tag_query_limit} tags at a time") if tags[:include].size > CurrentUser.tag_query_limit tag_query_sql << "(" + escape_string_for_tsquery(tags[:include]).join(" | ") + ")" has_constraints! end if tags[:related].any? - raise ::Post::SearchError.new("You cannot search for more than #{Danbooru.config.tag_query_limit} tags at a time") if tags[:related].size > Danbooru.config.tag_query_limit + raise ::Post::SearchError.new("You cannot search for more than #{CurrentUser.tag_query_limit} tags at a time") if tags[:related].size > CurrentUser.tag_query_limit tag_query_sql << "(" + escape_string_for_tsquery(tags[:related]).join(" & ") + ")" has_constraints! end if tags[:exclude].any? - raise ::Post::SearchError.new("You cannot search for more than #{Danbooru.config.tag_query_limit} tags at a time") if tags[:exclude].size > Danbooru.config.tag_query_limit + raise ::Post::SearchError.new("You cannot search for more than #{CurrentUser.tag_query_limit} tags at a time") if tags[:exclude].size > CurrentUser.tag_query_limit raise ::Post::SearchError.new("You cannot search for only excluded tags") unless has_constraints? tag_query_sql << "!(" + escape_string_for_tsquery(tags[:exclude]).join(" | ") + ")" diff --git a/app/models/tag.rb b/app/models/tag.rb index 39f07decd..9b9bc5773 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -210,7 +210,7 @@ class Tag < ActiveRecord::Base output[:include] << tag[1..-1] elsif tag =~ /\*/ - matches = Tag.name_matches(tag).all(:select => "name", :limit => Danbooru.config.tag_query_limit, :order => "post_count DESC").map(&:name) + matches = Tag.name_matches(tag).all(:select => "name", :limit => CurrentUser.tag_query_limit, :order => "post_count DESC").map(&:name) matches = ["~no_matches~"] if matches.empty? output[:include] += matches diff --git a/app/models/user.rb b/app/models/user.rb index 2150c0c62..bdc047a29 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,6 +8,7 @@ class User < ActiveRecord::Base BLOCKED = 10 MEMBER = 20 PRIVILEGED = 30 + PLATINUM = 31 BUILDER = 32 CONTRIBUTOR = 33 JANITOR = 35 @@ -262,7 +263,10 @@ class User < ActiveRecord::Base "Builder" when Levels::PRIVILEGED - "Privileged" + "Gold" + + when Levels::PLATINUM + "Platinum" when Levels::CONTRIBUTOR "Contributor" @@ -294,6 +298,10 @@ class User < ActiveRecord::Base level >= Levels::PRIVILEGED end + def is_platinum? + level >= Levels::PLATINUM + end + def is_contributor? level >= Levels::CONTRIBUTOR end @@ -403,6 +411,26 @@ class User < ActiveRecord::Base limit end + + def tag_query_limit + if is_privileged? + Danbooru.config.tag_query_limit + elsif is_platinum? + Danbooru.config.tag_query_limit * 2 + else + 2 + end + end + + def favorite_limit + if is_privileged? + 5_000 + elsif is_platinum? + nil + else + 1_000 + end + end end module ApiMethods diff --git a/app/presenters/post_set_presenters/post.rb b/app/presenters/post_set_presenters/post.rb index 4b64a70e4..1db6bb9cc 100644 --- a/app/presenters/post_set_presenters/post.rb +++ b/app/presenters/post_set_presenters/post.rb @@ -36,7 +36,7 @@ module PostSetPresenters end def pattern_tags - Tag.name_matches(post_set.tag_string).all(:select => "name", :limit => Danbooru.config.tag_query_limit, :order => "post_count DESC").map(&:name) + Tag.name_matches(post_set.tag_string).all(:select => "name", :limit => CurrentUser.tag_query_limit, :order => "post_count DESC").map(&:name) end def related_tags_for_group diff --git a/app/views/favorites/create.js.erb b/app/views/favorites/create.js.erb index 462528fcb..9acf965f2 100644 --- a/app/views/favorites/create.js.erb +++ b/app/views/favorites/create.js.erb @@ -1,2 +1,6 @@ -$("a#add-to-favorites").hide(); -$("a#remove-from-favorites").show(); +<% if @error_msg %> + Danbooru.error("<%= @error_msg %>"); +<% else %> + $("a#add-to-favorites").hide(); + $("a#remove-from-favorites").show(); +<% end %> diff --git a/test/factories/user.rb b/test/factories/user.rb index 8ea0aca43..79c4663a9 100644 --- a/test/factories/user.rb +++ b/test/factories/user.rb @@ -8,6 +8,7 @@ FactoryGirl.define do base_upload_limit 10 level 20 last_logged_in_at {Time.now} + favorite_count 0 factory(:banned_user) do is_banned true