From d84150b23270d903f0be1a7158a08a3fe0d6bf29 Mon Sep 17 00:00:00 2001 From: BrokenEagle Date: Fri, 6 Mar 2020 21:56:04 +0000 Subject: [PATCH 1/5] Fix posts not being processed by blacklist after post updates - Blacklisting individual posts was moved into its own function - Fixed Javascript variables being leaked into the user environment - Fixed post qTips being orphaned by replacements by destroying them first - Moved edit form check into post success to avoid repeating post check --- app/javascript/src/javascripts/blacklists.js | 30 +++++++++++--------- app/views/posts/update.js.erb | 20 +++++++------ 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/app/javascript/src/javascripts/blacklists.js b/app/javascript/src/javascripts/blacklists.js index c9e3c6e1b..d63c26768 100644 --- a/app/javascript/src/javascripts/blacklists.js +++ b/app/javascript/src/javascripts/blacklists.js @@ -118,24 +118,28 @@ Blacklist.apply = function() { var count = 0 $.each(this.posts(), function(i, post) { - var post_count = 0; - $.each(Blacklist.entries, function(j, entry) { - if (Blacklist.post_match(post, entry)) { - entry.hits += 1; - count += 1; - post_count += 1; - } - }); - if (post_count > 0) { - Blacklist.post_hide(post); - } else { - Blacklist.post_unhide(post); - } + count += Blacklist.apply_post(post); }); return count; } +Blacklist.apply_post = function(post) { + var post_count = 0; + $.each(Blacklist.entries, function(j, entry) { + if (Blacklist.post_match(post, entry)) { + entry.hits += 1; + post_count += 1; + } + }); + if (post_count > 0) { + Blacklist.post_hide(post); + } else { + Blacklist.post_unhide(post); + } + return post_count; +}; + Blacklist.posts = function() { return $(".post-preview, #image-container, #c-comments .post, .mod-queue-preview.post-preview"); } diff --git a/app/views/posts/update.js.erb b/app/views/posts/update.js.erb index 006499004..1da54c638 100644 --- a/app/views/posts/update.js.erb +++ b/app/views/posts/update.js.erb @@ -1,14 +1,16 @@ -var post_id = <%= @post.id %>; -var $post = $(`#post_${post_id}`); - <% if @post.valid? %> - $post.replaceWith("<%= j PostPresenter.preview(@post, show_deleted: true) %>"); + var $post = $("#post_<%= @post.id %>"); + <% if !CurrentUser.disable_post_tooltips %> + $post.find("img").qtip("destroy", true); + <% end %> + var $new_post = $("<%= j PostPresenter.preview(@post, show_deleted: true) %>"); + Danbooru.Blacklist.apply_post($new_post.get(0)); + $("#post_<%= @post.id %>").replaceWith($new_post); + <% if params[:mode] == "quick-edit" %> + Danbooru.PostModeMenu.close_edit_form(); + <% end %> <% else %> - Danbooru.error(`Post #${post_id}: <%= j @post.errors.full_messages.join("; ") %>`); -<% end %> - -<% if @post.valid? && params[:mode] == "quick-edit" %> - Danbooru.PostModeMenu.close_edit_form(); + Danbooru.error(`Post #<%= @post.id %>: <%= j @post.errors.full_messages.join("; ") %>`); <% end %> $(document).trigger("danbooru:post-preview-updated", <%= raw @post.to_json %>); From d1aed303fdfe8b8ff3529b7d9e316124d37aef63 Mon Sep 17 00:00:00 2001 From: BrokenEagle Date: Sat, 7 Mar 2020 01:06:52 +0000 Subject: [PATCH 2/5] Fix blacklist code executing muliple times - When enabling/disabling all, it was executing on every single blacklist line - When starting out disabled, it was executing several times --- app/javascript/src/javascripts/blacklists.js | 34 ++++++++++++++------ 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/app/javascript/src/javascripts/blacklists.js b/app/javascript/src/javascripts/blacklists.js index d63c26768..482b26595 100644 --- a/app/javascript/src/javascripts/blacklists.js +++ b/app/javascript/src/javascripts/blacklists.js @@ -84,28 +84,44 @@ Blacklist.update_sidebar = function() { $("#blacklist-box").show(); } +Blacklist.disable_all = function() { + Blacklist.entries.forEach(function(entry) { + entry.disabled = true; + }); + // There is no need to process the blacklist when disabling + Blacklist.posts().removeClass("blacklisted-active"); + $("#disable-all-blacklists").hide(); + $("#re-enable-all-blacklists").show(); + $("#blacklist-list a").addClass("blacklisted-inactive"); +} + +Blacklist.enable_all = function() { + Blacklist.entries.forEach(function(entry) { + entry.disabled = false; + }); + Blacklist.apply(); + $("#disable-all-blacklists").show(); + $("#re-enable-all-blacklists").hide(); + $("#blacklist-list a").removeClass("blacklisted-inactive"); +} + Blacklist.initialize_disable_all_blacklists = function() { if (Cookie.get("dab") === "1") { - $("#re-enable-all-blacklists").show(); - $("#blacklist-list a:not(.blacklisted-active)").click(); - Blacklist.apply(); + Blacklist.disable_all(); } else { + // The blacklist has already been processed by this point $("#disable-all-blacklists").show() } $("#disable-all-blacklists").on("click.danbooru", function(e) { - $("#disable-all-blacklists").hide(); - $("#re-enable-all-blacklists").show(); Cookie.put("dab", "1"); - $("#blacklist-list a:not(.blacklisted-active)").click(); + Blacklist.disable_all(); e.preventDefault(); }); $("#re-enable-all-blacklists").on("click.danbooru", function(e) { - $("#disable-all-blacklists").show(); - $("#re-enable-all-blacklists").hide(); Cookie.put("dab", "0"); - $("#blacklist-list a.blacklisted-active").click(); + Blacklist.enable_all(); e.preventDefault(); }); } From 161e776cf7a900b56c7e8acf4393c63c6c90d666 Mon Sep 17 00:00:00 2001 From: BrokenEagle Date: Sat, 7 Mar 2020 06:10:50 +0000 Subject: [PATCH 3/5] Add utility regexp split function for non-whitespace words --- app/javascript/src/javascripts/blacklists.js | 9 ++++----- app/javascript/src/javascripts/posts.js.erb | 4 ++-- app/javascript/src/javascripts/related_tag.js | 12 +++++------- app/javascript/src/javascripts/utility.js | 4 ++++ 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/app/javascript/src/javascripts/blacklists.js b/app/javascript/src/javascripts/blacklists.js index 482b26595..5f4dc3595 100644 --- a/app/javascript/src/javascripts/blacklists.js +++ b/app/javascript/src/javascripts/blacklists.js @@ -15,8 +15,7 @@ Blacklist.parse_entry = function(string) { "hits": 0, "min_score": null }; - var matches = string.match(/\S+/g) || []; - $.each(matches, function(i, tag) { + Utility.regexp_split(string).forEach(function(tag) { if (tag.charAt(0) === '-') { entry.exclude.push(tag.slice(1)); } else if (tag.charAt(0) === '~') { @@ -169,11 +168,11 @@ Blacklist.post_match = function(post, entry) { var score = parseInt($post.attr("data-score")); var score_test = entry.min_score === null || score < entry.min_score; - var tags = String($post.attr("data-tags")).match(/\S+/g) || []; - tags = tags.concat(String($post.attr("data-pools")).match(/\S+/g) || []); + var tags = Utility.regexp_split($post.attr("data-tags")); + tags.push(...Utility.regexp_split($post.attr("data-pools"))); tags.push("rating:" + $post.data("rating")); tags.push("uploaderid:" + $post.attr("data-uploader-id")); - $.each(String($post.data("flags")).match(/\S+/g) || [], function(i, v) { + Utility.regexp_split($post.data("flags")).forEach(function(v) { tags.push("status:" + v); }); diff --git a/app/javascript/src/javascripts/posts.js.erb b/app/javascript/src/javascripts/posts.js.erb index 2fda14e7d..2858a2913 100644 --- a/app/javascript/src/javascripts/posts.js.erb +++ b/app/javascript/src/javascripts/posts.js.erb @@ -550,8 +550,8 @@ Post.update_tag_count = function(event) { let count = 0; if (event) { - let tags = [...new Set($(event.target).val().match(/\S+/g))]; - if (tags) { + let tags = Utility.regexp_split($(event.target).val()); + if (tags.length) { count = tags.length; string = (count === 1) ? (count + " tag") : (count + " tags") } diff --git a/app/javascript/src/javascripts/related_tag.js b/app/javascript/src/javascripts/related_tag.js index f101073a5..3f09339ba 100644 --- a/app/javascript/src/javascripts/related_tag.js +++ b/app/javascript/src/javascripts/related_tag.js @@ -89,27 +89,25 @@ RelatedTag.current_tag = function() { } RelatedTag.update_selected = function(e) { - var current_tags = $("#upload_tag_string,#post_tag_string").val().toLowerCase().match(/\S+/g) || []; + var current_tags = RelatedTag.current_tags(); var $all_tags = $(".related-tags a.search-tag"); $all_tags.removeClass("selected"); - $all_tags.each(function(i, tag) { - if (current_tags.indexOf(tag.textContent.replace(/ /g, "_")) > -1) { + if (current_tags.includes(tag.textContent.replace(/ /g, "_"))) { $(tag).addClass("selected"); } }); } -RelatedTag.tags_include = function(name) { - var current = $("#upload_tag_string,#post_tag_string").val().toLowerCase().match(/\S+/g) || []; - return $.inArray(name.toLowerCase(), current) > -1; +RelatedTag.current_tags = function() { + return Utility.regexp_split($("#upload_tag_string,#post_tag_string").val().toLowerCase()); } RelatedTag.toggle_tag = function(e) { var $field = $("#upload_tag_string,#post_tag_string"); var tag = $(e.target).html().replace(/ /g, "_").replace(/>/g, ">").replace(/</g, "<").replace(/&/g, "&"); - if (RelatedTag.tags_include(tag)) { + if (RelatedTag.current_tags().includes(tag)) { var escaped_tag = Utility.regexp_escape(tag); $field.val($field.val().replace(new RegExp("(^|\\s)" + escaped_tag + "($|\\s)", "gi"), "$1$2")); } else { diff --git a/app/javascript/src/javascripts/utility.js b/app/javascript/src/javascripts/utility.js index 78811385a..4a335fd50 100644 --- a/app/javascript/src/javascripts/utility.js +++ b/app/javascript/src/javascripts/utility.js @@ -108,6 +108,10 @@ Utility.regexp_escape = function(string) { return string.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1"); } +Utility.regexp_split = function(string) { + return [...new Set(String(string === null || string === undefined ? "" : string).match(/\S+/g))]; +} + $.fn.selectEnd = function() { return this.each(function() { this.focus(); From 8c1f6020baacd339f6c3c777dd4a6ff4e9aa6d3e Mon Sep 17 00:00:00 2001 From: BrokenEagle Date: Sat, 7 Mar 2020 06:12:55 +0000 Subject: [PATCH 4/5] Adjust how blacklist classes get added - Rename class for links as it was a misnomer --- app/javascript/src/javascripts/blacklists.js | 9 +++++---- app/javascript/src/styles/common/blacklists.scss | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/javascript/src/javascripts/blacklists.js b/app/javascript/src/javascripts/blacklists.js index 5f4dc3595..e832ab589 100644 --- a/app/javascript/src/javascripts/blacklists.js +++ b/app/javascript/src/javascripts/blacklists.js @@ -41,16 +41,17 @@ Blacklist.parse_entries = function() { } Blacklist.toggle_entry = function(e) { - var tags = $(e.target).text(); + var $link = $(e.target); + var tags = $link.text(); var match = $.grep(Blacklist.entries, function(entry, i) { return entry.tags === tags; })[0]; if (match) { match.disabled = !match.disabled; if (match.disabled) { - Blacklist.post_hide(e.target); + $link.addClass("blacklisted-inactive"); } else { - Blacklist.post_unhide(e.target); + $link.removeClass("blacklisted-inactive"); } } Blacklist.apply(); @@ -183,7 +184,7 @@ Blacklist.post_match = function(post, entry) { Blacklist.post_hide = function(post) { var $post = $(post); - $post.addClass("blacklisted").addClass("blacklisted-active"); + $post.addClass("blacklisted blacklisted-active"); var $video = $post.find("video").get(0); if ($video) { diff --git a/app/javascript/src/styles/common/blacklists.scss b/app/javascript/src/styles/common/blacklists.scss index bd4499a4c..b025546c3 100644 --- a/app/javascript/src/styles/common/blacklists.scss +++ b/app/javascript/src/styles/common/blacklists.scss @@ -11,7 +11,7 @@ vertical-align: bottom; } - a.blacklisted-active { + a.blacklisted-inactive { text-decoration: line-through; } } From ffc57b6cfa50838de3a153afcf17edc7b84a6247 Mon Sep 17 00:00:00 2001 From: BrokenEagle Date: Sat, 7 Mar 2020 06:18:04 +0000 Subject: [PATCH 5/5] Use alternate iterators which have better performance --- app/javascript/src/javascripts/blacklists.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/javascript/src/javascripts/blacklists.js b/app/javascript/src/javascripts/blacklists.js index e832ab589..790cc4c31 100644 --- a/app/javascript/src/javascripts/blacklists.js +++ b/app/javascript/src/javascripts/blacklists.js @@ -34,7 +34,7 @@ Blacklist.parse_entries = function() { var entries = (Utility.meta("blacklisted-tags") || "nozomiisthebestlovelive").replace(/(rating:[qes])\w+/ig, "$1").toLowerCase().split(/,/); entries = entries.filter(e => e.trim() !== ""); - $.each(entries, function(i, tags) { + entries.forEach(function(tags) { var entry = Blacklist.parse_entry(tags); Blacklist.entries.push(entry); }); @@ -59,7 +59,7 @@ Blacklist.toggle_entry = function(e) { } Blacklist.update_sidebar = function() { - $.each(this.entries, function(i, entry) { + Blacklist.entries.forEach(function(entry) { if (entry.hits === 0) { return; } @@ -127,13 +127,13 @@ Blacklist.initialize_disable_all_blacklists = function() { } Blacklist.apply = function() { - $.each(this.entries, function(i, entry) { + Blacklist.entries.forEach(function(entry) { entry.hits = 0; }); var count = 0 - $.each(this.posts(), function(i, post) { + Blacklist.posts().each(function(i, post) { count += Blacklist.apply_post(post); }); @@ -142,7 +142,7 @@ Blacklist.apply = function() { Blacklist.apply_post = function(post) { var post_count = 0; - $.each(Blacklist.entries, function(j, entry) { + Blacklist.entries.forEach(function(entry) { if (Blacklist.post_match(post, entry)) { entry.hits += 1; post_count += 1;