diff --git a/cloud/emails.py b/cloud/emails.py index 540dde8..99fca08 100644 --- a/cloud/emails.py +++ b/cloud/emails.py @@ -79,6 +79,36 @@ async def send_clips_ready_email(email: str, job_title: str, clip_count: int, await send_email(email, f"Your clips are ready — {title}", html) +async def send_clips_expiring_email(email: str, clip_count: int): + """Free clips enter their last day before deletion — honest loss aversion. + + The deadline is real (FREE_CLIP_RETENTION_DAYS); this simply makes it + visible instead of deleting silently. Doubles as re-engagement for users + who clipped once and went quiet. + """ + dash = f"{settings.frontend_url}/dashboard" + n = clip_count + clips = f"{n} clip" + ("s" if n != 1 else "") + html = f""" +
+

Your {clips} will be deleted tomorrow ⏳

+

Free clips are stored for 7 days, and {('these' if n != 1 else 'this one')} + {'are' if n != 1 else 'is'} about to expire. Two ways to keep them:

+ +

Save my clips

+

After tomorrow they're gone for good + — we can't recover deleted clips.

+
+ """ + print(f"⏳ Clips-expiring email → {email} ({clips})") + await send_email(email, f"Your {clips} will be deleted tomorrow", html) + + async def send_out_of_minutes_email(email: str, upgrade_url: str): """Free user hit their monthly quota — the natural upgrade moment. diff --git a/cloud/videos.py b/cloud/videos.py index 9ff0ef3..f28e2eb 100644 --- a/cloud/videos.py +++ b/cloud/videos.py @@ -11,7 +11,7 @@ from sqlalchemy import select, delete from .config import settings, VIDEO_RETENTION_GRACE_DAYS, FREE_CLIP_RETENTION_DAYS from . import database, storage, metering -from .models import UserVideo, Subscription, Project +from .models import UserVideo, Subscription, Project, User from .auth import get_current_user_required router = APIRouter() @@ -333,6 +333,52 @@ async def purge_free_expired(): print(f"⚠️ Free retention purge failed for {user_id}: {e}") +# Video ids already covered by an expiry-warning email, so the 6-hourly sweep +# doesn't re-mail the same user four times during the warning window. Process +# memory only: after a restart the worst case is one duplicate warning. +_warned_video_ids: set = set() + + +async def warn_free_expiring(): + """Email free users whose clips enter their LAST day before deletion. + + The 7-day free retention is the plan's strongest honest upgrade lever, but + silently deleting clips converts nobody — the user never learns they were + at risk. One email on day 6 turns the real deadline into a visible one. + """ + from datetime import datetime, timezone + from .emails import send_clips_expiring_email + now = datetime.now(timezone.utc) + doomed_after = now - timedelta(days=FREE_CLIP_RETENTION_DAYS) # already purgeable + warn_cutoff = now - timedelta(days=FREE_CLIP_RETENTION_DAYS - 1) # < 1 day left + + async with database.session() as s: + expiring = list((await s.execute( + select(UserVideo).where(UserVideo.created_at < warn_cutoff, + UserVideo.created_at >= doomed_after) + )).scalars()) + + by_user = {} + for v in expiring: + if v.id in _warned_video_ids: + continue + by_user.setdefault(v.user_id, []).append(v) + + for user_id, vids in by_user.items(): + async with database.session() as s: + if not await metering.is_free_user(s, user_id): + continue + user = await s.get(User, user_id) + if not user or not user.email: + continue + try: + await send_clips_expiring_email(user.email, len(vids)) + _warned_video_ids.update(v.id for v in vids) + print(f"⏳ Expiry warning sent: {len(vids)} clip(s), user {user_id}") + except Exception as e: + print(f"⚠️ Expiry warning failed for {user_id}: {e}") + + _SWEEP_INTERVAL = 6 * 3600 # every 6 hours @@ -340,6 +386,7 @@ async def _sweeper_loop(): while True: try: await asyncio.sleep(_SWEEP_INTERVAL) + await warn_free_expiring() await purge_expired() await purge_free_expired() except asyncio.CancelledError: diff --git a/dashboard/src/App.jsx b/dashboard/src/App.jsx index 8750fc2..33ffdd7 100644 --- a/dashboard/src/App.jsx +++ b/dashboard/src/App.jsx @@ -780,9 +780,14 @@ function App() { /> )} - {/* Cloud: minutes meter + account/sign-in */} + {/* Cloud: minutes meter + account/sign-in. For free users the meter + opens the upgrade modal — otherwise the only path to a plan is + failing against the quota wall. */} {billingEnabled && isManaged && ( - { window.location.hash = '#/account'; }} /> + { + if (plan === 'free') { setTopUpInfo({ context: 'upsell' }); setShowTopUp(true); } + else { window.location.hash = '#/account'; } + }} /> )} {billingEnabled && isSignedIn && !isManaged && ( )} + )} @@ -1574,6 +1583,7 @@ function App() { onClose={() => setShowTopUp(false)} required={topUpInfo.required} remaining={topUpInfo.remaining} + context={topUpInfo.context || 'wall'} /> )} {showTrialUpgrade && ( diff --git a/dashboard/src/components/TopUpModal.jsx b/dashboard/src/components/TopUpModal.jsx index d66716c..8269598 100644 --- a/dashboard/src/components/TopUpModal.jsx +++ b/dashboard/src/components/TopUpModal.jsx @@ -23,14 +23,20 @@ const PLAN_BLURBS = { pro: 'for power users', }; -export default function TopUpModal({ onClose, required, remaining }) { +// context: 'wall' (default) = user hit the 402 quota wall mid-task. +// 'upsell' = user opened it voluntarily (results banner, header meter) +// — different framing: they still HAVE minutes, sell the watermark +// removal + permanence instead of "you ran out". +export default function TopUpModal({ onClose, required, remaining, context = 'wall' }) { const [plans, setPlans] = useState([]); const [topups, setTopups] = useState([]); const [showTopups, setShowTopups] = useState(false); const [busyPrice, setBusyPrice] = useState(null); + const isUpsell = context === 'upsell'; useEffect(() => { - track('QuotaWallSeen', { props: { required: required ?? null, remaining: remaining ?? null } }); + track(isUpsell ? 'UpsellModalSeen' : 'QuotaWallSeen', + { props: { required: required ?? null, remaining: remaining ?? null } }); apiJson('/api/billing/plans') .then((d) => { const monthly = (d.plans || []).filter((p) => p.interval === 'month'); @@ -44,7 +50,8 @@ export default function TopUpModal({ onClose, required, remaining }) { const buy = async (entry, kind) => { setBusyPrice(entry.price_id); - track('QuotaWallCheckout', { props: { kind, plan: entry.plan || null, minutes: entry.minutes } }); + track(isUpsell ? 'UpsellModalCheckout' : 'QuotaWallCheckout', + { props: { kind, plan: entry.plan || null, minutes: entry.minutes } }); try { const { url } = await apiJson('/api/billing/checkout', { method: 'POST', headers: { 'Content-Type': 'application/json' }, @@ -61,14 +68,18 @@ export default function TopUpModal({ onClose, required, remaining }) { const blockedByLength = typeof required === 'number' && typeof remaining === 'number'; return ( - + {/* Goal-gradient framing: they're one step from the thing they came for. */}

- {blockedByLength - ? <>This video needs {required} min and you have{' '} - {Math.max(0, Math.round((remaining || 0) * 10) / 10)} min left - this month. Upgrade and it starts processing right away. - : <>You've used your free minutes for this month. Upgrade and keep clipping right away.} + {isUpsell + ? <>Your free clips carry a watermark and are deleted after 7 days. + Upgrade to keep them clean, permanent, and with a bigger monthly quota. + : blockedByLength + ? <>This video needs {required} min and you have{' '} + {Math.max(0, Math.round((remaining || 0) * 10) / 10)} min left + this month. Upgrade and it starts processing right away. + : <>You've used your free minutes for this month. Upgrade and keep clipping right away.}