Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions apps/backend/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,14 @@ paths:
responses:
"200":
$ref: "#/components/responses/JsonOk"
/links/dev/user-links:
delete:
tags:
- links
summary: Wipe development user links
responses:
"200":
$ref: "#/components/responses/JsonOk"
/links/tags:
get:
tags:
Expand Down
53 changes: 53 additions & 0 deletions apps/backend/src/lib/data/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,59 @@ export async function deleteCollection(
await pb.collection("linksLists").delete(listId);
}

export async function wipeUserLinks(
userId: string,
): Promise<{ deletedCollections: number; deletedFolders: number; deletedItems: number }> {
const pb = getServerPB();
const lists = await pb.collection("linksLists").getFullList({
filter: `user = "${userId}"`,
});
const userLists = lists.filter((list: any) => {
const type = String(list.type ?? "").trim().toLowerCase();
const name = String(list.name ?? "").trim().toLowerCase();
return type !== "home" && name !== "home";
});

const records = await Promise.all(
userLists.map(async (list: any) => {
const [items, folders] = await Promise.all([
pb.collection("linkItems").getFullList({ filter: `collection = "${list.id}"` }),
pb.collection("linksFolders").getFullList({ filter: `list = "${list.id}"` }),
]);
return { items, folders, list };
}),
);
const items = records.flatMap(({ items: listItems }) => listItems);
const folders = records.flatMap(({ folders: listFolders }) => listFolders);

try {
const monitorPB = await getSuperuserPB();
const linkIds = new Set(items.map((item: any) => String(item.id || "")));
const monitors = await monitorPB.collection("monitors").getFullList({
filter: `userId = "${userId}"`,
});
await Promise.all(
monitors
.filter((monitor: any) => linkIds.has(getMonitorLinkId(monitor)))
.map((monitor: any) => monitorPB.collection("monitors").delete(monitor.id)),
);
} catch {
// Ignore monitoring cleanup failures; link wipe should continue.
}

await Promise.all([
...items.map((item: any) => pb.collection("linkItems").delete(item.id)),
...folders.map((folder: any) => pb.collection("linksFolders").delete(folder.id)),
]);
await Promise.all(userLists.map((list: any) => pb.collection("linksLists").delete(list.id)));

return {
deletedCollections: userLists.length,
deletedFolders: folders.length,
deletedItems: items.length,
};
}

export async function createLinkItem(data: {
url: string;
title: string;
Expand Down
11 changes: 10 additions & 1 deletion apps/backend/src/routes/links.route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Hono } from "hono";

import { createCollection, createCollectionLinkItem, createHomeLinkGroup, createHomeLinkItem, createLinkTag, createLinksFolder, deleteLinkItem, getHomeLinkGroups, getHomeLinks, getLinksCollections, getLinksFolders, getLinksItems, getLinksTags, reorderLinks, updateCollection, updateHomeLinkFolderIcon, updateHomeLinkItem, updateLinkTag } from "../lib/data/links";
import { createCollection, createCollectionLinkItem, createHomeLinkGroup, createHomeLinkItem, createLinkTag, createLinksFolder, deleteLinkItem, getHomeLinkGroups, getHomeLinks, getLinksCollections, getLinksFolders, getLinksItems, getLinksTags, reorderLinks, updateCollection, updateHomeLinkFolderIcon, updateHomeLinkItem, updateLinkTag, wipeUserLinks } from "../lib/data/links";

import { readAuthToken, readJsonBody, requireAuth, withJson } from "./shared";
import { config } from "../lib/config";
import { ApiActionError } from "../lib/data/auth";

const linksRoute = new Hono();

Expand Down Expand Up @@ -79,6 +81,13 @@ linksRoute
const { userId } = await requireAuth({ token: readAuthToken(c) });
return deleteLinkItem(userId, String(c.req.param("linkId") ?? ""));
}))
.delete("/api/v1/links/dev/user-links", withJson(async (c) => {
if (config.ENVIRONMENT !== "dev") {
throw new ApiActionError("Not found", 404, { error: "Not found" });
}
const { userId } = await requireAuth({ token: readAuthToken(c) });
return wipeUserLinks(userId);
}))
.post("/api/v1/links/reorder", withJson(async (c) => {
const body = await readJsonBody<any>(c);
const { userId } = await requireAuth({ token: readAuthToken(c) });
Expand Down
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"iconify-picker": "^0.7.2",
"lucide-react": "^0.471.2",
"qrcode": "^1.5.4",
"radix-ui": "^1.6.7",
"react": "^19.2.1",
"react-colorful": "^5.6.1",
"react-day-picker": "^9.11.1",
Expand Down
13 changes: 13 additions & 0 deletions apps/web/public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,19 @@
}
}
},
"/links/dev/user-links": {
"delete": {
"tags": [
"links"
],
"summary": "Wipe development user links",
"responses": {
"200": {
"$ref": "#/components/responses/JsonOk"
}
}
}
},
"/links/tags": {
"get": {
"tags": [
Expand Down
Loading
Loading