- Notifications
You must be signed in to change notification settings - Fork8.7k
DEV: Add page-aware plugin APIs for saving user preferences#36757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Conversation
Previously, plugins that needed to save user preferences on specificpreference pages had to either:1. Manually register value transformers for `preferences-save-attributes`2. Use `modifyClass` to push fields to `saveAttrNames`The second approach was broken because `saveAttrNames` is now a getterthat returns a fresh array each time, so pushed values were lost.This commit introduces three new plugin APIs that handle the transformerregistration automatically:- `addSaveableUserOption(name, { page })` - for user_options table fields- `addSaveableUserField(name, { page })` - for user_fields table fields- `addSaveableCustomFields({ page })` - ensures custom_fields object is saved on a page (auto-deduplicates across plugins)The `{ page }` option specifies which preferences page triggers saving:"account", "emails", "interface", "notifications", "profile", "tracking", etc.Also updates bundled plugins to use the new APIs:- chat: `addSaveableUserOption` with `{ page: "emails" }` for email frequency- discourse-ai: `addSaveableUserOption` with `{ page: "interface" }`- discourse-assign: `addSaveableUserOption` with `{ page: "tracking" }`, `addSaveableCustomFields` with `{ page: "notifications" }`- discourse-policy: `addSaveableUserOption` with `{ page: "emails" }`- discourse-rewind: `addSaveableUserOption` (no page, custom prefs page)Deprecates `addSaveableUserOptionField` in favor of `addSaveableUserOption`.Ref -https://meta.discourse.org/t/391509Follow up toee1a1c7Replace manual `modifyClass` controller override with the new`addSaveableCustomFields({ page: "notifications" })` API.The previous approach of pushing to `saveAttrNames` in a `save()` overridewas broken because `saveAttrNames` is now a getter returning a fresh array.cf.discourse/discourse#36757Replace manual `modifyClass` controller override with the new`addSaveableCustomFields({ page: "notifications" })` API.The previous approach of pushing to `saveAttrNames` in `init()` was brokenbecause `saveAttrNames` is now a getter returning a fresh array.cf.discourse/discourse#36757Simplify preference saving by using the new`addSaveableUserField(name, { page: "emails" })` API.This replaces manual value transformer registration and backwardcompatibility code with a single declarative API call.cf.discourse/discourse#36757Replace manual `modifyClass` controller override with the new`addSaveableCustomFields({ page: "notifications" })` API.The previous approach of pushing to `saveAttrNames` in a `save()` overridewas broken because `saveAttrNames` is now a getter returning a fresh array.cf.discourse/discourse#36757This updates the signature preferences connector to use modernEmber patterns and the new plugin API.Changes:- Convert to Glimmer component with @outletArgs pattern- Use@service injection for siteSettings- Replace classic <Input> with native <input> and {{on}} modifier- Use new api.addSaveableCustomFields({ page: "profile" }) API instead of modifyClass save() override- Remove dead @showUploadModal argument from DEditor- Add CSS to hide preview panel and match bio editor widthThe modifyClass pattern for saving custom_fields was problematicbecause saveAttrNames is now a getter that returns a fresh arrayeach call, so pushing to it in save() had no effect. The newaddSaveableCustomFields API uses value transformers to properlyadd "custom_fields" to the save attributes.cf.discourse/discourse#36757
pmusaraj left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This looks good to me. I wonder if someone from dev-xp can also review, though, to ensure consistency across APIs.
plugins/chat/assets/javascripts/discourse/initializers/chat-user-options.jsShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Changed from `addSaveableCustomFields({ page })` to`addSaveableCustomFields(page)` since `page` is the only requiredparameter. The options object pattern implied it was optional whenit's actually mandatory.Also improved error handling to throw in development/tests when`page` is missing, rather than just logging a warning.b66fca7 intomainUh oh!
There was an error while loading.Please reload this page.
Follow-up to discourse/discourse commit that simplified the methodsignature from `addSaveableCustomFields({ page })` to`addSaveableCustomFields(page)`.cf.discourse/discourse#36757Follow-up to discourse/discourse commit that simplified the methodsignature from `addSaveableCustomFields({ page })` to`addSaveableCustomFields(page)`.cf.discourse/discourse#36757Follow-up to discourse/discourse commit that simplified the methodsignature from `addSaveableCustomFields({ page })` to`addSaveableCustomFields(page)`.cf.discourse/discourse#36757
Uh oh!
There was an error while loading.Please reload this page.
Previously, plugins that needed to save user preferences on specific preference pages had to either:
preferences-save-attributesmodifyClassto push fields tosaveAttrNamesThe second approach was broken because
saveAttrNamesis now a getter that returns a fresh array each time, so pushed values were lost.This commit introduces three new plugin APIs that handle the transformer registration automatically:
addSaveableUserOption(name, { page })- foruser_optionstable fieldsaddSaveableUserField(name, { page })- foruser_fieldstable fieldsaddSaveableCustomFields(page)- ensurescustom_fieldsobject is saved on a page (auto-deduplicates across plugins)The
{ page }option specifies which preferences page triggers saving: "account", "emails", "interface", "notifications", "profile", "tracking", etc.Also updates bundled plugins to use the new APIs:
addSaveableUserOptionwith{ page: "emails" }for email frequencyaddSaveableUserOptionwith{ page: "interface" }addSaveableUserOptionwith{ page: "tracking" },addSaveableCustomFieldswith"notifications"addSaveableUserOptionwith{ page: "emails" }addSaveableUserOption(no page, custom prefs page)Deprecates
addSaveableUserOptionFieldin favor ofaddSaveableUserOption.Ref -https://meta.discourse.org/t/391509
Follow up toee1a1c7