A platform for community discussion. Free, open, simple.
DEV: Replace Category.findById with Category.asyncFindByIds in easy cases (#26270)
Daniel Waterworthcafdc2980645
diff --git a/app/assets/javascripts/discourse-common/addon/utils/multi-cache.js b/app/assets/javascripts/discourse-common/addon/utils/multi-cache.jsindex 49c25143bc4..7ea72fba5a6 100644--- a/app/assets/javascripts/discourse-common/addon/utils/multi-cache.js+++ b/app/assets/javascripts/discourse-common/addon/utils/multi-cache.js@@ -22,6 +22,7 @@ export class MultiCache {this.fetchTimes = [this.fetchTimes[this.fetchTimes.length - 1], new Date()];const notFound = [];+ ids = ids.uniq();for (const id of ids) {if (!this.values.has(id)) {diff --git a/app/assets/javascripts/discourse/app/components/modal/history.js b/app/assets/javascripts/discourse/app/components/modal/history.jsindex b76d3b20dca..761c96ffa61 100644--- a/app/assets/javascripts/discourse/app/components/modal/history.js+++ b/app/assets/javascripts/discourse/app/components/modal/history.js@@ -190,26 +190,27 @@ export default class History extends Component {);}- revert(post, postVersion) {- post- .revertToRevision(postVersion)- .then((result) => {- this.refresh(post.id, postVersion);- if (result.topic) {- post.set("topic.slug", result.topic.slug);- post.set("topic.title", result.topic.title);- post.set("topic.fancy_title", result.topic.fancy_title);- }- if (result.category_id) {- post.set("topic.category", Category.findById(result.category_id));- }- this.args.closeModal();- })- .catch((e) => {- if (e.jqXHR.responseJSON?.errors?.[0]) {- this.dialog.alert(e.jqXHR.responseJSON.errors[0]);- }- });+ async revert(post, postVersion) {+ try {+ const result = await post.revertToRevision(postVersion);+ this.refresh(post.id, postVersion);+ if (result.topic) {+ post.set("topic.slug", result.topic.slug);+ post.set("topic.title", result.topic.title);+ post.set("topic.fancy_title", result.topic.fancy_title);+ }+ if (result.category_id) {+ post.set(+ "topic.category",+ await Category.asyncFindById(result.category_id)+ );+ }+ this.args.closeModal();+ } catch (e) {+ if (e.jqXHR.responseJSON?.errors?.[0]) {+ this.dialog.alert(e.jqXHR.responseJSON.errors[0]);+ }+ }}get editButtonLabel() {diff --git a/app/assets/javascripts/discourse/app/models/category.js b/app/assets/javascripts/discourse/app/models/category.jsindex 17a1c35bf43..a74e77226bf 100644--- a/app/assets/javascripts/discourse/app/models/category.js+++ b/app/assets/javascripts/discourse/app/models/category.js@@ -157,6 +157,10 @@ export default class Category extends RestModel {return categories;}+ static async asyncFindById(id) {+ return (await Category.asyncFindByIds([id]))[0];+ }+static findBySlugAndParent(slug, parentCategory) {if (this.slugEncoded()) {slug = encodeURI(slug);diff --git a/app/assets/javascripts/discourse/app/models/group.js b/app/assets/javascripts/discourse/app/models/group.jsindex 9ae1df34713..eb5fca7b4d5 100644--- a/app/assets/javascripts/discourse/app/models/group.js+++ b/app/assets/javascripts/discourse/app/models/group.js@@ -450,7 +450,7 @@ export default class Group extends RestModel {});}- findPosts(opts) {+ async findPosts(opts) {opts = opts || {};const type = opts.type || "posts";const data = {};@@ -463,13 +463,16 @@ export default class Group extends RestModel {data.category_id = parseInt(opts.categoryId, 10);}- return ajax(`/groups/${this.name}/${type}.json`, { data }).then((posts) => {- return posts.map((p) => {- p.user = User.create(p.user);- p.topic = Topic.create(p.topic);- p.category = Category.findById(p.category_id);- return EmberObject.create(p);- });+ const posts = await ajax(`/groups/${this.name}/${type}.json`, { data });+ const categories = await Category.asyncFindByIds(+ posts.map((p) => p.category_id)+ );++ return posts.map((p) => {+ p.user = User.create(p.user);+ p.topic = Topic.create(p.topic);+ p.category = categories[p.category_id];+ return EmberObject.create(p);});}