DEV: Convert some simple model methods to async/await (#29594)

This commit is contained in:
Jarek Radosz 2024-11-05 14:59:51 +01:00 committed by GitHub
parent e65367d603
commit d076ed8c77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 80 additions and 88 deletions

View File

@ -7,21 +7,23 @@ export default (type) => {
return I18n.t(`user.messages.${type}`); return I18n.t(`user.messages.${type}`);
} }
model() { async model() {
const groupName = this.modelFor("group").get("name"); const groupName = this.modelFor("group").get("name");
const username = this.currentUser.get("username_lower"); const username = this.currentUser.get("username_lower");
let filter = `topics/private-messages-group/${username}/${groupName}`; let filter = `topics/private-messages-group/${username}/${groupName}`;
if (this._isArchive()) { if (this._isArchive()) {
filter = `${filter}/archive`; filter = `${filter}/archive`;
} }
return this.store.findFiltered("topicList", { filter }).then((model) => {
// andrei: we agreed that this is an anti pattern, const model = await this.store.findFiltered("topicList", { filter });
// it's better to avoid mutating a rest model like this
// this place we'll be refactored later // andrei: we agreed that this is an anti pattern,
// see https://github.com/discourse/discourse/pull/14313#discussion_r708784704 // it's better to avoid mutating a rest model like this
model.set("emptyState", this.emptyState()); // this place we'll be refactored later
return model; // see https://github.com/discourse/discourse/pull/14313#discussion_r708784704
}); model.set("emptyState", this.emptyState());
return model;
} }
setupController() { setupController() {

View File

@ -26,7 +26,7 @@ export default (inboxType, filter) => {
} }
} }
model(params = {}) { async model(params = {}) {
const username = this.modelFor("user").get("username_lower"); const username = this.modelFor("user").get("username_lower");
const groupName = this.modelFor("userPrivateMessages.group").name; const groupName = this.modelFor("userPrivateMessages.group").name;
@ -45,19 +45,17 @@ export default (inboxType, filter) => {
return lastTopicList; return lastTopicList;
} }
return this.store const topicList = await this.store.findFiltered("topicList", {
.findFiltered("topicList", { filter: topicListFilter,
filter: topicListFilter, params,
params, });
})
.then((topicList) => { // andrei: we agreed that this is an anti pattern,
// andrei: we agreed that this is an anti pattern, // it's better to avoid mutating a rest model like this
// it's better to avoid mutating a rest model like this // this place we'll be refactored later
// this place we'll be refactored later // see https://github.com/discourse/discourse/pull/14313#discussion_r708784704
// see https://github.com/discourse/discourse/pull/14313#discussion_r708784704 topicList.set("emptyState", this.emptyState());
topicList.set("emptyState", this.emptyState()); return topicList;
return topicList;
});
} }
afterModel(model) { afterModel(model) {

View File

@ -24,9 +24,10 @@ export default (inboxType, path, filter) => {
]; ];
} }
model(params = {}) { async model(params = {}) {
const topicListFilter = const topicListFilter = `topics/${path}/${this.modelFor("user").get(
"topics/" + path + "/" + this.modelFor("user").get("username_lower"); "username_lower"
)}`;
const lastTopicList = findOrResetCachedTopicList( const lastTopicList = findOrResetCachedTopicList(
this.session, this.session,
@ -37,19 +38,17 @@ export default (inboxType, path, filter) => {
return lastTopicList; return lastTopicList;
} }
return this.store const model = await this.store.findFiltered("topicList", {
.findFiltered("topicList", { filter: topicListFilter,
filter: topicListFilter, params,
params, });
})
.then((model) => { // andrei: we agreed that this is an anti pattern,
// andrei: we agreed that this is an anti pattern, // it's better to avoid mutating a rest model like this
// it's better to avoid mutating a rest model like this // this place we'll be refactored later
// this place we'll be refactored later // see https://github.com/discourse/discourse/pull/14313#discussion_r708784704
// see https://github.com/discourse/discourse/pull/14313#discussion_r708784704 model.set("emptyState", this.emptyState());
model.set("emptyState", this.emptyState()); return model;
return model;
});
} }
setupController() { setupController() {

View File

@ -4,17 +4,17 @@ import I18n from "discourse-i18n";
export default class UserActivityDrafts extends DiscourseRoute { export default class UserActivityDrafts extends DiscourseRoute {
templateName = "user/stream"; templateName = "user/stream";
model() { async model() {
const user = this.modelFor("user"); const user = this.modelFor("user");
const draftsStream = user.get("userDraftsStream"); const draftsStream = user.get("userDraftsStream");
draftsStream.reset(); draftsStream.reset();
return draftsStream.findItems(this.site).then(() => { await draftsStream.findItems(this.site);
return {
stream: draftsStream, return {
emptyState: this.emptyState(), stream: draftsStream,
}; emptyState: this.emptyState(),
}); };
} }
emptyState() { emptyState() {

View File

@ -10,20 +10,16 @@ export default class UserActivityPending extends DiscourseRoute {
this.username = this.modelFor("user").username_lower; this.username = this.modelFor("user").username_lower;
} }
model() { async model() {
return this.store const pendingPosts = await this.store.findAll("pending-post", {
.findAll("pending-post", { username: this.username,
username: this.username, });
})
.then((pendingPosts) => {
for (let pendingPost of pendingPosts.content) {
pendingPost.title = emojiUnescape(
escapeExpression(pendingPost.title)
);
}
return pendingPosts; for (let pendingPost of pendingPosts.content) {
}); pendingPost.title = emojiUnescape(escapeExpression(pendingPost.title));
}
return pendingPosts;
} }
activate() { activate() {

View File

@ -9,20 +9,18 @@ import I18n from "discourse-i18n";
export default class UserActivityRead extends UserTopicListRoute { export default class UserActivityRead extends UserTopicListRoute {
userActionType = UserAction.TYPES.topics; userActionType = UserAction.TYPES.topics;
model(params = {}) { async model(params = {}) {
return this.store const model = await this.store.findFiltered("topicList", {
.findFiltered("topicList", { filter: "read",
filter: "read", params,
params, });
})
.then((model) => { // andrei: we agreed that this is an anti pattern,
// andrei: we agreed that this is an anti pattern, // it's better to avoid mutating a rest model like this
// it's better to avoid mutating a rest model like this // this place we'll be refactored later
// this place we'll be refactored later // see https://github.com/discourse/discourse/pull/14313#discussion_r708784704
// see https://github.com/discourse/discourse/pull/14313#discussion_r708784704 model.set("emptyState", this.emptyState());
model.set("emptyState", this.emptyState()); return model;
return model;
});
} }
emptyState() { emptyState() {

View File

@ -8,21 +8,20 @@ import I18n from "discourse-i18n";
export default class UserActivityTopics extends UserTopicListRoute { export default class UserActivityTopics extends UserTopicListRoute {
userActionType = UserAction.TYPES.topics; userActionType = UserAction.TYPES.topics;
model(params = {}) { async model(params = {}) {
return this.store const model = await this.store.findFiltered("topicList", {
.findFiltered("topicList", { filter: `topics/created-by/${this.modelFor("user").get(
filter: "username_lower"
"topics/created-by/" + this.modelFor("user").get("username_lower"), )}`,
params, params,
}) });
.then((model) => {
// andrei: we agreed that this is an anti pattern, // andrei: we agreed that this is an anti pattern,
// it's better to avoid mutating a rest model like this // it's better to avoid mutating a rest model like this
// this place we'll be refactored later // this place we'll be refactored later
// see https://github.com/discourse/discourse/pull/14313#discussion_r708784704 // see https://github.com/discourse/discourse/pull/14313#discussion_r708784704
model.set("emptyState", this.emptyState()); model.set("emptyState", this.emptyState());
return model; return model;
});
} }
emptyState() { emptyState() {