@basicbenframework/cms 0.0.0-stage → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +9 -0
- package/README.md +206 -2
- package/db/migrations/001_create_users.js +15 -0
- package/db/migrations/002_create_posts.js +18 -0
- package/db/migrations/003_create_categories.js +19 -0
- package/db/migrations/004_create_tags.js +16 -0
- package/db/migrations/005_create_post_tags.js +16 -0
- package/db/migrations/006_create_pages.js +26 -0
- package/db/migrations/007_create_comments.js +24 -0
- package/db/migrations/008_create_media.js +22 -0
- package/db/migrations/009_update_posts_blogging.js +27 -0
- package/db/migrations/010_create_settings.js +29 -0
- package/db/migrations/011_add_user_roles.js +23 -0
- package/db/migrations/012_create_auth_tokens.js +30 -0
- package/db/migrations/013_add_email_verified.js +24 -0
- package/db/migrations/014_create_user_two_factor.js +30 -0
- package/db/migrations/015_create_user_credentials.js +35 -0
- package/db/migrations/016_create_rate_limits.js +31 -0
- package/db/migrations/017_add_content_html.js +38 -0
- package/db/migrations/018_create_api_tokens.js +34 -0
- package/db/migrations/019_create_post_categories.js +40 -0
- package/db/migrations/020_add_author_profiles.js +60 -0
- package/db/migrations/021_add_page_featured_image.js +23 -0
- package/db/migrations/022_backfill_post_slugs_and_excerpts.js +57 -0
- package/db/migrations/023_add_user_session_controls.js +25 -0
- package/db/migrations/024_add_post_status_and_trash.js +45 -0
- package/db/migrations/025_add_social_and_indexing_fields.js +40 -0
- package/db/migrations/026_create_post_revisions.js +29 -0
- package/db/migrations/027_add_media_dimensions.js +19 -0
- package/db/migrations/028_create_navigation_menus.js +65 -0
- package/db/migrations/029_create_redirects.js +40 -0
- package/db/migrations/030_add_comment_status_and_notifications.js +53 -0
- package/db/migrations/031_create_post_locks.js +38 -0
- package/db/migrations/032_create_snippets.js +31 -0
- package/db/migrations/033_create_members.js +70 -0
- package/db/migrations/034_add_post_visibility.js +28 -0
- package/db/migrations/035_create_newsletters.js +72 -0
- package/db/migrations/036_create_analytics.js +89 -0
- package/db/migrations/037_create_ops_heartbeats.js +30 -0
- package/db/migrations/038_create_content_types.js +84 -0
- package/db/migrations/039_create_content_type_fields.js +47 -0
- package/db/migrations/040_create_taxonomies.js +80 -0
- package/db/migrations/041_add_newsletter_handoff.js +28 -0
- package/db/migrations/042_create_mysendbox_contacts.js +63 -0
- package/lib/cli/backup.d.mts +2 -0
- package/lib/cli/backup.mjs +124 -0
- package/lib/cli/export.d.mts +2 -0
- package/lib/cli/export.mjs +54 -0
- package/lib/cli/import-ghost.d.mts +2 -0
- package/lib/cli/import-ghost.mjs +50 -0
- package/lib/cli/import-json.d.mts +2 -0
- package/lib/cli/import-json.mjs +61 -0
- package/lib/cli/import-wordpress.d.mts +2 -0
- package/lib/cli/import-wordpress.mjs +169 -0
- package/lib/cli/import-wxr.d.mts +2 -0
- package/lib/cli/import-wxr.mjs +55 -0
- package/lib/cli/index.d.mts +2 -0
- package/lib/cli/index.mjs +53 -0
- package/lib/cli/lib/backup.d.mts +112 -0
- package/lib/cli/lib/backup.mjs +297 -0
- package/lib/cli/lib/ghost.d.mts +30 -0
- package/lib/cli/lib/ghost.mjs +201 -0
- package/lib/cli/lib/html-to-markdown.d.mts +61 -0
- package/lib/cli/lib/html-to-markdown.mjs +523 -0
- package/lib/cli/lib/import-common.d.mts +100 -0
- package/lib/cli/lib/import-common.mjs +252 -0
- package/lib/cli/lib/json-import.d.mts +22 -0
- package/lib/cli/lib/json-import.mjs +422 -0
- package/lib/cli/lib/site-import.d.mts +32 -0
- package/lib/cli/lib/site-import.mjs +456 -0
- package/lib/cli/lib/wordpress-rest.d.mts +25 -0
- package/lib/cli/lib/wordpress-rest.mjs +177 -0
- package/lib/cli/lib/wxr.d.mts +18 -0
- package/lib/cli/lib/wxr.mjs +307 -0
- package/lib/cli/lib/xml.d.mts +33 -0
- package/lib/cli/lib/xml.mjs +186 -0
- package/lib/cli/restore.d.mts +2 -0
- package/lib/cli/restore.mjs +98 -0
- package/lib/client/components/Alert.d.ts +7 -0
- package/lib/client/components/Alert.js +8 -0
- package/lib/client/components/Avatar.d.ts +15 -0
- package/lib/client/components/Avatar.js +10 -0
- package/lib/client/components/BackLink.d.ts +7 -0
- package/lib/client/components/BackLink.js +6 -0
- package/lib/client/components/Button.d.ts +7 -0
- package/lib/client/components/Button.js +12 -0
- package/lib/client/components/Card.d.ts +7 -0
- package/lib/client/components/Card.js +6 -0
- package/lib/client/components/CommentThread.d.ts +15 -0
- package/lib/client/components/CommentThread.js +101 -0
- package/lib/client/components/Empty.d.ts +6 -0
- package/lib/client/components/Empty.js +6 -0
- package/lib/client/components/Input.d.ts +6 -0
- package/lib/client/components/Input.js +6 -0
- package/lib/client/components/Link.d.ts +31 -0
- package/lib/client/components/Link.js +49 -0
- package/lib/client/components/Loading.d.ts +1 -0
- package/lib/client/components/Loading.js +6 -0
- package/lib/client/components/Logo.d.ts +5 -0
- package/lib/client/components/Logo.js +4 -0
- package/lib/client/components/MarkdownEditor.d.ts +41 -0
- package/lib/client/components/MarkdownEditor.js +156 -0
- package/lib/client/components/Nav/DarkModeToggle.d.ts +6 -0
- package/lib/client/components/Nav/DarkModeToggle.js +6 -0
- package/lib/client/components/Nav/DesktopNav.d.ts +8 -0
- package/lib/client/components/Nav/DesktopNav.js +9 -0
- package/lib/client/components/Nav/MobileNav.d.ts +9 -0
- package/lib/client/components/Nav/MobileNav.js +15 -0
- package/lib/client/components/NavLink.d.ts +8 -0
- package/lib/client/components/NavLink.js +6 -0
- package/lib/client/components/PageHeader.d.ts +8 -0
- package/lib/client/components/PageHeader.js +6 -0
- package/lib/client/components/PostCard.d.ts +8 -0
- package/lib/client/components/PostCard.js +7 -0
- package/lib/client/components/Textarea.d.ts +6 -0
- package/lib/client/components/Textarea.js +6 -0
- package/lib/client/components/ThemeContext.d.ts +18 -0
- package/lib/client/components/ThemeContext.js +8 -0
- package/lib/client/components/admin/AdminIcons.d.ts +13 -0
- package/lib/client/components/admin/AdminIcons.js +66 -0
- package/lib/client/components/admin/ContentModelPanels.d.ts +22 -0
- package/lib/client/components/admin/ContentModelPanels.js +93 -0
- package/lib/client/components/admin/FeaturedImageBox.d.ts +21 -0
- package/lib/client/components/admin/FeaturedImageBox.js +24 -0
- package/lib/client/components/admin/MediaGrid.d.ts +23 -0
- package/lib/client/components/admin/MediaGrid.js +69 -0
- package/lib/client/components/admin/MediaPicker.d.ts +17 -0
- package/lib/client/components/admin/MediaPicker.js +61 -0
- package/lib/client/components/admin/Pagination.d.ts +27 -0
- package/lib/client/components/admin/Pagination.js +29 -0
- package/lib/client/components/admin/SnippetPicker.d.ts +27 -0
- package/lib/client/components/admin/SnippetPicker.js +65 -0
- package/lib/client/components/admin/TermPicker.d.ts +52 -0
- package/lib/client/components/admin/TermPicker.js +58 -0
- package/lib/client/contexts/ToastContext.d.ts +12 -0
- package/lib/client/contexts/ToastContext.js +49 -0
- package/lib/client/features.d.ts +11 -0
- package/lib/client/features.js +7 -0
- package/lib/client/hooks/useContentTypes.d.ts +48 -0
- package/lib/client/hooks/useContentTypes.js +48 -0
- package/lib/client/hooks/useMediaLibrary.d.ts +46 -0
- package/lib/client/hooks/useMediaLibrary.js +189 -0
- package/lib/client/hooks/usePostLock.d.ts +32 -0
- package/lib/client/hooks/usePostLock.js +107 -0
- package/lib/client/host/index.d.ts +71 -0
- package/lib/client/host/index.js +58 -0
- package/lib/client/i18n/index.d.ts +79 -0
- package/lib/client/i18n/index.js +177 -0
- package/lib/client/i18n/locales/es/content.d.ts +4 -0
- package/lib/client/i18n/locales/es/content.js +220 -0
- package/lib/client/i18n/locales/es/editor.d.ts +9 -0
- package/lib/client/i18n/locales/es/editor.js +144 -0
- package/lib/client/i18n/locales/es/index.d.ts +8 -0
- package/lib/client/i18n/locales/es/index.js +7 -0
- package/lib/client/i18n/locales/es/layout.d.ts +4 -0
- package/lib/client/i18n/locales/es/layout.js +35 -0
- package/lib/client/i18n/locales/es/people.d.ts +4 -0
- package/lib/client/i18n/locales/es/people.js +187 -0
- package/lib/client/i18n/locales/es/site.d.ts +4 -0
- package/lib/client/i18n/locales/es/site.js +208 -0
- package/lib/client/layouts/AdminLayout.d.ts +7 -0
- package/lib/client/layouts/AdminLayout.js +1046 -0
- package/lib/client/layouts/AppLayout.d.ts +6 -0
- package/lib/client/layouts/AppLayout.js +19 -0
- package/lib/client/layouts/AuthLayout.d.ts +6 -0
- package/lib/client/layouts/AuthLayout.js +14 -0
- package/lib/client/layouts/DocsLayout.d.ts +6 -0
- package/lib/client/layouts/DocsLayout.js +28 -0
- package/lib/client/layouts/RootLayout.d.ts +6 -0
- package/lib/client/layouts/RootLayout.js +11 -0
- package/lib/client/pages/AcceptInvite.d.ts +8 -0
- package/lib/client/pages/AcceptInvite.js +60 -0
- package/lib/client/pages/Auth.d.ts +1 -0
- package/lib/client/pages/Auth.js +40 -0
- package/lib/client/pages/Authentication.d.ts +1 -0
- package/lib/client/pages/Authentication.js +230 -0
- package/lib/client/pages/Content.d.ts +1 -0
- package/lib/client/pages/Content.js +21 -0
- package/lib/client/pages/Database.d.ts +1 -0
- package/lib/client/pages/Database.js +264 -0
- package/lib/client/pages/Extending.d.ts +1 -0
- package/lib/client/pages/Extending.js +28 -0
- package/lib/client/pages/Feed.d.ts +1 -0
- package/lib/client/pages/Feed.js +19 -0
- package/lib/client/pages/FeedPost.d.ts +1 -0
- package/lib/client/pages/FeedPost.js +31 -0
- package/lib/client/pages/ForgotPassword.d.ts +8 -0
- package/lib/client/pages/ForgotPassword.js +38 -0
- package/lib/client/pages/GettingStarted.d.ts +1 -0
- package/lib/client/pages/GettingStarted.js +84 -0
- package/lib/client/pages/Headless.d.ts +1 -0
- package/lib/client/pages/Headless.js +49 -0
- package/lib/client/pages/Home.d.ts +1 -0
- package/lib/client/pages/Home.js +67 -0
- package/lib/client/pages/PostForm.d.ts +1 -0
- package/lib/client/pages/PostForm.js +55 -0
- package/lib/client/pages/Posts.d.ts +1 -0
- package/lib/client/pages/Posts.js +35 -0
- package/lib/client/pages/Profile.d.ts +1 -0
- package/lib/client/pages/Profile.js +153 -0
- package/lib/client/pages/ResetPassword.d.ts +8 -0
- package/lib/client/pages/ResetPassword.js +52 -0
- package/lib/client/pages/Routing.d.ts +1 -0
- package/lib/client/pages/Routing.js +192 -0
- package/lib/client/pages/Storage.d.ts +1 -0
- package/lib/client/pages/Storage.js +54 -0
- package/lib/client/pages/Testing.d.ts +1 -0
- package/lib/client/pages/Testing.js +141 -0
- package/lib/client/pages/Validation.d.ts +1 -0
- package/lib/client/pages/Validation.js +123 -0
- package/lib/client/pages/admin/Categories.d.ts +2 -0
- package/lib/client/pages/admin/Categories.js +75 -0
- package/lib/client/pages/admin/Comments.d.ts +11 -0
- package/lib/client/pages/admin/Comments.js +247 -0
- package/lib/client/pages/admin/ContentTypes.d.ts +2 -0
- package/lib/client/pages/admin/ContentTypes.js +163 -0
- package/lib/client/pages/admin/Dashboard.d.ts +2 -0
- package/lib/client/pages/admin/Dashboard.js +66 -0
- package/lib/client/pages/admin/Health.d.ts +12 -0
- package/lib/client/pages/admin/Health.js +148 -0
- package/lib/client/pages/admin/Media.d.ts +2 -0
- package/lib/client/pages/admin/Media.js +101 -0
- package/lib/client/pages/admin/Navigation.d.ts +9 -0
- package/lib/client/pages/admin/Navigation.js +230 -0
- package/lib/client/pages/admin/Pages.d.ts +2 -0
- package/lib/client/pages/admin/Pages.js +53 -0
- package/lib/client/pages/admin/PostEditor.d.ts +17 -0
- package/lib/client/pages/admin/PostEditor.js +532 -0
- package/lib/client/pages/admin/Posts.d.ts +2 -0
- package/lib/client/pages/admin/Posts.js +152 -0
- package/lib/client/pages/admin/Redirects.d.ts +10 -0
- package/lib/client/pages/admin/Redirects.js +87 -0
- package/lib/client/pages/admin/Settings.d.ts +2 -0
- package/lib/client/pages/admin/Settings.js +111 -0
- package/lib/client/pages/admin/Snippets.d.ts +14 -0
- package/lib/client/pages/admin/Snippets.js +85 -0
- package/lib/client/pages/admin/Tags.d.ts +2 -0
- package/lib/client/pages/admin/Tags.js +74 -0
- package/lib/client/pages/admin/Taxonomies.d.ts +2 -0
- package/lib/client/pages/admin/Taxonomies.js +130 -0
- package/lib/client/pages/admin/Tokens.d.ts +9 -0
- package/lib/client/pages/admin/Tokens.js +260 -0
- package/lib/client/pages/admin/Tools.d.ts +12 -0
- package/lib/client/pages/admin/Tools.js +111 -0
- package/lib/client/pages/admin/Users.d.ts +13 -0
- package/lib/client/pages/admin/Users.js +313 -0
- package/lib/client/pages/api-reference.d.ts +33 -0
- package/lib/client/pages/api-reference.js +198 -0
- package/lib/client/styles.css +37 -0
- package/lib/config.d.ts +24 -0
- package/lib/config.js +39 -0
- package/lib/controllers/AdminController.d.ts +46 -0
- package/lib/controllers/AdminController.js +134 -0
- package/lib/controllers/AuthController.d.ts +15 -0
- package/lib/controllers/AuthController.js +194 -0
- package/lib/controllers/AuthorController.d.ts +17 -0
- package/lib/controllers/AuthorController.js +19 -0
- package/lib/controllers/CategoryController.d.ts +10 -0
- package/lib/controllers/CategoryController.js +88 -0
- package/lib/controllers/CommentController.d.ts +68 -0
- package/lib/controllers/CommentController.js +438 -0
- package/lib/controllers/ContentTypeController.d.ts +24 -0
- package/lib/controllers/ContentTypeController.js +91 -0
- package/lib/controllers/EmailVerificationController.d.ts +33 -0
- package/lib/controllers/EmailVerificationController.js +116 -0
- package/lib/controllers/ExportController.d.ts +15 -0
- package/lib/controllers/ExportController.js +41 -0
- package/lib/controllers/FeedController.d.ts +14 -0
- package/lib/controllers/FeedController.js +246 -0
- package/lib/controllers/HealthController.d.ts +21 -0
- package/lib/controllers/HealthController.js +176 -0
- package/lib/controllers/HomeController.d.ts +12 -0
- package/lib/controllers/HomeController.js +16 -0
- package/lib/controllers/MediaController.d.ts +24 -0
- package/lib/controllers/MediaController.js +189 -0
- package/lib/controllers/NavigationController.d.ts +22 -0
- package/lib/controllers/NavigationController.js +176 -0
- package/lib/controllers/PageController.d.ts +13 -0
- package/lib/controllers/PageController.js +172 -0
- package/lib/controllers/PasskeyController.d.ts +36 -0
- package/lib/controllers/PasskeyController.js +201 -0
- package/lib/controllers/PasswordResetController.d.ts +44 -0
- package/lib/controllers/PasswordResetController.js +160 -0
- package/lib/controllers/PostController.d.ts +51 -0
- package/lib/controllers/PostController.js +538 -0
- package/lib/controllers/PostLockController.d.ts +62 -0
- package/lib/controllers/PostLockController.js +101 -0
- package/lib/controllers/ProfileController.d.ts +13 -0
- package/lib/controllers/ProfileController.js +160 -0
- package/lib/controllers/PublicApiController.d.ts +23 -0
- package/lib/controllers/PublicApiController.js +100 -0
- package/lib/controllers/RedirectController.d.ts +32 -0
- package/lib/controllers/RedirectController.js +95 -0
- package/lib/controllers/SettingsController.d.ts +28 -0
- package/lib/controllers/SettingsController.js +105 -0
- package/lib/controllers/SnippetController.d.ts +27 -0
- package/lib/controllers/SnippetController.js +123 -0
- package/lib/controllers/TagController.d.ts +9 -0
- package/lib/controllers/TagController.js +70 -0
- package/lib/controllers/TaxonomyController.d.ts +24 -0
- package/lib/controllers/TaxonomyController.js +149 -0
- package/lib/controllers/TokenController.d.ts +27 -0
- package/lib/controllers/TokenController.js +83 -0
- package/lib/controllers/TwoFactorController.d.ts +31 -0
- package/lib/controllers/TwoFactorController.js +213 -0
- package/lib/controllers/UserController.d.ts +110 -0
- package/lib/controllers/UserController.js +504 -0
- package/lib/features/analytics/AnalyticsPage.d.ts +11 -0
- package/lib/features/analytics/AnalyticsPage.js +123 -0
- package/lib/features/analytics/admin.d.ts +3 -0
- package/lib/features/analytics/admin.js +9 -0
- package/lib/features/analytics/controller.d.ts +19 -0
- package/lib/features/analytics/controller.js +79 -0
- package/lib/features/analytics/helpers.d.ts +84 -0
- package/lib/features/analytics/helpers.js +181 -0
- package/lib/features/analytics/i18n/es.d.ts +4 -0
- package/lib/features/analytics/i18n/es.js +31 -0
- package/lib/features/analytics/index.d.ts +28 -0
- package/lib/features/analytics/index.js +91 -0
- package/lib/features/analytics/manifest.d.ts +8 -0
- package/lib/features/analytics/manifest.js +18 -0
- package/lib/features/analytics/model.d.ts +130 -0
- package/lib/features/analytics/model.js +206 -0
- package/lib/features/members/Member.d.ts +93 -0
- package/lib/features/members/Member.js +194 -0
- package/lib/features/members/MemberAdminController.d.ts +60 -0
- package/lib/features/members/MemberAdminController.js +262 -0
- package/lib/features/members/MemberController.d.ts +88 -0
- package/lib/features/members/MemberController.js +250 -0
- package/lib/features/members/MembersPage.d.ts +10 -0
- package/lib/features/members/MembersPage.js +216 -0
- package/lib/features/members/PostAccessBox.d.ts +15 -0
- package/lib/features/members/PostAccessBox.js +70 -0
- package/lib/features/members/admin.d.ts +3 -0
- package/lib/features/members/admin.js +12 -0
- package/lib/features/members/csv.d.ts +21 -0
- package/lib/features/members/csv.js +74 -0
- package/lib/features/members/events.d.ts +34 -0
- package/lib/features/members/events.js +34 -0
- package/lib/features/members/i18n/es.d.ts +4 -0
- package/lib/features/members/i18n/es.js +106 -0
- package/lib/features/members/index.d.ts +44 -0
- package/lib/features/members/index.js +106 -0
- package/lib/features/members/manifest.d.ts +8 -0
- package/lib/features/members/manifest.js +18 -0
- package/lib/features/members/middleware.d.ts +42 -0
- package/lib/features/members/middleware.js +74 -0
- package/lib/features/members/newsletter.d.ts +177 -0
- package/lib/features/members/newsletter.js +439 -0
- package/lib/features/members/rate-limits.d.ts +34 -0
- package/lib/features/members/rate-limits.js +50 -0
- package/lib/features/members/request.d.ts +13 -0
- package/lib/features/members/request.js +6 -0
- package/lib/features/members/routes.d.ts +15 -0
- package/lib/features/members/routes.js +59 -0
- package/lib/features/members/session.d.ts +102 -0
- package/lib/features/members/session.js +158 -0
- package/lib/features/members/settings.d.ts +14 -0
- package/lib/features/members/settings.js +23 -0
- package/lib/features/mysendbox/MySendboxSettingsCard.d.ts +7 -0
- package/lib/features/mysendbox/MySendboxSettingsCard.js +48 -0
- package/lib/features/mysendbox/admin.d.ts +3 -0
- package/lib/features/mysendbox/admin.js +9 -0
- package/lib/features/mysendbox/broadcasts.d.ts +61 -0
- package/lib/features/mysendbox/broadcasts.js +124 -0
- package/lib/features/mysendbox/client.d.ts +95 -0
- package/lib/features/mysendbox/client.js +61 -0
- package/lib/features/mysendbox/config.d.ts +27 -0
- package/lib/features/mysendbox/config.js +74 -0
- package/lib/features/mysendbox/contacts.d.ts +121 -0
- package/lib/features/mysendbox/contacts.js +378 -0
- package/lib/features/mysendbox/controller.d.ts +11 -0
- package/lib/features/mysendbox/controller.js +47 -0
- package/lib/features/mysendbox/i18n/es.d.ts +4 -0
- package/lib/features/mysendbox/i18n/es.js +28 -0
- package/lib/features/mysendbox/index.d.ts +18 -0
- package/lib/features/mysendbox/index.js +71 -0
- package/lib/features/mysendbox/manifest.d.ts +7 -0
- package/lib/features/mysendbox/manifest.js +6 -0
- package/lib/features/mysendbox/routes.d.ts +6 -0
- package/lib/features/mysendbox/routes.js +14 -0
- package/lib/features/mysendbox/webhook.d.ts +23 -0
- package/lib/features/mysendbox/webhook.js +104 -0
- package/lib/features/theme/OEmbedController.d.ts +31 -0
- package/lib/features/theme/OEmbedController.js +128 -0
- package/lib/features/theme/ThemeController.d.ts +20 -0
- package/lib/features/theme/ThemeController.js +256 -0
- package/lib/features/theme/ThemeSettingsCard.d.ts +7 -0
- package/lib/features/theme/ThemeSettingsCard.js +12 -0
- package/lib/features/theme/admin.d.ts +3 -0
- package/lib/features/theme/admin.js +9 -0
- package/lib/features/theme/html.d.ts +48 -0
- package/lib/features/theme/html.js +97 -0
- package/lib/features/theme/i18n/es.d.ts +4 -0
- package/lib/features/theme/i18n/es.js +9 -0
- package/lib/features/theme/index.d.ts +11 -0
- package/lib/features/theme/index.js +23 -0
- package/lib/features/theme/manifest.d.ts +7 -0
- package/lib/features/theme/manifest.js +6 -0
- package/lib/features/theme/oembed.d.ts +36 -0
- package/lib/features/theme/oembed.js +58 -0
- package/lib/features/theme/routes.d.ts +17 -0
- package/lib/features/theme/routes.js +54 -0
- package/lib/features/theme/templates.d.ts +103 -0
- package/lib/features/theme/templates.js +283 -0
- package/lib/features.d.ts +15 -0
- package/lib/features.js +6 -0
- package/lib/features.manifest.d.ts +10 -0
- package/lib/features.manifest.js +6 -0
- package/lib/helpers/access.d.ts +62 -0
- package/lib/helpers/access.js +92 -0
- package/lib/helpers/api.d.ts +20 -0
- package/lib/helpers/api.js +44 -0
- package/lib/helpers/comment-guard.d.ts +145 -0
- package/lib/helpers/comment-guard.js +253 -0
- package/lib/helpers/comment-mail.d.ts +53 -0
- package/lib/helpers/comment-mail.js +188 -0
- package/lib/helpers/content-model.d.ts +50 -0
- package/lib/helpers/content-model.js +84 -0
- package/lib/helpers/content-paths.d.ts +107 -0
- package/lib/helpers/content-paths.js +200 -0
- package/lib/helpers/export-data.d.ts +85 -0
- package/lib/helpers/export-data.js +229 -0
- package/lib/helpers/gating.d.ts +53 -0
- package/lib/helpers/gating.js +83 -0
- package/lib/helpers/health.d.ts +91 -0
- package/lib/helpers/health.js +279 -0
- package/lib/helpers/heartbeat.d.ts +35 -0
- package/lib/helpers/heartbeat.js +55 -0
- package/lib/helpers/image-size.d.ts +18 -0
- package/lib/helpers/image-size.js +83 -0
- package/lib/helpers/images.d.ts +29 -0
- package/lib/helpers/images.js +41 -0
- package/lib/helpers/mail-templates.d.ts +11 -0
- package/lib/helpers/mail-templates.js +42 -0
- package/lib/helpers/publish-due.d.ts +28 -0
- package/lib/helpers/publish-due.js +67 -0
- package/lib/helpers/redirects.d.ts +43 -0
- package/lib/helpers/redirects.js +80 -0
- package/lib/helpers/seo-fields.d.ts +13 -0
- package/lib/helpers/seo-fields.js +52 -0
- package/lib/helpers/session.d.ts +30 -0
- package/lib/helpers/session.js +27 -0
- package/lib/helpers/site-settings.d.ts +98 -0
- package/lib/helpers/site-settings.js +232 -0
- package/lib/helpers/site.d.ts +14 -0
- package/lib/helpers/site.js +23 -0
- package/lib/helpers/write-hooks.d.ts +33 -0
- package/lib/helpers/write-hooks.js +37 -0
- package/lib/hooks.d.ts +17 -0
- package/lib/hooks.js +101 -0
- package/lib/host/core-tasks.d.ts +4 -0
- package/lib/host/core-tasks.js +26 -0
- package/lib/host/cron.d.ts +39 -0
- package/lib/host/cron.js +85 -0
- package/lib/host/enabled.d.ts +4 -0
- package/lib/host/enabled.js +40 -0
- package/lib/host/index.d.ts +81 -0
- package/lib/host/index.js +87 -0
- package/lib/host/manifest.d.ts +34 -0
- package/lib/host/manifest.js +7 -0
- package/lib/host/menu.d.ts +18 -0
- package/lib/host/menu.js +15 -0
- package/lib/main.d.ts +1 -0
- package/lib/main.js +24 -0
- package/lib/middleware/api-auth.d.ts +59 -0
- package/lib/middleware/api-auth.js +164 -0
- package/lib/middleware/auth.d.ts +49 -0
- package/lib/middleware/auth.js +112 -0
- package/lib/middleware/cache.d.ts +17 -0
- package/lib/middleware/cache.js +52 -0
- package/lib/middleware/rate-limits.d.ts +199 -0
- package/lib/middleware/rate-limits.js +276 -0
- package/lib/middleware/viewer.d.ts +13 -0
- package/lib/middleware/viewer.js +16 -0
- package/lib/models/Category.d.ts +24 -0
- package/lib/models/Category.js +102 -0
- package/lib/models/Comment.d.ts +120 -0
- package/lib/models/Comment.js +283 -0
- package/lib/models/ContentType.d.ts +142 -0
- package/lib/models/ContentType.js +462 -0
- package/lib/models/Credential.d.ts +21 -0
- package/lib/models/Credential.js +31 -0
- package/lib/models/Media.d.ts +53 -0
- package/lib/models/Media.js +132 -0
- package/lib/models/Menu.d.ts +131 -0
- package/lib/models/Menu.js +312 -0
- package/lib/models/Page.d.ts +55 -0
- package/lib/models/Page.js +218 -0
- package/lib/models/Post.d.ts +136 -0
- package/lib/models/Post.js +406 -0
- package/lib/models/PostLock.d.ts +67 -0
- package/lib/models/PostLock.js +112 -0
- package/lib/models/PostRevision.d.ts +45 -0
- package/lib/models/PostRevision.js +82 -0
- package/lib/models/PublicContent.d.ts +283 -0
- package/lib/models/PublicContent.js +577 -0
- package/lib/models/Redirect.d.ts +123 -0
- package/lib/models/Redirect.js +225 -0
- package/lib/models/Settings.d.ts +61 -0
- package/lib/models/Settings.js +128 -0
- package/lib/models/Snippet.d.ts +36 -0
- package/lib/models/Snippet.js +64 -0
- package/lib/models/Tag.d.ts +23 -0
- package/lib/models/Tag.js +117 -0
- package/lib/models/Taxonomy.d.ts +127 -0
- package/lib/models/Taxonomy.js +321 -0
- package/lib/models/TwoFactor.d.ts +26 -0
- package/lib/models/TwoFactor.js +60 -0
- package/lib/models/User.d.ts +49 -0
- package/lib/models/User.js +128 -0
- package/lib/models/derive.d.ts +69 -0
- package/lib/models/derive.js +81 -0
- package/lib/models/pagination.d.ts +30 -0
- package/lib/models/pagination.js +36 -0
- package/lib/models/visibility.d.ts +31 -0
- package/lib/models/visibility.js +47 -0
- package/lib/routes/App.d.ts +2 -0
- package/lib/routes/App.js +146 -0
- package/lib/routes/api/admin.d.ts +6 -0
- package/lib/routes/api/admin.js +11 -0
- package/lib/routes/api/auth.d.ts +6 -0
- package/lib/routes/api/auth.js +24 -0
- package/lib/routes/api/authors.d.ts +5 -0
- package/lib/routes/api/authors.js +8 -0
- package/lib/routes/api/categories.d.ts +8 -0
- package/lib/routes/api/categories.js +15 -0
- package/lib/routes/api/comments.d.ts +8 -0
- package/lib/routes/api/comments.js +35 -0
- package/lib/routes/api/content-model.d.ts +17 -0
- package/lib/routes/api/content-model.js +67 -0
- package/lib/routes/api/cron.d.ts +6 -0
- package/lib/routes/api/cron.js +9 -0
- package/lib/routes/api/export.d.ts +5 -0
- package/lib/routes/api/export.js +11 -0
- package/lib/routes/api/feed.d.ts +5 -0
- package/lib/routes/api/feed.js +16 -0
- package/lib/routes/api/media.d.ts +8 -0
- package/lib/routes/api/media.js +25 -0
- package/lib/routes/api/navigation.d.ts +12 -0
- package/lib/routes/api/navigation.js +17 -0
- package/lib/routes/api/ops.d.ts +9 -0
- package/lib/routes/api/ops.js +19 -0
- package/lib/routes/api/pages.d.ts +8 -0
- package/lib/routes/api/pages.js +21 -0
- package/lib/routes/api/passkeys.d.ts +7 -0
- package/lib/routes/api/passkeys.js +13 -0
- package/lib/routes/api/posts.d.ts +8 -0
- package/lib/routes/api/posts.js +39 -0
- package/lib/routes/api/profile.d.ts +6 -0
- package/lib/routes/api/profile.js +8 -0
- package/lib/routes/api/redirects.d.ts +12 -0
- package/lib/routes/api/redirects.js +13 -0
- package/lib/routes/api/settings.d.ts +8 -0
- package/lib/routes/api/settings.js +15 -0
- package/lib/routes/api/snippets.d.ts +15 -0
- package/lib/routes/api/snippets.js +16 -0
- package/lib/routes/api/tags.d.ts +8 -0
- package/lib/routes/api/tags.js +14 -0
- package/lib/routes/api/tokens.d.ts +7 -0
- package/lib/routes/api/tokens.js +15 -0
- package/lib/routes/api/two-factor.d.ts +7 -0
- package/lib/routes/api/two-factor.js +15 -0
- package/lib/routes/api/users.d.ts +8 -0
- package/lib/routes/api/users.js +25 -0
- package/lib/routes/api/v1.d.ts +12 -0
- package/lib/routes/api/v1.js +55 -0
- package/lib/routes/api/verification.d.ts +6 -0
- package/lib/routes/api/verification.js +11 -0
- package/lib/server/index.d.ts +24 -0
- package/lib/server/index.js +110 -0
- package/lib/types/index.d.ts +298 -0
- package/lib/types/index.js +1 -0
- package/lib/vite.d.ts +2 -0
- package/lib/vite.js +101 -0
- package/mail/comment-moderation.html +27 -0
- package/mail/comment-moderation.txt +13 -0
- package/mail/comment-reply.html +28 -0
- package/mail/comment-reply.txt +14 -0
- package/mail/invitation.html +25 -0
- package/mail/invitation.txt +11 -0
- package/mail/member-magic-link.html +23 -0
- package/mail/member-magic-link.txt +10 -0
- package/mail/newsletter.html +26 -0
- package/mail/newsletter.txt +13 -0
- package/mail/password-reset.html +26 -0
- package/mail/password-reset.txt +10 -0
- package/mail/verify-email.html +25 -0
- package/mail/verify-email.txt +8 -0
- package/package.json +85 -4
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small client for MySendbox's REST API (https://api.mysendbox.com/v1).
|
|
3
|
+
*
|
|
4
|
+
* Only the calls this feature makes. Every call answers a result rather than
|
|
5
|
+
* throwing, so callers decide what a failure means: a 4xx is MySendbox saying
|
|
6
|
+
* no (not worth retrying until something changes), a 5xx, 429 or network
|
|
7
|
+
* error is MySendbox being unavailable (retry later).
|
|
8
|
+
*
|
|
9
|
+
* `fetch` and the base URL are injectable, so tests run against a fake.
|
|
10
|
+
*/
|
|
11
|
+
export const DEFAULT_BASE_URL = 'https://api.mysendbox.com/v1';
|
|
12
|
+
export function createClient({ apiKey, baseUrl = DEFAULT_BASE_URL, fetch: fetchImpl = fetch }) {
|
|
13
|
+
async function call(method, path, body) {
|
|
14
|
+
let res;
|
|
15
|
+
try {
|
|
16
|
+
res = await fetchImpl(`${baseUrl.replace(/\/$/, '')}${path}`, {
|
|
17
|
+
method,
|
|
18
|
+
headers: {
|
|
19
|
+
Authorization: `Bearer ${apiKey}`,
|
|
20
|
+
...(body === undefined ? {} : { 'Content-Type': 'application/json' })
|
|
21
|
+
},
|
|
22
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
23
|
+
signal: AbortSignal.timeout(15_000)
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
return { ok: false, status: 0, error: `MySendbox could not be reached: ${err?.message ?? err}`, retry: true };
|
|
28
|
+
}
|
|
29
|
+
const text = await res.text().catch(() => '');
|
|
30
|
+
let data = null;
|
|
31
|
+
try {
|
|
32
|
+
data = text ? JSON.parse(text) : null;
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
data = null;
|
|
36
|
+
}
|
|
37
|
+
if (res.ok)
|
|
38
|
+
return { ok: true, status: res.status, data: data };
|
|
39
|
+
const error = String(data?.error ?? data?.message ?? (text.slice(0, 200) || `HTTP ${res.status}`)).slice(0, 500);
|
|
40
|
+
return { ok: false, status: res.status, error, code: data?.code ?? data?.error_code, retry: res.status >= 500 || res.status === 429 };
|
|
41
|
+
}
|
|
42
|
+
const enc = encodeURIComponent;
|
|
43
|
+
return {
|
|
44
|
+
createAudience: (name) => call('POST', '/audiences', { name }),
|
|
45
|
+
listAudiences: () => call('GET', '/audiences'),
|
|
46
|
+
/** Add or update by email. 201 new, 200 existing; 409 for someone who unsubscribed or was erased. */
|
|
47
|
+
upsertContact: (contact) => call('POST', '/contacts', contact),
|
|
48
|
+
unsubscribeContact: (id, note) => call('POST', `/contacts/${enc(id)}/unsubscribe`, note ? { note } : {}),
|
|
49
|
+
eraseContact: (id) => call('DELETE', `/contacts/${enc(id)}`),
|
|
50
|
+
/** A signup form's double opt-in: MySendbox sends the confirmation mail. */
|
|
51
|
+
subscribeThroughForm: (formId, person) => call('POST', `/forms/${enc(formId)}/subscribe`, person),
|
|
52
|
+
listContacts: (query) => {
|
|
53
|
+
const params = new URLSearchParams(Object.entries(query).filter(([, v]) => v));
|
|
54
|
+
return call('GET', `/contacts?${params}`);
|
|
55
|
+
},
|
|
56
|
+
createBroadcast: (broadcast) => call('POST', '/broadcasts', broadcast),
|
|
57
|
+
scheduleNow: (id) => call('POST', `/broadcasts/${enc(id)}/schedule`, {}),
|
|
58
|
+
getBroadcast: (id) => call('GET', `/broadcasts/${enc(id)}`),
|
|
59
|
+
sendableCount: (audienceId) => call('GET', `/segments/count?audience=${enc(audienceId)}`)
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How the feature finds MySendbox: environment for secrets, site settings for
|
|
3
|
+
* everything an admin may choose.
|
|
4
|
+
*
|
|
5
|
+
* - `MYSENDBOX_API_KEY`: a key with contacts and broadcasts scopes and no
|
|
6
|
+
* domain or mailbox limits. Without it the feature does nothing at all.
|
|
7
|
+
* - `MYSENDBOX_WEBHOOK_SECRET`: the `whsec_…` secret of the webhook pointed
|
|
8
|
+
* at `/api/webhooks/mysendbox`. Without it that endpoint answers 404.
|
|
9
|
+
* - `MYSENDBOX_API_URL`: only to point at another MySendbox, such as in tests.
|
|
10
|
+
*/
|
|
11
|
+
import { type SettingDefinition } from '../../helpers/site-settings.ts';
|
|
12
|
+
import { type ClientOptions, type MySendboxClient } from './client.ts';
|
|
13
|
+
export declare const SETTINGS: Record<string, SettingDefinition>;
|
|
14
|
+
export declare function apiKey(): string;
|
|
15
|
+
export declare function webhookSecret(): string;
|
|
16
|
+
export declare function isConfigured(): boolean;
|
|
17
|
+
/** For tests: a fake `fetch`, or another base URL. */
|
|
18
|
+
export declare function setClientOptions(next: Partial<ClientOptions>): void;
|
|
19
|
+
export declare function client(): MySendboxClient;
|
|
20
|
+
/** A setting, trimmed; blank is ''. */
|
|
21
|
+
export declare function setting(key: keyof typeof SETTINGS & string): Promise<string>;
|
|
22
|
+
/**
|
|
23
|
+
* The site's audience id, making the audience when there is none yet.
|
|
24
|
+
* Throws when MySendbox can't be reached or refuses, which is what callers
|
|
25
|
+
* want: nothing can be synced or sent without it.
|
|
26
|
+
*/
|
|
27
|
+
export declare function audienceId(): Promise<string>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How the feature finds MySendbox: environment for secrets, site settings for
|
|
3
|
+
* everything an admin may choose.
|
|
4
|
+
*
|
|
5
|
+
* - `MYSENDBOX_API_KEY`: a key with contacts and broadcasts scopes and no
|
|
6
|
+
* domain or mailbox limits. Without it the feature does nothing at all.
|
|
7
|
+
* - `MYSENDBOX_WEBHOOK_SECRET`: the `whsec_…` secret of the webhook pointed
|
|
8
|
+
* at `/api/webhooks/mysendbox`. Without it that endpoint answers 404.
|
|
9
|
+
* - `MYSENDBOX_API_URL`: only to point at another MySendbox, such as in tests.
|
|
10
|
+
*/
|
|
11
|
+
import { Settings } from "../../models/Settings.js";
|
|
12
|
+
import { checks } from "../../helpers/site-settings.js";
|
|
13
|
+
import { createClient } from "./client.js";
|
|
14
|
+
export const SETTINGS = {
|
|
15
|
+
// The audience (MySendbox list) this site's members are in. Made on first
|
|
16
|
+
// use when blank, named after the site.
|
|
17
|
+
mysendbox_audience_id: { group: 'mysendbox', check: checks.text(64) },
|
|
18
|
+
// Blank: the workspace's default From in MySendbox's marketing settings.
|
|
19
|
+
mysendbox_from_address: { group: 'mysendbox', check: checks.emailOrBlank },
|
|
20
|
+
mysendbox_from_name: { group: 'mysendbox', check: checks.text(100) },
|
|
21
|
+
// How members an admin added or imported agreed to newsletters, in the
|
|
22
|
+
// admin's words. Blank: they are not copied as subscribed (see contacts.ts).
|
|
23
|
+
mysendbox_consent_attestation: { group: 'mysendbox', check: checks.text(1000) },
|
|
24
|
+
// A MySendbox signup form (`frm_…`). With one, an added or imported member
|
|
25
|
+
// without an attestation gets its double opt-in mail instead.
|
|
26
|
+
mysendbox_form_id: { group: 'mysendbox', check: checks.text(64) }
|
|
27
|
+
};
|
|
28
|
+
export function apiKey() {
|
|
29
|
+
return process.env.MYSENDBOX_API_KEY?.trim() || '';
|
|
30
|
+
}
|
|
31
|
+
export function webhookSecret() {
|
|
32
|
+
return process.env.MYSENDBOX_WEBHOOK_SECRET?.trim() || '';
|
|
33
|
+
}
|
|
34
|
+
export function isConfigured() {
|
|
35
|
+
return apiKey() !== '';
|
|
36
|
+
}
|
|
37
|
+
let overrides = {};
|
|
38
|
+
/** For tests: a fake `fetch`, or another base URL. */
|
|
39
|
+
export function setClientOptions(next) {
|
|
40
|
+
overrides = next;
|
|
41
|
+
}
|
|
42
|
+
export function client() {
|
|
43
|
+
return createClient({
|
|
44
|
+
apiKey: apiKey(),
|
|
45
|
+
...(process.env.MYSENDBOX_API_URL ? { baseUrl: process.env.MYSENDBOX_API_URL } : {}),
|
|
46
|
+
...overrides
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/** A setting, trimmed; blank is ''. */
|
|
50
|
+
export async function setting(key) {
|
|
51
|
+
return (await Settings.get(key))?.trim() || '';
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The site's audience id, making the audience when there is none yet.
|
|
55
|
+
* Throws when MySendbox can't be reached or refuses, which is what callers
|
|
56
|
+
* want: nothing can be synced or sent without it.
|
|
57
|
+
*/
|
|
58
|
+
export async function audienceId() {
|
|
59
|
+
const known = await setting('mysendbox_audience_id');
|
|
60
|
+
if (known)
|
|
61
|
+
return known;
|
|
62
|
+
const name = `${(await Settings.getSiteName()) || 'Site'} members`.slice(0, 100);
|
|
63
|
+
const created = await client().createAudience(name);
|
|
64
|
+
let id = created.ok ? created.data?.id : undefined;
|
|
65
|
+
if (!created.ok && created.status === 409) {
|
|
66
|
+
// Made by an earlier attempt whose answer was lost: find it by name.
|
|
67
|
+
const listed = await client().listAudiences();
|
|
68
|
+
id = listed.ok ? listed.data?.audiences?.find((a) => a.name === name)?.id : undefined;
|
|
69
|
+
}
|
|
70
|
+
if (!id)
|
|
71
|
+
throw new Error(created.ok ? 'MySendbox did not return an audience id' : `MySendbox refused to make the audience: ${created.error}`);
|
|
72
|
+
await Settings.set('mysendbox_audience_id', id, 'mysendbox');
|
|
73
|
+
return id;
|
|
74
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Members to MySendbox contacts, by state.
|
|
3
|
+
*
|
|
4
|
+
* A member change marks the member's row in `mysendbox_contacts` dirty;
|
|
5
|
+
* `syncMember` then reads the member as it is *now* and makes MySendbox
|
|
6
|
+
* match. So a lost event costs a delay, not a difference, and syncing twice
|
|
7
|
+
* does nothing the second time. Dirty rows are synced in the background right
|
|
8
|
+
* away, again before every broadcast, and by the daily reconcile.
|
|
9
|
+
*
|
|
10
|
+
* The members table is read directly (it is a core migration, and stays when
|
|
11
|
+
* the members feature is removed). Writes to members go through the members
|
|
12
|
+
* feature's `newsletter.subscription` hook, never to its table.
|
|
13
|
+
*
|
|
14
|
+
* ## Consent
|
|
15
|
+
*
|
|
16
|
+
* MySendbox keeps a consent record per contact. A member is copied as
|
|
17
|
+
* subscribed only when the CMS knows how they agreed:
|
|
18
|
+
*
|
|
19
|
+
* - they signed up themselves (`source = signup`), or ticked "send me posts"
|
|
20
|
+
* themselves later, with a confirmed address;
|
|
21
|
+
* - or an admin added or imported them and the site has a consent attestation
|
|
22
|
+
* (`mysendbox_consent_attestation`), the admin's own words.
|
|
23
|
+
*
|
|
24
|
+
* Otherwise, with a signup form set (`mysendbox_form_id`), they get MySendbox's
|
|
25
|
+
* double opt-in mail; and with neither they are left out (`needs_consent`)
|
|
26
|
+
* and the health page counts them.
|
|
27
|
+
*
|
|
28
|
+
* MySendbox decides unsubscribes. Someone who unsubscribed there can only
|
|
29
|
+
* subscribe again through a MySendbox form, so the CMS never re-adds them
|
|
30
|
+
* (MySendbox answers 409; the row is `refused`).
|
|
31
|
+
*/
|
|
32
|
+
/** The members feature's names, spelled out: features don't import each other. Checked by mysendbox.test.js. */
|
|
33
|
+
export declare const MEMBER_EVENTS: {
|
|
34
|
+
readonly CREATED: "member.created";
|
|
35
|
+
readonly UPDATED: "member.updated";
|
|
36
|
+
readonly DELETED: "member.deleted";
|
|
37
|
+
};
|
|
38
|
+
export declare const SUBSCRIPTION_HOOK = "newsletter.subscription";
|
|
39
|
+
export declare const PROVIDER = "mysendbox";
|
|
40
|
+
/** Tries before a sync failure that MySendbox called temporary stops being retried by every caller but the daily task. */
|
|
41
|
+
export declare const MAX_ATTEMPTS = 5;
|
|
42
|
+
interface MemberRow {
|
|
43
|
+
id: number;
|
|
44
|
+
email: string;
|
|
45
|
+
name: string | null;
|
|
46
|
+
status: 'active' | 'unsubscribed';
|
|
47
|
+
source: 'signup' | 'admin' | 'import';
|
|
48
|
+
email_verified_at: string | null;
|
|
49
|
+
created_at: string;
|
|
50
|
+
}
|
|
51
|
+
export interface ContactRow {
|
|
52
|
+
member_id: number;
|
|
53
|
+
email: string | null;
|
|
54
|
+
contact_id: string | null;
|
|
55
|
+
consent: string | null;
|
|
56
|
+
remote_status: string | null;
|
|
57
|
+
remote_at: number | null;
|
|
58
|
+
state: 'dirty' | 'synced' | 'erase' | 'needs_consent' | 'refused';
|
|
59
|
+
attempts: number;
|
|
60
|
+
last_error: string | null;
|
|
61
|
+
}
|
|
62
|
+
/** Make sure the member has a row, and mark it for syncing. `consent`, when given, is kept. */
|
|
63
|
+
export declare function markDirty(memberId: number, { consent, erase }?: {
|
|
64
|
+
consent?: string;
|
|
65
|
+
erase?: boolean;
|
|
66
|
+
}): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Make MySendbox match this member. Returns the row's state afterwards.
|
|
69
|
+
* Never throws for MySendbox's answers; throws only when the database does.
|
|
70
|
+
*/
|
|
71
|
+
export declare function syncMember(memberId: number): Promise<ContactRow['state'] | 'gone'>;
|
|
72
|
+
/** Sync up to `limit` dirty or erased rows, oldest change first. */
|
|
73
|
+
export declare function syncDirty(limit?: number, { includeExhausted }?: {
|
|
74
|
+
includeExhausted?: boolean | undefined;
|
|
75
|
+
}): Promise<Record<string, number>>;
|
|
76
|
+
/** For tests: run syncs only when asked, so counts are deterministic. */
|
|
77
|
+
export declare function setBackgroundSync(on: boolean): void;
|
|
78
|
+
/** Run a sync without the request waiting for it, and never let it throw. */
|
|
79
|
+
export declare function inBackground(job: () => Promise<unknown>): void;
|
|
80
|
+
export declare function onMemberCreated({ member }: {
|
|
81
|
+
member?: MemberRow;
|
|
82
|
+
}): Promise<void>;
|
|
83
|
+
export declare function onMemberUpdated({ member, previous, origin }: {
|
|
84
|
+
member?: MemberRow;
|
|
85
|
+
previous?: MemberRow;
|
|
86
|
+
origin?: string;
|
|
87
|
+
}): Promise<void>;
|
|
88
|
+
export declare function onMemberDeleted({ member }: {
|
|
89
|
+
member?: MemberRow;
|
|
90
|
+
}): Promise<void>;
|
|
91
|
+
/**
|
|
92
|
+
* Copy members across: mark every member without a synced contact dirty, and
|
|
93
|
+
* sync a batch. The admin's "Copy members to MySendbox" calls it until
|
|
94
|
+
* `remaining` is 0. Safe to run any number of times.
|
|
95
|
+
*/
|
|
96
|
+
export declare function backfill(batch?: number): Promise<{
|
|
97
|
+
done: Record<string, number>;
|
|
98
|
+
remaining: number;
|
|
99
|
+
}>;
|
|
100
|
+
/** Counts by state, for the settings card and the health page. */
|
|
101
|
+
export declare function contactCounts(): Promise<Record<string, number>>;
|
|
102
|
+
/**
|
|
103
|
+
* A contact's status as MySendbox reports it (webhook or the daily pull).
|
|
104
|
+
* The member follows, unless the CMS already heard something newer.
|
|
105
|
+
*/
|
|
106
|
+
export declare function applyRemoteStatus(contact: {
|
|
107
|
+
contactId?: string | null;
|
|
108
|
+
email?: string | null;
|
|
109
|
+
memberId?: number | null;
|
|
110
|
+
status: string;
|
|
111
|
+
at?: number | null;
|
|
112
|
+
}): Promise<'applied' | 'stale' | 'unknown'>;
|
|
113
|
+
/**
|
|
114
|
+
* The daily backstop for missed webhooks: everyone MySendbox stopped mailing
|
|
115
|
+
* in the site's audience, paged. Members still active here are unsubscribed.
|
|
116
|
+
*/
|
|
117
|
+
export declare function pullUnsubscribes(maxPages?: number): Promise<{
|
|
118
|
+
checked: number;
|
|
119
|
+
applied: number;
|
|
120
|
+
}>;
|
|
121
|
+
export {};
|
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Members to MySendbox contacts, by state.
|
|
3
|
+
*
|
|
4
|
+
* A member change marks the member's row in `mysendbox_contacts` dirty;
|
|
5
|
+
* `syncMember` then reads the member as it is *now* and makes MySendbox
|
|
6
|
+
* match. So a lost event costs a delay, not a difference, and syncing twice
|
|
7
|
+
* does nothing the second time. Dirty rows are synced in the background right
|
|
8
|
+
* away, again before every broadcast, and by the daily reconcile.
|
|
9
|
+
*
|
|
10
|
+
* The members table is read directly (it is a core migration, and stays when
|
|
11
|
+
* the members feature is removed). Writes to members go through the members
|
|
12
|
+
* feature's `newsletter.subscription` hook, never to its table.
|
|
13
|
+
*
|
|
14
|
+
* ## Consent
|
|
15
|
+
*
|
|
16
|
+
* MySendbox keeps a consent record per contact. A member is copied as
|
|
17
|
+
* subscribed only when the CMS knows how they agreed:
|
|
18
|
+
*
|
|
19
|
+
* - they signed up themselves (`source = signup`), or ticked "send me posts"
|
|
20
|
+
* themselves later, with a confirmed address;
|
|
21
|
+
* - or an admin added or imported them and the site has a consent attestation
|
|
22
|
+
* (`mysendbox_consent_attestation`), the admin's own words.
|
|
23
|
+
*
|
|
24
|
+
* Otherwise, with a signup form set (`mysendbox_form_id`), they get MySendbox's
|
|
25
|
+
* double opt-in mail; and with neither they are left out (`needs_consent`)
|
|
26
|
+
* and the health page counts them.
|
|
27
|
+
*
|
|
28
|
+
* MySendbox decides unsubscribes. Someone who unsubscribed there can only
|
|
29
|
+
* subscribe again through a MySendbox form, so the CMS never re-adds them
|
|
30
|
+
* (MySendbox answers 409; the row is `refused`).
|
|
31
|
+
*/
|
|
32
|
+
import { getDb } from '@basicbenframework/core/db';
|
|
33
|
+
import { hooks } from '@basicbenframework/core/hooks';
|
|
34
|
+
import { Settings } from "../../models/Settings.js";
|
|
35
|
+
import { client, audienceId, setting } from "./config.js";
|
|
36
|
+
/** The members feature's names, spelled out: features don't import each other. Checked by mysendbox.test.js. */
|
|
37
|
+
export const MEMBER_EVENTS = {
|
|
38
|
+
CREATED: 'member.created',
|
|
39
|
+
UPDATED: 'member.updated',
|
|
40
|
+
DELETED: 'member.deleted'
|
|
41
|
+
};
|
|
42
|
+
export const SUBSCRIPTION_HOOK = 'newsletter.subscription';
|
|
43
|
+
export const PROVIDER = 'mysendbox';
|
|
44
|
+
/** Tries before a sync failure that MySendbox called temporary stops being retried by every caller but the daily task. */
|
|
45
|
+
export const MAX_ATTEMPTS = 5;
|
|
46
|
+
const now = () => new Date().toISOString();
|
|
47
|
+
async function rowFor(memberId) {
|
|
48
|
+
const db = await getDb();
|
|
49
|
+
return db.get('SELECT * FROM mysendbox_contacts WHERE member_id = ?', [memberId]);
|
|
50
|
+
}
|
|
51
|
+
/** Make sure the member has a row, and mark it for syncing. `consent`, when given, is kept. */
|
|
52
|
+
export async function markDirty(memberId, { consent, erase = false } = {}) {
|
|
53
|
+
const db = await getDb();
|
|
54
|
+
const state = erase ? 'erase' : 'dirty';
|
|
55
|
+
const existing = await rowFor(memberId);
|
|
56
|
+
if (existing) {
|
|
57
|
+
await db.run('UPDATE mysendbox_contacts SET state = ?, attempts = 0, consent = COALESCE(?, consent), updated_at = ? WHERE member_id = ?', [state, consent ?? null, now(), memberId]);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
await db.run('INSERT INTO mysendbox_contacts (member_id, state, consent, attempts, updated_at) VALUES (?, ?, ?, 0, ?)', [memberId, state, consent ?? null, now()]);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async function save(memberId, changes) {
|
|
64
|
+
const db = await getDb();
|
|
65
|
+
const { synced, ...fields } = changes;
|
|
66
|
+
const keys = Object.keys(fields);
|
|
67
|
+
const sets = keys.map((k) => `${k} = ?`);
|
|
68
|
+
const params = keys.map((k) => fields[k]);
|
|
69
|
+
if (synced) {
|
|
70
|
+
sets.push('synced_at = ?');
|
|
71
|
+
params.push(now());
|
|
72
|
+
}
|
|
73
|
+
sets.push('updated_at = ?');
|
|
74
|
+
params.push(now());
|
|
75
|
+
await db.run(`UPDATE mysendbox_contacts SET ${sets.join(', ')} WHERE member_id = ?`, [...params, memberId]);
|
|
76
|
+
}
|
|
77
|
+
/** A MySendbox refusal or outage, recorded on the row. Temporary ones stay dirty and are retried. */
|
|
78
|
+
async function failed(memberId, row, result) {
|
|
79
|
+
const state = result.retry ? 'dirty' : 'refused';
|
|
80
|
+
await save(memberId, { state, attempts: Number(row.attempts) + 1, last_error: result.error.slice(0, 500) });
|
|
81
|
+
return state;
|
|
82
|
+
}
|
|
83
|
+
/** How this member agreed, or what to do instead. */
|
|
84
|
+
async function consentFor(member, row, siteName) {
|
|
85
|
+
if (member.source === 'signup' || row.consent) {
|
|
86
|
+
if (!member.email_verified_at)
|
|
87
|
+
return { none: 'Waiting for the member to confirm their address.' };
|
|
88
|
+
return {
|
|
89
|
+
confirmed: row.consent || `Signed up for ${siteName} with this address and confirmed it by an emailed link (${member.created_at}).`
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const attestation = await setting('mysendbox_consent_attestation');
|
|
93
|
+
if (attestation)
|
|
94
|
+
return { confirmed: `Added to ${siteName} by its admin (${member.source}). The admin's attestation: ${attestation}`.slice(0, 2000) };
|
|
95
|
+
const form = await setting('mysendbox_form_id');
|
|
96
|
+
if (form)
|
|
97
|
+
return { form };
|
|
98
|
+
return { none: 'Added by an admin or imported, with no consent attestation or signup form set in the MySendbox settings.' };
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Make MySendbox match this member. Returns the row's state afterwards.
|
|
102
|
+
* Never throws for MySendbox's answers; throws only when the database does.
|
|
103
|
+
*/
|
|
104
|
+
export async function syncMember(memberId) {
|
|
105
|
+
const db = await getDb();
|
|
106
|
+
const row = await rowFor(memberId);
|
|
107
|
+
if (!row)
|
|
108
|
+
return 'gone';
|
|
109
|
+
const member = (await db.get('SELECT id, email, name, status, source, email_verified_at, created_at FROM members WHERE id = ?', [memberId]));
|
|
110
|
+
const api = client();
|
|
111
|
+
// Deleted in the CMS: erased in MySendbox too. Deleting a member is the
|
|
112
|
+
// admin's "forget this person", and MySendbox's erase is the same request.
|
|
113
|
+
if (!member || row.state === 'erase') {
|
|
114
|
+
if (row.contact_id) {
|
|
115
|
+
const erased = await api.eraseContact(row.contact_id);
|
|
116
|
+
if (!erased.ok && erased.status !== 404)
|
|
117
|
+
return failed(memberId, row, erased);
|
|
118
|
+
}
|
|
119
|
+
if (!member) {
|
|
120
|
+
await db.run('DELETE FROM mysendbox_contacts WHERE member_id = ?', [memberId]);
|
|
121
|
+
return 'gone';
|
|
122
|
+
}
|
|
123
|
+
await save(memberId, { contact_id: null, remote_status: 'erased', state: 'synced', last_error: null, synced: true });
|
|
124
|
+
return 'synced';
|
|
125
|
+
}
|
|
126
|
+
// A new address is a new contact (MySendbox can't change a contact's
|
|
127
|
+
// address); the old one stops getting mail.
|
|
128
|
+
if (row.contact_id && row.email && row.email !== member.email) {
|
|
129
|
+
const stopped = await api.unsubscribeContact(row.contact_id, 'The member changed their address in the CMS.');
|
|
130
|
+
if (!stopped.ok && stopped.status !== 404)
|
|
131
|
+
return failed(memberId, row, stopped);
|
|
132
|
+
await save(memberId, { contact_id: null, remote_status: null });
|
|
133
|
+
row.contact_id = null;
|
|
134
|
+
row.remote_status = null;
|
|
135
|
+
}
|
|
136
|
+
if (member.status !== 'active') {
|
|
137
|
+
if (row.contact_id && row.remote_status !== 'unsubscribed' && row.remote_status !== 'erased') {
|
|
138
|
+
const stopped = await api.unsubscribeContact(row.contact_id, 'Unsubscribed in the CMS.');
|
|
139
|
+
if (!stopped.ok && stopped.status !== 404)
|
|
140
|
+
return failed(memberId, row, stopped);
|
|
141
|
+
await save(memberId, { remote_status: 'unsubscribed', remote_at: Date.now() });
|
|
142
|
+
}
|
|
143
|
+
await save(memberId, { email: member.email, state: 'synced', last_error: null, attempts: 0, synced: true });
|
|
144
|
+
return 'synced';
|
|
145
|
+
}
|
|
146
|
+
const siteName = (await Settings.getSiteName()) || 'this site';
|
|
147
|
+
const consent = await consentFor(member, row, siteName);
|
|
148
|
+
if ('none' in consent) {
|
|
149
|
+
await save(memberId, { email: member.email, state: 'needs_consent', last_error: consent.none });
|
|
150
|
+
return 'needs_consent';
|
|
151
|
+
}
|
|
152
|
+
let audience;
|
|
153
|
+
try {
|
|
154
|
+
audience = await audienceId();
|
|
155
|
+
}
|
|
156
|
+
catch (err) {
|
|
157
|
+
return failed(memberId, row, { ok: false, status: 0, error: err.message, retry: true });
|
|
158
|
+
}
|
|
159
|
+
if ('form' in consent) {
|
|
160
|
+
// Once: a second call would send a second confirmation mail.
|
|
161
|
+
if (row.contact_id) {
|
|
162
|
+
await save(memberId, { email: member.email, state: 'synced', last_error: null, synced: true });
|
|
163
|
+
return 'synced';
|
|
164
|
+
}
|
|
165
|
+
const asked = await api.subscribeThroughForm(consent.form, { email: member.email, name: member.name });
|
|
166
|
+
if (!asked.ok)
|
|
167
|
+
return failed(memberId, row, asked);
|
|
168
|
+
await save(memberId, {
|
|
169
|
+
email: member.email,
|
|
170
|
+
contact_id: asked.data?.contact?.id ?? null,
|
|
171
|
+
remote_status: asked.data?.contact?.status ?? 'pending',
|
|
172
|
+
remote_at: Date.now(),
|
|
173
|
+
state: 'synced',
|
|
174
|
+
attempts: 0,
|
|
175
|
+
last_error: null,
|
|
176
|
+
synced: true
|
|
177
|
+
});
|
|
178
|
+
return 'synced';
|
|
179
|
+
}
|
|
180
|
+
const added = await api.upsertContact({
|
|
181
|
+
email: member.email,
|
|
182
|
+
name: member.name,
|
|
183
|
+
audienceIds: [audience],
|
|
184
|
+
attributes: { cms_member_id: member.id },
|
|
185
|
+
consent: consent.confirmed,
|
|
186
|
+
confirmed: true
|
|
187
|
+
});
|
|
188
|
+
if (!added.ok) {
|
|
189
|
+
// 409: they unsubscribed (or were erased) in MySendbox, and only they
|
|
190
|
+
// can undo that. The CMS follows MySendbox rather than fighting it.
|
|
191
|
+
if (added.status === 409) {
|
|
192
|
+
// Through a signup form, they can: its confirmation mail is MySendbox's
|
|
193
|
+
// way of hearing it from them. Once per address, as above.
|
|
194
|
+
const form = await setting('mysendbox_form_id');
|
|
195
|
+
if (form && row.remote_status !== 'pending') {
|
|
196
|
+
const asked = await api.subscribeThroughForm(form, { email: member.email, name: member.name });
|
|
197
|
+
if (asked.ok) {
|
|
198
|
+
await save(memberId, {
|
|
199
|
+
email: member.email,
|
|
200
|
+
contact_id: asked.data?.contact?.id ?? row.contact_id,
|
|
201
|
+
remote_status: 'pending',
|
|
202
|
+
remote_at: Date.now(),
|
|
203
|
+
state: 'synced',
|
|
204
|
+
attempts: 0,
|
|
205
|
+
last_error: 'Asked to confirm by email: they had unsubscribed in MySendbox before.',
|
|
206
|
+
synced: true
|
|
207
|
+
});
|
|
208
|
+
return 'synced';
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
await save(memberId, { email: member.email, state: 'refused', attempts: Number(row.attempts) + 1, last_error: added.error.slice(0, 500) });
|
|
212
|
+
await hooks.fire(SUBSCRIPTION_HOOK, { memberId, email: member.email, subscribed: false, provider: PROVIDER });
|
|
213
|
+
return 'refused';
|
|
214
|
+
}
|
|
215
|
+
return failed(memberId, row, added);
|
|
216
|
+
}
|
|
217
|
+
await save(memberId, {
|
|
218
|
+
email: member.email,
|
|
219
|
+
contact_id: added.data?.id ?? row.contact_id,
|
|
220
|
+
remote_status: added.data?.status ?? 'subscribed',
|
|
221
|
+
remote_at: Date.now(),
|
|
222
|
+
state: 'synced',
|
|
223
|
+
attempts: 0,
|
|
224
|
+
last_error: null,
|
|
225
|
+
synced: true
|
|
226
|
+
});
|
|
227
|
+
return 'synced';
|
|
228
|
+
}
|
|
229
|
+
/** Sync up to `limit` dirty or erased rows, oldest change first. */
|
|
230
|
+
export async function syncDirty(limit = 100, { includeExhausted = false } = {}) {
|
|
231
|
+
const db = await getDb();
|
|
232
|
+
const rows = (await db.all(`SELECT member_id FROM mysendbox_contacts
|
|
233
|
+
WHERE state IN ('dirty', 'erase') ${includeExhausted ? '' : 'AND attempts < ?'}
|
|
234
|
+
ORDER BY updated_at ASC LIMIT ?`, includeExhausted ? [limit] : [MAX_ATTEMPTS, limit]));
|
|
235
|
+
const outcome = {};
|
|
236
|
+
for (const { member_id } of rows) {
|
|
237
|
+
const state = await syncMember(Number(member_id));
|
|
238
|
+
outcome[state] = (outcome[state] ?? 0) + 1;
|
|
239
|
+
}
|
|
240
|
+
return outcome;
|
|
241
|
+
}
|
|
242
|
+
let background = true;
|
|
243
|
+
/** For tests: run syncs only when asked, so counts are deterministic. */
|
|
244
|
+
export function setBackgroundSync(on) {
|
|
245
|
+
background = on;
|
|
246
|
+
}
|
|
247
|
+
/** Run a sync without the request waiting for it, and never let it throw. */
|
|
248
|
+
export function inBackground(job) {
|
|
249
|
+
if (!background)
|
|
250
|
+
return;
|
|
251
|
+
Promise.resolve().then(job).catch((err) => console.error('[mysendbox]', err?.message ?? err));
|
|
252
|
+
}
|
|
253
|
+
// --- The member event listeners ----------------------------------------------
|
|
254
|
+
export async function onMemberCreated({ member }) {
|
|
255
|
+
if (!member?.id)
|
|
256
|
+
return;
|
|
257
|
+
await markDirty(member.id);
|
|
258
|
+
inBackground(() => syncMember(member.id));
|
|
259
|
+
}
|
|
260
|
+
export async function onMemberUpdated({ member, previous, origin }) {
|
|
261
|
+
if (!member?.id)
|
|
262
|
+
return;
|
|
263
|
+
// Our own change coming back: MySendbox already has it.
|
|
264
|
+
if (origin === PROVIDER)
|
|
265
|
+
return;
|
|
266
|
+
// The member turning newsletters back on themselves is consent in its own right.
|
|
267
|
+
const resubscribed = origin === 'member' && member.status === 'active' && previous?.status !== 'active';
|
|
268
|
+
const consent = resubscribed ? `Turned newsletters on themselves in their ${new Date().toISOString().slice(0, 10)} account settings.` : undefined;
|
|
269
|
+
await markDirty(member.id, { consent });
|
|
270
|
+
inBackground(() => syncMember(member.id));
|
|
271
|
+
}
|
|
272
|
+
export async function onMemberDeleted({ member }) {
|
|
273
|
+
if (!member?.id)
|
|
274
|
+
return;
|
|
275
|
+
const row = await rowFor(member.id);
|
|
276
|
+
// Never in MySendbox: nothing to erase.
|
|
277
|
+
if (!row)
|
|
278
|
+
return;
|
|
279
|
+
await markDirty(member.id, { erase: true });
|
|
280
|
+
inBackground(() => syncMember(member.id));
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Copy members across: mark every member without a synced contact dirty, and
|
|
284
|
+
* sync a batch. The admin's "Copy members to MySendbox" calls it until
|
|
285
|
+
* `remaining` is 0. Safe to run any number of times.
|
|
286
|
+
*/
|
|
287
|
+
export async function backfill(batch = 50) {
|
|
288
|
+
const db = await getDb();
|
|
289
|
+
await db.run(`INSERT INTO mysendbox_contacts (member_id, state, attempts, updated_at)
|
|
290
|
+
SELECT m.id, 'dirty', 0, ? FROM members m
|
|
291
|
+
WHERE m.status = 'active' AND NOT EXISTS (SELECT 1 FROM mysendbox_contacts c WHERE c.member_id = m.id)`, [now()]);
|
|
292
|
+
// Ones that were waiting for consent get another look: the admin may just
|
|
293
|
+
// have written an attestation or set a form.
|
|
294
|
+
await db.run("UPDATE mysendbox_contacts SET state = 'dirty', attempts = 0 WHERE state = 'needs_consent'");
|
|
295
|
+
const done = await syncDirty(batch);
|
|
296
|
+
const left = await db.get("SELECT COUNT(*) AS n FROM mysendbox_contacts WHERE state IN ('dirty', 'erase') AND attempts < ?", [MAX_ATTEMPTS]);
|
|
297
|
+
return { done, remaining: Number(left?.n) || 0 };
|
|
298
|
+
}
|
|
299
|
+
/** Counts by state, for the settings card and the health page. */
|
|
300
|
+
export async function contactCounts() {
|
|
301
|
+
const db = await getDb();
|
|
302
|
+
try {
|
|
303
|
+
const rows = (await db.all('SELECT state, COUNT(*) AS n FROM mysendbox_contacts GROUP BY state'));
|
|
304
|
+
return Object.fromEntries(rows.map((r) => [r.state, Number(r.n) || 0]));
|
|
305
|
+
}
|
|
306
|
+
catch {
|
|
307
|
+
return {};
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
// --- MySendbox to the CMS --------------------------------------------------
|
|
311
|
+
const STOPPED = new Set(['unsubscribed', 'bounced', 'complained', 'cleaned']);
|
|
312
|
+
/**
|
|
313
|
+
* A contact's status as MySendbox reports it (webhook or the daily pull).
|
|
314
|
+
* The member follows, unless the CMS already heard something newer.
|
|
315
|
+
*/
|
|
316
|
+
export async function applyRemoteStatus(contact) {
|
|
317
|
+
const db = await getDb();
|
|
318
|
+
const at = Number(contact.at) || Date.now();
|
|
319
|
+
let row;
|
|
320
|
+
if (contact.contactId)
|
|
321
|
+
row = await db.get('SELECT * FROM mysendbox_contacts WHERE contact_id = ?', [contact.contactId]);
|
|
322
|
+
if (!row && contact.memberId)
|
|
323
|
+
row = await rowFor(contact.memberId);
|
|
324
|
+
if (!row && contact.email)
|
|
325
|
+
row = await db.get('SELECT * FROM mysendbox_contacts WHERE email = ?', [String(contact.email).toLowerCase()]);
|
|
326
|
+
if (!row)
|
|
327
|
+
return 'unknown';
|
|
328
|
+
if (row.remote_at && Number(row.remote_at) > at)
|
|
329
|
+
return 'stale';
|
|
330
|
+
await save(row.member_id, {
|
|
331
|
+
remote_status: contact.status,
|
|
332
|
+
remote_at: at,
|
|
333
|
+
...(contact.contactId && contact.status !== 'erased' ? { contact_id: contact.contactId } : {}),
|
|
334
|
+
...(contact.status === 'erased' ? { contact_id: null } : {})
|
|
335
|
+
});
|
|
336
|
+
if (STOPPED.has(contact.status) || contact.status === 'erased') {
|
|
337
|
+
await hooks.fire(SUBSCRIPTION_HOOK, { memberId: row.member_id, subscribed: false, provider: PROVIDER });
|
|
338
|
+
}
|
|
339
|
+
else if (contact.status === 'subscribed') {
|
|
340
|
+
await hooks.fire(SUBSCRIPTION_HOOK, { memberId: row.member_id, subscribed: true, provider: PROVIDER });
|
|
341
|
+
}
|
|
342
|
+
return 'applied';
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* The daily backstop for missed webhooks: everyone MySendbox stopped mailing
|
|
346
|
+
* in the site's audience, paged. Members still active here are unsubscribed.
|
|
347
|
+
*/
|
|
348
|
+
export async function pullUnsubscribes(maxPages = 20) {
|
|
349
|
+
const audience = await setting('mysendbox_audience_id');
|
|
350
|
+
if (!audience)
|
|
351
|
+
return { checked: 0, applied: 0 };
|
|
352
|
+
const outcome = { checked: 0, applied: 0 };
|
|
353
|
+
for (const status of STOPPED) {
|
|
354
|
+
let before;
|
|
355
|
+
for (let page = 0; page < maxPages; page++) {
|
|
356
|
+
const listed = await client().listContacts({ status, audience, before });
|
|
357
|
+
if (!listed.ok)
|
|
358
|
+
throw new Error(`MySendbox's contact list failed: ${listed.error}`);
|
|
359
|
+
for (const contact of listed.data?.contacts ?? []) {
|
|
360
|
+
outcome.checked++;
|
|
361
|
+
const memberId = Number(contact.attributes?.cms_member_id) || null;
|
|
362
|
+
// Not newer than what the CMS knows; `at` unknown, so only apply to
|
|
363
|
+
// members whose row doesn't already say so.
|
|
364
|
+
const db = await getDb();
|
|
365
|
+
const row = (await db.get('SELECT remote_status FROM mysendbox_contacts WHERE contact_id = ?', [contact.id]));
|
|
366
|
+
if (row?.remote_status === status)
|
|
367
|
+
continue;
|
|
368
|
+
const done = await applyRemoteStatus({ contactId: contact.id, email: contact.email, memberId, status, at: Date.now() });
|
|
369
|
+
if (done === 'applied')
|
|
370
|
+
outcome.applied++;
|
|
371
|
+
}
|
|
372
|
+
before = listed.data?.next ?? undefined;
|
|
373
|
+
if (!before)
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return outcome;
|
|
378
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Request, Response } from '../../types/index.ts';
|
|
2
|
+
export declare const MySendboxController: {
|
|
3
|
+
/**
|
|
4
|
+
* `GET /api/mysendbox/status`: whether it's set up, and how the members
|
|
5
|
+
* stand. `sendable` is MySendbox's own count of who a broadcast would reach
|
|
6
|
+
* now, after its six-month sunset.
|
|
7
|
+
*/
|
|
8
|
+
status(_req: Request, res: Response): Promise<void>;
|
|
9
|
+
/** `POST /api/mysendbox/backfill`: copy one batch. The admin calls it until `remaining` is 0. */
|
|
10
|
+
backfill(_req: Request, res: Response): Promise<void>;
|
|
11
|
+
};
|