-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
96 changes: 96 additions & 0 deletions
96
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,96 @@ | ||
| import { type FormEvent, useState } from 'react'; | ||
| import { InstanceConfigManager } from '../../../core/configuration-loader'; | ||
| import { t } from '../../../core/i18n'; | ||
| 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() { | ||
| 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, | ||
| }) | ||
|
qodo-code-review[bot] marked this conversation as resolved.
|
||
| ); | ||
|
|
||
| const [email, setEmail] = useState(''); | ||
| const [cadence, setCadence] = useState<'weekly' | 'monthly'>('weekly'); | ||
| const [state, setState] = useState<'idle' | 'busy' | 'done' | 'error'>('idle'); | ||
|
|
||
| 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)), | ||
| }); | ||
| setState(res.ok ? 'done' : 'error'); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } catch { | ||
| setState('error'); | ||
|
qodo-code-review[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
| }; | ||
|
|
||
| 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> | ||
| {state === 'done' ? ( | ||
| <p className="text-xs" role="status"> | ||
| {t('newsletterCheckInbox')} | ||
| </p> | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| ) : ( | ||
| <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> | ||
| {state === 'error' && ( | ||
| <p className="text-xs text-red-500" role="alert"> | ||
| {t('newsletterError')} | ||
| </p> | ||
| )} | ||
| </form> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
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', | ||
| }); | ||
| }); | ||
| }); |
46 changes: 46 additions & 0 deletions
46
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,46 @@ | ||
| /** | ||
| * 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}`), | ||
| }; | ||
| } | ||
|
|
||
| /** The body the form posts, exactly as the relay expects it. */ | ||
| export function newsletterSubscribeBody(t: NewsletterSignupTarget, email: string, cadence: 'weekly' | 'monthly') { | ||
| return { | ||
|
qodo-code-review[bot] marked this conversation as resolved.
Outdated
|
||
| email: email.trim(), | ||
| type: t.type, | ||
| target: t.target, | ||
| targetLabel: t.targetLabel, | ||
| cadence, | ||
| source: 'self-hosted-blog' as const, | ||
| }; | ||
| } | ||
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
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.