From 766e0f7b364c09e383528e3771746aabebaa1d30 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 8 Mar 2024 13:28:41 +0000 Subject: [PATCH] DEV: Add model transformation hooks in more locations (#24547) Motivation is https://github.com/discourse/discourse-encrypt/pull/294 --- .../javascripts/discourse/app/adapters/topic-list.js | 7 +++++++ .../app/controllers/user-activity-bookmarks.js | 3 ++- .../javascripts/discourse/app/models/topic-list.js | 2 ++ app/assets/javascripts/discourse/app/models/topic.js | 3 ++- .../discourse/app/routes/user-activity-bookmarks.js | 4 +++- .../discourse/app/services/route-scroll-manager.js | 11 +++++++---- .../javascripts/discourse/app/services/store.js | 12 +++++++++--- .../discourse/tests/acceptance/homepage-test.js | 6 +++++- 8 files changed, 37 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/discourse/app/adapters/topic-list.js b/app/assets/javascripts/discourse/app/adapters/topic-list.js index 5975ece8a44..3ba5e116cf8 100644 --- a/app/assets/javascripts/discourse/app/adapters/topic-list.js +++ b/app/assets/javascripts/discourse/app/adapters/topic-list.js @@ -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); + } + }, }); diff --git a/app/assets/javascripts/discourse/app/controllers/user-activity-bookmarks.js b/app/assets/javascripts/discourse/app/controllers/user-activity-bookmarks.js index 09a6b5b552d..afcfec005c8 100644 --- a/app/assets/javascripts/discourse/app/controllers/user-activity-bookmarks.js +++ b/app/assets/javascripts/discourse/app/controllers/user-activity-bookmarks.js @@ -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); } diff --git a/app/assets/javascripts/discourse/app/models/topic-list.js b/app/assets/javascripts/discourse/app/models/topic-list.js index b15d6efc941..f649b38223f 100644 --- a/app/assets/javascripts/discourse/app/models/topic-list.js +++ b/app/assets/javascripts/discourse/app/models/topic-list.js @@ -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); diff --git a/app/assets/javascripts/discourse/app/models/topic.js b/app/assets/javascripts/discourse/app/models/topic.js index 224baf27022..f82a439aa3c 100644 --- a/app/assets/javascripts/discourse/app/models/topic.js +++ b/app/assets/javascripts/discourse/app/models/topic.js @@ -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; }); } diff --git a/app/assets/javascripts/discourse/app/routes/user-activity-bookmarks.js b/app/assets/javascripts/discourse/app/routes/user-activity-bookmarks.js index 17e0a8f60c2..3c696a8f7a3 100644 --- a/app/assets/javascripts/discourse/app/routes/user-activity-bookmarks.js +++ b/app/assets/javascripts/discourse/app/routes/user-activity-bookmarks.js @@ -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 }; diff --git a/app/assets/javascripts/discourse/app/services/route-scroll-manager.js b/app/assets/javascripts/discourse/app/services/route-scroll-manager.js index 9f0135763c1..e20233e9a15 100644 --- a/app/assets/javascripts/discourse/app/services/route-scroll-manager.js +++ b/app/assets/javascripts/discourse/app/services/route-scroll-manager.js @@ -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) { diff --git a/app/assets/javascripts/discourse/app/services/store.js b/app/assets/javascripts/discourse/app/services/store.js index 45a63029a3a..1b7f3c828cf 100644 --- a/app/assets/javascripts/discourse/app/services/store.js +++ b/app/assets/javascripts/discourse/app/services/store.js @@ -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) { diff --git a/app/assets/javascripts/discourse/tests/acceptance/homepage-test.js b/app/assets/javascripts/discourse/tests/acceptance/homepage-test.js index 621f1642e2a..8c9f5a7ac4e 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/homepage-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/homepage-test.js @@ -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");