Publish milestones per channel and add Instagram

Consent is bound to the channel it was given for: approving a post on X says
nothing about Instagram. Publishing state moves from the SocialMilestone row
into SocialMilestonePost, one row per channel, where a missing row means no
consent. The dialog asks per channel, shows the text each one will publish and
keeps a separate handle for each; Instagram captions end in hashtags because a
link there is not clickable.

Also fixes three problems in the existing X path:

- A QR code already past several thresholds produced one prompt per threshold,
  and since the post quotes the current scan count, every one of them would
  have published the same number. Only the highest threshold is announced now.
- Detection ran after every unique scan and re-read the QR code's full scan
  history just to hit skipDuplicates. Known milestones are filtered first.
- A failed post stayed failed forever because the consent dialog only opens
  once. The queue now retries three times on its own, spaces first attempts by
  SOCIAL_MILESTONE_MIN_GAP_HOURS, and Settings lists every milestone per
  channel with restart and revoke.

The worker no longer renders the card itself; it downloads the image the app
renders at /s/m/<token>/og, which also serves the new square and portrait
formats. Instagram publishing stays off until SOCIAL_MILESTONE_CHANNELS and
SOCIAL_WORKER_CHANNELS both name it.

Schema changes are manual SQL, see sql/2026-08-16_*.sql. Run both before
deploying this version.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-16 13:46:13 +02:00
parent 55c04761ce
commit eb932ebdaa
22 changed files with 1159 additions and 298 deletions

View File

@@ -20,6 +20,18 @@ export function getSocialMilestoneThresholds(): number[] {
return thresholds.length ? thresholds : [...DEFAULT_SOCIAL_MILESTONE_THRESHOLDS];
}
/**
* Channels the consent dialog may offer. A channel is only offered where a
* publisher is actually configured - asking for consent for a post that nobody
* can publish would be dishonest. Server-side read; the dialog receives the
* resulting list in its payload.
*/
export function getEnabledSocialChannels(): SocialChannel[] {
const configured = (process.env.SOCIAL_MILESTONE_CHANNELS || 'x')
.split(',').map(value => value.trim().toLowerCase()).filter(isSocialChannel);
return configured.length ? Array.from(new Set(configured)) : ['x'];
}
const useCaseLabels: Record<string, string> = {
menu_pdf: 'menu QR code',
marketing_campaign: 'campaign QR code',
@@ -141,3 +153,60 @@ export function normalizeXHandle(value: string): string | null {
const handle = value.trim().replace(/^@/, '');
return /^[A-Za-z0-9_]{1,15}$/.test(handle) ? handle : null;
}
export function normalizeInstagramHandle(value: string): string | null {
const handle = value.trim().replace(/^@/, '');
return /^[A-Za-z0-9._]{1,30}$/.test(handle) ? handle : null;
}
/**
* Publishing channels of the QR Master brand accounts.
*
* Consent is bound to a channel: agreeing to a post on X says nothing about
* Instagram. Every channel therefore carries its own approval, its own text and
* its own handle.
*/
export const SOCIAL_CHANNELS = ['x', 'instagram'] as const;
export type SocialChannel = typeof SOCIAL_CHANNELS[number];
export function isSocialChannel(value: unknown): value is SocialChannel {
return typeof value === 'string' && (SOCIAL_CHANNELS as readonly string[]).includes(value);
}
export function normalizeChannelHandle(channel: SocialChannel, value: string): string | null {
return channel === 'instagram' ? normalizeInstagramHandle(value) : normalizeXHandle(value);
}
function instagramHashtags(locale: SocialLocale): string {
return locale === 'de'
? '#qrcode #qrcodes #marketing #kleinunternehmen #analytics #digitalisierung'
: '#qrcode #qrcodes #qrcodemarketing #smallbusiness #marketing #analytics';
}
/**
* The post split into the parts the consent dialog recombines while the
* customer types a handle. Server and client must never build this text
* differently - what stands in the preview is what gets published.
*/
export function milestonePostParts(input: {
channel: SocialChannel;
primaryUseCase: string | null;
totalUniqueScans: number;
locale: SocialLocale;
qrTitle: string;
shareUrl: string;
}) {
return {
head: buildMilestonePostForQr(input.primaryUseCase, input.totalUniqueScans, null, input.locale, input.qrTitle),
// A link in an Instagram caption is not clickable, so the share URL would
// be dead weight there. Hashtags do the reach work instead.
tail: input.channel === 'instagram' ? `\n\n${instagramHashtags(input.locale)}` : `\n\n${input.shareUrl}`,
mentionWord: input.locale === 'de' ? 'Glückwunsch' : 'Congratulations',
};
}
export function buildChannelPost(input: Parameters<typeof milestonePostParts>[0] & { handle: string | null }): string {
const { head, tail, mentionWord } = milestonePostParts(input);
const mention = input.handle ? `\n\n${mentionWord} @${input.handle.replace(/^@/, '')}.` : '';
return `${head}${mention}${tail}`;
}