From d5a2029026c943412e449c70a287bf6d7caf3e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 7 Sep 2015 18:52:53 +0200 Subject: [PATCH] FIX: category permissions weren't properly loaded when /categories is the homepage FIX: don't scope to a specific category when creating a new topic from /categories --- .../components/category-chooser.js.es6 | 61 ++++++++----------- .../controllers/discovery/categories.js.es6 | 3 + .../discourse/models/category-list.js.es6 | 4 +- app/models/category_list.rb | 8 ++- lib/guardian/topic_guardian.rb | 2 +- 5 files changed, 37 insertions(+), 41 deletions(-) diff --git a/app/assets/javascripts/discourse/components/category-chooser.js.es6 b/app/assets/javascripts/discourse/components/category-chooser.js.es6 index 57a7ed9697d..df61b75ddeb 100644 --- a/app/assets/javascripts/discourse/components/category-chooser.js.es6 +++ b/app/assets/javascripts/discourse/components/category-chooser.js.es6 @@ -1,5 +1,7 @@ import ComboboxView from 'discourse/components/combo-box'; import { categoryBadgeHTML } from 'discourse/helpers/category-link'; +import computed from 'ember-addons/ember-computed-decorators'; +import { observes, on } from 'ember-addons/ember-computed-decorators'; export default ComboboxView.extend({ classNames: ['combobox category-combobox'], @@ -8,46 +10,34 @@ export default ComboboxView.extend({ valueBinding: Ember.Binding.oneWay('source'), castInteger: true, - content: function() { - let scopedCategoryId = this.get('scopedCategoryId'); - + @computed("scopedCategoryId", "categories") + content(scopedCategoryId, categories) { // Always scope to the parent of a category, if present if (scopedCategoryId) { const scopedCat = Discourse.Category.findById(scopedCategoryId); scopedCategoryId = scopedCat.get('parent_category_id') || scopedCat.get('id'); } - return this.get('categories').filter(function(c) { - if (scopedCategoryId && (c.get('id') !== scopedCategoryId) && (c.get('parent_category_id') !== scopedCategoryId)) { - return false; - } - return c.get('permission') === Discourse.PermissionType.FULL && !c.get('isUncategorizedCategory'); + return categories.filter(c => { + if (scopedCategoryId && c.get('id') !== scopedCategoryId && c.get('parent_category_id') !== scopedCategoryId) { return false; } + if (c.get('isUncategorizedCategory')) { return false; } + return c.get('permission') === Discourse.PermissionType.FULL; }); - }.property('scopedCategoryId', 'categories'), + }, - _setCategories: function() { + @on("init") + @observes("site.sortedCategories") + _updateCategories() { + const categories = Discourse.SiteSettings.fixed_category_positions_on_create ? + Discourse.Category.list() : + Discourse.Category.listByActivity(); + this.set('categories', categories); + }, - if (!this.get('categories')) { - this.set('automatic', true); - } - - this._updateCategories(); - - }.on('init'), - - _updateCategories: function() { - - if (this.get('automatic')) { - this.set('categories', - Discourse.SiteSettings.fixed_category_positions_on_create ? - Discourse.Category.list() : Discourse.Category.listByActivity() - ); - } - }.observes('automatic', 'site.sortedCategories'), - - none: function() { + @computed("rootNone") + none(rootNone) { if (Discourse.User.currentProp('staff') || Discourse.SiteSettings.allow_uncategorized_topics) { - if (this.get('rootNone')) { + if (rootNone) { return "category.none"; } else { return Discourse.Category.findUncategorized(); @@ -55,10 +45,9 @@ export default ComboboxView.extend({ } else { return 'category.choose'; } - }.property(), + }, comboTemplate(item) { - let category; // If we have no id, but text with the uncategorized name, we can use that badge. @@ -79,16 +68,14 @@ export default ComboboxView.extend({ result = categoryBadgeHTML(Discourse.Category.findById(parentCategoryId), {link: false}) + " " + result; } - result += " × " + category.get('topic_count') + ""; + result += ` × ${category.get('topic_count')}`; const description = category.get('description'); // TODO wtf how can this be null?; if (description && description !== 'null') { - result += '
' + - description.substr(0,200) + - (description.length > 200 ? '…' : '') + - '
'; + result += `
${description.substr(0, 200)}${description.length > 200 ? '…' : ''}
`; } + return result; } diff --git a/app/assets/javascripts/discourse/controllers/discovery/categories.js.es6 b/app/assets/javascripts/discourse/controllers/discovery/categories.js.es6 index ba9877d485d..c4d639f3612 100644 --- a/app/assets/javascripts/discourse/controllers/discovery/categories.js.es6 +++ b/app/assets/javascripts/discourse/controllers/discovery/categories.js.es6 @@ -6,6 +6,9 @@ export default DiscoveryController.extend({ withLogo: Em.computed.filterBy('model.categories', 'logo_url'), showPostsColumn: Em.computed.empty('withLogo'), + // this makes sure the composer isn't scoping to a specific category + category: null, + actions: { refresh() { diff --git a/app/assets/javascripts/discourse/models/category-list.js.es6 b/app/assets/javascripts/discourse/models/category-list.js.es6 index fcbe90bd071..b5e70970540 100644 --- a/app/assets/javascripts/discourse/models/category-list.js.es6 +++ b/app/assets/javascripts/discourse/models/category-list.js.es6 @@ -34,7 +34,7 @@ CategoryList.reopenClass({ }, listForParent(store, category) { - return Discourse.ajax('/categories.json?parent_category_id=' + category.get('id')).then((result) => { + return Discourse.ajax(`/categories.json?parent_category_id=${category.get("id")}`).then(result => { return Discourse.CategoryList.create({ categories: this.categoriesFrom(store, result), parentCategory: category @@ -44,7 +44,7 @@ CategoryList.reopenClass({ list(store) { const getCategories = () => Discourse.ajax("/categories.json"); - return PreloadStore.getAndRemove("categories_list", getCategories).then((result) => { + return PreloadStore.getAndRemove("categories_list", getCategories).then(result => { return Discourse.CategoryList.create({ categories: this.categoriesFrom(store, result), can_create_category: result.category_list.can_create_category, diff --git a/app/models/category_list.rb b/app/models/category_list.rb index 2e32369fda1..1472a27501f 100644 --- a/app/models/category_list.rb +++ b/app/models/category_list.rb @@ -78,10 +78,16 @@ class CategoryList end if latest_post_only? - @categories = @categories.includes(:latest_post => {:topic => :last_poster} ) + @categories = @categories.includes(latest_post: { topic: :last_poster }) end @categories = @categories.to_a + + allowed_topic_create = Set.new(Category.topic_create_allowed(@guardian).pluck(:id)) + @categories.each do |category| + category.permission = CategoryGroup.permission_types[:full] if allowed_topic_create.include?(category.id) + end + if @options[:parent_category_id].blank? subcategories = {} to_delete = Set.new diff --git a/lib/guardian/topic_guardian.rb b/lib/guardian/topic_guardian.rb index 6543208b53a..8c03cc3e382 100644 --- a/lib/guardian/topic_guardian.rb +++ b/lib/guardian/topic_guardian.rb @@ -15,7 +15,7 @@ module TopicGuardian def can_create_topic_on_category?(category) can_create_topic?(nil) && - (!category || Category.topic_create_allowed(self).where(:id => category.id).count == 1) + (!category || Category.topic_create_allowed(self).where(id: category.id).count == 1) end def can_create_post_on_topic?(topic)