mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-27 02:05:26 +08:00
Previously, plugins that needed to save user preferences on specific
preference 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 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 })` - 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 `"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/391509
Follow up to ee1a1c7219
17 lines
392 B
JavaScript
Vendored
17 lines
392 B
JavaScript
Vendored
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
|
|
export default {
|
|
name: "policy-user-options",
|
|
|
|
initialize(container) {
|
|
withPluginApi((api) => {
|
|
const { policy_enabled } = container.lookup("service:site-settings");
|
|
|
|
if (policy_enabled) {
|
|
api.addSaveableUserOption("policy_email_frequency", {
|
|
page: "emails",
|
|
});
|
|
}
|
|
});
|
|
},
|
|
};
|