A platform for community discussion. Free, open, simple.
FIX: Stop revising embedded topics whose tags can't be saved as given
Régis Hanole6287c7837b8
diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rbindex 31e643d133c..a47662cd0de 100644--- a/app/models/topic_embed.rb+++ b/app/models/topic_embed.rb@@ -131,10 +131,11 @@ class TopicEmbed < ActiveRecord::Basepost.reloadend- existing_tag_names = post.topic.tags.pluck(:name).sortincoming_tag_names = Array(tags).map { |tag| tag.respond_to?(:name) ? tag.name : tag }.sort- tags_changed = !tags.nil? && existing_tag_names != incoming_tag_names+ tags_changed =+ SiteSetting.tagging_enabled && !tags.nil? &&+ tags_changed?(post.topic, user, incoming_tag_names)content_changed = content_sha1 != embed.content_sha1title_changed = title.present? && title != post.topic.titlecook_method_changed = !cook_method.nil? && cook_method != post.cook_method@@ -144,7 +145,7 @@ class TopicEmbed < ActiveRecord::Baseif content_changed || title_changed || tags_changedchanges = { raw: absolutize_urls(url, contents) }- changes[:tags] = incoming_tag_names if SiteSetting.tagging_enabled && tags_changed+ changes[:tags] = incoming_tag_names if tags_changedchanges[:title] = title if title_changedpost.revise(user, changes, skip_validations: true, bypass_rate_limiter: true)@@ -169,6 +170,18 @@ class TopicEmbed < ActiveRecord::Basepostend+ def self.tags_changed?(topic, user, incoming_tag_names)+ existing_tag_names = topic.tags.pluck(:name)+ return false if existing_tag_names.sort == incoming_tag_names++ savable_tag_names =+ Array(DiscourseTagging.tags_for_saving(incoming_tag_names, user.guardian, unlimited: true))+ saved_count = [savable_tag_names.size, SiteSetting.max_tags_per_topic].min++ existing_tag_names.size != saved_count || (existing_tag_names - savable_tag_names).present?+ end+ private_class_method :tags_changed?+def self.find_remote(url)url = UrlHelper.normalized_encode(url)URI.parse(url) # ensure url parses, will raise if notdiff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rbindex d18fbb93a93..181417ff8d1 100644--- a/spec/models/topic_embed_spec.rb+++ b/spec/models/topic_embed_spec.rb@@ -395,6 +395,22 @@ RSpec.describe TopicEmbed doimported_page = TopicEmbed.import(user, url, title, contents, tags: [])expect(imported_page.topic.tags).to match_array([])end++ it "does not revise the post when the tags cannot all be saved" do+ SiteSetting.max_tags_per_topic = 1+ TopicEmbed.import(user, url, title, contents, tags: tags)++ Post.any_instance.expects(:revise).never+ TopicEmbed.import(user, url, title, contents, tags: tags)+ end++ it "does not revise the post when the existing tags match the incoming tags" do+ TopicEmbed.import(user, url, title, contents, tags: tags)+ SiteSetting.max_tags_per_topic = 1++ Post.any_instance.expects(:revise).never+ TopicEmbed.import(user, url, title, contents, tags: tags)+ endendcontext "with specified user and tags" do