fix(cycle): turning the feed off wins against a rotation in either order

DELETE revoked the one feed it had read, so a rotation committing while it waited left the successor live behind a 200. It now revokes every live feed of the record in two passes. A rotation that loses to a turn-off answers 404 cycle.feed.none instead of 409; PATCH documents its 409. The in-repo feed guide describes the three forecast styles.
This commit is contained in:
Marc-André Bombeck committed 2026-11-10 19:53:47 +01:00
1 parent c62abad965
commit c14cc9ab47
5 files changed
+83 -22

No files matched your search

@@ -10,7 +10,8 @@
* URL is returned exactly once. Of two rotations racing, the second finds
* the feed already revoked (or its successor meets the one-live-feed index)
* and gets the mint path's 409 `cycle.feed.exists`; the winner's link
* stands. A subscription turned off during a rotation stays off, and an
* stands. A subscription turned off during a rotation stays off, whichever
* commits first, and the rotation then answers 404 `cycle.feed.none`; an
* option changed during one lands on the link that stays live.
*
* Same gates as turning the subscription on: cookie session only, the owner or
@@ -36,7 +37,11 @@ import {
feedLinks,
noLiveFeed,
} from "@/lib/cycle/feed/manage";
import { FEED_SUMMARY_SELECT, toFeedSummary } from "@/lib/cycle/feed/status";
import {
FEED_SUMMARY_SELECT,
findLiveFeed,
toFeedSummary,
} from "@/lib/cycle/feed/status";
import { mintFeedToken } from "@/lib/cycle/feed/token";
import { prisma } from "@/lib/db";
import { resolveOrigin } from "@/lib/http/origin";
@@ -128,7 +133,11 @@ export const POST = apiHandler(async (request: NextRequest) => {
if (isP2002(err)) return "raced" as const;
throw err;
});
if (rotated === "raced") return feedExists();
// Lost to another rotation, the link that won stands; lost to the
// subscription being turned off, there is none.
if (rotated === "raced") {
return (await findLiveFeed(user.id)) ? feedExists() : noLiveFeed();
}
if (!rotated) return noLiveFeed();
return apiSuccess(
+11 -4
View File
@@ -245,10 +245,17 @@ export const DELETE = apiHandler(async () => {
const live = await findLiveFeed(user.id);
if (!live) return noLiveFeed();
await prisma.cycleCalendarFeed.update({
where: { id: live.id },
data: { revokedAt: new Date() },
});
// Every live feed of the record, not only the one read above, and twice:
// a rotation that held that row while this waited commits its successor
// after the first statement looked, and the second finds it. Off stays
// off even when the link was being renewed at the same moment.
const revokedAt = new Date();
for (let pass = 0; pass < 2; pass++) {
await prisma.cycleCalendarFeed.updateMany({
where: { userId: user.id, revokedAt: null },
data: { revokedAt },
});
}
// The link stops working on the provider's next poll. What the provider
// already copied is beyond reach, which the settings card says before the
// link is ever made.