mirror of
https://github.com/discourse/discourse.git
synced 2025-03-26 03:25:44 +08:00
DEV: Simplify client and server side code to support removing tags.
Follow up to 834c86678fc9b0900d8ce83365068c41bc34f63f.
This commit is contained in:
parent
678a9a61c4
commit
148bfc9be5
@ -652,15 +652,9 @@ Topic.reopenClass({
|
|||||||
delete props.category_id;
|
delete props.category_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Annoyingly, empty arrays are not sent across the wire. This
|
if (props.tags && props.tags.length === 0) {
|
||||||
// allows us to make a distinction between arrays that were not
|
props.tags = [""];
|
||||||
// sent and arrays that we specifically want to be empty.
|
}
|
||||||
Object.keys(props).forEach(function(k) {
|
|
||||||
const v = props[k];
|
|
||||||
if (v instanceof Array && v.length === 0) {
|
|
||||||
props[`${k}_empty_array`] = true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return ajax(topic.get("url"), { type: "PUT", data: props }).then(result => {
|
return ajax(topic.get("url"), { type: "PUT", data: props }).then(result => {
|
||||||
// The title can be cleaned up server side
|
// The title can be cleaned up server side
|
||||||
|
@ -324,13 +324,13 @@ class TopicsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
changes = {}
|
changes = {}
|
||||||
|
|
||||||
PostRevisor.tracked_topic_fields.each_key do |f|
|
PostRevisor.tracked_topic_fields.each_key do |f|
|
||||||
changes[f] = params[f] if params.has_key?(f)
|
changes[f] = params[f] if params.has_key?(f)
|
||||||
end
|
end
|
||||||
|
|
||||||
changes.delete(:title) if topic.title == changes[:title]
|
changes.delete(:title) if topic.title == changes[:title]
|
||||||
changes.delete(:category_id) if topic.category_id.to_i == changes[:category_id].to_i
|
changes.delete(:category_id) if topic.category_id.to_i == changes[:category_id].to_i
|
||||||
changes.delete(:tags_empty_array) if !topic.tags.exists?
|
|
||||||
|
|
||||||
success = true
|
success = true
|
||||||
|
|
||||||
|
@ -101,17 +101,6 @@ class PostRevisor
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
track_topic_field(:tags_empty_array) do |tc, val|
|
|
||||||
if val.present? && tc.guardian.can_tag_topics?
|
|
||||||
prev_tags = tc.topic.tags.map(&:name)
|
|
||||||
if !DiscourseTagging.tag_topic_by_names(tc.topic, tc.guardian, [])
|
|
||||||
tc.check_result(false)
|
|
||||||
next
|
|
||||||
end
|
|
||||||
tc.record_change('tags', prev_tags, nil)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
track_topic_field(:featured_link) do |topic_changes, featured_link|
|
track_topic_field(:featured_link) do |topic_changes, featured_link|
|
||||||
if SiteSetting.topic_featured_link_enabled &&
|
if SiteSetting.topic_featured_link_enabled &&
|
||||||
topic_changes.guardian.can_edit_featured_link?(topic_changes.topic.category_id)
|
topic_changes.guardian.can_edit_featured_link?(topic_changes.topic.category_id)
|
||||||
|
@ -677,14 +677,6 @@ describe PostRevisor do
|
|||||||
expect(post.topic.tags.size).to eq(0)
|
expect(post.topic.tags.size).to eq(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "can remove all tags using tags_empty_array" do
|
|
||||||
topic.tags = [Fabricate(:tag, name: "stuff")]
|
|
||||||
result = subject.revise!(user, raw: "lets totally update the body", tags_empty_array: "true")
|
|
||||||
expect(result).to eq(true)
|
|
||||||
post.reload
|
|
||||||
expect(post.topic.tags.size).to eq(0)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "can't add staff-only tags" do
|
it "can't add staff-only tags" do
|
||||||
create_staff_tags(['important'])
|
create_staff_tags(['important'])
|
||||||
result = subject.revise!(user, raw: "lets totally update the body", tags: ['important', 'stuff'])
|
result = subject.revise!(user, raw: "lets totally update the body", tags: ['important', 'stuff'])
|
||||||
|
@ -1002,14 +1002,55 @@ RSpec.describe TopicsController do
|
|||||||
it "doesn't call the PostRevisor when there is no changes" do
|
it "doesn't call the PostRevisor when there is no changes" do
|
||||||
expect do
|
expect do
|
||||||
put "/t/#{topic.slug}/#{topic.id}.json", params: {
|
put "/t/#{topic.slug}/#{topic.id}.json", params: {
|
||||||
category_id: topic.category_id,
|
category_id: topic.category_id
|
||||||
tags_empty_array: true
|
|
||||||
}
|
}
|
||||||
end.not_to change(PostRevision.all, :count)
|
end.not_to change(PostRevision.all, :count)
|
||||||
|
|
||||||
expect(response.status).to eq(200)
|
expect(response.status).to eq(200)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'tags' do
|
||||||
|
fab!(:tag) { Fabricate(:tag) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
SiteSetting.tagging_enabled = true
|
||||||
|
end
|
||||||
|
|
||||||
|
it "can add a tag to topic" do
|
||||||
|
expect do
|
||||||
|
put "/t/#{topic.slug}/#{topic.id}.json", params: {
|
||||||
|
tags: [tag.name]
|
||||||
|
}
|
||||||
|
end.to change { topic.reload.first_post.revisions.count }.by(1)
|
||||||
|
|
||||||
|
expect(response.status).to eq(200)
|
||||||
|
expect(topic.tags.pluck(:id)).to contain_exactly(tag.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not remove tag if no params is given' do
|
||||||
|
topic.tags << tag
|
||||||
|
|
||||||
|
expect do
|
||||||
|
put "/t/#{topic.slug}/#{topic.id}.json"
|
||||||
|
end.to_not change { topic.reload.tags.count }
|
||||||
|
|
||||||
|
expect(response.status).to eq(200)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'can remove a tag' do
|
||||||
|
topic.tags << tag
|
||||||
|
|
||||||
|
expect do
|
||||||
|
put "/t/#{topic.slug}/#{topic.id}.json", params: {
|
||||||
|
tags: [""]
|
||||||
|
}
|
||||||
|
end.to change { topic.reload.first_post.revisions.count }.by(1)
|
||||||
|
|
||||||
|
expect(response.status).to eq(200)
|
||||||
|
expect(topic.tags).to eq([])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when topic is private' do
|
context 'when topic is private' do
|
||||||
before do
|
before do
|
||||||
topic.update!(
|
topic.update!(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user