From 8b0af19f7fb1c13ffd4f6ffd940ac4f53188789c Mon Sep 17 00:00:00 2001 From: Albert Yi Date: Wed, 2 Jan 2019 17:27:01 -0800 Subject: [PATCH] fixes #3824: render tag requests dynamically in the forum post refactoring --- .../bulk_update_requests_controller.rb | 4 +- app/helpers/forum_topics_helper.rb | 45 +++++++++++++++++++ app/logical/forum_updater.rb | 2 + app/logical/tag_alias_request.rb | 8 +++- app/logical/tag_implication_request.rb | 8 +++- app/models/bulk_update_request.rb | 13 +++++- app/models/tag_alias.rb | 13 +++++- app/models/tag_implication.rb | 13 +++++- app/models/tag_relationship.rb | 13 ++++++ app/views/bulk_update_requests/_form.html.erb | 30 +++++++++---- .../bulk_update_requests/_listing.html.erb | 1 + app/views/bulk_update_requests/show.html.erb | 29 ++++++++++++ app/views/forum_posts/_forum_post.html.erb | 2 +- .../forum_topics/_secondary_links.html.erb | 1 + app/views/tag_aliases/show.html.erb | 5 +++ app/views/tag_implications/show.html.erb | 5 +++ test/unit/tag_alias_test.rb | 2 +- test/unit/tag_implication_test.rb | 2 +- 18 files changed, 171 insertions(+), 25 deletions(-) create mode 100644 app/views/bulk_update_requests/show.html.erb diff --git a/app/controllers/bulk_update_requests_controller.rb b/app/controllers/bulk_update_requests_controller.rb index 59793447d..a89cc4113 100644 --- a/app/controllers/bulk_update_requests_controller.rb +++ b/app/controllers/bulk_update_requests_controller.rb @@ -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 diff --git a/app/helpers/forum_topics_helper.rb b/app/helpers/forum_topics_helper.rb index 98b9ad8ed..b56ebb9ee 100644 --- a/app/helpers/forum_topics_helper.rb +++ b/app/helpers/forum_topics_helper.rb @@ -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 diff --git a/app/logical/forum_updater.rb b/app/logical/forum_updater.rb index 6ac8c8f35..c2b81441f 100644 --- a/app/logical/forum_updater.rb +++ b/app/logical/forum_updater.rb @@ -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 diff --git a/app/logical/tag_alias_request.rb b/app/logical/tag_alias_request.rb index 3b369cea2..decb13d41 100644 --- a/app/logical/tag_alias_request.rb +++ b/app/logical/tag_alias_request.rb @@ -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 ) diff --git a/app/logical/tag_implication_request.rb b/app/logical/tag_implication_request.rb index ddfd984af..fb4b0ecc1 100644 --- a/app/logical/tag_implication_request.rb +++ b/app/logical/tag_implication_request.rb @@ -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 ) diff --git a/app/models/bulk_update_request.rb b/app/models/bulk_update_request.rb index 974f6f0b4..81af0ff5e 100644 --- a/app/models/bulk_update_request.rb +++ b/app/models/bulk_update_request.rb @@ -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:(?\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 diff --git a/app/models/tag_alias.rb b/app/models/tag_alias.rb index 7db5d7fa2..ae15295a3 100644 --- a/app/models/tag_alias.rb +++ b/app/models/tag_alias.rb @@ -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:(?\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 diff --git a/app/models/tag_implication.rb b/app/models/tag_implication.rb index 2f0d1a50b..1a79eeeb2 100644 --- a/app/models/tag_implication.rb +++ b/app/models/tag_implication.rb @@ -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:(?\d+)\]/m + end + end + end + def reload(options = {}) flush_cache super diff --git a/app/models/tag_relationship.rb b/app/models/tag_relationship.rb index 3bdefd263..233807662 100644 --- a/app/models/tag_relationship.rb +++ b/app/models/tag_relationship.rb @@ -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" diff --git a/app/views/bulk_update_requests/_form.html.erb b/app/views/bulk_update_requests/_form.html.erb index d7c8dae71..13549b93a 100644 --- a/app/views/bulk_update_requests/_form.html.erb +++ b/app/views/bulk_update_requests/_form.html.erb @@ -7,16 +7,16 @@
-
-Use the following format:
-unalias aaa -> bbb
-unimply aaa -> bbb
-alias aaa -> bbb
-imply aaa -> bbb
-update aaa -> bbb
-category tag_name -> category_name
-
<%= text_area :bulk_update_request, :script, :size => "50x10" %> +

Help

+
    +
  • unalias aaa -> bbb
  • +
  • unimply aaa -> bbb
  • +
  • alias aaa -> bbb
  • +
  • imply aaa -> bbb
  • +
  • update aaa -> bbb
  • +
  • category tag_name -> category_name
  • +
<% 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 %> + +<% end %> \ No newline at end of file diff --git a/app/views/bulk_update_requests/_listing.html.erb b/app/views/bulk_update_requests/_listing.html.erb index 7119a27b7..262b973f9 100644 --- a/app/views/bulk_update_requests/_listing.html.erb +++ b/app/views/bulk_update_requests/_listing.html.erb @@ -24,6 +24,7 @@ <%= script_with_line_breaks(request.script) %> <%= request.status %> + <%= 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 %> diff --git a/app/views/bulk_update_requests/show.html.erb b/app/views/bulk_update_requests/show.html.erb new file mode 100644 index 000000000..7d4ff758c --- /dev/null +++ b/app/views/bulk_update_requests/show.html.erb @@ -0,0 +1,29 @@ +
+
+

Bulk Update Request: <%= @bulk_update_request.title %>

+ +
    + <% if @bulk_update_request.forum_topic_id %> +
  • Reference <%= link_to "topic ##{@bulk_update_request.forum_topic_id}", forum_topic_path(@bulk_update_request.forum_topic_id) %>
  • + <% end %> +
  • Creator <%= link_to_user @bulk_update_request.user %>
  • +
  • Date <%= @bulk_update_request.created_at %>
  • +
  • Status: <%= @bulk_update_request.status %>
  • + + <% if CurrentUser.is_admin? && @bulk_update_request.is_pending? %> +
  • Commands <%= link_to "Approve", approve_bulk_update_request_path(@bulk_update_request), :method => :post %>
  • + <% end %> +
+ +
+

Script

+
<%= format_text @bulk_update_request.script_with_links %>
+
+
+
+ +<%= render "secondary_links" %> + +<% content_for(:page_title) do %> + Bulk Update Request - <%= Danbooru.config.app_name %> +<% end %> \ No newline at end of file diff --git a/app/views/forum_posts/_forum_post.html.erb b/app/views/forum_posts/_forum_post.html.erb index caf34f263..61aef07d9 100644 --- a/app/views/forum_posts/_forum_post.html.erb +++ b/app/views/forum_posts/_forum_post.html.erb @@ -15,7 +15,7 @@
- <%= format_text(forum_post.body) %> + <%= format_text(parse_embedded_tag_request_text(forum_post.body)) %>
<%= render "update_notice", record: forum_post %> diff --git a/app/views/forum_topics/_secondary_links.html.erb b/app/views/forum_topics/_secondary_links.html.erb index de0f0399f..59d2b66b9 100644 --- a/app/views/forum_topics/_secondary_links.html.erb +++ b/app/views/forum_topics/_secondary_links.html.erb @@ -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 %> diff --git a/app/views/tag_aliases/show.html.erb b/app/views/tag_aliases/show.html.erb index 0a7568e79..8f30f5586 100644 --- a/app/views/tag_aliases/show.html.erb +++ b/app/views/tag_aliases/show.html.erb @@ -13,6 +13,11 @@ <% if @tag_alias.respond_to?(:reason) && @tag_alias.reason.present? %>
  • Reason <%= format_text @tag_alias.reason %>
  • <% end %> +
  • Status: <%= @tag_alias.status %>
  • + + <% if CurrentUser.is_admin? && @tag_alias.is_pending? %> +
  • Commands <%= link_to "Approve", approve_tag_alias_path(@tag_alias), :method => :post %>
  • + <% end %>
    diff --git a/app/views/tag_implications/show.html.erb b/app/views/tag_implications/show.html.erb index bb5ba3f76..3887d7225 100644 --- a/app/views/tag_implications/show.html.erb +++ b/app/views/tag_implications/show.html.erb @@ -13,6 +13,11 @@ <% if @tag_implication.respond_to?(:reason) && @tag_implication.reason.present? %>
  • Reason <%= format_text @tag_implication.reason %>
  • <% end %> +
  • Status: <%= @tag_implication.status %>
  • + + <% if CurrentUser.is_admin? && @tag_implication.is_pending? %> +
  • Commands <%= link_to "Approve", approve_tag_implication_path(@tag_implication), :method => :post %>
  • + <% end %> diff --git a/test/unit/tag_alias_test.rb b/test/unit/tag_alias_test.rb index 7f86e43a8..937c0616c 100644 --- a/test/unit/tag_alias_test.rb +++ b/test/unit/tag_alias_test.rb @@ -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 diff --git a/test/unit/tag_implication_test.rb b/test/unit/tag_implication_test.rb index a21005209..0a62450fa 100644 --- a/test/unit/tag_implication_test.rb +++ b/test/unit/tag_implication_test.rb @@ -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