A platform for community discussion. Free, open, simple.
DEV: Introduce `composer-minimum-post-length` valueTransformer (#42686)
David Taylor529282ca1e39
diff --git a/frontend/discourse/app/components/composer-editor.gjs b/frontend/discourse/app/components/composer-editor.gjsindex 27289201a0a..5837883ee2f 100644--- a/frontend/discourse/app/components/composer-editor.gjs+++ b/frontend/discourse/app/components/composer-editor.gjs@@ -368,21 +368,23 @@ export default class ComposerEditor extends Component {}let reason;- if (this.composer?.model?.replyLength < 1) {- reason = i18n("composer.error.post_missing");- } else if (this.composer?.model?.missingReplyCharacters > 0) {- reason = i18n("composer.error.post_length", {- count: this.composer?.model?.minimumPostLength,- });- const tl = this.get("currentUser.trust_level");- if ((tl === 0 || tl === 1) && !this._isNewTopic) {- reason +=- "<br/>" +- i18n("composer.error.try_like", {- heart: iconHTML("heart", {- label: i18n("likes_lowercase", { count: 1 }),- }),- });+ if (this.composer?.model?.missingReplyCharacters > 0) {+ if (this.composer?.model?.replyLength < 1) {+ reason = i18n("composer.error.post_missing");+ } else {+ reason = i18n("composer.error.post_length", {+ count: this.composer?.model?.minimumPostLength,+ });+ const tl = this.get("currentUser.trust_level");+ if ((tl === 0 || tl === 1) && !this._isNewTopic) {+ reason +=+ "<br/>" ++ i18n("composer.error.try_like", {+ heart: iconHTML("heart", {+ label: i18n("likes_lowercase", { count: 1 }),+ }),+ });+ }}}diff --git a/frontend/discourse/app/lib/registry/transformers.js b/frontend/discourse/app/lib/registry/transformers.jsindex a6454058c84..3a543983338 100644--- a/frontend/discourse/app/lib/registry/transformers.js+++ b/frontend/discourse/app/lib/registry/transformers.js@@ -64,6 +64,7 @@ export const VALUE_TRANSFORMERS = Object.freeze(["composer-editor-reply-placeholder","composer-force-editor-mode","composer-message-components",+ "composer-minimum-post-length","composer-reply-options-user-avatar-template","composer-reply-options-user-link-name","composer-save-button-label",diff --git a/frontend/discourse/app/models/composer.js b/frontend/discourse/app/models/composer.jsindex 837ca7bb4b3..54158c91584 100644--- a/frontend/discourse/app/models/composer.js+++ b/frontend/discourse/app/models/composer.js@@ -767,16 +767,22 @@ export default class Composer extends RestModel {@computed("privateMessage", "topicFirstPost", "topic.pm_with_non_human_user")get minimumPostLength() {+ let length;+if (this.topic?.pm_with_non_human_user) {- return 1;+ length = 1;} else if (this.privateMessage) {- return this.siteSettings.min_personal_message_post_length;+ length = this.siteSettings.min_personal_message_post_length;} else if (this.topicFirstPost) {// first post (topic body)- return this.siteSettings.min_first_post_length;+ length = this.siteSettings.min_first_post_length;} else {- return this.siteSettings.min_post_length;+ length = this.siteSettings.min_post_length;}++ return applyValueTransformer("composer-minimum-post-length", length, {+ composer: this,+ });}@computed("title")diff --git a/frontend/discourse/tests/acceptance/transformers/composer-minimum-post-length-test.js b/frontend/discourse/tests/acceptance/transformers/composer-minimum-post-length-test.jsnew file mode 100644index 00000000000..d555f004b31--- /dev/null+++ b/frontend/discourse/tests/acceptance/transformers/composer-minimum-post-length-test.js@@ -0,0 +1,46 @@+import { click, visit } from "@ember/test-helpers";+import { test } from "qunit";+import { withPluginApi } from "discourse/lib/plugin-api";+import { acceptance } from "discourse/tests/helpers/qunit-helpers";++acceptance("composer-minimum-post-length transformer", function (needs) {+ needs.user();+ needs.settings({+ allow_uncategorized_topics: true,+ min_first_post_length: 20,+ });++ test("the default value comes from the site settings", async function (assert) {+ await visit("/new-topic?title=topic title that is pretty long");+ await click(".submit-panel .create");++ assert.dom(".d-editor-input").exists("the body is still required");+ });++ test("returning 0 lets an empty body through", async function (assert) {+ withPluginApi((api) => {+ api.registerValueTransformer("composer-minimum-post-length", () => 0);+ });++ await visit("/new-topic?title=topic title that is pretty long");+ await click(".submit-panel .create");++ assert+ .dom(".d-editor-input")+ .doesNotExist("closes the composer on successful creation");+ });++ test("returning 0 stops the editor asking for a body", async function (assert) {+ withPluginApi((api) => {+ api.registerValueTransformer("composer-minimum-post-length", () => 0);+ });++ await visit("/new-topic");+ await click(".submit-panel .create");++ assert.dom(".title-input .popup-tip.bad").exists("the title is required");+ assert+ .dom(".d-editor-textarea-wrapper .popup-tip.bad")+ .doesNotExist("the body is not");+ });+});