A platform for community discussion. Free, open, simple.
DEV: Add `post-stream-update-from-json` behavior transformer and `TopicList` refactor (#38766)
Sérgio Saquetim7e6ca8762aca
diff --git a/frontend/discourse/app/components/topic-list/list.gjs b/frontend/discourse/app/components/topic-list/list.gjsindex 642d1d1a868..0bcff205d82 100644--- a/frontend/discourse/app/components/topic-list/list.gjs+++ b/frontend/discourse/app/components/topic-list/list.gjs@@ -32,10 +32,18 @@ import ItemViewsCell from "./item/views-cell";export default class TopicList extends Component {@service currentUser;- // eslint-disable-next-line discourse/no-unused-services+@service topicTrackingState; // accessed via `self` variable@service moreTopicsTabs;+ get #transformerContext() {+ return {+ listContext: this.args.listContext,+ category: this.topicTrackingState.filterCategory,+ filter: this.topicTrackingState.filter,+ };+ }+@cachedget columns() {const defaultColumns = new DAG({@@ -92,25 +100,10 @@ export default class TopicList extends Component {item: ItemActivityCell,});- const self = this;- const context = {- get listContext() {- return self.args.listContext;- },-- get category() {- return self.topicTrackingState.get("filterCategory");- },-- get filter() {- return self.topicTrackingState.get("filter");- },- };-return applyMutableValueTransformer("topic-list-columns",defaultColumns,- context+ this.#transformerContext).resolve();}@@ -183,7 +176,7 @@ export default class TopicList extends Component {get additionalClasses() {return applyValueTransformer("topic-list-class", [], {topics: this.args.topics,- listContext: this.args.listContext,+ ...this.#transformerContext,});}diff --git a/frontend/discourse/app/lib/registry/transformers.js b/frontend/discourse/app/lib/registry/transformers.jsindex 9df69ce6fe0..2840334b747 100644--- a/frontend/discourse/app/lib/registry/transformers.js+++ b/frontend/discourse/app/lib/registry/transformers.js@@ -15,6 +15,7 @@ export const BEHAVIOR_TRANSFORMERS = Object.freeze(["full-page-search-load-more","post-menu-toggle-like-action","post-stream-error-loading",+ "post-stream-update-from-json","topic-controller:finished-editing","topic-list-item-click",]);diff --git a/frontend/discourse/app/models/post-stream.js b/frontend/discourse/app/models/post-stream.jsindex d6353f59e24..2e7dac98aa9 100644--- a/frontend/discourse/app/models/post-stream.js+++ b/frontend/discourse/app/models/post-stream.js@@ -1060,23 +1060,29 @@ export default class PostStream extends RestModel {}updateFromJson(postStreamData) {- this.posts.length = 0;- this.gaps = null;-- if (postStreamData) {- // Load posts if present- postStreamData.posts.forEach((p) =>- this.appendPost(this.store.createRecord("post", p))- );- delete postStreamData.posts;-- // Update our attributes- postStreamData.gaps = {- before: new TrackedObject(postStreamData.gaps?.before || {}),- after: new TrackedObject(postStreamData.gaps?.after || {}),- };- this.setProperties(postStreamData);- }+ applyBehaviorTransformer(+ "post-stream-update-from-json",+ () => {+ this.posts.length = 0;+ this.gaps = null;++ if (postStreamData) {+ // Load posts if present+ postStreamData.posts.forEach((p) =>+ this.appendPost(this.store.createRecord("post", p))+ );+ delete postStreamData.posts;++ // Update our attributes+ postStreamData.gaps = {+ before: new TrackedObject(postStreamData.gaps?.before || {}),+ after: new TrackedObject(postStreamData.gaps?.after || {}),+ };+ this.setProperties(postStreamData);+ }+ },+ { postStream: this }+ );}/**diff --git a/frontend/discourse/tests/integration/components/topic-list-test.gjs b/frontend/discourse/tests/integration/components/topic-list-test.gjsindex 4e7d3dfb4f6..9b684472538 100644--- a/frontend/discourse/tests/integration/components/topic-list-test.gjs+++ b/frontend/discourse/tests/integration/components/topic-list-test.gjs@@ -3,6 +3,7 @@ import { click, render } from "@ember/test-helpers";import { module, test } from "qunit";import TopicList from "discourse/components/topic-list/list";import BulkSelectHelper from "discourse/lib/bulk-select-helper";+import { withPluginApi } from "discourse/lib/plugin-api";import { setupRenderingTest } from "discourse/tests/helpers/component-test";module("Integration | Component | topic-list", function (hooks) {@@ -71,4 +72,134 @@ module("Integration | Component | topic-list", function (hooks) {assert.dom(".topic-status .d-icon-thumbtack").exists({ count: 2 });});++ test("topic-list-columns transformer receives context with category and filter", async function (assert) {+ let receivedContext;++ withPluginApi((api) => {+ api.registerValueTransformer(+ "topic-list-columns",+ ({ value, context }) => {+ receivedContext = context;+ return value;+ }+ );+ });++ const store = getOwner(this).lookup("service:store");+ const topicTrackingState = getOwner(this).lookup(+ "service:topic-tracking-state"+ );+ topicTrackingState.filterCategory = { id: 42, name: "test" };+ topicTrackingState.filter = "latest";++ const topics = [store.createRecord("topic", { id: 24234 })];++ await render(+ <template>+ <TopicList @topics={{topics}} @listContext="test-context" />+ </template>+ );++ assert.strictEqual(+ receivedContext.category?.id,+ 42,+ "context includes the filter category"+ );+ assert.strictEqual(+ receivedContext.filter,+ "latest",+ "context includes the filter"+ );+ assert.strictEqual(+ receivedContext.listContext,+ "test-context",+ "context includes the listContext"+ );+ });++ test("topic-list-class transformer receives context with category and filter", async function (assert) {+ let receivedContext;++ withPluginApi((api) => {+ api.registerValueTransformer("topic-list-class", ({ value, context }) => {+ receivedContext = context;+ return value;+ });+ });++ const store = getOwner(this).lookup("service:store");+ const topicTrackingState = getOwner(this).lookup(+ "service:topic-tracking-state"+ );+ topicTrackingState.filterCategory = { id: 99, name: "docs" };+ topicTrackingState.filter = "top";++ const topics = [store.createRecord("topic", { id: 24234 })];++ await render(+ <template>+ <TopicList @topics={{topics}} @listContext="other-context" />+ </template>+ );++ assert.strictEqual(+ receivedContext.category?.id,+ 99,+ "context includes the filter category"+ );+ assert.strictEqual(+ receivedContext.filter,+ "top",+ "context includes the filter"+ );+ assert.strictEqual(+ receivedContext.listContext,+ "other-context",+ "context includes the listContext"+ );+ });++ test("topic-list-columns transformer can remove columns", async function (assert) {+ withPluginApi((api) => {+ api.registerValueTransformer(+ "topic-list-columns",+ ({ value: columns }) => {+ columns.delete("views");+ return columns;+ }+ );+ });++ const store = getOwner(this).lookup("service:store");+ const topics = [store.createRecord("topic", { id: 24234 })];++ await render(<template><TopicList @topics={{topics}} /></template>);++ assert+ .dom(".topic-list th[data-sort-order='views']")+ .doesNotExist("views column is removed");+ assert+ .dom(".topic-list th[data-sort-order='posts']")+ .exists("replies column remains");+ assert+ .dom(".topic-list th[data-sort-order='activity']")+ .exists("activity column remains");+ });++ test("topic-list-class transformer can add classes", async function (assert) {+ withPluginApi((api) => {+ api.registerValueTransformer("topic-list-class", ({ value: classes }) => {+ classes.push("custom-class");+ return classes;+ });+ });++ const store = getOwner(this).lookup("service:store");+ const topics = [store.createRecord("topic", { id: 24234 })];++ await render(<template><TopicList @topics={{topics}} /></template>);++ assert.dom("table.topic-list.custom-class").exists("custom class is added");+ });});diff --git a/frontend/discourse/tests/unit/models/post-stream-test.js b/frontend/discourse/tests/unit/models/post-stream-test.jsindex 3bb3474c5dc..a61f162ff0b 100644--- a/frontend/discourse/tests/unit/models/post-stream-test.js+++ b/frontend/discourse/tests/unit/models/post-stream-test.js@@ -2,6 +2,7 @@ import { getOwner } from "@ember/owner";import { setupTest } from "ember-qunit";import { module, test } from "qunit";import sinon from "sinon";+import { withPluginApi } from "discourse/lib/plugin-api";import Post from "discourse/models/post";import pretender, { response } from "discourse/tests/helpers/create-pretender";@@ -207,6 +208,62 @@ module("Unit | Model | post-stream", function (hooks) {assert.strictEqual(postStream.extra_property, 12);});+ test("updateFromJson - post-stream-update-from-json behavior transformer", function (assert) {+ const postStream = buildStream.call(this, 1231);+ let transformerCalled = false;+ let receivedContext = null;++ withPluginApi((api) => {+ api.registerBehaviorTransformer(+ "post-stream-update-from-json",+ ({ next, context }) => {+ transformerCalled = true;+ receivedContext = context;+ next();+ }+ );+ });++ postStream.updateFromJson({+ posts: [{ id: 1 }],+ stream: [1],+ });++ assert.true(transformerCalled, "the transformer is called");+ assert.strictEqual(+ receivedContext.postStream,+ postStream,+ "the transformer receives the postStream as context"+ );+ assert.strictEqual(postStream.posts.length, 1, "posts are loaded normally");+ });++ test("updateFromJson - behavior transformer can modify stream after default behavior", function (assert) {+ const postStream = buildStream.call(this, 1231);++ withPluginApi((api) => {+ api.registerBehaviorTransformer(+ "post-stream-update-from-json",+ ({ next, context }) => {+ next();+ // Truncate stream to first element after default behavior runs+ context.postStream.stream.length = 1;+ }+ );+ });++ postStream.updateFromJson({+ posts: [{ id: 1 }, { id: 2 }, { id: 3 }],+ stream: [1, 2, 3],+ });++ assert.deepEqual(+ [...postStream.stream],+ [1],+ "the transformer can modify the stream after next()"+ );+ });+test("removePosts", function (assert) {const postStream = buildStream.call(this, 10000001, [1, 2, 3]);const store = getOwner(this).lookup("service:store");