From 4e48e80e1f3041390b4c6036a1700d2ca04cad54 Mon Sep 17 00:00:00 2001 From: Albert Yi Date: Tue, 1 Nov 2016 16:47:46 -0700 Subject: [PATCH] stub in preview for bulk revert --- .../moderator/bulk_reverts_controller.rb | 24 +++++ app/logical/bulk_revert.rb | 41 +++++++++ app/logical/google_big_query/post_version.rb | 30 ++++++ app/models/post_approval.rb | 16 ++-- app/views/moderator/bulk_reverts/new.html.erb | 92 +++++++++++++++++++ config/routes.rb | 1 + 6 files changed, 196 insertions(+), 8 deletions(-) create mode 100644 app/controllers/moderator/bulk_reverts_controller.rb create mode 100644 app/logical/bulk_revert.rb create mode 100644 app/views/moderator/bulk_reverts/new.html.erb diff --git a/app/controllers/moderator/bulk_reverts_controller.rb b/app/controllers/moderator/bulk_reverts_controller.rb new file mode 100644 index 000000000..3d708f5a9 --- /dev/null +++ b/app/controllers/moderator/bulk_reverts_controller.rb @@ -0,0 +1,24 @@ +module Moderator + class BulkRevertsController < ApplicationController + before_filter :moderator_only + helper PostVersionsHelper + + def new + @constraints = params[:constraints] || {} + end + + def create + @constraints = params[:constraints] || {} + @bulk_revert = BulkRevert.new(@constraints) + + if params[:commit] == "Test" + @bulk_revert.preview + render action: "new" + else + @bulk_revert.process! + flash[:notice] = "Reverts queued" + redirect_to new_bulk_revert_path + end + end + end +end diff --git a/app/logical/bulk_revert.rb b/app/logical/bulk_revert.rb new file mode 100644 index 000000000..148569b09 --- /dev/null +++ b/app/logical/bulk_revert.rb @@ -0,0 +1,41 @@ +class BulkRevert + attr_reader :constraints + + def initialize(constraints) + @constraints = constraints + end + + def preview + @_preview ||= find_post_versions + end + + def find_post_versions + q = PostVersion.where("true") + + if constraints[:user_name] + constraints[:user_id] = User.find_by_name(constraints[:user_name]).try(:id) + end + + if constraints[:user_id] + q = q.where("post_versions.updater_id = ?", constraints[:user_id]) + + if constraints[:added_tags] || constraints[:removed_tags] + hash = CityHash.hash64("#{constraints[:added_tags]} #{constraints{removed_tags}}").to_s(36) + sub_ids = Cache.get("br/fpv/#{hash}", 300) do + sub_ids = GoogleBigQuery::PostVersion.new.find(constraints[:user_id], constraints[:added_tags], constraints[:removed_tags]) + end + q = q.where("post_versions.id in (?)", sub_ids) + end + end + + if constraints[:min_version_id].present? + q = q.where("post_versions.id >= ?", constraints[:min_version_id]) + end + + if constraints[:max_version_id].present? + q = q.where("post_versions.id <= ?", constraints[:max_version_id]) + end + + q + end +end diff --git a/app/logical/google_big_query/post_version.rb b/app/logical/google_big_query/post_version.rb index b3d675741..be0424f55 100644 --- a/app/logical/google_big_query/post_version.rb +++ b/app/logical/google_big_query/post_version.rb @@ -11,5 +11,35 @@ module GoogleBigQuery limit = limit.to_i query("select id, post_id, updated_at, updater_id, updater_ip_addr, tags, added_tags, removed_tags, parent_id, rating, source from [#{data_set}.post_versions] where regexp_match(added_tags, \"(?:^| )#{tag}(?:$| )\") order by updated_at desc limit #{limit}") end + + def find(user_id, added_tags, removed_tags, limit = 1_000) + constraints = [] + + constraints << "updater_id = #{user_id.to_i}" + + if added_tags + added_tags.scan(/\S+/).each do |tag| + escaped = escape(tag) + constraints << "regexp_match(added_tags, \"(?:^| )#{escaped}(?:$| )\")" + end + end + + if removed_tags + removed_tags.scan(/\S+/).each do |tag| + escaped = escape(tag) + constraints << "not regexp_match(added_tags, \"(?:^| )#{escaped}(?:$| )\")" + end + end + + limit = limit.to_i + sql = "select id from [#{data_set}.post_versions] where " + constraints.join(" and ") + " order by updated_at desc limit #{limit}" + result = query(sql) + + if result["rows"] + result["rows"].map {|x| x["f"][0]["v"].to_i} + else + [] + end + end end end diff --git a/app/models/post_approval.rb b/app/models/post_approval.rb index 395401011..7fbfe741f 100644 --- a/app/models/post_approval.rb +++ b/app/models/post_approval.rb @@ -1,12 +1,12 @@ class PostApproval < ActiveRecord::Base - belongs_to :user - belongs_to :post + belongs_to :user + belongs_to :post - def self.prune! - where("created_at < ?", 1.month.ago).delete_all - end + def self.prune! + where("created_at < ?", 1.month.ago).delete_all + end - def self.approved?(user_id, post_id) - where(user_id: user_id, post_id: post_id).exists? - end + def self.approved?(user_id, post_id) + where(user_id: user_id, post_id: post_id).exists? + end end diff --git a/app/views/moderator/bulk_reverts/new.html.erb b/app/views/moderator/bulk_reverts/new.html.erb new file mode 100644 index 000000000..af8caf1c6 --- /dev/null +++ b/app/views/moderator/bulk_reverts/new.html.erb @@ -0,0 +1,92 @@ +
+
+

New Bulk Revert

+ + <%= form_tag(moderator_bulk_revert_path, :class => "simple_form") do %> +
+ + <%= text_field :constraints, :user_name, :value => @constraints[:user_name] %> +
+ +
+ + <%= text_field :constraints, :min_version_id, :value => @constraints[:min_version_id] %> +
+ +
+ + <%= text_field :constraints, :max_version_id, :value => @constraints[:max_version_id] %> +
+ +
+ + <%= text_field :constraints, :added_tags, :value => @constraints[:added_tags] %> +
+ +
+ + <%= text_field :constraints, :removed_tags, :value => @constraints[:removed_tags] %> +
+ + <%= submit_tag "Test" %> + <%#= submit_tag %> + <% end %> + + <% if @bulk_revert %> +
+

Preview limited to 200 changes

+ + + + + + + + + + + + + <% @bulk_revert.preview.limit(200).each do |post_version| %> + + + + + + + <% end %> + +
PostDateUserChanges
<%= link_to("#{post_version.post_id}.#{post_version.id}", post_path(post_version.post_id)) %><%= compact_time(post_version.updated_at) %> + <% if post_version.updater %> + <%= link_to_user(post_version.updater) %> + <% end %> + <%= post_version_diff(post_version) %>
+
+ <% end %> +
+
+ +<% content_for(:page_title) do %> + New Bulk Revert - <%= Danbooru.config.app_name %> +<% end %> + +<% content_for(:html_header) do %> + <%= javascript_tag do %> + $(function() { + if ($("#constraints_user_name").val().length === 0) { + $("#added-tags-input").hide(); + $("#removed-tags-input").hide(); + } + + $("#constraints_user_name").keyup(function() { + if ($("#constraints_user_name").val().length > 0) { + $("#added-tags-input").show(); + $("#removed-tags-input").show(); + } else { + $("#added-tags-input").hide(); + $("#removed-tags-input").hide(); + } + }); + }); + <% end %> +<% end %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index ffdcc6d9e..b69f2cd18 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,7 @@ Rails.application.routes.draw do resources :posts, :only => [:index, :show] end namespace :moderator do + resource :bulk_revert, :only => [:new, :create] resource :dashboard, :only => [:show] resources :ip_addrs, :only => [:index] do collection do