mirror of
https://github.com/discourse/discourse.git
synced 2025-05-23 12:11:12 +08:00
DEV: Use Category.findById instead of Array.find (#26445)
Category.findById uses a Map to look up categories by ID which makes it faster and preferable over Site.categories.find.
This commit is contained in:
@ -90,10 +90,7 @@ export default Controller.extend({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const model = this.model;
|
const model = this.model;
|
||||||
const parentCategory = this.site.categories.findBy(
|
const parentCategory = Category.findById(model.parent_category_id);
|
||||||
"id",
|
|
||||||
parseInt(model.parent_category_id, 10)
|
|
||||||
);
|
|
||||||
|
|
||||||
this.set("saving", true);
|
this.set("saving", true);
|
||||||
const previousParentCategory = model.get("parentCategory");
|
const previousParentCategory = model.get("parentCategory");
|
||||||
|
@ -12,6 +12,7 @@ import { propertyNotEqual } from "discourse/lib/computed";
|
|||||||
import { QUOTE_REGEXP } from "discourse/lib/quote";
|
import { QUOTE_REGEXP } from "discourse/lib/quote";
|
||||||
import { prioritizeNameFallback } from "discourse/lib/settings";
|
import { prioritizeNameFallback } from "discourse/lib/settings";
|
||||||
import { emailValid, escapeExpression } from "discourse/lib/utilities";
|
import { emailValid, escapeExpression } from "discourse/lib/utilities";
|
||||||
|
import Category from "discourse/models/category";
|
||||||
import Draft from "discourse/models/draft";
|
import Draft from "discourse/models/draft";
|
||||||
import RestModel from "discourse/models/rest";
|
import RestModel from "discourse/models/rest";
|
||||||
import Site from "discourse/models/site";
|
import Site from "discourse/models/site";
|
||||||
@ -253,7 +254,7 @@ export default class Composer extends RestModel {
|
|||||||
|
|
||||||
@discourseComputed("categoryId")
|
@discourseComputed("categoryId")
|
||||||
category(categoryId) {
|
category(categoryId) {
|
||||||
return categoryId ? this.site.categories.findBy("id", categoryId) : null;
|
return categoryId ? Category.findById(categoryId) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@discourseComputed("category.minimumRequiredTags")
|
@discourseComputed("category.minimumRequiredTags")
|
||||||
@ -760,7 +761,7 @@ export default class Composer extends RestModel {
|
|||||||
|
|
||||||
// If the user didn't change the template, clear it
|
// If the user didn't change the template, clear it
|
||||||
if (oldCategoryId) {
|
if (oldCategoryId) {
|
||||||
const oldCat = this.site.categories.findBy("id", oldCategoryId);
|
const oldCat = Category.findById(oldCategoryId);
|
||||||
if (oldCat && oldCat.topic_template === reply) {
|
if (oldCat && oldCat.topic_template === reply) {
|
||||||
reply = "";
|
reply = "";
|
||||||
}
|
}
|
||||||
@ -770,7 +771,7 @@ export default class Composer extends RestModel {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const category = this.site.categories.findBy("id", categoryId);
|
const category = Category.findById(categoryId);
|
||||||
if (category) {
|
if (category) {
|
||||||
this.set("reply", category.topic_template || "");
|
this.set("reply", category.topic_template || "");
|
||||||
}
|
}
|
||||||
@ -1184,9 +1185,7 @@ export default class Composer extends RestModel {
|
|||||||
|
|
||||||
// Update topic_count for the category
|
// Update topic_count for the category
|
||||||
const postCategoryId = parseInt(createdPost.category, 10) || 1;
|
const postCategoryId = parseInt(createdPost.category, 10) || 1;
|
||||||
const category = this.site.categories.find(
|
const category = Category.findById(postCategoryId);
|
||||||
(x) => x.id === postCategoryId
|
|
||||||
);
|
|
||||||
|
|
||||||
category?.incrementProperty("topic_count");
|
category?.incrementProperty("topic_count");
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ import { Promise } from "rsvp";
|
|||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
import { cook, emojiUnescape, excerpt } from "discourse/lib/text";
|
import { cook, emojiUnescape, excerpt } from "discourse/lib/text";
|
||||||
import { escapeExpression } from "discourse/lib/utilities";
|
import { escapeExpression } from "discourse/lib/utilities";
|
||||||
|
import Category from "discourse/models/category";
|
||||||
import {
|
import {
|
||||||
NEW_PRIVATE_MESSAGE_KEY,
|
NEW_PRIVATE_MESSAGE_KEY,
|
||||||
NEW_TOPIC_KEY,
|
NEW_TOPIC_KEY,
|
||||||
@ -78,9 +79,7 @@ export default class UserDraftsStream extends RestModel {
|
|||||||
}
|
}
|
||||||
draft.title = emojiUnescape(escapeExpression(draft.title));
|
draft.title = emojiUnescape(escapeExpression(draft.title));
|
||||||
if (draft.data.categoryId) {
|
if (draft.data.categoryId) {
|
||||||
draft.category =
|
draft.category = Category.findById(draft.data.categoryId) || null;
|
||||||
this.site.categories.findBy("id", draft.data.categoryId) ||
|
|
||||||
null;
|
|
||||||
}
|
}
|
||||||
this.content.push(UserDraft.create(draft));
|
this.content.push(UserDraft.create(draft));
|
||||||
});
|
});
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
|
import Category from "discourse/models/category";
|
||||||
import DiscourseRoute from "discourse/routes/discourse";
|
import DiscourseRoute from "discourse/routes/discourse";
|
||||||
import I18n from "discourse-i18n";
|
import I18n from "discourse-i18n";
|
||||||
|
|
||||||
@ -24,7 +25,7 @@ export default DiscourseRoute.extend({
|
|||||||
const { category_moderators: categoryModerators } = result.about;
|
const { category_moderators: categoryModerators } = result.about;
|
||||||
if (categoryModerators && categoryModerators.length) {
|
if (categoryModerators && categoryModerators.length) {
|
||||||
categoryModerators.forEach((obj, index) => {
|
categoryModerators.forEach((obj, index) => {
|
||||||
const category = this.site.categories.findBy("id", obj.category_id);
|
const category = Category.findById(obj.category_id);
|
||||||
result.about.category_moderators[index].category = category;
|
result.about.category_moderators[index].category = category;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@ import {
|
|||||||
} from "discourse/lib/uploads";
|
} from "discourse/lib/uploads";
|
||||||
import DiscourseURL from "discourse/lib/url";
|
import DiscourseURL from "discourse/lib/url";
|
||||||
import { escapeExpression, modKeysPressed } from "discourse/lib/utilities";
|
import { escapeExpression, modKeysPressed } from "discourse/lib/utilities";
|
||||||
|
import Category from "discourse/models/category";
|
||||||
import Composer, {
|
import Composer, {
|
||||||
CREATE_TOPIC,
|
CREATE_TOPIC,
|
||||||
NEW_TOPIC_KEY,
|
NEW_TOPIC_KEY,
|
||||||
@ -1322,17 +1323,14 @@ export default class ComposerService extends Service {
|
|||||||
|
|
||||||
// Scope the categories drop down to the category we opened the composer with.
|
// Scope the categories drop down to the category we opened the composer with.
|
||||||
if (opts.categoryId && !opts.disableScopedCategory) {
|
if (opts.categoryId && !opts.disableScopedCategory) {
|
||||||
const category = this.site.categories.findBy("id", opts.categoryId);
|
const category = Category.findById(opts.categoryId);
|
||||||
if (category) {
|
if (category) {
|
||||||
this.set("scopedCategoryId", opts.categoryId);
|
this.set("scopedCategoryId", opts.categoryId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts.prioritizedCategoryId) {
|
if (opts.prioritizedCategoryId) {
|
||||||
const category = this.site.categories.findBy(
|
const category = Category.findById(opts.prioritizedCategoryId);
|
||||||
"id",
|
|
||||||
opts.prioritizedCategoryId
|
|
||||||
);
|
|
||||||
|
|
||||||
if (category) {
|
if (category) {
|
||||||
this.set("prioritizedCategoryId", opts.prioritizedCategoryId);
|
this.set("prioritizedCategoryId", opts.prioritizedCategoryId);
|
||||||
|
@ -8,6 +8,7 @@ import { isBlank, isPresent } from "@ember/utils";
|
|||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
import { extractError } from "discourse/lib/ajax-error";
|
import { extractError } from "discourse/lib/ajax-error";
|
||||||
import { escapeExpression } from "discourse/lib/utilities";
|
import { escapeExpression } from "discourse/lib/utilities";
|
||||||
|
import Category from "discourse/models/category";
|
||||||
import discourseDebounce from "discourse-common/lib/debounce";
|
import discourseDebounce from "discourse-common/lib/debounce";
|
||||||
import I18n from "discourse-i18n";
|
import I18n from "discourse-i18n";
|
||||||
|
|
||||||
@ -67,9 +68,7 @@ export default class ChatModalCreateChannel extends Component {
|
|||||||
|
|
||||||
@action
|
@action
|
||||||
onCategoryChange(categoryId) {
|
onCategoryChange(categoryId) {
|
||||||
const category = categoryId
|
const category = categoryId ? Category.findById(categoryId) : null;
|
||||||
? this.site.categories.findBy("id", categoryId)
|
|
||||||
: null;
|
|
||||||
this.#updatePermissionsHint(category);
|
this.#updatePermissionsHint(category);
|
||||||
|
|
||||||
const name = this.name || category?.name || "";
|
const name = this.name || category?.name || "";
|
||||||
|
Reference in New Issue
Block a user