DEV: Add model transformation hooks in more locations (#24547)

Motivation is https://github.com/discourse/discourse-encrypt/pull/294
This commit is contained in:
David Taylor 2024-03-08 13:28:41 +00:00 committed by GitHub
parent 21a7ebf1bc
commit 766e0f7b36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 37 additions and 11 deletions

View File

@ -1,6 +1,7 @@
import RestAdapter from "discourse/adapters/rest";
import { ajax } from "discourse/lib/ajax";
import PreloadStore from "discourse/lib/preload-store";
import Topic from "discourse/models/topic";
export default RestAdapter.extend({
find(store, type, { filter, params }) {
@ -38,4 +39,10 @@ export default RestAdapter.extend({
return result;
});
},
async applyTransformations(results) {
for (const topicList of results) {
await Topic.applyTransformations(topicList.topics);
}
},
});

View File

@ -96,7 +96,7 @@ export default Controller.extend({
this.set("permissionDenied", true);
},
_processLoadResponse(searchTerm, response) {
async _processLoadResponse(searchTerm, response) {
if (!response || !response.user_bookmark_list) {
this.model.loadMoreUrl = null;
return;
@ -108,6 +108,7 @@ export default Controller.extend({
if (response.bookmarks) {
const bookmarkModels = response.bookmarks.map(this.transform);
await Bookmark.applyTransformations(bookmarkModels);
this.model.bookmarks.pushObjects(bookmarkModels);
this.session.set("bookmarksModel", this.model);
}

View File

@ -10,6 +10,7 @@ import Site from "discourse/models/site";
import User from "discourse/models/user";
import deprecated from "discourse-common/lib/deprecated";
import { getOwnerWithFallback } from "discourse-common/lib/get-owner";
import Topic from "./topic";
function extractByKey(collection, klass) {
const retval = {};
@ -190,6 +191,7 @@ export default class TopicList extends RestModel {
if (result) {
// the new topics loaded from the server
const newTopics = TopicList.topicsFrom(this.store, result);
Topic.applyTransformations(newTopics);
this.forEachNew(newTopics, (t) => {
t.set("highlight", topicsAdded++ === 0);

View File

@ -35,9 +35,10 @@ export function loadTopicView(topic, args) {
return PreloadStore.getAndRemove(`topic_${topic.id}`, () =>
ajax(jsonUrl, { data })
).then((json) => {
).then(async (json) => {
json.categories?.forEach((c) => topic.site.updateCategory(c));
topic.updateFromJson(json);
await Topic.applyTransformations([topic]);
return json;
});
}

View File

@ -3,6 +3,7 @@ import { service } from "@ember/service";
import $ from "jquery";
import { Promise } from "rsvp";
import { ajax } from "discourse/lib/ajax";
import Bookmark from "discourse/models/bookmark";
import DiscourseRoute from "discourse/routes/discourse";
import I18n from "discourse-i18n";
@ -33,7 +34,7 @@ export default DiscourseRoute.extend({
controller.set("loading", true);
return this._loadBookmarks(params)
.then((response) => {
.then(async (response) => {
if (!response.user_bookmark_list) {
return { bookmarks: [] };
}
@ -41,6 +42,7 @@ export default DiscourseRoute.extend({
const bookmarks = response.user_bookmark_list.bookmarks.map(
controller.transform
);
await Bookmark.applyTransformations(bookmarks);
const loadMoreUrl = response.user_bookmark_list.more_bookmarks_url;
const model = { bookmarks, loadMoreUrl };

View File

@ -1,4 +1,4 @@
import { schedule } from "@ember/runloop";
import { next, schedule } from "@ember/runloop";
import Service, { service } from "@ember/service";
import { disableImplicitInjections } from "discourse/lib/implicit-injections";
import { isTesting } from "discourse-common/config/environment";
@ -43,9 +43,12 @@ export default class RouteScrollManager extends Service {
}
const scrollLocation = this.historyStore.get(STORE_KEY) || [0, 0];
schedule("afterRender", () => {
this.scrollElement.scrollTo(...scrollLocation);
});
next(() =>
schedule("afterRender", () =>
this.scrollElement.scrollTo(...scrollLocation)
)
);
}
#shouldScroll(routeInfo) {

View File

@ -74,7 +74,7 @@ export default class StoreService extends Service {
const adapter = this.adapterFor(type);
let store = this;
return adapter.findAll(this, type, findArgs).then((result) => {
return adapter.findAll(this, type, findArgs).then(async (result) => {
let results = this._resultSet(type, result);
if (adapter.afterFindAll) {
results = adapter.afterFindAll(results, {
@ -83,15 +83,21 @@ export default class StoreService extends Service {
},
});
}
await adapter.applyTransformations?.([result]);
return results;
});
}
// Mostly for legacy, things like TopicList without ResultSets
findFiltered(type, findArgs) {
return this.adapterFor(type)
const adapter = this.adapterFor(type);
return adapter
.find(this, type, findArgs)
.then((result) => this._build(type, result));
.then((result) => this._build(type, result))
.then(async (result) => {
await adapter.applyTransformations?.([result]);
return result;
});
}
_hydrateFindResults(result, type, findArgs) {

View File

@ -1,5 +1,5 @@
import { getOwner } from "@ember/application";
import { click, visit } from "@ember/test-helpers";
import { click, settled, visit } from "@ember/test-helpers";
import { test } from "qunit";
import { setDefaultHomepage } from "discourse/lib/utilities";
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
@ -27,9 +27,11 @@ acceptance("Dynamic homepage handling", function () {
assertOnLatest("/");
await router.transitionTo("/").followRedirects();
await settled();
assertOnLatest("/");
await router.transitionTo("discovery.index").followRedirects();
await settled();
assertOnLatest("/");
await click(".nav-item_latest a");
@ -66,9 +68,11 @@ acceptance("Dynamic homepage handling", function () {
assertOnCategories("/");
await router.transitionTo("/").followRedirects();
await settled();
assertOnCategories("/");
await router.transitionTo("discovery.index").followRedirects();
await settled();
assertOnCategories("/");
await click(".nav-item_categories a");