Skip to content

Commit

Permalink
FIX: category permissions weren't properly loaded when /categories is…
Browse files Browse the repository at this point in the history
… the homepage

FIX: don't scope to a specific category when creating a new topic from /categories
  • Loading branch information
ZogStriP committed Sep 7, 2015
1 parent 21f8197 commit d5a2029
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 43 deletions.
65 changes: 26 additions & 39 deletions app/assets/javascripts/discourse/components/category-chooser.js.es6
Original file line number Diff line number Diff line change
@@ -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'],
Expand All @@ -8,57 +10,44 @@ 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() {

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() {
},

@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);
},

@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();
}
} 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.
Expand All @@ -79,16 +68,14 @@ export default ComboboxView.extend({
result = categoryBadgeHTML(Discourse.Category.findById(parentCategoryId), {link: false}) + " " + result;
}

result += " <span class='topic-count'>&times; " + category.get('topic_count') + "</span>";
result += ` <span class='topic-count'>&times; ${category.get('topic_count')}</span>`;

const description = category.get('description');
// TODO wtf how can this be null?;
if (description && description !== 'null') {
result += '<div class="category-desc">' +
description.substr(0,200) +
(description.length > 200 ? '&hellip;' : '') +
'</div>';
result += `<div class="category-desc">${description.substr(0, 200)}${description.length > 200 ? '&hellip;' : ''}</div>`;
}

return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions app/assets/javascripts/discourse/models/category-list.js.es6
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion app/models/category_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@ def find_categories
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
Expand Down
2 changes: 1 addition & 1 deletion lib/guardian/topic_guardian.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def can_create_topic?(parent)

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)
Expand Down

0 comments on commit d5a2029

Please sign in to comment.