@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,504 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The people who can sign in: listing them, changing what they may do,
|
|
3
|
+
* switching them off, removing them, and inviting new ones.
|
|
4
|
+
*
|
|
5
|
+
* Every route here is behind `user.manage`, which only an admin holds. That
|
|
6
|
+
* makes the admin role the one thing this controller must never let go to
|
|
7
|
+
* zero: an install with no active admin has nobody who can grant the role
|
|
8
|
+
* back, and the only fix is a hand-written SQL statement on the server. So
|
|
9
|
+
* every write that can take admin away from someone — a role change, a
|
|
10
|
+
* disable, a delete — goes through a conditional statement that refuses to
|
|
11
|
+
* remove the last active admin, and refuses to let an admin do it to
|
|
12
|
+
* themselves.
|
|
13
|
+
*
|
|
14
|
+
* The last-admin rule is enforced in the UPDATE/DELETE itself rather than by
|
|
15
|
+
* counting first and writing second. Two admins demoting each other at the
|
|
16
|
+
* same moment would both count two, both proceed, and leave none; a single
|
|
17
|
+
* conditional statement cannot interleave like that.
|
|
18
|
+
*/
|
|
19
|
+
import { validate, rules } from '@basicbenframework/core/validation';
|
|
20
|
+
import { hashPassword } from '@basicbenframework/core/auth';
|
|
21
|
+
import { ROLES, isValidRole } from '@basicbenframework/core/auth/permissions';
|
|
22
|
+
import { issueToken, redeemToken, hashToken } from '@basicbenframework/core/auth/tokens';
|
|
23
|
+
import { sendMail } from '@basicbenframework/core/mail';
|
|
24
|
+
// The site's mail/ first, then the CMS's.
|
|
25
|
+
import { renderMail } from "../helpers/mail-templates.js";
|
|
26
|
+
import { getDb } from '@basicbenframework/core/db';
|
|
27
|
+
import { User } from "../models/User.js";
|
|
28
|
+
import { paginationFrom, meta } from "../models/pagination.js";
|
|
29
|
+
import { issueSessionToken, revokeSessions } from "../helpers/session.js";
|
|
30
|
+
const appUrl = () => process.env.APP_URL || 'http://localhost:3000';
|
|
31
|
+
const siteName = () => process.env.SITE_NAME || 'BasicBen';
|
|
32
|
+
/**
|
|
33
|
+
* The auth_tokens kind for an invitation.
|
|
34
|
+
*
|
|
35
|
+
* Kinds are plain strings in the framework, not a closed enum, so this needs
|
|
36
|
+
* no framework change and no table of its own. The row's `user_id` is the
|
|
37
|
+
* person who sent the invitation — the invitee has no account to point at
|
|
38
|
+
* yet, and the column is NOT NULL — and who it is for travels in `metadata`.
|
|
39
|
+
* A consequence worth knowing: deleting the sender cascades their pending
|
|
40
|
+
* invitations away, which is the behaviour you would want anyway.
|
|
41
|
+
*/
|
|
42
|
+
export const INVITATION_KIND = 'invitation';
|
|
43
|
+
const INVITATION_TTL = 7 * 24 * 60 * 60 * 1000; // 7 days
|
|
44
|
+
/**
|
|
45
|
+
* The guard every admin-removing statement carries.
|
|
46
|
+
*
|
|
47
|
+
* True when the row being changed is not an active admin, or when some other
|
|
48
|
+
* active admin would remain afterwards. `?` is the id of the row being
|
|
49
|
+
* changed, so it is excluded from the count.
|
|
50
|
+
*/
|
|
51
|
+
const KEEPS_AN_ADMIN = `(
|
|
52
|
+
role != 'admin'
|
|
53
|
+
OR disabled_at IS NOT NULL
|
|
54
|
+
OR (SELECT COUNT(*) FROM users AS others
|
|
55
|
+
WHERE others.role = 'admin' AND others.disabled_at IS NULL AND others.id != ?) > 0
|
|
56
|
+
)`;
|
|
57
|
+
const LAST_ADMIN = 'This is the only active admin. Make someone else an admin first.';
|
|
58
|
+
function present(row) {
|
|
59
|
+
return {
|
|
60
|
+
id: row.id,
|
|
61
|
+
name: row.name,
|
|
62
|
+
email: row.email,
|
|
63
|
+
role: row.role,
|
|
64
|
+
slug: row.slug,
|
|
65
|
+
email_verified: Boolean(row.email_verified),
|
|
66
|
+
disabled_at: row.disabled_at,
|
|
67
|
+
created_at: row.created_at,
|
|
68
|
+
// Postgres returns COUNT(*) as a bigint, which arrives as a string.
|
|
69
|
+
post_count: Number(row.post_count ?? 0)
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
const SELECT_USERS = `
|
|
73
|
+
SELECT users.id, users.name, users.email, users.role, users.slug,
|
|
74
|
+
users.email_verified, users.disabled_at, users.created_at,
|
|
75
|
+
(SELECT COUNT(*) FROM posts WHERE posts.user_id = users.id) AS post_count
|
|
76
|
+
FROM users
|
|
77
|
+
`;
|
|
78
|
+
async function findRow(id) {
|
|
79
|
+
const db = await getDb();
|
|
80
|
+
return db.get(`${SELECT_USERS} WHERE users.id = ?`, [id]);
|
|
81
|
+
}
|
|
82
|
+
function idParam(req) {
|
|
83
|
+
const id = Number.parseInt(req.params.id, 10);
|
|
84
|
+
return Number.isInteger(id) && id > 0 ? id : null;
|
|
85
|
+
}
|
|
86
|
+
export const UserController = {
|
|
87
|
+
/**
|
|
88
|
+
* Everyone, a page at a time, newest first.
|
|
89
|
+
*
|
|
90
|
+
* `?search=` matches name or address; `?role=` narrows to one role. The post
|
|
91
|
+
* count rides along so the delete dialog knows whether to ask where their
|
|
92
|
+
* posts should go before the admin clicks anything.
|
|
93
|
+
*/
|
|
94
|
+
async index(req, res) {
|
|
95
|
+
const { page, perPage, offset } = paginationFrom(req.query);
|
|
96
|
+
const db = await getDb();
|
|
97
|
+
const conditions = [];
|
|
98
|
+
const params = [];
|
|
99
|
+
const search = (req.query.search ?? '').trim();
|
|
100
|
+
if (search) {
|
|
101
|
+
// Lowered on both sides because LIKE is case-insensitive on SQLite and
|
|
102
|
+
// not on Postgres. `%` and `_` in the search are literals, not wildcards.
|
|
103
|
+
const needle = `%${search.toLowerCase().replace(/[\\%_]/g, (c) => '\\' + c)}%`;
|
|
104
|
+
conditions.push("(LOWER(users.name) LIKE ? ESCAPE '\\' OR LOWER(users.email) LIKE ? ESCAPE '\\')");
|
|
105
|
+
params.push(needle, needle);
|
|
106
|
+
}
|
|
107
|
+
const role = req.query.role;
|
|
108
|
+
if (role && isValidRole(role)) {
|
|
109
|
+
conditions.push('users.role = ?');
|
|
110
|
+
params.push(role);
|
|
111
|
+
}
|
|
112
|
+
const where = conditions.length ? `WHERE ${conditions.join(' AND ')}` : '';
|
|
113
|
+
const counted = await db.get(`SELECT COUNT(*) AS total FROM users ${where}`, params);
|
|
114
|
+
const total = Number(counted?.total ?? 0);
|
|
115
|
+
// `id` breaks ties: created_at alone is not a total order, and paginating
|
|
116
|
+
// a partial one serves a row twice and another never.
|
|
117
|
+
const rows = (await db.all(`${SELECT_USERS} ${where}
|
|
118
|
+
ORDER BY users.created_at DESC, users.id DESC
|
|
119
|
+
LIMIT ? OFFSET ?`, [...params, perPage, offset]));
|
|
120
|
+
res.json({
|
|
121
|
+
users: rows.map(present),
|
|
122
|
+
roles: Object.values(ROLES),
|
|
123
|
+
meta: meta(page, perPage, total)
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
/**
|
|
127
|
+
* Rename someone, or change their role.
|
|
128
|
+
*
|
|
129
|
+
* A role change ends the account's sessions. The auth middleware reads the
|
|
130
|
+
* role fresh on every request, so the new role would apply anyway; revoking
|
|
131
|
+
* makes the change unmistakable — the client re-signs in and gets claims
|
|
132
|
+
* that match, rather than carrying a token whose `role` says otherwise.
|
|
133
|
+
*/
|
|
134
|
+
async update(req, res) {
|
|
135
|
+
const id = idParam(req);
|
|
136
|
+
if (!id)
|
|
137
|
+
return res.json({ error: 'User not found' }, 404);
|
|
138
|
+
const target = await findRow(id);
|
|
139
|
+
if (!target)
|
|
140
|
+
return res.json({ error: 'User not found' }, 404);
|
|
141
|
+
const { name, role } = req.body;
|
|
142
|
+
if (name !== undefined) {
|
|
143
|
+
const result = await validate({ name }, { name: [rules.required, rules.string, rules.min(2)] });
|
|
144
|
+
if (result.fails())
|
|
145
|
+
return res.json({ errors: result.errors }, 422);
|
|
146
|
+
}
|
|
147
|
+
if (role !== undefined && (typeof role !== 'string' || !isValidRole(role))) {
|
|
148
|
+
return res.json({ errors: { role: ['Unknown role'] } }, 422);
|
|
149
|
+
}
|
|
150
|
+
const roleChanges = typeof role === 'string' && role !== target.role;
|
|
151
|
+
if (roleChanges) {
|
|
152
|
+
// Your own role is the one change that can strand you: demote yourself
|
|
153
|
+
// and the screen you are on stops working mid-click. Another admin can
|
|
154
|
+
// do it for you.
|
|
155
|
+
if (id === req.userId) {
|
|
156
|
+
return res.json({ error: 'You cannot change your own role. Ask another admin.' }, 400);
|
|
157
|
+
}
|
|
158
|
+
// Promoting to admin can never strand the site, so only a change *away*
|
|
159
|
+
// from admin carries the guard.
|
|
160
|
+
const db = await getDb();
|
|
161
|
+
const changed = role === ROLES.ADMIN
|
|
162
|
+
? await db.run('UPDATE users SET role = ? WHERE id = ?', [role, id])
|
|
163
|
+
: await db.run(`UPDATE users SET role = ? WHERE id = ? AND ${KEEPS_AN_ADMIN}`, [role, id, id]);
|
|
164
|
+
if ((changed.changes ?? 0) !== 1) {
|
|
165
|
+
return res.json({ error: LAST_ADMIN }, 409);
|
|
166
|
+
}
|
|
167
|
+
await revokeSessions(id);
|
|
168
|
+
}
|
|
169
|
+
if (typeof name === 'string' && name !== target.name) {
|
|
170
|
+
await User.update(id, { name });
|
|
171
|
+
}
|
|
172
|
+
res.json({ user: present((await findRow(id))) });
|
|
173
|
+
},
|
|
174
|
+
/**
|
|
175
|
+
* Switch an account off without deleting it.
|
|
176
|
+
*
|
|
177
|
+
* Their posts keep their byline and nothing has to be reassigned. Sessions
|
|
178
|
+
* end immediately, and sign-in refuses the account until it is enabled.
|
|
179
|
+
*/
|
|
180
|
+
async disable(req, res) {
|
|
181
|
+
const id = idParam(req);
|
|
182
|
+
if (!id)
|
|
183
|
+
return res.json({ error: 'User not found' }, 404);
|
|
184
|
+
const target = await findRow(id);
|
|
185
|
+
if (!target)
|
|
186
|
+
return res.json({ error: 'User not found' }, 404);
|
|
187
|
+
if (id === req.userId) {
|
|
188
|
+
return res.json({ error: 'You cannot disable your own account.' }, 400);
|
|
189
|
+
}
|
|
190
|
+
if (target.disabled_at) {
|
|
191
|
+
return res.json({ user: present(target) });
|
|
192
|
+
}
|
|
193
|
+
const db = await getDb();
|
|
194
|
+
const changed = await db.run(`UPDATE users SET disabled_at = ? WHERE id = ? AND ${KEEPS_AN_ADMIN}`, [new Date().toISOString(), id, id]);
|
|
195
|
+
if ((changed.changes ?? 0) !== 1) {
|
|
196
|
+
return res.json({ error: LAST_ADMIN }, 409);
|
|
197
|
+
}
|
|
198
|
+
await revokeSessions(id);
|
|
199
|
+
res.json({ user: present((await findRow(id))) });
|
|
200
|
+
},
|
|
201
|
+
/**
|
|
202
|
+
* Switch an account back on.
|
|
203
|
+
*
|
|
204
|
+
* Their old sessions stay dead — disabling bumped the token version — so
|
|
205
|
+
* they sign in again, which is what you would want after a suspension.
|
|
206
|
+
*/
|
|
207
|
+
async enable(req, res) {
|
|
208
|
+
const id = idParam(req);
|
|
209
|
+
if (!id)
|
|
210
|
+
return res.json({ error: 'User not found' }, 404);
|
|
211
|
+
const db = await getDb();
|
|
212
|
+
const changed = await db.run('UPDATE users SET disabled_at = NULL WHERE id = ?', [id]);
|
|
213
|
+
if ((changed.changes ?? 0) !== 1)
|
|
214
|
+
return res.json({ error: 'User not found' }, 404);
|
|
215
|
+
res.json({ user: present((await findRow(id))) });
|
|
216
|
+
},
|
|
217
|
+
/**
|
|
218
|
+
* Remove an account.
|
|
219
|
+
*
|
|
220
|
+
* A post cannot lose its author — `posts.user_id` is NOT NULL and has no
|
|
221
|
+
* cascade, deliberately, because deleting someone should never silently
|
|
222
|
+
* delete what they published. So someone with posts is deleted only with
|
|
223
|
+
* `?reassign=<userId>`, which moves their posts (trashed ones included) and
|
|
224
|
+
* their uploads to that person first, the way WordPress asks "attribute all
|
|
225
|
+
* content to…". Pages carry no author in this schema, so there is nothing of
|
|
226
|
+
* theirs to move.
|
|
227
|
+
*
|
|
228
|
+
* Their credentials — API tokens, single-use links, second factors,
|
|
229
|
+
* passkeys — are deleted explicitly rather than left to ON DELETE CASCADE,
|
|
230
|
+
* since not every driver this runs on enforces foreign keys by default.
|
|
231
|
+
*/
|
|
232
|
+
async destroy(req, res) {
|
|
233
|
+
const id = idParam(req);
|
|
234
|
+
if (!id)
|
|
235
|
+
return res.json({ error: 'User not found' }, 404);
|
|
236
|
+
const target = await findRow(id);
|
|
237
|
+
if (!target)
|
|
238
|
+
return res.json({ error: 'User not found' }, 404);
|
|
239
|
+
if (id === req.userId) {
|
|
240
|
+
return res.json({ error: 'You cannot delete your own account.' }, 400);
|
|
241
|
+
}
|
|
242
|
+
const postCount = Number(target.post_count ?? 0);
|
|
243
|
+
const reassignTo = req.query.reassign ? Number.parseInt(req.query.reassign, 10) : null;
|
|
244
|
+
if (postCount > 0 && !reassignTo) {
|
|
245
|
+
return res.json({
|
|
246
|
+
error: `${target.name} has ${postCount} post${postCount === 1 ? '' : 's'}. Choose someone to reassign them to.`,
|
|
247
|
+
post_count: postCount
|
|
248
|
+
}, 409);
|
|
249
|
+
}
|
|
250
|
+
if (reassignTo !== null) {
|
|
251
|
+
if (!Number.isInteger(reassignTo) || reassignTo === id || !(await User.find(reassignTo))) {
|
|
252
|
+
return res.json({ errors: { reassign: ['Choose another existing user to receive the content'] } }, 422);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
const db = await getDb();
|
|
256
|
+
// One transaction, so a refused delete does not leave the posts already
|
|
257
|
+
// moved to someone else, and a failed move does not leave a half-deleted
|
|
258
|
+
// account. Throwing is how the refusal rolls the moves back.
|
|
259
|
+
const refused = Symbol('last admin');
|
|
260
|
+
try {
|
|
261
|
+
await db.transaction(async (tx) => {
|
|
262
|
+
if (reassignTo !== null) {
|
|
263
|
+
await tx.run('UPDATE posts SET user_id = ? WHERE user_id = ?', [reassignTo, id]);
|
|
264
|
+
await tx.run('UPDATE media SET user_id = ? WHERE user_id = ?', [reassignTo, id]);
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
await tx.run('UPDATE media SET user_id = NULL WHERE user_id = ?', [id]);
|
|
268
|
+
}
|
|
269
|
+
await tx.run('UPDATE comments SET user_id = NULL WHERE user_id = ?', [id]);
|
|
270
|
+
await tx.run('DELETE FROM api_tokens WHERE user_id = ?', [id]);
|
|
271
|
+
await tx.run('DELETE FROM auth_tokens WHERE user_id = ?', [id]);
|
|
272
|
+
await tx.run('DELETE FROM user_two_factor WHERE user_id = ?', [id]);
|
|
273
|
+
await tx.run('DELETE FROM user_credentials WHERE user_id = ?', [id]);
|
|
274
|
+
const deleted = await tx.run(`DELETE FROM users WHERE id = ? AND ${KEEPS_AN_ADMIN}`, [id, id]);
|
|
275
|
+
if ((deleted.changes ?? 0) !== 1)
|
|
276
|
+
throw refused;
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
catch (err) {
|
|
280
|
+
if (err === refused)
|
|
281
|
+
return res.json({ error: LAST_ADMIN }, 409);
|
|
282
|
+
throw err;
|
|
283
|
+
}
|
|
284
|
+
res.json({ message: `${target.name} was deleted.`, reassigned: reassignTo ? postCount : 0 });
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
function parseMetadata(raw) {
|
|
288
|
+
if (!raw)
|
|
289
|
+
return null;
|
|
290
|
+
try {
|
|
291
|
+
const parsed = JSON.parse(raw);
|
|
292
|
+
return parsed && typeof parsed.email === 'string' ? parsed : null;
|
|
293
|
+
}
|
|
294
|
+
catch {
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
async function pendingInvitations() {
|
|
299
|
+
const db = await getDb();
|
|
300
|
+
const rows = (await db.all(`SELECT auth_tokens.id, auth_tokens.metadata, auth_tokens.expires_at, auth_tokens.created_at,
|
|
301
|
+
users.name AS invited_by
|
|
302
|
+
FROM auth_tokens
|
|
303
|
+
LEFT JOIN users ON users.id = auth_tokens.user_id
|
|
304
|
+
WHERE auth_tokens.kind = ? AND auth_tokens.used_at IS NULL AND auth_tokens.expires_at > ?
|
|
305
|
+
ORDER BY auth_tokens.created_at DESC, auth_tokens.id DESC`, [INVITATION_KIND, new Date().toISOString()]));
|
|
306
|
+
return rows.flatMap((row) => {
|
|
307
|
+
const parsed = parseMetadata(row.metadata);
|
|
308
|
+
return parsed ? [{ ...row, meta: parsed }] : [];
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Look a live invitation up without spending it.
|
|
313
|
+
*
|
|
314
|
+
* The framework's redeemToken consumes on read, which is right for the accept
|
|
315
|
+
* but wrong for the page that has to show who the invitation is for before
|
|
316
|
+
* anyone has typed a password.
|
|
317
|
+
*/
|
|
318
|
+
async function peekInvitation(token) {
|
|
319
|
+
if (!token || typeof token !== 'string')
|
|
320
|
+
return null;
|
|
321
|
+
const db = await getDb();
|
|
322
|
+
const hash = hashToken(token);
|
|
323
|
+
const row = await db.get(`SELECT metadata FROM auth_tokens
|
|
324
|
+
WHERE token_hash = ? AND kind = ? AND used_at IS NULL AND expires_at > ?`, [hash, INVITATION_KIND, new Date().toISOString()]);
|
|
325
|
+
const parsed = row ? parseMetadata(row.metadata) : null;
|
|
326
|
+
return parsed ? { hash, meta: parsed } : null;
|
|
327
|
+
}
|
|
328
|
+
const INVALID_INVITATION = 'This invitation is invalid, has expired, or has already been used.';
|
|
329
|
+
export const InvitationController = {
|
|
330
|
+
/**
|
|
331
|
+
* Invite someone by address.
|
|
332
|
+
*
|
|
333
|
+
* Sending another invitation to the same address replaces the earlier one,
|
|
334
|
+
* so the newest email is always the one that works and the pending list
|
|
335
|
+
* never shows the same person twice.
|
|
336
|
+
*/
|
|
337
|
+
async store(req, res) {
|
|
338
|
+
const result = await validate(req.body, {
|
|
339
|
+
email: [rules.required, rules.email],
|
|
340
|
+
role: [rules.required, rules.string]
|
|
341
|
+
});
|
|
342
|
+
if (result.fails())
|
|
343
|
+
return res.json({ errors: result.errors }, 422);
|
|
344
|
+
const body = req.body;
|
|
345
|
+
const email = body.email.trim();
|
|
346
|
+
const role = body.role;
|
|
347
|
+
const name = typeof body.name === 'string' && body.name.trim() ? body.name.trim() : null;
|
|
348
|
+
if (!isValidRole(role)) {
|
|
349
|
+
return res.json({ errors: { role: ['Unknown role'] } }, 422);
|
|
350
|
+
}
|
|
351
|
+
// Behind user.manage, so saying the address is taken leaks nothing the
|
|
352
|
+
// admin could not see in the users table anyway.
|
|
353
|
+
if (await User.findByEmail(email)) {
|
|
354
|
+
return res.json({ error: 'Someone with that email already has an account.' }, 409);
|
|
355
|
+
}
|
|
356
|
+
const db = await getDb();
|
|
357
|
+
const now = new Date().toISOString();
|
|
358
|
+
for (const pending of await pendingInvitations()) {
|
|
359
|
+
if (pending.meta.email.toLowerCase() === email.toLowerCase()) {
|
|
360
|
+
await db.run('UPDATE auth_tokens SET used_at = ? WHERE id = ?', [now, pending.id]);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
const { token, expiresAt } = await issueToken(req.userId, INVITATION_KIND, {
|
|
364
|
+
ttl: INVITATION_TTL,
|
|
365
|
+
metadata: { email, role, name }
|
|
366
|
+
});
|
|
367
|
+
const inviteUrl = `${appUrl()}/invite/${token}`;
|
|
368
|
+
const inviter = await User.find(req.userId);
|
|
369
|
+
try {
|
|
370
|
+
await sendMail({
|
|
371
|
+
to: email,
|
|
372
|
+
subject: `You're invited to ${siteName()}`,
|
|
373
|
+
...renderMail('invitation', {
|
|
374
|
+
inviterName: inviter?.name ?? 'An administrator',
|
|
375
|
+
siteName: siteName(),
|
|
376
|
+
roleLabel: /^[aeiou]/.test(role) ? `an ${role}` : `a ${role}`,
|
|
377
|
+
inviteUrl
|
|
378
|
+
})
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
catch (err) {
|
|
382
|
+
// The invitation exists either way, and the admin can resend it. Saying
|
|
383
|
+
// "invited" when the mail never left would leave them waiting on it.
|
|
384
|
+
console.error('Failed to send invitation email:', err.message);
|
|
385
|
+
return res.json({ error: 'The invitation was created but the email could not be sent. Try again shortly.' }, 502);
|
|
386
|
+
}
|
|
387
|
+
res.json({ message: `Invitation sent to ${email}.`, expiresAt: expiresAt.toISOString() }, 201);
|
|
388
|
+
},
|
|
389
|
+
/** Invitations nobody has accepted yet, and which have not expired. */
|
|
390
|
+
async index(_req, res) {
|
|
391
|
+
const pending = await pendingInvitations();
|
|
392
|
+
res.json({
|
|
393
|
+
invitations: pending.map((row) => ({
|
|
394
|
+
id: row.id,
|
|
395
|
+
email: row.meta.email,
|
|
396
|
+
name: row.meta.name ?? null,
|
|
397
|
+
role: row.meta.role,
|
|
398
|
+
invited_by: row.invited_by,
|
|
399
|
+
expires_at: row.expires_at,
|
|
400
|
+
created_at: row.created_at
|
|
401
|
+
}))
|
|
402
|
+
});
|
|
403
|
+
},
|
|
404
|
+
/** Withdraw an invitation, so its link stops working. */
|
|
405
|
+
async destroy(req, res) {
|
|
406
|
+
const id = idParam(req);
|
|
407
|
+
if (!id)
|
|
408
|
+
return res.json({ error: 'Invitation not found' }, 404);
|
|
409
|
+
const db = await getDb();
|
|
410
|
+
const revoked = await db.run('UPDATE auth_tokens SET used_at = ? WHERE id = ? AND kind = ? AND used_at IS NULL', [new Date().toISOString(), id, INVITATION_KIND]);
|
|
411
|
+
if ((revoked.changes ?? 0) !== 1)
|
|
412
|
+
return res.json({ error: 'Invitation not found' }, 404);
|
|
413
|
+
res.json({ message: 'Invitation revoked.' });
|
|
414
|
+
},
|
|
415
|
+
/**
|
|
416
|
+
* What the accept page needs to show: who this is for, and where.
|
|
417
|
+
*
|
|
418
|
+
* Public, because the person holding the link has no account yet. The link
|
|
419
|
+
* is the credential — 32 random bytes — so answering for it reveals nothing
|
|
420
|
+
* to someone who does not already have the email.
|
|
421
|
+
*/
|
|
422
|
+
async show(req, res) {
|
|
423
|
+
const invitation = await peekInvitation(req.params.token);
|
|
424
|
+
if (!invitation)
|
|
425
|
+
return res.json({ error: INVALID_INVITATION }, 404);
|
|
426
|
+
res.json({
|
|
427
|
+
email: invitation.meta.email,
|
|
428
|
+
name: invitation.meta.name ?? null,
|
|
429
|
+
role: invitation.meta.role,
|
|
430
|
+
siteName: siteName()
|
|
431
|
+
});
|
|
432
|
+
},
|
|
433
|
+
/**
|
|
434
|
+
* Turn an invitation into an account and sign it in.
|
|
435
|
+
*
|
|
436
|
+
* Deliberately not subject to DISABLE_REGISTRATION: on a closed site an
|
|
437
|
+
* invitation is the *only* way a new person gets in, and it is an admin who
|
|
438
|
+
* opened the door, not the visitor. The address counts as verified because
|
|
439
|
+
* following the emailed link is exactly what verification asks for.
|
|
440
|
+
*/
|
|
441
|
+
async accept(req, res) {
|
|
442
|
+
const result = await validate(req.body, {
|
|
443
|
+
name: [rules.required, rules.string, rules.min(2)],
|
|
444
|
+
password: [rules.required, rules.min(8)]
|
|
445
|
+
});
|
|
446
|
+
if (result.fails())
|
|
447
|
+
return res.json({ errors: result.errors }, 422);
|
|
448
|
+
const { name, password } = req.body;
|
|
449
|
+
const token = req.params.token;
|
|
450
|
+
const invitation = await peekInvitation(token);
|
|
451
|
+
if (!invitation)
|
|
452
|
+
return res.json({ error: INVALID_INVITATION }, 404);
|
|
453
|
+
const { email, role } = invitation.meta;
|
|
454
|
+
// The address may have registered on its own since the invitation went
|
|
455
|
+
// out. Checked before the token is spent, so the link still works if the
|
|
456
|
+
// other account is removed.
|
|
457
|
+
if (await User.findByEmail(email) || !isValidRole(role)) {
|
|
458
|
+
return res.json({ error: 'An account with this email already exists. Sign in instead.' }, 409);
|
|
459
|
+
}
|
|
460
|
+
// Hashed before the claim, so the slow part cannot widen the window
|
|
461
|
+
// between spending the token and creating the account.
|
|
462
|
+
const hashed = await hashPassword(password);
|
|
463
|
+
// Spending the token is the atomic step: a single conditional UPDATE, so
|
|
464
|
+
// two tabs submitting the same link cannot both create an account.
|
|
465
|
+
const claimed = await redeemToken(token, INVITATION_KIND);
|
|
466
|
+
if (!claimed)
|
|
467
|
+
return res.json({ error: INVALID_INVITATION }, 404);
|
|
468
|
+
let user;
|
|
469
|
+
try {
|
|
470
|
+
user = await User.create({
|
|
471
|
+
name,
|
|
472
|
+
email,
|
|
473
|
+
password: hashed,
|
|
474
|
+
role,
|
|
475
|
+
email_verified: 1,
|
|
476
|
+
email_verified_at: new Date().toISOString()
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
catch (err) {
|
|
480
|
+
// Most likely the address was taken between the check and the insert.
|
|
481
|
+
// Hand the link back, so the invitation is not lost to a race the
|
|
482
|
+
// invitee did not cause.
|
|
483
|
+
const db = await getDb();
|
|
484
|
+
await db.run('UPDATE auth_tokens SET used_at = NULL WHERE token_hash = ? AND kind = ?', [
|
|
485
|
+
invitation.hash,
|
|
486
|
+
INVITATION_KIND
|
|
487
|
+
]);
|
|
488
|
+
console.error('Failed to create invited account:', err.message);
|
|
489
|
+
return res.json({ error: 'Could not create the account. Please try again.' }, 409);
|
|
490
|
+
}
|
|
491
|
+
const sessionToken = issueSessionToken({ id: user.id, role, email_verified: 1, token_version: 0 });
|
|
492
|
+
// The same shape login returns, so the client stores it the same way.
|
|
493
|
+
res.json({
|
|
494
|
+
user: {
|
|
495
|
+
id: user.id,
|
|
496
|
+
name: user.name,
|
|
497
|
+
email: user.email,
|
|
498
|
+
role,
|
|
499
|
+
email_verified: true
|
|
500
|
+
},
|
|
501
|
+
token: sessionToken
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Page views and visitors, counted without cookies.
|
|
4
|
+
*
|
|
5
|
+
* What is behind these numbers is deliberately thin — daily counts per path
|
|
6
|
+
* and per referring domain, nothing per person (src/features/analytics/README.md) — so the
|
|
7
|
+
* page shows exactly that and says what it cannot: visitors over a range are
|
|
8
|
+
* a sum of daily visitors, because the hashes that would make them unique
|
|
9
|
+
* across days are deleted every day.
|
|
10
|
+
*/
|
|
11
|
+
export default function AdminAnalytics(): React.JSX.Element;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useState } from 'react';
|
|
3
|
+
import { useAuth } from '@basicbenframework/core/client';
|
|
4
|
+
import { can } from '@basicbenframework/core/auth/permissions';
|
|
5
|
+
import { api } from "../../helpers/api.js";
|
|
6
|
+
import AdminLayout from "../../client/layouts/AdminLayout.js";
|
|
7
|
+
import { useT, currentLocale, slots } from "../../client/i18n/index.js";
|
|
8
|
+
const RANGES = [7, 30, 90];
|
|
9
|
+
const fmt = (n) => n.toLocaleString(currentLocale());
|
|
10
|
+
/**
|
|
11
|
+
* Page views and visitors, counted without cookies.
|
|
12
|
+
*
|
|
13
|
+
* What is behind these numbers is deliberately thin — daily counts per path
|
|
14
|
+
* and per referring domain, nothing per person (src/features/analytics/README.md) — so the
|
|
15
|
+
* page shows exactly that and says what it cannot: visitors over a range are
|
|
16
|
+
* a sum of daily visitors, because the hashes that would make them unique
|
|
17
|
+
* across days are deleted every day.
|
|
18
|
+
*/
|
|
19
|
+
export default function AdminAnalytics() {
|
|
20
|
+
const { t, tn } = useT();
|
|
21
|
+
const { user } = useAuth();
|
|
22
|
+
const mayConfigure = can(user, 'settings.manage');
|
|
23
|
+
const [days, setDays] = useState(30);
|
|
24
|
+
const [data, setData] = useState(null);
|
|
25
|
+
const [settings, setSettings] = useState(null);
|
|
26
|
+
const [error, setError] = useState('');
|
|
27
|
+
const [saved, setSaved] = useState('');
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
let live = true;
|
|
30
|
+
setError('');
|
|
31
|
+
api.get(`/api/admin/analytics?days=${days}`)
|
|
32
|
+
.then((res) => {
|
|
33
|
+
if (!live)
|
|
34
|
+
return;
|
|
35
|
+
setData(res.data);
|
|
36
|
+
setSettings((current) => current ?? res.settings);
|
|
37
|
+
})
|
|
38
|
+
.catch((err) => live && setError(err.message));
|
|
39
|
+
return () => { live = false; };
|
|
40
|
+
}, [days]);
|
|
41
|
+
const save = async (e) => {
|
|
42
|
+
e.preventDefault();
|
|
43
|
+
if (!settings)
|
|
44
|
+
return;
|
|
45
|
+
setSaved('');
|
|
46
|
+
try {
|
|
47
|
+
await api.put('/api/settings', {
|
|
48
|
+
settings: {
|
|
49
|
+
analytics_enabled: settings.analytics_enabled,
|
|
50
|
+
analytics_dedup_hours: settings.analytics_dedup_hours,
|
|
51
|
+
analytics_retention_days: settings.analytics_retention_days
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
setSaved(t('Saved.'));
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
setSaved(err.message);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
return (_jsxs(AdminLayout, { title: t('Analytics'), children: [_jsx("style", { children: analyticsStyles }), _jsx("div", { className: "an-toolbar", role: "group", "aria-label": t('Range'), children: RANGES.map((n) => (_jsx("button", { type: "button", className: `admin-btn ${n === days ? 'admin-btn-primary' : 'admin-btn-secondary'}`, "aria-pressed": n === days, onClick: () => setDays(n), children: tn('{count} day', '{count} days', n) }, n))) }), error && _jsx("div", { className: "an-error", children: error }), data && (_jsxs(_Fragment, { children: [_jsxs("div", { className: "an-tiles", children: [_jsxs("div", { className: "admin-card an-tile", children: [_jsx("div", { className: "an-tile-label", children: t('Page views') }), _jsx("div", { className: "an-tile-value", children: fmt(data.totals.views) })] }), _jsxs("div", { className: "admin-card an-tile", children: [_jsx("div", { className: "an-tile-label", children: t('Daily visitors, summed') }), _jsx("div", { className: "an-tile-value", children: fmt(data.totals.visitors) })] }), _jsxs("div", { className: "admin-card an-tile", children: [_jsx("div", { className: "an-tile-label", children: t('Views per day') }), _jsx("div", { className: "an-tile-value", children: fmt(Math.round(data.totals.views / data.days)) })] })] }), _jsxs("div", { className: "admin-card", children: [_jsxs("div", { className: "admin-card-header", children: [_jsx("h2", { className: "admin-card-title", children: t('{from} to {to} (UTC)', { from: data.from, to: data.to }) }), _jsxs("div", { className: "an-legend", children: [_jsxs("span", { children: [_jsx("i", { className: "an-key an-key-views" }), " ", t('Views')] }), _jsxs("span", { children: [_jsx("i", { className: "an-key an-key-visitors" }), " ", t('Visitors')] })] })] }), _jsx(Chart, { series: data.series })] }), _jsxs("div", { className: "an-columns", children: [_jsxs("div", { className: "admin-card", children: [_jsx("div", { className: "admin-card-header", children: _jsx("h2", { className: "admin-card-title", children: t('Top pages') }) }), data.top_paths.length === 0 ? _jsx("p", { className: "an-muted", children: t('No page views yet.') }) : (_jsxs("table", { className: "admin-table", children: [_jsx("thead", { children: _jsxs("tr", { children: [_jsx("th", { children: t('Path') }), _jsx("th", { className: "an-num", children: t('Views') }), _jsx("th", { className: "an-num", children: t('Visitors') })] }) }), _jsx("tbody", { children: data.top_paths.map((row) => (_jsxs("tr", { children: [_jsx("td", { className: "an-path", children: row.path }), _jsx("td", { className: "an-num", children: fmt(row.views) }), _jsx("td", { className: "an-num", children: fmt(row.visitors) })] }, row.path))) })] }))] }), _jsxs("div", { className: "admin-card", children: [_jsx("div", { className: "admin-card-header", children: _jsx("h2", { className: "admin-card-title", children: t('Top referrers') }) }), data.top_referrers.length === 0 ? _jsx("p", { className: "an-muted", children: t('No referred visits yet.') }) : (_jsxs("table", { className: "admin-table", children: [_jsx("thead", { children: _jsxs("tr", { children: [_jsx("th", { children: t('Domain') }), _jsx("th", { className: "an-num", children: t('Views') })] }) }), _jsx("tbody", { children: data.top_referrers.map((row) => (_jsxs("tr", { children: [_jsx("td", { className: "an-path", children: row.domain }), _jsx("td", { className: "an-num", children: fmt(row.views) })] }, row.domain))) })] }))] })] })] })), _jsxs("div", { className: "admin-card", children: [_jsx("div", { className: "admin-card-header", children: _jsx("h2", { className: "admin-card-title", children: t('How this is counted') }) }), _jsxs("div", { className: "an-body", children: [_jsx("p", { children: t("No cookies, no stored IP addresses or user agents. A visitor is a hash of their address, browser and a random salt that changes every UTC day and is then deleted, so nobody — including you — can tell who visited, or link one day's visitors to the next. Browsers sending Do Not Track or Global Privacy Control are not counted at all, and neither are known bots.") }), _jsx("p", { className: "an-muted", children: slots(t('Your front end reports page views with a short snippet: {endpoint}. See {doc}.'), { endpoint: _jsx("code", { children: 'POST /api/v1/analytics/hit' }), doc: _jsx("code", { children: 'src/features/analytics/README.md' }) }) })] }), mayConfigure && settings && (_jsxs("form", { className: "an-settings", onSubmit: save, children: [_jsxs("label", { className: "an-check", children: [_jsx("input", { type: "checkbox", checked: settings.analytics_enabled, onChange: (e) => setSettings({ ...settings, analytics_enabled: e.target.checked }) }), t('Count page views')] }), _jsxs("label", { children: [t('Forget visitor hashes after (hours, 1–24)'), _jsx("input", { className: "admin-input", type: "number", min: 1, max: 24, value: settings.analytics_dedup_hours, onChange: (e) => setSettings({ ...settings, analytics_dedup_hours: Number(e.target.value) }) })] }), _jsxs("label", { children: [t('Keep daily counts for (days, 0 = forever)'), _jsx("input", { className: "admin-input", type: "number", min: 0, max: 3650, value: settings.analytics_retention_days, onChange: (e) => setSettings({ ...settings, analytics_retention_days: Number(e.target.value) }) })] }), _jsxs("div", { className: "an-save", children: [_jsx("button", { type: "submit", className: "admin-btn admin-btn-primary", children: t('Save') }), saved && _jsx("span", { className: "an-muted", children: saved })] })] }))] })] }));
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Views as bars, visitors as a line, in plain SVG.
|
|
64
|
+
*
|
|
65
|
+
* A fixed coordinate space scaled to the card's width keeps the labels
|
|
66
|
+
* undistorted; each bar carries a <title>, which is the tooltip and the
|
|
67
|
+
* accessible text for the value.
|
|
68
|
+
*/
|
|
69
|
+
function Chart({ series }) {
|
|
70
|
+
const { t, tn } = useT();
|
|
71
|
+
const W = 720;
|
|
72
|
+
const H = 220;
|
|
73
|
+
const pad = { top: 12, right: 8, bottom: 24, left: 40 };
|
|
74
|
+
const innerW = W - pad.left - pad.right;
|
|
75
|
+
const innerH = H - pad.top - pad.bottom;
|
|
76
|
+
const max = Math.max(1, ...series.map((d) => d.views));
|
|
77
|
+
const step = innerW / Math.max(1, series.length);
|
|
78
|
+
const barW = Math.max(1, step * 0.7);
|
|
79
|
+
const y = (v) => pad.top + innerH - (v / max) * innerH;
|
|
80
|
+
const line = series
|
|
81
|
+
.map((d, i) => `${(pad.left + i * step + step / 2).toFixed(1)},${y(d.visitors).toFixed(1)}`)
|
|
82
|
+
.join(' ');
|
|
83
|
+
const labelEvery = Math.ceil(series.length / 6);
|
|
84
|
+
return (_jsxs("svg", { className: "an-chart", viewBox: `0 0 ${W} ${H}`, role: "img", "aria-label": t('Views and visitors per day'), children: [_jsx("line", { x1: pad.left, x2: W - pad.right, y1: pad.top + innerH, y2: pad.top + innerH, className: "an-axis" }), _jsx("line", { x1: pad.left, x2: W - pad.right, y1: pad.top, y2: pad.top, className: "an-grid" }), _jsx("text", { x: pad.left - 6, y: pad.top + 4, textAnchor: "end", className: "an-label", children: fmt(max) }), _jsx("text", { x: pad.left - 6, y: pad.top + innerH, textAnchor: "end", className: "an-label", children: "0" }), series.map((d, i) => (_jsx("rect", { x: pad.left + i * step + (step - barW) / 2, y: y(d.views), width: barW, height: pad.top + innerH - y(d.views), className: "an-bar", children: _jsx("title", { children: t('{day}: {views}, {visitors}', { day: d.day, views: tn('{count} view', '{count} views', d.views), visitors: tn('{count} visitor', '{count} visitors', d.visitors) }) }) }, d.day))), _jsx("polyline", { points: line, className: "an-line" }), series.map((d, i) => (i % labelEvery === 0 || i === series.length - 1) && (_jsx("text", { x: pad.left + i * step + step / 2, y: H - 6, textAnchor: "middle", className: "an-label", children: d.day.slice(5) }, `l-${d.day}`)))] }));
|
|
85
|
+
}
|
|
86
|
+
const analyticsStyles = `
|
|
87
|
+
.an-toolbar { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
|
|
88
|
+
.an-error {
|
|
89
|
+
padding: 10px 14px; border-radius: 8px; border: 1px solid var(--danger);
|
|
90
|
+
background: var(--tint-danger); color: var(--danger); font-size: 13px; margin-bottom: 16px;
|
|
91
|
+
}
|
|
92
|
+
.an-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; }
|
|
93
|
+
.an-tile-label { color: var(--fg-muted); font-size: 13px; }
|
|
94
|
+
.an-tile-value { font-size: 28px; font-weight: 600; font-variant-numeric: tabular-nums; }
|
|
95
|
+
.an-legend { display: flex; gap: 14px; font-size: 13px; color: var(--fg-muted); }
|
|
96
|
+
.an-key { display: inline-block; width: 10px; height: 10px; border-radius: 2px; vertical-align: middle; }
|
|
97
|
+
.an-key-views { background: var(--accent); opacity: 0.55; }
|
|
98
|
+
.an-key-visitors { background: var(--fg); height: 2px; }
|
|
99
|
+
.an-chart { width: 100%; height: auto; display: block; }
|
|
100
|
+
.an-bar { fill: var(--accent); opacity: 0.55; }
|
|
101
|
+
.an-bar:hover { opacity: 0.85; }
|
|
102
|
+
.an-line { fill: none; stroke: var(--fg); stroke-width: 1.5; }
|
|
103
|
+
.an-axis { stroke: var(--border-strong); }
|
|
104
|
+
.an-grid { stroke: var(--border); stroke-dasharray: 3 3; }
|
|
105
|
+
.an-label { fill: var(--fg-muted); font-size: 11px; }
|
|
106
|
+
.an-columns { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; }
|
|
107
|
+
.an-num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
|
|
108
|
+
.an-path { overflow-wrap: anywhere; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
|
|
109
|
+
.an-muted { color: var(--fg-muted); font-size: 13px; }
|
|
110
|
+
.an-body { font-size: 14px; display: flex; flex-direction: column; gap: 8px; }
|
|
111
|
+
.an-body p { margin: 0; }
|
|
112
|
+
.an-body code {
|
|
113
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px;
|
|
114
|
+
background: var(--surface-hover); border-radius: 4px; padding: 1px 4px;
|
|
115
|
+
}
|
|
116
|
+
.an-settings {
|
|
117
|
+
display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px;
|
|
118
|
+
margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); font-size: 13px; align-items: end;
|
|
119
|
+
}
|
|
120
|
+
.an-settings label { display: flex; flex-direction: column; gap: 6px; }
|
|
121
|
+
.an-settings .an-check { flex-direction: row; align-items: center; gap: 8px; }
|
|
122
|
+
.an-save { display: flex; align-items: center; gap: 10px; }
|
|
123
|
+
`;
|