A platform for community discussion. Free, open, simple.
DEV: Add preferences-save-attributes value transformer
zogstripee1a1c7219fe
diff --git a/frontend/discourse/app/controllers/preferences/account.js b/frontend/discourse/app/controllers/preferences/account.jsindex a0c331f102d..937b123c9d7 100644--- a/frontend/discourse/app/controllers/preferences/account.js+++ b/frontend/discourse/app/controllers/preferences/account.js@@ -11,6 +11,7 @@ import { propertyNotEqual, setting } from "discourse/lib/computed";import discourseComputed from "discourse/lib/decorators";import { exportUserArchive } from "discourse/lib/export-csv";import getURL from "discourse/lib/get-url";+import { applyValueTransformer } from "discourse/lib/transformer";import DiscourseURL from "discourse/lib/url";import { findAll } from "discourse/models/login-method";import { i18n } from "discourse-i18n";@@ -40,17 +41,17 @@ export default class AccountController extends Controller {init() {super.init(...arguments);-- this.saveAttrNames = [- "name",- "title",- "primary_group_id",- "flair_group_id",- "status",- ];this.set("revoking", {});}+ get saveAttrNames() {+ return applyValueTransformer(+ "preferences-save-attributes",+ ["name", "title", "primary_group_id", "flair_group_id", "status"],+ { page: "account" }+ );+ }+reset() {this.set("passwordProgress", null);}diff --git a/frontend/discourse/app/controllers/preferences/emails.js b/frontend/discourse/app/controllers/preferences/emails.jsindex 7d3a70664ea..603803d1815 100644--- a/frontend/discourse/app/controllers/preferences/emails.js+++ b/frontend/discourse/app/controllers/preferences/emails.js@@ -3,6 +3,7 @@ import { action } from "@ember/object";import { equal } from "@ember/object/computed";import { popupAjaxError } from "discourse/lib/ajax-error";import discourseComputed from "discourse/lib/decorators";+import { applyValueTransformer } from "discourse/lib/transformer";import { i18n } from "discourse-i18n";const EMAIL_LEVELS = {@@ -20,18 +21,6 @@ export default class EmailsController extends Controller {@equal("model.user_option.email_level", EMAIL_LEVELS.ONLY_WHEN_AWAY)emailLevelAway;- saveAttrNames = [- "email_level",- "email_messages_level",- "mailing_list_mode",- "mailing_list_mode_frequency",- "email_digests",- "email_in_reply_to",- "email_previous_replies",- "digest_after_minutes",- "include_tl0_in_digests",- ];-previousRepliesOptions = [{ name: i18n("user.email_previous_replies.always"), value: 0 },{ name: i18n("user.email_previous_replies.unless_emailed"), value: 1 },@@ -56,6 +45,24 @@ export default class EmailsController extends Controller {{ name: i18n("user.email_digests.every_six_months"), value: 259200 },];+ get saveAttrNames() {+ return applyValueTransformer(+ "preferences-save-attributes",+ [+ "email_level",+ "email_messages_level",+ "mailing_list_mode",+ "mailing_list_mode_frequency",+ "email_digests",+ "email_in_reply_to",+ "email_previous_replies",+ "digest_after_minutes",+ "include_tl0_in_digests",+ ],+ { page: "emails" }+ );+ }+@discourseComputed()frequencyEstimate() {let estimate = this.get("model.mailing_list_posts_per_day");diff --git a/frontend/discourse/app/controllers/preferences/interface.js b/frontend/discourse/app/controllers/preferences/interface.jsindex dc33c11c930..789a1454c9c 100644--- a/frontend/discourse/app/controllers/preferences/interface.js+++ b/frontend/discourse/app/controllers/preferences/interface.js@@ -17,6 +17,7 @@ import {listThemes,setLocalTheme,} from "discourse/lib/theme-selector";+import { applyValueTransformer } from "discourse/lib/transformer";import { setDefaultHomepage } from "discourse/lib/utilities";import { AUTO_DELETE_PREFERENCES } from "discourse/models/bookmark";import { i18n } from "discourse-i18n";@@ -86,7 +87,9 @@ export default class InterfaceController extends Controller {attrs.push("theme_ids");}- return attrs;+ return applyValueTransformer("preferences-save-attributes", attrs, {+ page: "interface",+ });}@discourseComputed()diff --git a/frontend/discourse/app/controllers/preferences/navigation-menu.js b/frontend/discourse/app/controllers/preferences/navigation-menu.jsindex 2faf4aa953a..58908560b8d 100644--- a/frontend/discourse/app/controllers/preferences/navigation-menu.js+++ b/frontend/discourse/app/controllers/preferences/navigation-menu.js@@ -2,6 +2,7 @@ import { tracked } from "@glimmer/tracking";import Controller from "@ember/controller";import { action } from "@ember/object";import { popupAjaxError } from "discourse/lib/ajax-error";+import { applyValueTransformer } from "discourse/lib/transformer";import { i18n } from "discourse-i18n";export default class extends Controller {@@ -9,10 +10,13 @@ export default class extends Controller {subpageTitle = i18n("user.preferences_nav.navigation_menu");- saveAttrNames = [- "sidebar_link_to_filtered_list",- "sidebar_show_count_of_new_items",- ];+ get saveAttrNames() {+ return applyValueTransformer(+ "preferences-save-attributes",+ ["sidebar_link_to_filtered_list", "sidebar_show_count_of_new_items"],+ { page: "navigation-menu" }+ );+ }@actionsave() {diff --git a/frontend/discourse/app/controllers/preferences/notifications.js b/frontend/discourse/app/controllers/preferences/notifications.jsindex a783924d832..78eef28eb11 100644--- a/frontend/discourse/app/controllers/preferences/notifications.js+++ b/frontend/discourse/app/controllers/preferences/notifications.js@@ -1,35 +1,38 @@import Controller from "@ember/controller";import { action } from "@ember/object";import { popupAjaxError } from "discourse/lib/ajax-error";+import { applyValueTransformer } from "discourse/lib/transformer";import { i18n } from "discourse-i18n";export default class NotificationsController extends Controller {subpageTitle = i18n("user.preferences_nav.notifications");- init() {- super.init(...arguments);+ likeNotificationFrequencies = [+ { name: i18n("user.like_notification_frequency.always"), value: 0 },+ {+ name: i18n("user.like_notification_frequency.first_time_and_daily"),+ value: 1,+ },+ { name: i18n("user.like_notification_frequency.first_time"), value: 2 },+ { name: i18n("user.like_notification_frequency.never"), value: 3 },+ ];- this.saveAttrNames = [- "allow_private_messages",- "auto_track_topics_after_msecs",- "enable_allowed_pm_users",- "like_notification_frequency",- "muted_usernames",- "new_topic_duration_minutes",- "notification_level_when_replying",- "notify_on_linked_posts",- "user_notification_schedule",- ];-- this.likeNotificationFrequencies = [- { name: i18n("user.like_notification_frequency.always"), value: 0 },- {- name: i18n("user.like_notification_frequency.first_time_and_daily"),- value: 1,- },- { name: i18n("user.like_notification_frequency.first_time"), value: 2 },- { name: i18n("user.like_notification_frequency.never"), value: 3 },- ];+ get saveAttrNames() {+ return applyValueTransformer(+ "preferences-save-attributes",+ [+ "allow_private_messages",+ "auto_track_topics_after_msecs",+ "enable_allowed_pm_users",+ "like_notification_frequency",+ "muted_usernames",+ "new_topic_duration_minutes",+ "notification_level_when_replying",+ "notify_on_linked_posts",+ "user_notification_schedule",+ ],+ { page: "notifications" }+ );}@actiondiff --git a/frontend/discourse/app/controllers/preferences/profile.js b/frontend/discourse/app/controllers/preferences/profile.jsindex 4516d4b930b..7d24a4e86f6 100644--- a/frontend/discourse/app/controllers/preferences/profile.js+++ b/frontend/discourse/app/controllers/preferences/profile.js@@ -7,6 +7,7 @@ import FeatureTopicOnProfileModal from "discourse/components/modal/feature-topicimport { ajax } from "discourse/lib/ajax";import { popupAjaxError } from "discourse/lib/ajax-error";import discourseComputed from "discourse/lib/decorators";+import { applyValueTransformer } from "discourse/lib/transformer";import { i18n } from "discourse-i18n";export default class ProfileController extends Controller {@@ -22,25 +23,31 @@ export default class ProfileController extends Controller {@readOnly("model.can_upload_user_card_background")canUploadUserCardBackground;- saveAttrNames = [- "bio_raw",- "website",- "location",- "custom_fields",- "user_fields",- "profile_background_upload_url",- "card_background_upload_url",- "date_of_birth",- "timezone",- "default_calendar",- "hide_profile",- ];-calendarOptions = [{ name: i18n("download_calendar.google"), value: "google" },{ name: i18n("download_calendar.ics"), value: "ics" },];+ get saveAttrNames() {+ return applyValueTransformer(+ "preferences-save-attributes",+ [+ "bio_raw",+ "website",+ "location",+ "custom_fields",+ "user_fields",+ "profile_background_upload_url",+ "card_background_upload_url",+ "date_of_birth",+ "timezone",+ "default_calendar",+ "hide_profile",+ ],+ { page: "profile" }+ );+ }+@discourseComputed("[email protected]")userFields() {let siteUserFields = this.site.user_fields;diff --git a/frontend/discourse/app/controllers/preferences/tags.js b/frontend/discourse/app/controllers/preferences/tags.jsindex 75bb53075b1..053a380ddbd 100644--- a/frontend/discourse/app/controllers/preferences/tags.js+++ b/frontend/discourse/app/controllers/preferences/tags.js@@ -2,14 +2,21 @@ import Controller from "@ember/controller";import { action } from "@ember/object";import { popupAjaxError } from "discourse/lib/ajax-error";import discourseComputed from "discourse/lib/decorators";+import { applyValueTransformer } from "discourse/lib/transformer";export default class TagsController extends Controller {- saveAttrNames = [- "muted_tags",- "tracked_tags",- "watched_tags",- "watching_first_post_tags",- ];+ get saveAttrNames() {+ return applyValueTransformer(+ "preferences-save-attributes",+ [+ "muted_tags",+ "tracked_tags",+ "watched_tags",+ "watching_first_post_tags",+ ],+ { page: "tags" }+ );+ }@discourseComputed("model.watched_tags.[]",diff --git a/frontend/discourse/app/controllers/preferences/tracking.js b/frontend/discourse/app/controllers/preferences/tracking.jsindex 2560d37ebcd..7b29b9685f3 100644--- a/frontend/discourse/app/controllers/preferences/tracking.js+++ b/frontend/discourse/app/controllers/preferences/tracking.js@@ -4,6 +4,7 @@ import { action, computed } from "@ember/object";import { service } from "@ember/service";import { popupAjaxError } from "discourse/lib/ajax-error";import { NotificationLevels } from "discourse/lib/notification-levels";+import { applyValueTransformer } from "discourse/lib/transformer";import { i18n } from "discourse-i18n";export default class extends Controller {@@ -185,7 +186,9 @@ export default class extends Controller {attrs.push(...this.customAttrNames);}- return attrs;+ return applyValueTransformer("preferences-save-attributes", attrs, {+ page: "tracking",+ });}@actiondiff --git a/frontend/discourse/app/controllers/preferences/users.js b/frontend/discourse/app/controllers/preferences/users.jsindex 96d700a69e5..4aa5d3323f2 100644--- a/frontend/discourse/app/controllers/preferences/users.js+++ b/frontend/discourse/app/controllers/preferences/users.js@@ -5,6 +5,7 @@ import { popupAjaxError } from "discourse/lib/ajax-error";import { uniqueItemsFromArray } from "discourse/lib/array-tools";import discourseComputed from "discourse/lib/decorators";import { makeArray } from "discourse/lib/helpers";+import { applyValueTransformer } from "discourse/lib/transformer";import { i18n } from "discourse-i18n";export default class UsersController extends Controller {@@ -16,15 +17,17 @@ export default class UsersController extends Controller {subpageTitle = i18n("user.preferences_nav.users");- init() {- super.init(...arguments);-- this.saveAttrNames = [- "allow_private_messages",- "muted_usernames",- "allowed_pm_usernames",- "enable_allowed_pm_users",- ];+ get saveAttrNames() {+ return applyValueTransformer(+ "preferences-save-attributes",+ [+ "allow_private_messages",+ "muted_usernames",+ "allowed_pm_usernames",+ "enable_allowed_pm_users",+ ],+ { page: "users" }+ );}@computed("model.muted_usernames")diff --git a/frontend/discourse/app/lib/transformer/registry.js b/frontend/discourse/app/lib/transformer/registry.jsindex 7858ac4ce95..9b7f3946369 100644--- a/frontend/discourse/app/lib/transformer/registry.js+++ b/frontend/discourse/app/lib/transformer/registry.js@@ -73,6 +73,7 @@ export const VALUE_TRANSFORMERS = Object.freeze(["poster-name-class","poster-name-icons","poster-name-user-title",+ "preferences-save-attributes","quote-params","small-user-attrs","tag-separator",diff --git a/frontend/discourse/tests/acceptance/transformers/preferences-save-attributes-test.js b/frontend/discourse/tests/acceptance/transformers/preferences-save-attributes-test.jsnew file mode 100644index 00000000000..2419ecc8e12--- /dev/null+++ b/frontend/discourse/tests/acceptance/transformers/preferences-save-attributes-test.js@@ -0,0 +1,47 @@+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("preferences-save-attributes transformer", function (needs) {+ needs.user();++ let lastUserData;+ needs.pretender((server, helper) => {+ server.put("/u/eviltrout.json", (request) => {+ lastUserData = helper.parsePostData(request.requestBody);+ return helper.response({ user: {} });+ });+ });++ test("allows plugins to add attributes to the save list for a specific page", async function (assert) {+ // auto_track_topics_after_msecs is in userOptionFields but NOT in+ // the default saveAttrNames for the interface controller+ let transformerContext = null;+ withPluginApi((api) => {+ api.registerValueTransformer(+ "preferences-save-attributes",+ ({ value: attrs, context }) => {+ transformerContext = context;+ if (context.page === "interface") {+ attrs.push("auto_track_topics_after_msecs");+ }+ return attrs;+ }+ );+ });++ await visit("/u/eviltrout/preferences/interface");+ await click(".save-changes");++ assert.strictEqual(+ transformerContext.page,+ "interface",+ "transformer receives correct page context"+ );+ assert.true(+ "auto_track_topics_after_msecs" in lastUserData,+ "transformer-added attribute is included in save request"+ );+ });+});diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-search-discoveries.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-search-discoveries.jsindex f4904dd338d..5c37449751d 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-search-discoveries.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-search-discoveries.js@@ -1,4 +1,3 @@-import { action } from "@ember/object";import { apiInitializer } from "discourse/lib/api";const FIELD_NAME = "ai_search_discoveries";@@ -16,16 +15,14 @@ export default apiInitializer((api) => {api.addSaveableUserOptionField(FIELD_NAME);- api.modifyClass(- "controller:preferences/interface",- (Superclass) =>- class extends Superclass {- @action- save() {- this.saveAttrNames.push(FIELD_NAME);- return super.save(...arguments);- }+ api.registerValueTransformer(+ "preferences-save-attributes",+ ({ value: attrs, context }) => {+ if (context.page === "interface") {+ attrs.push(FIELD_NAME);}+ return attrs;+ });const discobotDiscoveries = api.container.lookup(