From c8afd34d158018f3284bdf1f8d28685a5195be00 Mon Sep 17 00:00:00 2001 From: albert Date: Fri, 29 Jul 2011 18:04:50 -0400 Subject: [PATCH] added ip addr search --- app/assets/stylesheets/application.css.scss | 6 ++ app/controllers/favorites_controller.rb | 2 + .../moderator/ip_addrs_controller.rb | 1 + .../tag_subscriptions_controller.rb | 6 ++ app/helpers/application_helper.rb | 8 ++- app/helpers/pagination_helper.rb | 8 +-- .../moderator/{ => dashboard}/report.rb | 0 app/logical/moderator/ip_addr_search.rb | 59 +++++++++++++++++++ app/presenters/post_set_presenter.rb | 4 +- app/views/artist_versions/index.html.erb | 4 +- app/views/artists/edit.html.erb | 2 +- app/views/artists/index.html.erb | 4 +- app/views/comments/index_by_comment.html.erb | 4 +- app/views/comments/index_by_post.html.erb | 4 +- app/views/favorites/index.html.erb | 15 +---- app/views/forum_posts/edit.html.erb | 2 +- app/views/forum_posts/index.html.erb | 48 +++++++-------- app/views/forum_posts/new.html.erb | 2 +- app/views/forum_posts/search.html.erb | 2 +- app/views/forum_topics/_paginator.html.erb | 4 +- app/views/forum_topics/edit.html.erb | 2 +- app/views/forum_topics/new.html.erb | 2 +- app/views/moderator/ip_addrs/index.html.erb | 22 +++++++ app/views/moderator/ip_addrs/search.html.erb | 19 ++++++ .../moderator/post/dashboards/show.html.erb | 1 + app/views/note_versions/index.html.erb | 5 +- app/views/notes/index_by_note.html.erb | 4 +- app/views/notes/index_by_post.html.erb | 4 +- app/views/pool_versions/index.html.erb | 6 +- app/views/pools/index.html.erb | 4 +- app/views/pools/show.html.erb | 6 +- app/views/post_versions/_listing.html.erb | 4 +- app/views/posts/index.html.erb | 21 +------ .../posts/partials/common/_search.html.erb | 16 +++-- .../partials/common/_secondary_links.html.erb | 5 +- .../posts/partials/index/_blacklist.html.erb | 6 ++ .../posts/partials/index/_mode_menu.html.erb | 46 ++++++++------- .../posts/partials/index/_posts.html.erb | 6 +- app/views/posts/show.html.erb | 5 +- app/views/tag_aliases/index.html.erb | 6 +- app/views/tag_implications/index.html.erb | 6 +- app/views/tag_subscriptions/posts.html.erb | 26 ++++++++ app/views/tags/index.html.erb | 44 +++++++------- app/views/uploads/index.html.erb | 4 +- app/views/user_feedbacks/index.html.erb | 4 +- app/views/users/edit.html.erb | 2 + app/views/users/index.html.erb | 4 +- app/views/wiki_page_versions/index.html.erb | 4 +- app/views/wiki_pages/index.html.erb | 4 +- config/routes.rb | 7 ++- db/seeds.rb | 32 ++++++++++ .../moderator/ip_addrs_controller_test.rb | 35 +++++++++++ .../tag_subscriptions_controller_test.rb | 11 ++++ test/unit/ip_ban_test.rb | 2 +- test/unit/moderator/ip_addr_search_test.rb | 35 +++++++++++ 55 files changed, 406 insertions(+), 189 deletions(-) rename app/logical/moderator/{ => dashboard}/report.rb (100%) create mode 100644 app/logical/moderator/ip_addr_search.rb create mode 100644 app/views/moderator/ip_addrs/index.html.erb create mode 100644 app/views/moderator/ip_addrs/search.html.erb create mode 100644 app/views/posts/partials/index/_blacklist.html.erb create mode 100644 app/views/tag_subscriptions/posts.html.erb create mode 100644 test/functional/moderator/ip_addrs_controller_test.rb create mode 100644 test/unit/moderator/ip_addr_search_test.rb diff --git a/app/assets/stylesheets/application.css.scss b/app/assets/stylesheets/application.css.scss index c167df168..6ff47eb82 100644 --- a/app/assets/stylesheets/application.css.scss +++ b/app/assets/stylesheets/application.css.scss @@ -342,6 +342,12 @@ div.clearfix { } +/*** Post previews ***/ +div.post-previews { + overflow: hidden; +} + + /*** Paginator ***/ div.paginator { display: block; diff --git a/app/controllers/favorites_controller.rb b/app/controllers/favorites_controller.rb index 63829f140..193973345 100644 --- a/app/controllers/favorites_controller.rb +++ b/app/controllers/favorites_controller.rb @@ -1,4 +1,6 @@ class FavoritesController < ApplicationController + before_filter :member_only + def index if params[:tags] redirect_to(posts_path(:tags => "fav:#{CurrentUser.name} #{params[:tags]}")) diff --git a/app/controllers/moderator/ip_addrs_controller.rb b/app/controllers/moderator/ip_addrs_controller.rb index ed6c25035..de497b4d0 100644 --- a/app/controllers/moderator/ip_addrs_controller.rb +++ b/app/controllers/moderator/ip_addrs_controller.rb @@ -1,6 +1,7 @@ module Moderator class IpAddrsController < ApplicationController def index + @search = IpAddrSearch.new(params[:search]) end def search diff --git a/app/controllers/tag_subscriptions_controller.rb b/app/controllers/tag_subscriptions_controller.rb index 89e6cefec..374f964a8 100644 --- a/app/controllers/tag_subscriptions_controller.rb +++ b/app/controllers/tag_subscriptions_controller.rb @@ -40,6 +40,12 @@ class TagSubscriptionsController < ApplicationController respond_with(@tag_subscription) end + def posts + @user = User.find(params[:id]) + @post_set = PostSets::Post.new("sub:#{@user.name} #{params[:tags]}", params[:page]) + @posts = @post_set.posts + end + private def check_privilege(tag_subscription) raise User::PrivilegeError unless tag_subscription.editable_by?(CurrentUser.user) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 7c3581e30..7c3425f44 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -73,7 +73,13 @@ protected when "artist_versions" /^\/artist/ - + + when "moderator/post/dashboards" + /^\/post/ + + when "moderator/dashboards" + /^\/moderator/ + else /^\/#{controller}/ end diff --git a/app/helpers/pagination_helper.rb b/app/helpers/pagination_helper.rb index 7646b702f..5e80668f5 100644 --- a/app/helpers/pagination_helper.rb +++ b/app/helpers/pagination_helper.rb @@ -1,6 +1,6 @@ module PaginationHelper def sequential_paginator(records) - html = "" + html = '
' if records.any? if params[:page] =~ /[ab]/ @@ -10,7 +10,7 @@ module PaginationHelper html << '
  • ' + link_to("Next >", params.merge(:page => "b#{records[-1].id}")) + '
  • ' end - html << "
    " + html << "
    " html.html_safe end @@ -23,7 +23,7 @@ module PaginationHelper return sequential_paginator(records) end - html = "" + html = '
    ' window = 3 if records.total_pages <= (window * 2) + 5 1.upto(records.total_pages) do |page| @@ -50,7 +50,7 @@ module PaginationHelper html << numbered_paginator_item("...", records.current_page) html << numbered_paginator_final_item(records.total_pages, records.current_page) end - html << "" + html << "
    " html.html_safe end diff --git a/app/logical/moderator/report.rb b/app/logical/moderator/dashboard/report.rb similarity index 100% rename from app/logical/moderator/report.rb rename to app/logical/moderator/dashboard/report.rb diff --git a/app/logical/moderator/ip_addr_search.rb b/app/logical/moderator/ip_addr_search.rb new file mode 100644 index 000000000..85e572a37 --- /dev/null +++ b/app/logical/moderator/ip_addr_search.rb @@ -0,0 +1,59 @@ +module Moderator + class IpAddrSearch + attr_reader :params + + def initialize(params) + @params = params + end + + def execute + if params[:user_id] + search_by_user_id(params[:user_id].split(/,/)) + elsif params[:user_name] + search_by_user_name(params[:user_name].split(/,/)) + elsif params[:ip_addr] + search_by_ip_addr(params[:ip_addr].split(/,/)) + end + end + + private + def select_all_sql(sql, *params) + ActiveRecord::Base.select_all_sql(sql, *params) + end + + def search_by_ip_addr(ip_addrs) + sums = Hash.new {|h, k| h[k] = 0} + + add_row(sums, "select creator_id as k, count(*) from comments where ip_addr in (?) group by k", ip_addrs) + add_row(sums, "select updater_id as k, count(*) from post_versions where updater_ip_addr in (?) group by k", ip_addrs) + add_row(sums, "select updater_id as k, count(*) from note_versions where updater_ip_addr in (?) group by k", ip_addrs) + add_row(sums, "select updater_id as k, count(*) from pool_versions where updater_ip_addr in (?) group by k", ip_addrs) + add_row(sums, "select updater_id as k, count(*) from wiki_page_versions where updater_ip_addr in (?) group by k", ip_addrs) + + sums + end + + def search_by_user_name(user_names) + users = User.where("name in (?)", user_names) + search_by_user_id(users.map(&:id)) + end + + def search_by_user_id(user_ids) + sums = Hash.new {|h, k| h[k] = 0} + + add_row(sums, "select ip_addr as k, count(*) from comments where creator_id in (?) group by k", user_ids) + add_row(sums, "select updater_ip_addr as k, count(*) from post_versions where updater_id in (?) group by k", user_ids) + add_row(sums, "select updater_ip_addr as k, count(*) from note_versions where updater_id in (?) group by k", user_ids) + add_row(sums, "select updater_ip_addr as k, count(*) from pool_versions where updater_id in (?) group by k", user_ids) + add_row(sums, "select updater_ip_addr as k, count(*) from wiki_page_versions where updater_id in (?) group by k", user_ids) + + sums + end + + def add_row(sums, sql, ip_addrs) + select_all_sql(sql, ip_addrs).each do |row| + sums[row["k"]] += row["count"].to_i + end + end + end +end diff --git a/app/presenters/post_set_presenter.rb b/app/presenters/post_set_presenter.rb index c3bf97814..4e4db8f10 100644 --- a/app/presenters/post_set_presenter.rb +++ b/app/presenters/post_set_presenter.rb @@ -15,7 +15,7 @@ class PostSetPresenter < Presenter end def post_previews_html(template) - html = "" + html = "
    " if posts.empty? return template.render(:partial => "post_sets/blank") @@ -25,6 +25,8 @@ class PostSetPresenter < Presenter html << PostPresenter.preview(post) end + html << '
    ' + html.html_safe end end diff --git a/app/views/artist_versions/index.html.erb b/app/views/artist_versions/index.html.erb index bfbd733e7..f6577aa2d 100644 --- a/app/views/artist_versions/index.html.erb +++ b/app/views/artist_versions/index.html.erb @@ -31,9 +31,7 @@ -
    - <%= sequential_paginator(@artist_versions) %> -
    + <%= sequential_paginator(@artist_versions) %> diff --git a/app/views/artists/edit.html.erb b/app/views/artists/edit.html.erb index d632ead87..c80dce4b4 100644 --- a/app/views/artists/edit.html.erb +++ b/app/views/artists/edit.html.erb @@ -1,6 +1,6 @@
    -

    Edit Artist

    +

    Edit Artist

    <%= render "form" %>
    diff --git a/app/views/artists/index.html.erb b/app/views/artists/index.html.erb index 04db54711..9de28e6bc 100644 --- a/app/views/artists/index.html.erb +++ b/app/views/artists/index.html.erb @@ -34,9 +34,7 @@ -
    - <%= numbered_paginator(@artists) %> -
    + <%= numbered_paginator(@artists) %> <%= render "secondary_links" %> diff --git a/app/views/comments/index_by_comment.html.erb b/app/views/comments/index_by_comment.html.erb index 8033b96c9..1360609df 100644 --- a/app/views/comments/index_by_comment.html.erb +++ b/app/views/comments/index_by_comment.html.erb @@ -8,9 +8,7 @@ -
    - <%= sequential_paginator(@comments) %> -
    + <%= sequential_paginator(@comments) %> diff --git a/app/views/comments/index_by_post.html.erb b/app/views/comments/index_by_post.html.erb index b21a945c4..1d0f24057 100644 --- a/app/views/comments/index_by_post.html.erb +++ b/app/views/comments/index_by_post.html.erb @@ -16,9 +16,7 @@ <% end %> -
    - <%= sequential_paginator(@posts) %> -
    + <%= sequential_paginator(@posts) %> diff --git a/app/views/favorites/index.html.erb b/app/views/favorites/index.html.erb index dff51ddd4..26c97dbcf 100644 --- a/app/views/favorites/index.html.erb +++ b/app/views/favorites/index.html.erb @@ -1,13 +1,7 @@
    diff --git a/app/views/pools/show.html.erb b/app/views/pools/show.html.erb index 60049d9f4..84cf11445 100644 --- a/app/views/pools/show.html.erb +++ b/app/views/pools/show.html.erb @@ -9,11 +9,7 @@
    <%= @post_set.presenter.post_previews_html(self) %> -
    - -
    - <%= numbered_paginator(@post_set) %> -
    + <%= numbered_paginator(@post_set) %>
    diff --git a/app/views/post_versions/_listing.html.erb b/app/views/post_versions/_listing.html.erb index a439326d4..0949a2001 100644 --- a/app/views/post_versions/_listing.html.erb +++ b/app/views/post_versions/_listing.html.erb @@ -39,6 +39,4 @@ -
    - <%= sequential_paginator(post_versions) %> -
    \ No newline at end of file +<%= sequential_paginator(post_versions) %> diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 9ddec2df7..73f24b4fb 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -1,26 +1,11 @@
    -
    - <%= numbered_paginator(@tag_implications) %> -
    - <%= render "secondary_links" %> diff --git a/app/views/tag_subscriptions/posts.html.erb b/app/views/tag_subscriptions/posts.html.erb new file mode 100644 index 000000000..f9bb2133b --- /dev/null +++ b/app/views/tag_subscriptions/posts.html.erb @@ -0,0 +1,26 @@ +
    +
    + + +
    +

    Posts

    + + <%= @post_set.presenter.post_previews_html(self) %> + + <%= numbered_paginator(@posts) %> +
    + + <% content_for(:page_title) do %> + /fav:<%= CurrentUser.name %> + <% end %> + + <%= render :partial => "posts/partials/common/secondary_links" %> +
    +
    + diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erb index aacb7aefa..9ab0e80cc 100644 --- a/app/views/tags/index.html.erb +++ b/app/views/tags/index.html.erb @@ -1,25 +1,27 @@ - - - - - - - - - <% @tags.each do |tag| %> - - - - - <% end %> - -
    CountName
    <%= tag.post_count %> - <%= link_to("?", wiki_pages_path(:title => tag.name)) %> - <%= link_to(tag.name, posts_path(:tags => tag.name)) %> -
    +
    +
    + + + + + + + + + <% @tags.each do |tag| %> + + + + + <% end %> + +
    CountName
    <%= tag.post_count %> + <%= link_to("?", wiki_pages_path(:title => tag.name)) %> + <%= link_to(tag.name, posts_path(:tags => tag.name)) %> +
    -
    - <%= numbered_paginator(@tags) %> + <%= numbered_paginator(@tags) %> +
    <%= render "secondary_links" %> diff --git a/app/views/uploads/index.html.erb b/app/views/uploads/index.html.erb index 014fdff23..4e69ab83e 100644 --- a/app/views/uploads/index.html.erb +++ b/app/views/uploads/index.html.erb @@ -23,9 +23,7 @@ -
    - <%= sequential_paginator(@uploads) %> -
    + <%= sequential_paginator(@uploads) %>
    diff --git a/app/views/user_feedbacks/index.html.erb b/app/views/user_feedbacks/index.html.erb index 2a1ca3570..259474184 100644 --- a/app/views/user_feedbacks/index.html.erb +++ b/app/views/user_feedbacks/index.html.erb @@ -23,9 +23,7 @@ -
    - <%= numbered_paginator(@user_feedbacks) %> -
    + <%= numbered_paginator(@user_feedbacks) %> <%= render "secondary_links" %> diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index fa09e077d..d06887826 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -29,3 +29,5 @@ <% content_for(:page_title) do %> /Users/<%= @user.name %>/Settings <% end %> + +<%= render "secondary_links" %> \ No newline at end of file diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 073afcce1..55062620e 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -48,9 +48,7 @@ -
    - <%= numbered_paginator(@users) %> -
    + <%= numbered_paginator(@users) %> <%= render "secondary_links" %> diff --git a/app/views/wiki_page_versions/index.html.erb b/app/views/wiki_page_versions/index.html.erb index babd341e9..13706c24d 100644 --- a/app/views/wiki_page_versions/index.html.erb +++ b/app/views/wiki_page_versions/index.html.erb @@ -32,9 +32,7 @@ -
    - <%= numbered_paginator(@wiki_page_versions) %> -
    + <%= numbered_paginator(@wiki_page_versions) %> diff --git a/app/views/wiki_pages/index.html.erb b/app/views/wiki_pages/index.html.erb index 2c414892b..5444615bc 100644 --- a/app/views/wiki_pages/index.html.erb +++ b/app/views/wiki_pages/index.html.erb @@ -22,9 +22,7 @@ -
    - <%= numbered_paginator(@wiki_pages) %> -
    + <%= numbered_paginator(@wiki_pages) %> diff --git a/config/routes.rb b/config/routes.rb index dc72b45af..00ece4f46 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,6 +9,7 @@ Danbooru::Application.routes.draw do get :search end end + resource :tag namespace :post do resource :dashboard, :only => [:show] resource :approval, :only => [:create] @@ -107,7 +108,11 @@ Danbooru::Application.routes.draw do end end resources :tag_implications - resources :tag_subscriptions + resources :tag_subscriptions do + member do + get :posts + end + end resources :uploads resources :users resources :user_feedbacks diff --git a/db/seeds.rb b/db/seeds.rb index f80b1eaa9..74146914e 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,6 +5,14 @@ if User.count == 0 :password => "password1", :password_confirmation => "password1" ) + + 0.upto(100) do |i| + User.create( + :name => i.to_s * 5, + :password => i.to_s * 5, + :password_confirmation => i.to_s * 5 + ) + end else puts "Skipping users" user = User.first @@ -93,3 +101,27 @@ if Pool.count == 0 end end end + +if Favorite.count == 0 + puts "Creating favorites" + + Post.order("random()").limit(50).each do |post| + user = User.order("random()").first + post.add_favorite!(user) + post.add_favorite!(CurrentUser.user) + end +else + puts "Skipping favorites" +end + +if TagSubscription.count == 0 + puts "Creating tag subscriptions" + TagSubscription.create(:name => "0", :tag_query => Tag.order("random()").first.name) + 1.upto(50) do |i| + CurrentUser.user = User.order("random()").first + TagSubscription.create(:name => i.to_s, :tag_query => Tag.order("random()").first.name) + end +else + puts "Skipping tag subscriptions" +end + diff --git a/test/functional/moderator/ip_addrs_controller_test.rb b/test/functional/moderator/ip_addrs_controller_test.rb new file mode 100644 index 000000000..e9ea911af --- /dev/null +++ b/test/functional/moderator/ip_addrs_controller_test.rb @@ -0,0 +1,35 @@ +require 'test_helper' + +module Moderator + class IpAddrsControllerTest < ActionController::TestCase + context "The ip addrs controller" do + setup do + @user = Factory.create(:moderator_user) + CurrentUser.user = @user + CurrentUser.ip_addr = "127.0.0.1" + Factory.create(:comment) + MEMCACHE.flush_all + end + + should "find by ip addr" do + get :index, {:search => {:ip_addr => "127.0.0.1"}}, {:user_id => @user.id} + assert_response :success + end + + should "find by user id" do + get :index, {:search => {:user_id => @user.id.to_s}}, {:user_id => @user.id} + assert_response :success + end + + should "find by user name" do + get :index, {:search => {:user_name => @user.name}}, {:user_id => @user.id} + assert_response :success + end + + should "render the search page" do + get :search, {}, {:user_id => @user.id} + assert_response :success + end + end + end +end diff --git a/test/functional/tag_subscriptions_controller_test.rb b/test/functional/tag_subscriptions_controller_test.rb index 1fe3a4dd6..1923098ac 100644 --- a/test/functional/tag_subscriptions_controller_test.rb +++ b/test/functional/tag_subscriptions_controller_test.rb @@ -23,6 +23,17 @@ class TagSubscriptionsControllerTest < ActionController::TestCase assert_response :success end end + + context "posts action" do + setup do + @tag_subscription = Factory.create(:tag_subscription, :name => "aaa") + end + + should "list all visible tag subscriptions" do + get :posts, {:id => @tag_subscription.creator_id} + assert_response :success + end + end context "edit action" do setup do diff --git a/test/unit/ip_ban_test.rb b/test/unit/ip_ban_test.rb index 7ac376db1..195e66350 100644 --- a/test/unit/ip_ban_test.rb +++ b/test/unit/ip_ban_test.rb @@ -1,4 +1,4 @@ -require_relative '../test_helper' +require 'test_helper' class IpBanTest < ActiveSupport::TestCase setup do diff --git a/test/unit/moderator/ip_addr_search_test.rb b/test/unit/moderator/ip_addr_search_test.rb new file mode 100644 index 000000000..3652c8fd0 --- /dev/null +++ b/test/unit/moderator/ip_addr_search_test.rb @@ -0,0 +1,35 @@ +require "test_helper" + +module Moderator + class IpAddrSearchTest < ActiveSupport::TestCase + context "an ip addr search" do + setup do + @user = Factory.create(:user) + CurrentUser.user = @user + CurrentUser.ip_addr = "127.0.0.1" + Factory.create(:comment) + MEMCACHE.flush_all + end + + teardown do + CurrentUser.user = nil + CurrentUser.ip_addr = nil + end + + should "find by ip addr" do + @search = IpAddrSearch.new(:ip_addr => "127.0.0.1") + assert_equal({@user.id.to_s => 2}, @search.execute) + end + + should "find by user id" do + @search = IpAddrSearch.new(:user_id => @user.id.to_s) + assert_equal({"127.0.0.1" => 2}, @search.execute) + end + + should "find by user name" do + @search = IpAddrSearch.new(:user_name => @user.name) + assert_equal({"127.0.0.1" => 2}, @search.execute) + end + end + end +end