A platform for community discussion. Free, open, simple.
DEV: Convert some simple model methods to async/await (#29594)
Jarek Radoszd076ed8c77c2
diff --git a/app/assets/javascripts/discourse/app/routes/build-group-messages-route.js b/app/assets/javascripts/discourse/app/routes/build-group-messages-route.jsindex cfa8333e6ac..a9bf510efc5 100644--- a/app/assets/javascripts/discourse/app/routes/build-group-messages-route.js+++ b/app/assets/javascripts/discourse/app/routes/build-group-messages-route.js@@ -7,21 +7,23 @@ export default (type) => {return I18n.t(`user.messages.${type}`);}- model() {+ async model() {const groupName = this.modelFor("group").get("name");const username = this.currentUser.get("username_lower");+let filter = `topics/private-messages-group/${username}/${groupName}`;if (this._isArchive()) {filter = `${filter}/archive`;}- return this.store.findFiltered("topicList", { filter }).then((model) => {- // andrei: we agreed that this is an anti pattern,- // it's better to avoid mutating a rest model like this- // this place we'll be refactored later- // see https://github.com/discourse/discourse/pull/14313#discussion_r708784704- model.set("emptyState", this.emptyState());- return model;- });++ const model = await this.store.findFiltered("topicList", { filter });++ // andrei: we agreed that this is an anti pattern,+ // it's better to avoid mutating a rest model like this+ // this place we'll be refactored later+ // see https://github.com/discourse/discourse/pull/14313#discussion_r708784704+ model.set("emptyState", this.emptyState());+ return model;}setupController() {diff --git a/app/assets/javascripts/discourse/app/routes/build-private-messages-group-route.js b/app/assets/javascripts/discourse/app/routes/build-private-messages-group-route.jsindex d141f655fdc..eb90fdb79a8 100644--- a/app/assets/javascripts/discourse/app/routes/build-private-messages-group-route.js+++ b/app/assets/javascripts/discourse/app/routes/build-private-messages-group-route.js@@ -26,7 +26,7 @@ export default (inboxType, filter) => {}}- model(params = {}) {+ async model(params = {}) {const username = this.modelFor("user").get("username_lower");const groupName = this.modelFor("userPrivateMessages.group").name;@@ -45,19 +45,17 @@ export default (inboxType, filter) => {return lastTopicList;}- return this.store- .findFiltered("topicList", {- filter: topicListFilter,- params,- })- .then((topicList) => {- // andrei: we agreed that this is an anti pattern,- // it's better to avoid mutating a rest model like this- // this place we'll be refactored later- // see https://github.com/discourse/discourse/pull/14313#discussion_r708784704- topicList.set("emptyState", this.emptyState());- return topicList;- });+ const topicList = await this.store.findFiltered("topicList", {+ filter: topicListFilter,+ params,+ });++ // andrei: we agreed that this is an anti pattern,+ // it's better to avoid mutating a rest model like this+ // this place we'll be refactored later+ // see https://github.com/discourse/discourse/pull/14313#discussion_r708784704+ topicList.set("emptyState", this.emptyState());+ return topicList;}afterModel(model) {diff --git a/app/assets/javascripts/discourse/app/routes/build-private-messages-route.js b/app/assets/javascripts/discourse/app/routes/build-private-messages-route.jsindex 8e512282589..c8fa983a1bb 100644--- a/app/assets/javascripts/discourse/app/routes/build-private-messages-route.js+++ b/app/assets/javascripts/discourse/app/routes/build-private-messages-route.js@@ -24,9 +24,10 @@ export default (inboxType, path, filter) => {];}- model(params = {}) {- const topicListFilter =- "topics/" + path + "/" + this.modelFor("user").get("username_lower");+ async model(params = {}) {+ const topicListFilter = `topics/${path}/${this.modelFor("user").get(+ "username_lower"+ )}`;const lastTopicList = findOrResetCachedTopicList(this.session,@@ -37,19 +38,17 @@ export default (inboxType, path, filter) => {return lastTopicList;}- return this.store- .findFiltered("topicList", {- filter: topicListFilter,- params,- })- .then((model) => {- // andrei: we agreed that this is an anti pattern,- // it's better to avoid mutating a rest model like this- // this place we'll be refactored later- // see https://github.com/discourse/discourse/pull/14313#discussion_r708784704- model.set("emptyState", this.emptyState());- return model;- });+ const model = await this.store.findFiltered("topicList", {+ filter: topicListFilter,+ params,+ });++ // andrei: we agreed that this is an anti pattern,+ // it's better to avoid mutating a rest model like this+ // this place we'll be refactored later+ // see https://github.com/discourse/discourse/pull/14313#discussion_r708784704+ model.set("emptyState", this.emptyState());+ return model;}setupController() {diff --git a/app/assets/javascripts/discourse/app/routes/user-activity-drafts.js b/app/assets/javascripts/discourse/app/routes/user-activity-drafts.jsindex b83d1ae48da..195bd92dd5f 100644--- a/app/assets/javascripts/discourse/app/routes/user-activity-drafts.js+++ b/app/assets/javascripts/discourse/app/routes/user-activity-drafts.js@@ -4,17 +4,17 @@ import I18n from "discourse-i18n";export default class UserActivityDrafts extends DiscourseRoute {templateName = "user/stream";- model() {+ async model() {const user = this.modelFor("user");const draftsStream = user.get("userDraftsStream");draftsStream.reset();- return draftsStream.findItems(this.site).then(() => {- return {- stream: draftsStream,- emptyState: this.emptyState(),- };- });+ await draftsStream.findItems(this.site);++ return {+ stream: draftsStream,+ emptyState: this.emptyState(),+ };}emptyState() {diff --git a/app/assets/javascripts/discourse/app/routes/user-activity-pending.js b/app/assets/javascripts/discourse/app/routes/user-activity-pending.jsindex aca95f46f9b..e064a84ff59 100644--- a/app/assets/javascripts/discourse/app/routes/user-activity-pending.js+++ b/app/assets/javascripts/discourse/app/routes/user-activity-pending.js@@ -10,20 +10,16 @@ export default class UserActivityPending extends DiscourseRoute {this.username = this.modelFor("user").username_lower;}- model() {- return this.store- .findAll("pending-post", {- username: this.username,- })- .then((pendingPosts) => {- for (let pendingPost of pendingPosts.content) {- pendingPost.title = emojiUnescape(- escapeExpression(pendingPost.title)- );- }-- return pendingPosts;- });+ async model() {+ const pendingPosts = await this.store.findAll("pending-post", {+ username: this.username,+ });++ for (let pendingPost of pendingPosts.content) {+ pendingPost.title = emojiUnescape(escapeExpression(pendingPost.title));+ }++ return pendingPosts;}activate() {diff --git a/app/assets/javascripts/discourse/app/routes/user-activity-read.js b/app/assets/javascripts/discourse/app/routes/user-activity-read.jsindex cb10e258a7a..342f4fd4299 100644--- a/app/assets/javascripts/discourse/app/routes/user-activity-read.js+++ b/app/assets/javascripts/discourse/app/routes/user-activity-read.js@@ -9,20 +9,18 @@ import I18n from "discourse-i18n";export default class UserActivityRead extends UserTopicListRoute {userActionType = UserAction.TYPES.topics;- model(params = {}) {- return this.store- .findFiltered("topicList", {- filter: "read",- params,- })- .then((model) => {- // andrei: we agreed that this is an anti pattern,- // it's better to avoid mutating a rest model like this- // this place we'll be refactored later- // see https://github.com/discourse/discourse/pull/14313#discussion_r708784704- model.set("emptyState", this.emptyState());- return model;- });+ async model(params = {}) {+ const model = await this.store.findFiltered("topicList", {+ filter: "read",+ params,+ });++ // andrei: we agreed that this is an anti pattern,+ // it's better to avoid mutating a rest model like this+ // this place we'll be refactored later+ // see https://github.com/discourse/discourse/pull/14313#discussion_r708784704+ model.set("emptyState", this.emptyState());+ return model;}emptyState() {diff --git a/app/assets/javascripts/discourse/app/routes/user-activity-topics.js b/app/assets/javascripts/discourse/app/routes/user-activity-topics.jsindex 32722ca0374..ea50e143ecc 100644--- a/app/assets/javascripts/discourse/app/routes/user-activity-topics.js+++ b/app/assets/javascripts/discourse/app/routes/user-activity-topics.js@@ -8,21 +8,20 @@ import I18n from "discourse-i18n";export default class UserActivityTopics extends UserTopicListRoute {userActionType = UserAction.TYPES.topics;- model(params = {}) {- return this.store- .findFiltered("topicList", {- filter:- "topics/created-by/" + this.modelFor("user").get("username_lower"),- params,- })- .then((model) => {- // andrei: we agreed that this is an anti pattern,- // it's better to avoid mutating a rest model like this- // this place we'll be refactored later- // see https://github.com/discourse/discourse/pull/14313#discussion_r708784704- model.set("emptyState", this.emptyState());- return model;- });+ async model(params = {}) {+ const model = await this.store.findFiltered("topicList", {+ filter: `topics/created-by/${this.modelFor("user").get(+ "username_lower"+ )}`,+ params,+ });++ // andrei: we agreed that this is an anti pattern,+ // it's better to avoid mutating a rest model like this+ // this place we'll be refactored later+ // see https://github.com/discourse/discourse/pull/14313#discussion_r708784704+ model.set("emptyState", this.emptyState());+ return model;}emptyState() {