mirror of
https://github.com/juherr/kill-the-news.git
synced 2026-06-20 22:03:48 +00:00
feat(unsubscribe): RFC 8058 one-click unsubscribe on feed deletion
Capture each sender's List-Unsubscribe one-click URL during ingestion (stored per sender in feed metadata, mirroring the iconDomain pattern) and fire one-click POSTs via ctx.waitUntil when a feed is deleted, so newsletters stop mailing the now-dead address. Tracked with a new unsubscribes_sent counter surfaced on the status page and /api/stats. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+116
-1
@@ -1,7 +1,9 @@
|
||||
import { describe, it, expect, beforeEach } from "vitest";
|
||||
import { http, HttpResponse } from "msw";
|
||||
import { Hono } from "hono";
|
||||
import app from "./admin";
|
||||
import { createMockEnv } from "../test/setup";
|
||||
import { createMockEnv, server } from "../test/setup";
|
||||
import { getCounters } from "../utils/stats";
|
||||
import { Env } from "../types";
|
||||
|
||||
describe("Admin Routes", () => {
|
||||
@@ -328,6 +330,119 @@ describe("Admin Routes", () => {
|
||||
expect(payload.feedId).toBe(feedId);
|
||||
});
|
||||
|
||||
it("fires one-click unsubscribe requests on feed deletion and bumps the counter", async () => {
|
||||
const authCookie = await loginAndGetCookie();
|
||||
const formData = new FormData();
|
||||
formData.append("title", "Unsub Feed");
|
||||
|
||||
await request("/admin/feeds/create", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Cookie: authCookie,
|
||||
Origin: "https://test.getmynews.app",
|
||||
},
|
||||
body: formData,
|
||||
});
|
||||
|
||||
const feedList = (await mockEnv.EMAIL_STORAGE.get(
|
||||
"feeds:list",
|
||||
"json",
|
||||
)) as {
|
||||
feeds: Array<{ id: string }>;
|
||||
} | null;
|
||||
const feedId = feedList?.feeds[0].id as string;
|
||||
|
||||
// Simulate an ingested email having captured an unsubscribe URL.
|
||||
await mockEnv.EMAIL_STORAGE.put(
|
||||
`feed:${feedId}:metadata`,
|
||||
JSON.stringify({
|
||||
emails: [],
|
||||
unsubscribe: { "news@example.com": "https://example.com/u/1" },
|
||||
}),
|
||||
);
|
||||
|
||||
let unsubHit = false;
|
||||
server.use(
|
||||
http.post("https://example.com/u/1", () => {
|
||||
unsubHit = true;
|
||||
return HttpResponse.text("ok");
|
||||
}),
|
||||
);
|
||||
|
||||
const pending: Promise<unknown>[] = [];
|
||||
const ctx = {
|
||||
waitUntil: (p: Promise<unknown>) => pending.push(p),
|
||||
passThroughOnException: () => {},
|
||||
} as unknown as ExecutionContext;
|
||||
|
||||
const deleteRes = await testApp.request(
|
||||
`/admin/feeds/${feedId}/delete`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Cookie: authCookie,
|
||||
Origin: "https://test.getmynews.app",
|
||||
},
|
||||
},
|
||||
mockEnv,
|
||||
ctx,
|
||||
);
|
||||
expect(deleteRes.status).toBe(302);
|
||||
|
||||
await Promise.all(pending);
|
||||
|
||||
expect(unsubHit).toBe(true);
|
||||
const counters = await getCounters(mockEnv.EMAIL_STORAGE);
|
||||
expect(counters.unsubscribes_sent).toBe(1);
|
||||
});
|
||||
|
||||
it("sends no unsubscribe requests when the feed has none", async () => {
|
||||
const authCookie = await loginAndGetCookie();
|
||||
const formData = new FormData();
|
||||
formData.append("title", "No Unsub Feed");
|
||||
|
||||
await request("/admin/feeds/create", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Cookie: authCookie,
|
||||
Origin: "https://test.getmynews.app",
|
||||
},
|
||||
body: formData,
|
||||
});
|
||||
|
||||
const feedList = (await mockEnv.EMAIL_STORAGE.get(
|
||||
"feeds:list",
|
||||
"json",
|
||||
)) as {
|
||||
feeds: Array<{ id: string }>;
|
||||
} | null;
|
||||
const feedId = feedList?.feeds[0].id as string;
|
||||
|
||||
const pending: Promise<unknown>[] = [];
|
||||
const ctx = {
|
||||
waitUntil: (p: Promise<unknown>) => pending.push(p),
|
||||
passThroughOnException: () => {},
|
||||
} as unknown as ExecutionContext;
|
||||
|
||||
await testApp.request(
|
||||
`/admin/feeds/${feedId}/delete`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Cookie: authCookie,
|
||||
Origin: "https://test.getmynews.app",
|
||||
},
|
||||
},
|
||||
mockEnv,
|
||||
ctx,
|
||||
);
|
||||
|
||||
await Promise.all(pending);
|
||||
|
||||
const counters = await getCounters(mockEnv.EMAIL_STORAGE);
|
||||
expect(counters.unsubscribes_sent).toBe(0);
|
||||
});
|
||||
|
||||
it("should allow bulk feed deletion with valid authentication", async () => {
|
||||
const authCookie = await loginAndGetCookie();
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import { bumpCounters } from "../../utils/stats";
|
||||
import { waitUntilSafe } from "../../utils/worker";
|
||||
import { feedRssUrl, feedEmailAddress } from "../../utils/urls";
|
||||
import { logger } from "../../lib/logger";
|
||||
import { sendUnsubscribes } from "../../utils/unsubscribe";
|
||||
import { Layout } from "./ui";
|
||||
import {
|
||||
addFeedToList,
|
||||
@@ -13,6 +14,7 @@ import {
|
||||
removeFeedFromList,
|
||||
removeFeedsFromListBulk,
|
||||
purgeFeedKeysStep,
|
||||
collectUnsubscribeUrls,
|
||||
} from "./helpers";
|
||||
|
||||
type AppEnv = { Bindings: Env };
|
||||
@@ -537,12 +539,19 @@ feedsRouter.post("/:feedId/delete", async (c) => {
|
||||
const wantsJson = (c.req.header("Accept") || "").includes("application/json");
|
||||
|
||||
try {
|
||||
// Read unsubscribe URLs before the metadata is deleted below.
|
||||
const unsubscribeUrls = await collectUnsubscribeUrls(emailStorage, feedId);
|
||||
|
||||
await deleteFeedFast(emailStorage, feedId);
|
||||
const removed = await removeFeedFromList(emailStorage, feedId);
|
||||
if (removed) {
|
||||
await bumpCounters(emailStorage, { feeds_deleted: 1 });
|
||||
}
|
||||
|
||||
if (unsubscribeUrls.length > 0) {
|
||||
waitUntilSafe(c, sendUnsubscribes(unsubscribeUrls, env));
|
||||
}
|
||||
|
||||
waitUntilSafe(
|
||||
c,
|
||||
purgeFeedKeysStep(emailStorage, feedId, {
|
||||
@@ -638,9 +647,12 @@ feedsRouter.post("/bulk-delete", async (c) => {
|
||||
const okIds: string[] = [];
|
||||
const failures: Array<{ feedId: string; error: string }> = [];
|
||||
const warnings: Array<{ feedId: string; warning: string }> = [];
|
||||
const unsubscribeUrls: string[] = [];
|
||||
|
||||
for (const feedId of parsedFeedIds) {
|
||||
try {
|
||||
// Read unsubscribe URLs before the feed metadata is deleted.
|
||||
const urls = await collectUnsubscribeUrls(emailStorage, feedId);
|
||||
const result = await deleteFeedFastDetailed(emailStorage, feedId);
|
||||
if (!result.ok) {
|
||||
failures.push({
|
||||
@@ -660,6 +672,7 @@ feedsRouter.post("/bulk-delete", async (c) => {
|
||||
});
|
||||
}
|
||||
|
||||
unsubscribeUrls.push(...urls);
|
||||
okIds.push(feedId);
|
||||
} catch (error) {
|
||||
logger.error("Error bulk deleting feed", {
|
||||
@@ -677,6 +690,10 @@ feedsRouter.post("/bulk-delete", async (c) => {
|
||||
});
|
||||
}
|
||||
|
||||
if (unsubscribeUrls.length > 0) {
|
||||
waitUntilSafe(c, sendUnsubscribes(unsubscribeUrls, env));
|
||||
}
|
||||
|
||||
const removed = new Set(deletedFeedIds);
|
||||
okIds.forEach((feedId) => {
|
||||
if (!removed.has(feedId)) {
|
||||
@@ -711,11 +728,17 @@ feedsRouter.post("/bulk-delete", async (c) => {
|
||||
}
|
||||
|
||||
const okIds: string[] = [];
|
||||
const unsubscribeUrls: string[] = [];
|
||||
|
||||
for (const feedId of parsedFeedIds) {
|
||||
try {
|
||||
// Read unsubscribe URLs before the feed metadata is deleted.
|
||||
const urls = await collectUnsubscribeUrls(emailStorage, feedId);
|
||||
const result = await deleteFeedFastDetailed(emailStorage, feedId);
|
||||
if (result.ok) okIds.push(feedId);
|
||||
if (result.ok) {
|
||||
unsubscribeUrls.push(...urls);
|
||||
okIds.push(feedId);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error("Error bulk deleting feed", {
|
||||
feedId,
|
||||
@@ -731,6 +754,10 @@ feedsRouter.post("/bulk-delete", async (c) => {
|
||||
});
|
||||
}
|
||||
|
||||
if (unsubscribeUrls.length > 0) {
|
||||
waitUntilSafe(c, sendUnsubscribes(unsubscribeUrls, env));
|
||||
}
|
||||
|
||||
return c.redirect(
|
||||
`${redirectBase}&message=bulkDeleted&count=${deletedFeedIds.length}`,
|
||||
);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { EmailData, FeedList, FeedListItem } from "../../types";
|
||||
import { EmailData, FeedList, FeedListItem, FeedMetadata } from "../../types";
|
||||
import { FEEDS_LIST_KEY } from "../../config/constants";
|
||||
import { logger } from "../../lib/logger";
|
||||
|
||||
@@ -132,6 +132,28 @@ export async function removeFeedFromList(
|
||||
return removed.includes(feedId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a feed's stored RFC 8058 one-click unsubscribe URLs (one per sender).
|
||||
* Must be called before the feed metadata is deleted. Never throws.
|
||||
*/
|
||||
export async function collectUnsubscribeUrls(
|
||||
emailStorage: KVNamespace,
|
||||
feedId: string,
|
||||
): Promise<string[]> {
|
||||
try {
|
||||
const metadata = (await emailStorage.get(`feed:${feedId}:metadata`, {
|
||||
type: "json",
|
||||
})) as FeedMetadata | null;
|
||||
return Object.values(metadata?.unsubscribe ?? {});
|
||||
} catch (error) {
|
||||
logger.error("Error reading unsubscribe URLs", {
|
||||
feedId,
|
||||
error: String(error),
|
||||
});
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function purgeFeedKeysStep(
|
||||
emailStorage: KVNamespace,
|
||||
feedId: string,
|
||||
|
||||
@@ -119,6 +119,7 @@ export async function handle(c: Context<{ Bindings: Env }>): Promise<Response> {
|
||||
tone="danger"
|
||||
/>
|
||||
<Stat label="Net feeds" value={netFeeds} />
|
||||
<Stat label="Unsubscribes sent" value={stats.unsubscribes_sent} />
|
||||
<Stat
|
||||
label="Last feed created"
|
||||
value={formatRelative(stats.last_feed_created_at)}
|
||||
|
||||
Reference in New Issue
Block a user