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

View File

@ -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) {

View File

@ -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() {

View File

@ -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() {

View File

@ -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)
);
}
async model() {
const pendingPosts = await this.store.findAll("pending-post", {
username: this.username,
});
return pendingPosts;
});
for (let pendingPost of pendingPosts.content) {
pendingPost.title = emojiUnescape(escapeExpression(pendingPost.title));
}
return pendingPosts;
}
activate() {

View File

@ -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() {

View File

@ -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() {