From a85cb9bee7917f82536226eea310b459bbc58d5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Saquetim?= <1108771+megothss@users.noreply.github.com> Date: Wed, 18 Dec 2024 16:36:31 -0300 Subject: [PATCH] DEV: Deprecate `api.includePostAttributes` in favor of `api.addTrackedPostProperties` (#30345) --- .../discourse/app/lib/plugin-api.gjs | 28 +++++++++++++++---- .../discourse/app/lib/transform-post.js | 4 +-- .../discourse/tests/unit/models/post-test.js | 8 +++++- docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md | 5 ++++ 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.gjs b/app/assets/javascripts/discourse/app/lib/plugin-api.gjs index 774b755570e..7a2497b4ac2 100644 --- a/app/assets/javascripts/discourse/app/lib/plugin-api.gjs +++ b/app/assets/javascripts/discourse/app/lib/plugin-api.gjs @@ -3,7 +3,7 @@ // docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md whenever you change the version // using the format described at https://keepachangelog.com/en/1.0.0/. -export const PLUGIN_API_VERSION = "1.39.0"; +export const PLUGIN_API_VERSION = "1.39.1"; import $ from "jquery"; import { h } from "virtual-dom"; @@ -823,18 +823,34 @@ class PluginApi { * **/ includePostAttributes(...attributes) { + deprecated( + "`api.includePostAttributes` has been deprecated. Use api.addTrackedPostProperties instead.", + { + id: "discourse.api.include-post-attributes", + since: "v3.4.0.beta3-dev", + dropFrom: "v3.5.0", + } + ); + includeAttributes(...attributes); } /** - * Adds a tracked property to the post model. + * Adds tracked properties to the post model. * - * This method is used to mark a property as tracked for post updates. + * This method is used to mark properties as tracked for post updates. * - * @param {string} name - The name of the property to track. + * It will also add the properties to the list of Post's attributes passed to + * widgets. + * + * You'll need to do this if you've added properties to a Post and want to use + * them when you're rendering. + * + * @param {...string} names - The names of the properties to be tracked. */ - addTrackedPostProperty(name) { - _addTrackedPostProperty(name); + addTrackedPostProperties(...names) { + names.forEach((name) => _addTrackedPostProperty(name)); + includeAttributes(...names); // compatibility with widget's attributes } /** diff --git a/app/assets/javascripts/discourse/app/lib/transform-post.js b/app/assets/javascripts/discourse/app/lib/transform-post.js index 82a2b3e3d94..fc75259c34e 100644 --- a/app/assets/javascripts/discourse/app/lib/transform-post.js +++ b/app/assets/javascripts/discourse/app/lib/transform-post.js @@ -4,10 +4,10 @@ import Badge from "discourse/models/badge"; import getURL from "discourse-common/lib/get-url"; import { i18n } from "discourse-i18n"; -const _additionalAttributes = []; +const _additionalAttributes = new Set(); export function includeAttributes(...attributes) { - attributes.forEach((a) => _additionalAttributes.push(a)); + attributes.forEach((a) => _additionalAttributes.add(a)); } export function transformBasicPost(post) { diff --git a/app/assets/javascripts/discourse/tests/unit/models/post-test.js b/app/assets/javascripts/discourse/tests/unit/models/post-test.js index 0a44de6e418..ab5993ca65e 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/post-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/post-test.js @@ -34,7 +34,7 @@ module("Unit | Model | post", function (hooks) { test("updateFromPost", function (assert) { withPluginApi("1.39.0", (api) => { - api.addTrackedPostProperty("plugin_property"); + api.addTrackedPostProperties("plugin_property", "other_plugin_property"); }); const post = this.store.createRecord("post", { @@ -55,6 +55,7 @@ module("Unit | Model | post", function (hooks) { yours: false, likeAction: { count: 1 }, plugin_property: "different plugin value", + other_plugin_property: "other different plugin value", }) ); @@ -69,6 +70,11 @@ module("Unit | Model | post", function (hooks) { "different plugin value", "`plugin_property` field was updated" ); + assert.strictEqual( + post.other_plugin_property, + "other different plugin value", + "`other_plugin_property` field was updated" + ); }); test("destroy by staff", async function (assert) { diff --git a/docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md b/docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md index a04b66aeff4..1f720332d46 100644 --- a/docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md +++ b/docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md @@ -7,6 +7,11 @@ in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.39.1] - 2024-12-18 + +- Renamed `addTrackedPostProperty` to `addTrackedPostProperties` to allow plugins/TCs to add multiple new tracked properties to the post model. +- Deprecated `includePostAttributes` in favor of `addTrackedPostProperties`. + ## [1.39.0] - 2024-11-27 - Added `addTrackedPostProperty` which allows plugins/TCs to add a new tracked property to the post model.