A platform for community discussion. Free, open, simple.
FEATURE: wiki editors are allowed edit tags for wiki topics.
Vinoth Kannan31577b21312a
diff --git a/app/assets/javascripts/discourse/components/composer-title.js.es6 b/app/assets/javascripts/discourse/components/composer-title.js.es6index 5302ab85f45..adc0b330e84 100644--- a/app/assets/javascripts/discourse/components/composer-title.js.es6+++ b/app/assets/javascripts/discourse/components/composer-title.js.es6@@ -10,6 +10,7 @@ import afterTransition from "discourse/lib/after-transition";export default Ember.Component.extend({classNames: ["title-input"],watchForLink: Ember.computed.alias("composer.canEditTopicFeaturedLink"),+ disabled: Ember.computed.or("composer.loading", "composer.disableTitleInput"),didInsertElement() {this._super(...arguments);diff --git a/app/assets/javascripts/discourse/controllers/composer.js.es6 b/app/assets/javascripts/discourse/controllers/composer.js.es6index 595568eec2f..b1f7daafbe1 100644--- a/app/assets/javascripts/discourse/controllers/composer.js.es6+++ b/app/assets/javascripts/discourse/controllers/composer.js.es6@@ -186,6 +186,9 @@ export default Ember.Controller.extend({);},+ disableCategoryChooser: Ember.computed.not("model.topic.details.can_edit"),+ disableTagsChooser: Ember.computed.not("model.topic.canEditTags"),+isStaffUser: Ember.computed.reads("currentUser.staff"),canUnlistTopic: Ember.computed.and("model.creatingTopic", "isStaffUser"),diff --git a/app/assets/javascripts/discourse/models/composer.js.es6 b/app/assets/javascripts/discourse/models/composer.js.es6index 4517051df34..34b44985df0 100644--- a/app/assets/javascripts/discourse/models/composer.js.es6+++ b/app/assets/javascripts/discourse/models/composer.js.es6@@ -141,6 +141,7 @@ const Composer = RestModel.extend({creatingPrivateMessage: Ember.computed.equal("action", PRIVATE_MESSAGE),notCreatingPrivateMessage: Ember.computed.not("creatingPrivateMessage"),notPrivateMessage: Ember.computed.not("privateMessage"),+ disableTitleInput: Ember.computed.not("topic.details.can_edit"),@computed("privateMessage", "archetype.hasOptions")showCategoryChooser(isPrivateMessage, hasOptions) {@@ -784,31 +785,31 @@ const Composer = RestModel.extend({let promise = Ember.RSVP.resolve();// Update the topic if we're editing the first post- if (- this.title &&- post.post_number === 1 &&- this.get("topic.details.can_edit")- ) {- const topicProps = this.getProperties(- Object.keys(_edit_topic_serializer)- );- // frontend should have featuredLink but backend needs featured_link- if (topicProps.featuredLink) {- topicProps.featured_link = topicProps.featuredLink;- delete topicProps.featuredLink;- }-+ if (this.title && post.post_number === 1) {const topic = this.topic;- // If we're editing a shared draft, keep the original category- if (this.action === EDIT_SHARED_DRAFT) {- const destinationCategoryId = topicProps.categoryId;- promise = promise.then(() =>- topic.updateDestinationCategory(destinationCategoryId)+ if (topic.details.can_edit) {+ const topicProps = this.getProperties(+ Object.keys(_edit_topic_serializer));- topicProps.categoryId = topic.get("category.id");+ // frontend should have featuredLink but backend needs featured_link+ if (topicProps.featuredLink) {+ topicProps.featured_link = topicProps.featuredLink;+ delete topicProps.featuredLink;+ }++ // If we're editing a shared draft, keep the original category+ if (this.action === EDIT_SHARED_DRAFT) {+ const destinationCategoryId = topicProps.categoryId;+ promise = promise.then(() =>+ topic.updateDestinationCategory(destinationCategoryId)+ );+ topicProps.categoryId = topic.get("category.id");+ }+ promise = promise.then(() => Topic.update(topic, topicProps));+ } else if (topic.details.can_edit_tags) {+ promise = promise.then(() => topic.updateTags(this.tags));}- promise = promise.then(() => Topic.update(topic, topicProps));}const props = {diff --git a/app/assets/javascripts/discourse/models/topic.js.es6 b/app/assets/javascripts/discourse/models/topic.js.es6index 1af3697106e..03345d77446 100644--- a/app/assets/javascripts/discourse/models/topic.js.es6+++ b/app/assets/javascripts/discourse/models/topic.js.es6@@ -546,6 +546,7 @@ const Topic = RestModel.extend({readLastPost: propertyEqual("last_read_post_number", "highest_post_number"),canClearPin: Ember.computed.and("pinned", "readLastPost"),+ canEditTags: Ember.computed.or("details.can_edit", "details.can_edit_tags"),archiveMessage() {this.set("archiving", true);@@ -610,6 +611,17 @@ const Topic = RestModel.extend({return ajax(`/t/${this.id}/reset-bump-date`, { type: "PUT" }).catch(popupAjaxError);+ },++ updateTags(tags) {+ if (!tags || tags.length === 0) {+ tags = [""];+ }++ return ajax(`/t/${this.id}/tags`, {+ type: "PUT",+ data: { tags: tags }+ });}});diff --git a/app/assets/javascripts/discourse/templates/components/composer-title.hbs b/app/assets/javascripts/discourse/templates/components/composer-title.hbsindex 76f63dac1f0..a8483595de6 100644--- a/app/assets/javascripts/discourse/templates/components/composer-title.hbs+++ b/app/assets/javascripts/discourse/templates/components/composer-title.hbs@@ -3,7 +3,7 @@id="reply-title"maxLength=titleMaxLengthplaceholderKey=composer.titlePlaceholder- disabled=composer.loading+ disabled=disabledautocomplete="discourse"}}{{popup-input-tip validation=validation}}diff --git a/app/assets/javascripts/discourse/templates/composer.hbs b/app/assets/javascripts/discourse/templates/composer.hbsindex 43c3599ec8d..f738efc3625 100644--- a/app/assets/javascripts/discourse/templates/composer.hbs+++ b/app/assets/javascripts/discourse/templates/composer.hbs@@ -76,12 +76,13 @@fullWidthOnMobile=truevalue=model.categoryIdscopedCategoryId=scopedCategoryId+ isDisabled=disableCategoryChoosertabindex="3"}}{{popup-input-tip validation=categoryValidation}}</div>{{/if}}{{#if canEditTags}}- {{mini-tag-chooser tags=model.tags tabindex="4" categoryId=model.categoryId minimum=model.minimumRequiredTags}}+ {{mini-tag-chooser tags=model.tags tabindex="4" categoryId=model.categoryId minimum=model.minimumRequiredTags isDisabled=disableTagsChooser}}{{popup-input-tip validation=tagValidation}}{{/if}}</div>diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rbindex 7e50ec7400d..2c29594e66b 100644--- a/app/controllers/topics_controller.rb+++ b/app/controllers/topics_controller.rb@@ -374,6 +374,16 @@ class TopicsController < ApplicationControllersuccess ? render_serialized(topic, BasicTopicSerializer) : render_json_error(topic)end+ def update_tags+ params.require(:tags)+ topic = Topic.find_by(id: params[:topic_id])+ guardian.ensure_can_edit_tags!(topic)++ success = PostRevisor.new(topic.first_post, topic).revise!(current_user, { tags: params[:tags] }, validate_post: false)++ success ? render_serialized(topic, BasicTopicSerializer) : render_json_error(topic)+ end+def feature_statsparams.require(:category_id)category_id = params[:category_id].to_idiff --git a/app/serializers/topic_view_details_serializer.rb b/app/serializers/topic_view_details_serializer.rbindex c80ba6dece8..d21b1a778b0 100644--- a/app/serializers/topic_view_details_serializer.rb+++ b/app/serializers/topic_view_details_serializer.rb@@ -14,7 +14,8 @@ class TopicViewDetailsSerializer < ApplicationSerializer:can_reply_as_new_topic,:can_flag_topic,:can_convert_topic,- :can_review_topic]+ :can_review_topic,+ :can_edit_tags]endattributes(@@ -128,6 +129,10 @@ class TopicViewDetailsSerializer < ApplicationSerializerscope.can_convert_topic?(object.topic)end+ def include_can_edit_tags?+ !scope.can_edit?(object.topic) && scope.can_edit_tags?(object.topic)+ end+def allowed_usersobject.topic.allowed_users.reject { |user| object.group_allowed_user_ids.include?(user.id) }enddiff --git a/config/routes.rb b/config/routes.rbindex 55f2cd7a536..290282be38c 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -754,6 +754,7 @@ Discourse::Application.routes.draw dodelete "t/:topic_id/timings" => "topics#destroy_timings", constraints: { topic_id: /\d+/ }put "t/:topic_id/bookmark" => "topics#bookmark", constraints: { topic_id: /\d+/ }put "t/:topic_id/remove_bookmarks" => "topics#remove_bookmarks", constraints: { topic_id: /\d+/ }+ put "t/:topic_id/tags" => "topics#update_tags", constraints: { topic_id: /\d+/ }post "t/:topic_id/notifications" => "topics#set_notifications" , constraints: { topic_id: /\d+/ }diff --git a/lib/guardian/topic_guardian.rb b/lib/guardian/topic_guardian.rbindex 80cc2a3cf3e..b6d8a273068 100644--- a/lib/guardian/topic_guardian.rb+++ b/lib/guardian/topic_guardian.rb@@ -184,4 +184,16 @@ module TopicGuardiandef can_banner_topic?(topic)topic && authenticated? && !topic.private_message? && is_staff?end++ def can_edit_tags?(topic)+ return false unless can_tag_topics?+ return false if topic.private_message? && !can_tag_pms?+ return true if can_edit_topic?(topic)++ if topic&.first_post&.wiki && (@user.trust_level >= SiteSetting.min_trust_to_edit_wiki_post.to_i)+ return can_create_post?(topic)+ end++ false+ endenddiff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 14200831541..357ce90fbbe 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -1027,6 +1027,31 @@ RSpec.describe TopicsController doexpect(topic.tags.pluck(:id)).to contain_exactly(tag.id)end+ it "can add a tag to wiki topic" do+ SiteSetting.min_trust_to_edit_wiki_post = 2+ topic.first_post.update!(wiki: true)+ user = Fabricate(:user)+ sign_in(user)++ expect do+ put "/t/#{topic.id}/tags.json", params: {+ tags: [tag.name]+ }+ end.not_to change { topic.reload.first_post.revisions.count }++ expect(response.status).to eq(403)+ user.update!(trust_level: 2)++ expect do+ put "/t/#{topic.id}/tags.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' dotopic.tags << tagdiff --git a/spec/serializers/topic_view_serializer_spec.rb b/spec/serializers/topic_view_serializer_spec.rbindex ccf8a647582..b0627ab05f5 100644--- a/spec/serializers/topic_view_serializer_spec.rb+++ b/spec/serializers/topic_view_serializer_spec.rb@@ -257,6 +257,26 @@ describe TopicViewSerializer doexpect(details[:allowed_users].find { |au| au[:id] == pm.user_id }).to be_presentexpect(details[:allowed_groups].find { |ag| ag[:id] == group.id }).to be_presentend++ context "can_edit_tags" do+ before do+ SiteSetting.tagging_enabled = true+ SiteSetting.min_trust_to_edit_wiki_post = 2+ end++ it "returns true when user can edit a wiki topic" do+ post = Fabricate(:post, wiki: true)+ topic = Fabricate(:topic, first_post: post)++ json = serialize_topic(topic, user)+ expect(json[:details][:can_edit_tags]).to be_nil++ user.update!(trust_level: 2)++ json = serialize_topic(topic, user)+ expect(json[:details][:can_edit_tags]).to eq(true)+ end+ endendend