-
Notifications
You must be signed in to change notification settings - Fork 7
Creator digests for every creator; the self-hosted subscribe embed #1542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
feruzm
wants to merge
2
commits into
develop
Choose a base branch
from
feature/self-hosted-newsletter-embed
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+329
−89
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
apps/self-hosted/src/features/blog/components/newsletter-signup.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { type FormEvent, type ReactElement, useEffect, useRef, useState } from 'react'; | ||
| import { InstanceConfigManager } from '../../../core/configuration-loader'; | ||
| import { t } from '../../../core/i18n'; | ||
| import { LiveRegion } from '../../shared/live-region'; | ||
| import { newsletterSignupTarget, newsletterSubscribeBody } from '../utils/newsletter-signup-target'; | ||
|
|
||
| /** | ||
| * The email-digest signup form (vision-web#1537). Managed instances only, by | ||
| * two fences: the form renders only when the served config carries `managed` | ||
| * (never on a true self-host or the unclaimed template), and it posts to the | ||
| * host's own `/api/newsletter/subscribe`, a path only the managed nginx | ||
| * forwards to ecency.com's public relay. Double opt-in end to end: the service | ||
| * answers `pending_confirmation` and the reader confirms from their inbox, so | ||
| * the form always says "check your inbox" on success and can learn nothing | ||
| * about an address it does not own. | ||
| */ | ||
| export function NewsletterSignup(): ReactElement | null { | ||
| const target = InstanceConfigManager.useConfig(({ configuration }) => | ||
| newsletterSignupTarget({ | ||
| username: configuration.instanceConfiguration.username, | ||
| managed: configuration.instanceConfiguration.managed, | ||
| template: configuration.instanceConfiguration.template, | ||
| enabled: configuration.instanceConfiguration.features.newsletter?.enabled ?? true, | ||
| siteTitle: configuration.instanceConfiguration.meta?.title, | ||
| }) | ||
| ); | ||
|
|
||
| const [email, setEmail] = useState(''); | ||
| const [cadence, setCadence] = useState<'weekly' | 'monthly'>('weekly'); | ||
| const [state, setState] = useState<'idle' | 'busy' | 'done' | 'error'>('idle'); | ||
| // The submit awaits a network call; a navigation mid-flight must not update | ||
| // an unmounted component. | ||
| const mounted = useRef(true); | ||
| useEffect(() => { | ||
| mounted.current = true; | ||
| return () => { | ||
| mounted.current = false; | ||
| }; | ||
| }, []); | ||
|
|
||
| if (!target) return null; | ||
| const isCommunity = target.type === 'community'; | ||
|
|
||
| const submit = async (e: FormEvent) => { | ||
| e.preventDefault(); | ||
| if (state === 'busy' || !email.trim()) return; | ||
| setState('busy'); | ||
| try { | ||
| const res = await fetch('/api/newsletter/subscribe', { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify(newsletterSubscribeBody(target, email, cadence)), | ||
| }); | ||
| if (mounted.current) setState(res.ok ? 'done' : 'error'); | ||
| } catch { | ||
| if (mounted.current) setState('error'); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="border-t border-theme pt-4 mt-4 sidebar-newsletter-section" data-testid="newsletter-signup"> | ||
| <h3 className="text-sm font-semibold mb-1">{t('newsletterTitle')}</h3> | ||
| <p className="text-xs text-theme-muted mb-2">{t(isCommunity ? 'newsletterCommunityBlurb' : 'newsletterBlurb')}</p> | ||
| {/* Mounted from the first render, per the live-region contract: a region | ||
| that appears at the same moment as its message is often not announced. */} | ||
| <LiveRegion message={state === 'done' ? t('newsletterCheckInbox') : state === 'error' ? t('newsletterError') : null} className="text-xs block mb-1" /> | ||
| {state !== 'done' && ( | ||
| <form onSubmit={submit} className="flex flex-col gap-2"> | ||
| <input | ||
| type="email" | ||
| required | ||
| value={email} | ||
| onChange={(e) => setEmail(e.target.value)} | ||
| placeholder={t('newsletterEmail')} | ||
| aria-label={t('newsletterEmail')} | ||
| className="input-theme w-full text-sm px-2 py-1.5 rounded" | ||
| /> | ||
| <div className="flex gap-2"> | ||
| <select | ||
| value={cadence} | ||
| onChange={(e) => setCadence(e.target.value as 'weekly' | 'monthly')} | ||
| aria-label={t('newsletterSubscribe')} | ||
| className="input-theme flex-1 text-sm px-2 py-1.5 rounded" | ||
| > | ||
| <option value="weekly">{t('newsletterWeekly')}</option> | ||
| <option value="monthly">{t('newsletterMonthly')}</option> | ||
| </select> | ||
| <button | ||
| type="submit" | ||
| disabled={state === 'busy'} | ||
| className="btn-theme-primary text-sm px-3 py-1.5 rounded disabled:opacity-60" | ||
| > | ||
| {t('newsletterSubscribe')} | ||
| </button> | ||
| </div> | ||
| </form> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
apps/self-hosted/src/features/blog/utils/newsletter-signup-target.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { newsletterSignupTarget, newsletterSubscribeBody } from './newsletter-signup-target'; | ||
|
|
||
| describe('newsletterSignupTarget', () => { | ||
| const managedBlog = { username: 'Alice', managed: true, siteTitle: 'Alice Writes' }; | ||
|
|
||
| it('offers the form only on a managed, claimed instance with the feature on', () => { | ||
| expect(newsletterSignupTarget(managedBlog)).toEqual({ type: 'creator', target: 'alice', targetLabel: 'Alice Writes' }); | ||
| // A true self-host never gets it: the config has no managed marker. | ||
| expect(newsletterSignupTarget({ username: 'alice' })).toBeNull(); | ||
| expect(newsletterSignupTarget({ ...managedBlog, managed: undefined })).toBeNull(); | ||
| // The unclaimed template, though served as managed, is not a tenant. | ||
| expect(newsletterSignupTarget({ ...managedBlog, template: true })).toBeNull(); | ||
| // The owner's toggle. | ||
| expect(newsletterSignupTarget({ ...managedBlog, enabled: false })).toBeNull(); | ||
| expect(newsletterSignupTarget({ ...managedBlog, enabled: true })).not.toBeNull(); | ||
| expect(newsletterSignupTarget({ managed: true })).toBeNull(); | ||
| }); | ||
|
|
||
| it('a hive-… instance subscribes to the community digest; labels fall back to the handle', () => { | ||
| expect(newsletterSignupTarget({ username: 'hive-125125', managed: true, siteTitle: 'Town Square' })).toEqual({ | ||
| type: 'community', | ||
| target: 'hive-125125', | ||
| targetLabel: 'Town Square', | ||
| }); | ||
| expect(newsletterSignupTarget({ username: 'HIVE-125125', managed: true, siteTitle: ' ' })).toEqual({ | ||
| type: 'community', | ||
| target: 'hive-125125', | ||
| targetLabel: 'hive-125125', | ||
| }); | ||
| expect(newsletterSignupTarget({ username: 'bob', managed: true })).toEqual({ type: 'creator', target: 'bob', targetLabel: '@bob' }); | ||
| }); | ||
|
|
||
| it('builds the exact relay body, source self-hosted-blog', () => { | ||
| const t = newsletterSignupTarget(managedBlog)!; | ||
| expect(newsletterSubscribeBody(t, ' reader@example.com ', 'monthly')).toEqual({ | ||
| email: 'reader@example.com', | ||
| type: 'creator', | ||
| target: 'alice', | ||
| targetLabel: 'Alice Writes', | ||
| cadence: 'monthly', | ||
| source: 'self-hosted-blog', | ||
| }); | ||
| }); | ||
| }); |
55 changes: 55 additions & 0 deletions
55
apps/self-hosted/src/features/blog/utils/newsletter-signup-target.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /** | ||
| * The pure half of the newsletter signup form (vision-web#1537): whether this | ||
| * instance offers it and what a submission subscribes to. Kept out of the | ||
| * component so the rules are testable in the node test environment. | ||
| * | ||
| * Managed instances only: the form renders only when the served config carries | ||
| * `managed` (never a true self-host, never the unclaimed template), and its | ||
| * POST goes to the host's own /api/newsletter/subscribe, a path only the | ||
| * managed nginx forwards to ecency.com's public relay. | ||
| */ | ||
| export interface NewsletterSignupTarget { | ||
| type: 'creator' | 'community'; | ||
| target: string; | ||
| targetLabel: string; | ||
| } | ||
|
|
||
| const COMMUNITY_RE = /^hive-\d+$/i; | ||
|
|
||
| export function newsletterSignupTarget(cfg: { | ||
| username?: string; | ||
| managed?: boolean; | ||
| template?: boolean; | ||
| enabled?: boolean; | ||
| siteTitle?: string; | ||
| }): NewsletterSignupTarget | null { | ||
| if (cfg.managed !== true || cfg.template === true || cfg.enabled === false || !cfg.username) return null; | ||
| const target = cfg.username.toLowerCase(); | ||
| const isCommunity = COMMUNITY_RE.test(target); | ||
| return { | ||
| type: isCommunity ? 'community' : 'creator', | ||
| target, | ||
| targetLabel: cfg.siteTitle?.trim() || (isCommunity ? target : `@${target}`), | ||
| }; | ||
| } | ||
|
|
||
| export interface NewsletterSubscribeBody { | ||
| email: string; | ||
| type: 'creator' | 'community'; | ||
| target: string; | ||
| targetLabel: string; | ||
| cadence: 'weekly' | 'monthly'; | ||
| source: 'self-hosted-blog'; | ||
| } | ||
|
|
||
| /** The body the form posts, exactly as the relay expects it. */ | ||
| export function newsletterSubscribeBody(t: NewsletterSignupTarget, email: string, cadence: 'weekly' | 'monthly'): NewsletterSubscribeBody { | ||
| return { | ||
| email: email.trim(), | ||
| type: t.type, | ||
| target: t.target, | ||
| targetLabel: t.targetLabel, | ||
| cadence, | ||
| source: 'self-hosted-blog', | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.