Skip to content
Merged
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
30 changes: 20 additions & 10 deletions apps/backend/src/lib/data/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,23 @@ async function getOrCreateHomeCollection(pb: any, userId: string) {
return homeCollection;
}

async function findHomeCollection(pb: any, userId: string) {
const homeCollection = await pb
.collection("linksLists")
.getFirstListItem(`user = "${userId}" && type = "home"`)
.catch(() => null);
if (homeCollection) return homeCollection;

// Older accounts may still have the home list by name without its type migrated.
const legacyHomeCollection = await pb
.collection("linksLists")
.getFirstListItem(`user = "${userId}" && name = "Home"`)
.catch(() => null);
if (!legacyHomeCollection) return null;

return pb.collection("linksLists").update(legacyHomeCollection.id, { type: "home" });
}

async function getOrCreateFolderInList(
pb: any,
listId: string,
Expand Down Expand Up @@ -596,10 +613,7 @@ export async function deleteUnusedHomeLinkGroups(userId: string) {

export async function getHomeLinkGroups(userId: string): Promise<string[]> {
const pb = await getSuperuserPB();
const homeCollection = await pb
.collection("linksLists")
.getFirstListItem(`user = "${userId}" && type = "home"`)
.catch(() => null);
const homeCollection = await findHomeCollection(pb, userId);

if (!homeCollection) return [];

Expand Down Expand Up @@ -629,9 +643,7 @@ export async function renameHomeLinkGroup(
throw new ApiActionError("Link group name must be between 1 and 100 characters", 400);
}

const homeCollection = await pb.collection("linksLists").getFirstListItem(
`user = "${userId}" && type = "home"`,
).catch(() => null);
const homeCollection = await findHomeCollection(pb, userId);
if (!homeCollection) throw new ApiActionError("Home links not found", 404);

const rootFolders = await pb.collection("linksFolders").getFullList({
Expand Down Expand Up @@ -659,9 +671,7 @@ export async function deleteHomeLinkGroup(
const normalizedGroupName = String(groupName || "").trim();
if (!normalizedGroupName) throw new ApiActionError("Link group name is required", 400);

const homeCollection = await pb.collection("linksLists").getFirstListItem(
`user = "${userId}" && type = "home"`,
).catch(() => null);
const homeCollection = await findHomeCollection(pb, userId);
if (!homeCollection) throw new ApiActionError("Home links not found", 404);

const [folders, links] = await Promise.all([
Expand Down
Loading