Skip to content

Commit bb2fc83

Browse files
committed
Sync from main repo
1 parent 5b5a5de commit bb2fc83

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { rateLimiter } from "./middleware/rate-limit.js";
1111
import {
1212
handleAddItem,
1313
handleAtom,
14+
handleClearItems,
1415
handleCreateFeed,
1516
handleGetConfig,
1617
handleGetItems,
@@ -82,6 +83,10 @@ feedRoutes.get("/:feedId/rss.xml", handleRss);
8283
feedRoutes.get("/:feedId/atom.xml", handleAtom);
8384
feedRoutes.get("/:feedId/feed.json", handleJsonFeed);
8485
feedRoutes.get("/:feedId/raw.json", handleRawJson);
86+
feedRoutes.get("/:feedId", (c) => {
87+
const feedId = c.req.param("feedId");
88+
return c.redirect(`/${feedId}/feed.json`, 301);
89+
});
8590

8691
// Protected API routes (authentication required)
8792
const protectedRoutes = new Hono();
@@ -91,6 +96,7 @@ protectedRoutes.get("/api/feeds/:feedId/config", handleGetConfig);
9196
protectedRoutes.put("/api/feeds/:feedId/config", handleUpdateConfig);
9297
protectedRoutes.get("/api/feeds/:feedId/items", handleGetItems);
9398
protectedRoutes.post("/api/feeds/:feedId/items", handleAddItem);
99+
protectedRoutes.delete("/api/feeds/:feedId/items", handleClearItems);
94100

95101
app.route("/", publicRoutes);
96102
app.route("/", feedRoutes);

src/routes.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DuplicateItemError, FeedNotFoundError } from "./errors.js";
44
import { formatItems, generateFeed } from "./formatters.js";
55
import {
66
addItem,
7+
clearItems,
78
createFeed,
89
feedExists,
910
getAllFeedIds,
@@ -83,6 +84,55 @@ export async function handleListFeeds(c: Context): Promise<Response> {
8384
}
8485
}
8586

87+
/**
88+
* Clear all items from a specific feed
89+
*/
90+
export async function handleClearItems(c: Context): Promise<Response> {
91+
const feedId = c.req.param("feedId");
92+
if (!feedId) {
93+
return c.json({ error: "Feed ID is required" }, 400);
94+
}
95+
96+
try {
97+
if (!(await feedExists(feedId))) {
98+
return c.json(
99+
{
100+
error: "Feed Not Found",
101+
message: `Feed with ID '${feedId}' does not exist`,
102+
},
103+
404,
104+
);
105+
}
106+
107+
await clearItems(feedId);
108+
109+
return c.json({
110+
message: "All items cleared successfully",
111+
feedId,
112+
});
113+
} catch (error) {
114+
console.error("Failed to clear feed items:", error);
115+
116+
if (error instanceof FeedNotFoundError) {
117+
return c.json(
118+
{
119+
error: "Feed Not Found",
120+
message: error.message,
121+
},
122+
404,
123+
);
124+
}
125+
126+
return c.json(
127+
{
128+
error: "Server Error",
129+
message: `Failed to clear feed items: ${error}`,
130+
},
131+
500,
132+
);
133+
}
134+
}
135+
86136
/**
87137
* Create a new feed
88138
*/

src/storage.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,31 @@ export async function addItem(feedId: string, item: RssItem): Promise<void> {
332332
}
333333
}
334334

335+
/**
336+
* Clear all items from a specific feed
337+
*/
338+
export async function clearItems(feedId: string): Promise<void> {
339+
try {
340+
if (!(await feedExists(feedId))) {
341+
throw new FeedNotFoundError(feedId);
342+
}
343+
344+
const itemsKey = `feed:${feedId}:items`;
345+
const guidsKey = `feed:${feedId}:guids`;
346+
347+
// Delete all items and GUIDs
348+
await redis.del(itemsKey);
349+
await redis.del(guidsKey);
350+
351+
console.log(`Cleared all items from feed: ${feedId}`);
352+
} catch (error) {
353+
if (error instanceof FeedNotFoundError) {
354+
throw error;
355+
}
356+
throw new StorageError(`Failed to clear items: ${error}`);
357+
}
358+
}
359+
335360
/**
336361
* Get a list of all feed IDs
337362
*/

0 commit comments

Comments
 (0)