bulk revert
This commit is contained in:
@@ -1,24 +1,35 @@
|
|||||||
module Moderator
|
module Moderator
|
||||||
class BulkRevertsController < ApplicationController
|
class BulkRevertsController < ApplicationController
|
||||||
before_filter :moderator_only
|
before_filter :moderator_only
|
||||||
|
before_filter :init_constraints
|
||||||
helper PostVersionsHelper
|
helper PostVersionsHelper
|
||||||
|
rescue_from BulkRevert::ConstraintTooGeneralError, :with => :tag_constraint_too_general
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@constraints = params[:constraints] || {}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@constraints = params[:constraints] || {}
|
|
||||||
@bulk_revert = BulkRevert.new(@constraints)
|
@bulk_revert = BulkRevert.new(@constraints)
|
||||||
|
|
||||||
if params[:commit] == "Test"
|
if params[:commit] == "Test"
|
||||||
@bulk_revert.preview
|
@bulk_revert.preview
|
||||||
render action: "new"
|
render action: "new"
|
||||||
else
|
else
|
||||||
@bulk_revert.process!
|
BulkRevert.delay(:queue => "default").process(@constraints)
|
||||||
flash[:notice] = "Reverts queued"
|
flash[:notice] = "Reverts queued"
|
||||||
redirect_to new_bulk_revert_path
|
redirect_to new_bulk_revert_path
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def init_constraints
|
||||||
|
@constraints = params[:constraints] || {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def tag_constraint_too_general
|
||||||
|
flash[:notice] = "Your tag constraints are too general; try adding min and max version ids"
|
||||||
|
render action: "new"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,17 @@
|
|||||||
class BulkRevert
|
class BulkRevert
|
||||||
|
BIG_QUERY_LIMIT = 5_000
|
||||||
attr_reader :constraints
|
attr_reader :constraints
|
||||||
|
|
||||||
|
class ConstraintTooGeneralError < Exception ; end
|
||||||
|
|
||||||
|
def self.process(constraints)
|
||||||
|
obj = BulkRevert.new(constraints)
|
||||||
|
|
||||||
|
obj.find_post_versions.order("updated_at, id").each do |version|
|
||||||
|
version.undo!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(constraints)
|
def initialize(constraints)
|
||||||
@constraints = constraints
|
@constraints = constraints
|
||||||
end
|
end
|
||||||
@@ -18,14 +29,19 @@ class BulkRevert
|
|||||||
|
|
||||||
if constraints[:user_id]
|
if constraints[:user_id]
|
||||||
q = q.where("post_versions.updater_id = ?", constraints[:user_id])
|
q = q.where("post_versions.updater_id = ?", constraints[:user_id])
|
||||||
|
end
|
||||||
|
|
||||||
if constraints[:added_tags] || constraints[:removed_tags]
|
if constraints[:added_tags] || constraints[:removed_tags]
|
||||||
hash = CityHash.hash64("#{constraints[:added_tags]} #{constraints{removed_tags}}").to_s(36)
|
hash = CityHash.hash64("#{constraints[:added_tags]} #{constraints{removed_tags}} #{constraints[:min_version_id]} #{constraints[:max_version_id]}").to_s(36)
|
||||||
sub_ids = Cache.get("br/fpv/#{hash}", 300) do
|
sub_ids = Cache.get("br/fpv/#{hash}", 300) do
|
||||||
sub_ids = GoogleBigQuery::PostVersion.new.find(constraints[:user_id], constraints[:added_tags], constraints[:removed_tags])
|
GoogleBigQuery::PostVersion.new.find(constraints[:user_id], constraints[:added_tags], constraints[:removed_tags], constraints[:min_version_id], constraints[:max_version_id], BIG_QUERY_LIMIT)
|
||||||
end
|
|
||||||
q = q.where("post_versions.id in (?)", sub_ids)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if sub_ids.size >= BIG_QUERY_LIMIT
|
||||||
|
raise ConstraintTooGeneralError.new
|
||||||
|
end
|
||||||
|
|
||||||
|
q = q.where("post_versions.id in (?)", sub_ids)
|
||||||
end
|
end
|
||||||
|
|
||||||
if constraints[:min_version_id].present?
|
if constraints[:min_version_id].present?
|
||||||
|
|||||||
@@ -3,34 +3,50 @@ module GoogleBigQuery
|
|||||||
def find_removed(tag, limit = 1_000)
|
def find_removed(tag, limit = 1_000)
|
||||||
tag = escape(tag)
|
tag = escape(tag)
|
||||||
limit = limit.to_i
|
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(removed_tags, \"(?:^| )#{tag}(?:$| )\") order by updated_at desc limit #{limit}")
|
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 #{remove_tag_condition(tag)} order by updated_at desc limit #{limit}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_added(tag, limit = 1_000)
|
def find_added(tag, limit = 1_000)
|
||||||
tag = escape(tag)
|
tag = escape(tag)
|
||||||
limit = limit.to_i
|
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}")
|
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 #{add_tag_condition(tag)} order by updated_at desc limit #{limit}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def find(user_id, added_tags, removed_tags, limit = 1_000)
|
def add_tag_condition(t)
|
||||||
|
es = escape(t)
|
||||||
|
"regexp_match(added_tags, \"(?:^| )#{es}(?:$| )\")"
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_tag_condition(t)
|
||||||
|
es = escape(t)
|
||||||
|
"regexp_match(removed_tags, \"(?:^| )#{es}(?:$| )\")"
|
||||||
|
end
|
||||||
|
|
||||||
|
def find(user_id, added_tags, removed_tags, min_version_id, max_version_id, limit = 1_000)
|
||||||
constraints = []
|
constraints = []
|
||||||
|
|
||||||
constraints << "updater_id = #{user_id.to_i}"
|
constraints << "updater_id = #{user_id.to_i}"
|
||||||
|
|
||||||
if added_tags
|
if added_tags
|
||||||
added_tags.scan(/\S+/).each do |tag|
|
added_tags.scan(/\S+/).each do |tag|
|
||||||
escaped = escape(tag)
|
constraints << add_tag_condition(tag)
|
||||||
constraints << "regexp_match(added_tags, \"(?:^| )#{escaped}(?:$| )\")"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if removed_tags
|
if removed_tags
|
||||||
removed_tags.scan(/\S+/).each do |tag|
|
removed_tags.scan(/\S+/).each do |tag|
|
||||||
escaped = escape(tag)
|
constraints << remove_tag_condition(tag)
|
||||||
constraints << "not regexp_match(added_tags, \"(?:^| )#{escaped}(?:$| )\")"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if min_version_id
|
||||||
|
constraints << "id >= #{min_version_id.to_i}"
|
||||||
|
end
|
||||||
|
|
||||||
|
if max_version_id
|
||||||
|
constraints << "id <= #{max_version_id.to_i}"
|
||||||
|
end
|
||||||
|
|
||||||
limit = limit.to_i
|
limit = limit.to_i
|
||||||
sql = "select id from [#{data_set}.post_versions] where " + constraints.join(" and ") + " order by updated_at desc limit #{limit}"
|
sql = "select id from [#{data_set}.post_versions] where " + constraints.join(" and ") + " order by updated_at desc limit #{limit}"
|
||||||
result = query(sql)
|
result = query(sql)
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
<% if CurrentUser.user.blacklisted_tags.present? %>
|
<% if CurrentUser.user.blacklisted_tags.present? %>
|
||||||
<meta name="blacklisted-tags" content="<%= CurrentUser.user.blacklisted_tags.gsub(/(?:\r|\n)+/, ",") %>">
|
<meta name="blacklisted-tags" content="<%= CurrentUser.user.blacklisted_tags.gsub(/(?:\r|\n)+/, ",") %>">
|
||||||
<% end %>
|
<% end %>
|
||||||
<% if flash[:notice] =~ /error/ %>
|
<% if flash[:notice] =~ /error/i %>
|
||||||
<meta name="errors" content="true">
|
<meta name="errors" content="true">
|
||||||
<% end %>
|
<% end %>
|
||||||
<meta name="enable-js-navigation" content="<%= CurrentUser.user.enable_post_navigation %>">
|
<meta name="enable-js-navigation" content="<%= CurrentUser.user.enable_post_navigation %>">
|
||||||
|
|||||||
@@ -2,9 +2,11 @@
|
|||||||
<div id="a-new">
|
<div id="a-new">
|
||||||
<h1>New Bulk Revert</h1>
|
<h1>New Bulk Revert</h1>
|
||||||
|
|
||||||
|
<p>You must preview a bulk revert before processing it. Previews may take several minutes to generate.</p>
|
||||||
|
|
||||||
<%= form_tag(moderator_bulk_revert_path, :class => "simple_form") do %>
|
<%= form_tag(moderator_bulk_revert_path, :class => "simple_form") do %>
|
||||||
<div class="input">
|
<div class="input">
|
||||||
<label>Updater</label>
|
<label>User Name</label>
|
||||||
<%= text_field :constraints, :user_name, :value => @constraints[:user_name] %>
|
<%= text_field :constraints, :user_name, :value => @constraints[:user_name] %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -29,7 +31,10 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<%= submit_tag "Test" %>
|
<%= submit_tag "Test" %>
|
||||||
<%#= submit_tag %>
|
|
||||||
|
<% if params[:commit] == "Test" %>
|
||||||
|
<%#= submit_tag %>
|
||||||
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% if @bulk_revert %>
|
<% if @bulk_revert %>
|
||||||
|
|||||||
Reference in New Issue
Block a user