A platform for community discussion. Free, open, simple.
DEV: Introduce DAG API for post metadata infos (#33354)
Sérgio Saquetim0f2996a3b3c8
diff --git a/app/assets/javascripts/discourse/app/components/post/meta-data.gjs b/app/assets/javascripts/discourse/app/components/post/meta-data.gjsindex efba4ae0c69..ea606812ad5 100644--- a/app/assets/javascripts/discourse/app/components/post/meta-data.gjs+++ b/app/assets/javascripts/discourse/app/components/post/meta-data.gjs@@ -1,6 +1,8 @@import Component from "@glimmer/component";+import { cached } from "@glimmer/tracking";import { getOwner } from "@ember/owner";-import { service } from "@ember/service";+import DAG from "discourse/lib/dag";+import { applyMutableValueTransformer } from "discourse/lib/transformer";import PostMetaDataDate from "./meta-data/date";import PostMetaDataEditsIndicator from "./meta-data/edits-indicator";import PostMetaDataEmailIndicator from "./meta-data/email-indicator";@@ -12,82 +14,114 @@ import PostMetaDataReplyToTab from "./meta-data/reply-to-tab";import PostMetaDataSelectPost from "./meta-data/select-post";import PostMetaDataWhisperIndicator from "./meta-data/whisper-indicator";-export default class PostMetaData extends Component {- @service currentUser;+const metaDataInfoKeys = Object.freeze({+ WHISPER_INDICATOR: "whisper_indicator",+ EMAIL_INDICATOR: "email_indicator",+ LOCKED_INDICATOR: "locked_indicator",+ EDITS_INDICATOR: "edits_indicator",+ SELECT_POST: "select_post",+ REPLY_TO_TAB: "reply_to_tab",+ LANGUAGE: "language",+ DATE: "date",+ READ_INDICATOR: "read_indicator",+});- get displayPosterName() {- return this.args.displayPosterName ?? true;- }+const INFO_DEFINITIONS = {+ [metaDataInfoKeys.WHISPER_INDICATOR]: {+ Component: PostMetaDataWhisperIndicator,+ shouldRender: (args) => args.post.isWhisper,+ },+ [metaDataInfoKeys.EMAIL_INDICATOR]: {+ Component: PostMetaDataEmailIndicator,+ shouldRender: (args) => args.post.via_email,+ },+ [metaDataInfoKeys.LOCKED_INDICATOR]: {+ Component: PostMetaDataLockedIndicator,+ shouldRender: (args) => args.post.locked,+ },+ [metaDataInfoKeys.EDITS_INDICATOR]: {+ Component: PostMetaDataEditsIndicator,+ shouldRender: (args) => args.post.version > 1 || args.post.wiki,+ },+ [metaDataInfoKeys.SELECT_POST]: {+ Component: PostMetaDataSelectPost,+ shouldRender: (args) => args.multiSelect,+ },+ [metaDataInfoKeys.REPLY_TO_TAB]: {+ Component: PostMetaDataReplyToTab,+ shouldRender: (args, owner) =>+ PostMetaDataReplyToTab.shouldRender(args, null, owner),+ },+ [metaDataInfoKeys.LANGUAGE]: {+ Component: PostMetaDataLanguage,+ shouldRender: (args) => args.post.is_localized && args.post.language,+ },+ [metaDataInfoKeys.DATE]: {+ Component: PostMetaDataDate,+ shouldRender: () => true,+ },+ [metaDataInfoKeys.READ_INDICATOR]: {+ Component: PostMetaDataReadIndicator,+ shouldRender: () => true,+ },+};++const INFO_COMPONENTS = Array.from(Object.entries(INFO_DEFINITIONS)).map(+ ([key, { Component: InfoComponent }]) => [key, InfoComponent]+);- get shouldDisplayEditsIndicator() {- return this.args.post.version > 1 || this.args.post.wiki;+export default class PostMetaData extends Component {+ @cached+ get availableInfoComponents() {+ return this.#infoComponentsDag+ .resolve()+ .filter(({ key }) => {+ const shouldRender = INFO_DEFINITIONS[key]?.shouldRender;+ return shouldRender ? shouldRender(this.args, getOwner(this)) : true;+ })+ .map(({ key, value: InfoComponent }) => ({ key, InfoComponent }));}- get shouldDisplayReplyToTab() {- return PostMetaDataReplyToTab.shouldRender(this.args, null, getOwner(this));+ get shouldDisplayPosterName() {+ return this.args.displayPosterName ?? true;}- get shouldDisplayLanguage() {- return this.args.post.is_localized && this.args.post.language;+ // The metadata components are managed in a Directed Acyclic Graph (DAG)+ // to allow plugins to modify the list (e.g., add new items or reorder).+ get #infoComponentsDag() {+ return applyMutableValueTransformer(+ "post-meta-data-infos",+ DAG.from(INFO_COMPONENTS, {+ throwErrorOnCycle: false,+ }),+ { post: this.args.post, metaDataInfoKeys }+ );}<template><div class="topic-meta-data" role="heading" aria-level="2">- {{#if this.displayPosterName}}+ {{#if this.shouldDisplayPosterName}}<PostMetaDataPosterName @post={{@post}} />{{/if}}<div class="post-infos">- {{#if @post.isWhisper}}- <PostMetaDataWhisperIndicator @post={{@post}} />- {{/if}}-- {{#if @post.via_email}}- <PostMetaDataEmailIndicator- @post={{@post}}- @showRawEmail={{@showRawEmail}}- />- {{/if}}-- {{#if @post.locked}}- <PostMetaDataLockedIndicator @post={{@post}} />- {{/if}}-- {{#if this.shouldDisplayEditsIndicator}}- <PostMetaDataEditsIndicator+ {{! do not include PluginOutlets here, use the DAG API instead }}+ {{#each this.availableInfoComponents key="key" as |item|}}+ <item.InfoComponent@post={{@post}}@editPost={{@editPost}}- @showHistory={{@showHistory}}- />- {{/if}}-- {{#if @multiSelect}}- <PostMetaDataSelectPost- @post={{@post}}- @selected={{@selected}}- @selectReplies={{@selectReplies}}- @selectBelow={{@selectBelow}}- @togglePostSelection={{@togglePostSelection}}- />- {{/if}}-- {{#if this.shouldDisplayReplyToTab}}- <PostMetaDataReplyToTab- @post={{@post}}@hasRepliesAbove={{@hasRepliesAbove}}@isReplyingDirectlyToPostAbove={{@isReplyingDirectlyToPostAbove}}@repliesAbove={{@repliesAbove}}+ @selectBelow={{@selectBelow}}+ @selectReplies={{@selectReplies}}+ @selected={{@selected}}+ @showHistory={{@showHistory}}+ @showRawEmail={{@showRawEmail}}+ @togglePostSelection={{@togglePostSelection}}@toggleReplyAbove={{@toggleReplyAbove}}/>- {{/if}}-- {{#if this.shouldDisplayLanguage}}- <PostMetaDataLanguage @post={{@post}} />- {{/if}}-- <PostMetaDataDate @post={{@post}} />-- <PostMetaDataReadIndicator @post={{@post}} />+ {{/each}}</div></div></template>diff --git a/app/assets/javascripts/discourse/app/components/post/meta-data/language.gjs b/app/assets/javascripts/discourse/app/components/post/meta-data/language.gjsindex be2b69227cf..a4691facd7e 100644--- a/app/assets/javascripts/discourse/app/components/post/meta-data/language.gjs+++ b/app/assets/javascripts/discourse/app/components/post/meta-data/language.gjs@@ -3,7 +3,7 @@ import { i18n } from "discourse-i18n";import DTooltip from "float-kit/components/d-tooltip";export default class PostMetaDataLanguage extends Component {- // once we switch to glimmer, we can remove `this.args.data.x` from the following 2 getters+ // TODO (glimmer-post-stream) once we switch to glimmer, we can remove `this.args.data.x` from the following 2 gettersget language() {return this.args.data?.language || this.args.post?.language;diff --git a/app/assets/javascripts/discourse/app/lib/transformer/registry.js b/app/assets/javascripts/discourse/app/lib/transformer/registry.jsindex b6a977452c5..f464ee3d30d 100644--- a/app/assets/javascripts/discourse/app/lib/transformer/registry.js+++ b/app/assets/javascripts/discourse/app/lib/transformer/registry.js@@ -47,6 +47,7 @@ export const VALUE_TRANSFORMERS = Object.freeze(["post-flag-title","post-menu-buttons","post-menu-collapsed",+ "post-meta-data-infos","post-meta-data-poster-name-suppress-similar-name","post-notice-component","post-show-topic-map",diff --git a/app/assets/javascripts/discourse/tests/integration/components/post/meta-data-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/post/meta-data-test.gjsnew file mode 100644index 00000000000..fc6be593a14--- /dev/null+++ b/app/assets/javascripts/discourse/tests/integration/components/post/meta-data-test.gjs@@ -0,0 +1,67 @@+import { getOwner } from "@ember/owner";+import { render } from "@ember/test-helpers";+import { module, test } from "qunit";+import PostMetaData from "discourse/components/post/meta-data";+import { withPluginApi } from "discourse/lib/plugin-api";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";++function renderComponent(post) {+ return render(<template><PostMetaData @post={{post}} /></template>);+}++module("Integration | Component | Post | PostMetaData", function (hooks) {+ setupRenderingTest(hooks);++ hooks.beforeEach(function () {+ this.siteSettings.glimmer_post_stream_mode = "enabled";++ this.store = getOwner(this).lookup("service:store");+ const topic = this.store.createRecord("topic", { id: 1 });+ const post = this.store.createRecord("post", {+ id: 123,+ post_number: 1,+ topic,+ created_at: "2025-06-01T00:31:24.008Z",+ like_count: 3,+ actions_summary: [{ id: 2, count: 1, hidden: false, can_act: true }],+ });++ this.post = post;+ });++ test("post-meta-data-infos value transformer (DAG API)", async function (assert) {+ this.post.username = "eviltrout";+ this.post.user = this.store.createRecord("user", {+ username: "eviltrout",+ });+ withPluginApi((api) => {+ const TestComponent = <template>+ <div class="post-info post-metadata-test"></div>+ </template>;++ api.registerValueTransformer(+ "post-meta-data-infos",+ ({ value: metadata, context: { metaDataInfoKeys } }) => {+ metadata.add("activity-pub-indicator", TestComponent, {+ before: metaDataInfoKeys.DATE,+ after: metaDataInfoKeys.REPLY_TO_TAB,+ });+ }+ );+ });++ await renderComponent(this.post);++ assert+ .dom("div.post-info.post-date")+ .exists("the date metadata is present");+ assert+ .dom("div.post-info.post-metadata-test")+ .exists("the test component is present");+ assert+ .dom(+ "div.post-info.post-metadata-test:nth-child(1) + div.post-info.post-date:nth-child(2)"+ )+ .exists("the expected order is correct");+ });+});