fixes #3824: render tag requests dynamically in the forum post

refactoring
This commit is contained in:
Albert Yi
2019-01-02 17:27:01 -08:00
parent 5c54c61d65
commit 8b0af19f7f
18 changed files with 171 additions and 25 deletions

View File

@@ -15,9 +15,7 @@ class BulkUpdateRequestsController < ApplicationController
end
def show
respond_with(@bulk_update_request) do |fmt|
fmt.html { redirect_to bulk_update_requests_path(search: { id: @bulk_update_request.id }) }
end
@bulk_update_request = BulkUpdateRequest.find(params[:id])
end
def edit

View File

@@ -6,4 +6,49 @@ module ForumTopicsHelper
def available_min_user_levels
ForumTopic::MIN_LEVELS.select { |name, level| level <= CurrentUser.level }.to_a
end
def tag_request_message(obj)
if obj.is_a?(TagRelationship)
if obj.is_active?
return "The #{obj.relationship} ##{obj.id} [[#{obj.antecedent_name}]] -> [[#{obj.consequent_name}]] is active."
elsif obj.is_retired?
return "The #{obj.relationship} ##{obj.id} [[#{obj.antecedent_name}]] -> [[#{obj.consequent_name}]] has been retired"
elsif obj.is_pending?
return "The #{obj.relationship} ##{obj.id} [[#{obj.antecedent_name}]] -> [[#{obj.consequent_name}]] is pending approval."
elsif obj.is_errored?
return "The #{obj.relationship} ##{obj.id} [[#{obj.antecedent_name}]] -> [[#{obj.consequent_name}]] (#{relationship} failed during processing."
end
end
if obj.is_a?(BulkUpdateRequest)
if obj.is_approved?
return "The bulk update request ##{obj.id} is active.\n\n[expand]#{obj.script_with_links}[/expand]"
elsif obj.is_pending?
return "The \"bulk update request ##{obj.id}\":/bulk_update_requests/#{obj.id} is pending approval.\n\n[expand]#{obj.script_with_links}[/expand]"
elsif obj.is_rejected?
return "The bulk update request ##{obj.id} has been rejected.\n\n[expand]#{obj.script_with_links}[/expand]"
end
end
end
def parse_embedded_tag_request_text(text)
# for dev mode, prime these
if Rails.env.development?
[TagAlias, TagImplication]
end
[*TagRelationship.subclasses, BulkUpdateRequest].each do |tag_request|
text.gsub!(tag_request.embedded_pattern) do |match|
begin
obj = tag_request.find($~[:id])
tag_request_message(obj) || match
rescue ActiveRecord::RecordNotFound
match
end
end
end
text
end
end

View File

@@ -5,6 +5,7 @@ class ForumUpdater
@forum_topic = forum_topic
@forum_post = options[:forum_post]
@expected_title = options[:expected_title]
@skip_update = options[:skip_update]
end
def update(message, title_tag = nil)
@@ -31,6 +32,7 @@ class ForumUpdater
end
def update_post(body)
return if @skip_update
forum_post.update(body: "#{forum_post.body}\n\nEDIT: #{body}", skip_mention_notifications: true)
end
end

View File

@@ -10,7 +10,11 @@ class TagAliasRequest
"Tag alias: #{antecedent_name} -> #{consequent_name}"
end
def self.command_string(antecedent_name, consequent_name)
def self.command_string(antecedent_name, consequent_name, id=nil)
if id
return "[ta:#{id}]"
end
"create alias [[#{antecedent_name}]] -> [[#{consequent_name}]]"
end
@@ -51,7 +55,7 @@ class TagAliasRequest
ForumTopic.new(
:title => TagAliasRequest.topic_title(antecedent_name, consequent_name),
:original_post_attributes => {
:body => TagAliasRequest.command_string(antecedent_name, consequent_name) + "\n\n\"Link to alias\":/tag_aliases?search[id]=#{tag_alias_id}\n\n#{reason}"
:body => TagAliasRequest.command_string(antecedent_name, consequent_name, tag_alias_id) + "\n\nReason: #{reason}"
},
:category_id => 1
)

View File

@@ -10,7 +10,11 @@ class TagImplicationRequest
"Tag implication: #{antecedent_name} -> #{consequent_name}"
end
def self.command_string(antecedent_name, consequent_name)
def self.command_string(antecedent_name, consequent_name, id=nil)
if id
return "[ti:#{id}]"
end
"create implication [[#{antecedent_name}]] -> [[#{consequent_name}]]"
end
@@ -51,7 +55,7 @@ class TagImplicationRequest
ForumTopic.new(
:title => TagImplicationRequest.topic_title(antecedent_name, consequent_name),
:original_post_attributes => {
:body => TagImplicationRequest.command_string(antecedent_name, consequent_name) + "\n\n\"Link to implication\":/tag_implications?search[id]=#{tag_implication_id}\n\n#{reason}"
:body => TagImplicationRequest.command_string(antecedent_name, consequent_name, tag_implication_id) + "\n\nReason: #{reason}"
},
:category_id => 1
)

View File

@@ -90,7 +90,8 @@ class BulkUpdateRequest < ApplicationRecord
ForumUpdater.new(
forum_topic,
forum_post: post,
expected_title: title
expected_title: title,
skip_update: !TagRelationship::SUPPORT_HARD_CODED
)
end
end
@@ -168,12 +169,20 @@ class BulkUpdateRequest < ApplicationRecord
include ApprovalMethods
include ValidationMethods
concerning :EmbeddedText do
class_methods do
def embedded_pattern
/\[bur:(?<id>\d+)\]/m
end
end
end
def editable?(user)
user_id == user.id || user.is_builder?
end
def reason_with_link
"#{script_with_links}\n\n\"Link to request\":/bulk_update_requests?search[id]=#{id}\n\n#{reason}"
"[bur:#{id}]\n\nReason: #{reason}"
end
def script_with_links

View File

@@ -36,14 +36,15 @@ class TagAlias < TagRelationship
def forum_updater
@forum_updater ||= begin
post = if forum_topic
forum_post || forum_topic.posts.where("body like ?", TagAliasRequest.command_string(antecedent_name, consequent_name) + "%").last
forum_post || forum_topic.posts.where("body like ?", TagAliasRequest.command_string(antecedent_name, consequent_name, id) + "%").last
else
nil
end
ForumUpdater.new(
forum_topic,
forum_post: post,
expected_title: TagAliasRequest.topic_title(antecedent_name, consequent_name)
expected_title: TagAliasRequest.topic_title(antecedent_name, consequent_name),
skip_update: !TagRelationship::SUPPORT_HARD_CODED
)
end
end
@@ -53,6 +54,14 @@ class TagAlias < TagRelationship
include ApprovalMethods
include ForumMethods
concerning :EmbeddedText do
class_methods do
def embedded_pattern
/\[ta:(?<id>\d+)\]/m
end
end
end
def self.to_aliased(names)
Cache.get_multi(Array(names), "ta") do |tag|
ActiveRecord::Base.select_value_sql("select consequent_name from tag_aliases where status in ('active', 'processing') and antecedent_name = ?", tag) || tag.to_s

View File

@@ -196,14 +196,15 @@ class TagImplication < TagRelationship
def forum_updater
post = if forum_topic
forum_post || forum_topic.posts.where("body like ?", TagImplicationRequest.command_string(antecedent_name, consequent_name) + "%").last
forum_post || forum_topic.posts.where("body like ?", TagImplicationRequest.command_string(antecedent_name, consequent_name, id) + "%").last
else
nil
end
ForumUpdater.new(
forum_topic,
forum_post: post,
expected_title: TagImplicationRequest.topic_title(antecedent_name, consequent_name)
expected_title: TagImplicationRequest.topic_title(antecedent_name, consequent_name),
skip_update: !TagRelationship::SUPPORT_HARD_CODED
)
end
memoize :forum_updater
@@ -214,6 +215,14 @@ class TagImplication < TagRelationship
include ValidationMethods
include ApprovalMethods
concerning :EmbeddedText do
class_methods do
def embedded_pattern
/\[ti:(?<id>\d+)\]/m
end
end
end
def reload(options = {})
flush_cache
super

View File

@@ -1,6 +1,7 @@
class TagRelationship < ApplicationRecord
self.abstract_class = true
SUPPORT_HARD_CODED = true
EXPIRY = 60
EXPIRY_WARNING = 55
@@ -54,6 +55,10 @@ class TagRelationship < ApplicationRecord
status == "active"
end
def is_errored?
status =~ /\Aerror:/
end
def deletable_by?(user)
return true if user.is_admin?
return true if is_pending? && user.is_builder?
@@ -173,6 +178,14 @@ class TagRelationship < ApplicationRecord
end
end
concerning :EmbeddedText do
class_methods do
def embedded_pattern
raise NotImplementedError
end
end
end
def antecedent_and_consequent_are_different
if antecedent_name == consequent_name
errors[:base] << "Cannot alias or implicate a tag to itself"

View File

@@ -7,16 +7,16 @@
<div class="input">
<label class="text optional" for="bulk_update_request_script">Script</label>
<pre class="hint">
Use the following format:
unalias aaa -> bbb
unimply aaa -> bbb
alias aaa -> bbb
imply aaa -> bbb
update aaa -> bbb
category tag_name -> category_name
</pre>
<%= text_area :bulk_update_request, :script, :size => "50x10" %>
<p><a href="#" id="script-help-link">Help</a></p>
<ul class="hint" id="script-help" title="Bulk Update Request Help">
<li>unalias aaa -> bbb</li>
<li>unimply aaa -> bbb</li>
<li>alias aaa -> bbb</li>
<li>imply aaa -> bbb</li>
<li>update aaa -> bbb</li>
<li>category tag_name -> category_name</li>
</ul>
</div>
<% if @bulk_update_request.new_record? %>
@@ -45,3 +45,15 @@ category tag_name -> category_name
<%= f.button :submit, :value => "Submit" %>
<%= dtext_preview_button "bulk_update_request", "reason" %>
<% end %>
<%= content_for(:html_header) do %>
<script>
$(function() {
$("#script-help").hide();
$("#script-help-link").click(function() {
$("#script-help").dialog({width: "30em"});
return false;
});
});
</script>
<% end %>

View File

@@ -24,6 +24,7 @@
<td><%= script_with_line_breaks(request.script) %></td>
<td id="request-status-for-<%= request.id %>"><%= request.status %></td>
<td>
<%= link_to "Show", bulk_update_request_path(request) %> |
<% if CurrentUser.is_admin? && request.status == "pending" %>
<%= link_to "Approve", approve_bulk_update_request_path(request), :remote => true, :method => :post %> |
<% end %>

View File

@@ -0,0 +1,29 @@
<div id="c-bulk-update-requests">
<div id="a-show">
<h1>Bulk Update Request: <%= @bulk_update_request.title %></h1>
<ul>
<% if @bulk_update_request.forum_topic_id %>
<li><strong>Reference</strong> <%= link_to "topic ##{@bulk_update_request.forum_topic_id}", forum_topic_path(@bulk_update_request.forum_topic_id) %></li>
<% end %>
<li><strong>Creator</strong> <%= link_to_user @bulk_update_request.user %></li>
<li><strong>Date</strong> <%= @bulk_update_request.created_at %></li>
<li><strong>Status</strong>: <%= @bulk_update_request.status %></li>
<% if CurrentUser.is_admin? && @bulk_update_request.is_pending? %>
<li><strong>Commands</strong> <%= link_to "Approve", approve_bulk_update_request_path(@bulk_update_request), :method => :post %></li>
<% end %>
</ul>
<div style="margin: 1em 0;">
<h2>Script</h2>
<pre><%= format_text @bulk_update_request.script_with_links %></pre>
</div>
</div>
</div>
<%= render "secondary_links" %>
<% content_for(:page_title) do %>
Bulk Update Request - <%= Danbooru.config.app_name %>
<% end %>

View File

@@ -15,7 +15,7 @@
</div>
<div class="content">
<div class="prose">
<%= format_text(forum_post.body) %>
<%= format_text(parse_embedded_tag_request_text(forum_post.body)) %>
</div>
<%= render "update_notice", record: forum_post %>
<menu>

View File

@@ -7,6 +7,7 @@
<%= subnav_link_to "New", new_forum_topic_path %>
<%= subnav_link_to "Request alias", new_tag_alias_request_path %>
<%= subnav_link_to "Request implication", new_tag_implication_request_path %>
<%= subnav_link_to "Request BUR", new_bulk_update_request_path %>
<%= subnav_link_to "Mark all as read", mark_all_as_read_forum_topics_path, :method => :post, :"data-shortcut" => "shift+r" %>
<% end %>

View File

@@ -13,6 +13,11 @@
<% if @tag_alias.respond_to?(:reason) && @tag_alias.reason.present? %>
<li><strong>Reason</strong> <%= format_text @tag_alias.reason %></li>
<% end %>
<li><strong>Status</strong>: <%= @tag_alias.status %></li>
<% if CurrentUser.is_admin? && @tag_alias.is_pending? %>
<li><strong>Commands</strong> <%= link_to "Approve", approve_tag_alias_path(@tag_alias), :method => :post %></li>
<% end %>
</ul>
</div>
</div>

View File

@@ -13,6 +13,11 @@
<% if @tag_implication.respond_to?(:reason) && @tag_implication.reason.present? %>
<li><strong>Reason</strong> <%= format_text @tag_implication.reason %></li>
<% end %>
<li><strong>Status</strong>: <%= @tag_implication.status %></li>
<% if CurrentUser.is_admin? && @tag_implication.is_pending? %>
<li><strong>Commands</strong> <%= link_to "Approve", approve_tag_implication_path(@tag_implication), :method => :post %></li>
<% end %>
</ul>
</div>
</div>

View File

@@ -178,7 +178,7 @@ class TagAliasTest < ActiveSupport::TestCase
CurrentUser.scoped(@admin) do
@topic = FactoryBot.create(:forum_topic, :title => TagAliasRequest.topic_title("aaa", "bbb"))
@post = FactoryBot.create(:forum_post, :topic_id => @topic.id, :body => TagAliasRequest.command_string("aaa", "bbb"))
@alias = FactoryBot.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb", :forum_topic => @topic, :status => "pending")
@alias = FactoryBot.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb", :forum_topic => @topic, :forum_post => @post, :status => "pending")
end
end

View File

@@ -232,7 +232,7 @@ class TagImplicationTest < ActiveSupport::TestCase
@admin = FactoryBot.create(:admin_user)
@topic = FactoryBot.create(:forum_topic, :title => TagImplicationRequest.topic_title("aaa", "bbb"))
@post = FactoryBot.create(:forum_post, topic_id: @topic.id, :body => TagImplicationRequest.command_string("aaa", "bbb"))
@implication = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb", :forum_topic => @topic, :status => "pending")
@implication = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb", :forum_topic => @topic, :forum_post => @post, :status => "pending")
end
should "update the topic when processed" do