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
44 changes: 44 additions & 0 deletions .github/workflows/aio-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Build and Publish aio-test

on:
push:
branches:
- test

jobs:
build-and-deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Bun
uses: oven-sh/setup-bun@v1

- name: Install dependencies and build web app
run: |
cd apps/web
bun install
bun run build

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}

- name: Build and push multi-architecture Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
platforms: linux/amd64,linux/arm64
tags: ${{ secrets.DOCKER_USERNAME }}/dashwise:test
9 changes: 5 additions & 4 deletions apps/backend/src/lib/data/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClientResponseError, getServerPB } from "../pb/pocketbase";
import { ClientResponseError, getServerPB, getSuperuserPB } from "../pb/pocketbase";
import { defaultHomeConfig } from "@dashwise/assets";
import type { UsersResponse } from "@dashwise/types";

Expand Down Expand Up @@ -276,7 +276,7 @@ export async function signupUser(payload: {
search?: Record<string, unknown>;
};
homeConfig?: Record<string, unknown>;
linksConfig?: Record<string, unknown>;
linksConfig?: Array<Record<string, unknown>>;
};
}) {
if (config.DISABLE_USER_SIGNUP) {
Expand Down Expand Up @@ -320,9 +320,10 @@ export async function signupUser(payload: {
searchPreferences: userConfig?.preferences?.search || {},
});

await pb.collection("pageConfig").create({
const admin = await getSuperuserPB();
await admin.collection("pageConfig").create({
associatedUserId: user.id,
config: defaultHomeConfig,
config: userConfig?.homeConfig ?? defaultHomeConfig,
pageName: "home",
});

Expand Down
111 changes: 73 additions & 38 deletions apps/backend/src/lib/data/links.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiActionError } from "./auth";
import { config } from "../config";
import { getServerPB, getSuperuserPB } from "../pb/pocketbase";
import { getSuperuserPB } from "../pb/pocketbase";

export interface LinkList {
collectionId: string;
Expand Down Expand Up @@ -76,7 +76,7 @@ interface HomeLinkInput {
}

export async function getLinksCollections(userId: string) {
const pb = getServerPB();
const pb = await getSuperuserPB();
const records = await pb.collection("linksLists").getFullList({
filter: `user = "${userId}"`,
sort: "-created",
Expand All @@ -93,7 +93,7 @@ export async function getLinksCollections(userId: string) {
}));
}

async function requireUserList(pb: ReturnType<typeof getServerPB>, userId: string, listId: string) {
async function requireUserList(pb: any, userId: string, listId: string) {
const list = await pb.collection("linksLists").getOne(listId);
if (list.user !== userId) {
throw new ApiActionError("Unauthorized", 401, { error: "Unauthorized" });
Expand All @@ -103,7 +103,7 @@ async function requireUserList(pb: ReturnType<typeof getServerPB>, userId: strin
}

export async function getLinksFolders(userId: string, listId: string) {
const pb = getServerPB();
const pb = await getSuperuserPB();
await requireUserList(pb, userId, listId);

const records = await pb.collection("linksFolders").getFullList({
Expand All @@ -128,7 +128,7 @@ export async function createLinksFolder(
userId: string,
data: { list: string; name: string; parentFolder?: string; icon?: string },
) {
const pb = getServerPB();
const pb = await getSuperuserPB();
await requireUserList(pb, userId, data.list);

const normalizedName = String(data.name || "").trim();
Expand Down Expand Up @@ -174,7 +174,7 @@ export async function createLinksFolder(
}

export async function getLinksItems(userId: string, listId: string, folderId?: string) {
const pb = getServerPB();
const pb = await getSuperuserPB();
await requireUserList(pb, userId, listId);

let filter = `collection = "${listId}"`;
Expand Down Expand Up @@ -208,7 +208,7 @@ export async function getLinksItems(userId: string, listId: string, folderId?: s
}

export async function getLinksTags() {
const pb = getServerPB();
const pb = await getSuperuserPB();
const records = await pb.collection("linksTags").getFullList({
sort: "name",
});
Expand All @@ -224,7 +224,7 @@ export async function createLinkTag(
userId: string,
data: { name: string; color?: string },
): Promise<{ id: string; name: string; color: string }> {
const pb = getServerPB();
const pb = await getSuperuserPB();
const normalizedName = String(data.name || "").trim();

if (!normalizedName) {
Expand All @@ -250,7 +250,7 @@ export async function updateCollection(
listId: string,
data: { name?: string; description?: string },
): Promise<{ id: string; name: string; description: string; type: string }> {
const pb = getServerPB();
const pb = await getSuperuserPB();

const list = await pb.collection("linksLists").getOne(listId);
if (list.user !== userId) throw new ApiActionError("Unauthorized", 401, { error: "Unauthorized" });
Expand All @@ -273,7 +273,7 @@ export async function updateLinkTag(
tagId: string,
data: { name?: string; color?: string },
): Promise<{ id: string; name: string; color: string }> {
const pb = getServerPB();
const pb = await getSuperuserPB();
void userId;

const record = await pb.collection("linksTags").update(tagId, {
Expand Down Expand Up @@ -410,22 +410,19 @@ async function syncHomeLinkMonitor(
const homeListIdByUser = new Map<string, string>();

async function getHomeListId(userId: string) {
const pb = getServerPB();
const pb = await getSuperuserPB();

const cachedHomeListId = homeListIdByUser.get(userId);
if (cachedHomeListId) return cachedHomeListId;

const list = await pb.collection("linksLists").getFirstListItem(
`user = "${userId}" && type = "home"`,
{ fields: "id", skipTotal: true },
);
const list = await getOrCreateHomeCollection(pb, userId);
homeListIdByUser.set(userId, list.id);
return list.id;
}

export async function getHomeLinks(userId: string) {

const pb = getServerPB();
const pb = await getSuperuserPB();
const userHomeListId = await getHomeListId(userId);
const monitorByLinkId = new Map<string, any>();

Expand All @@ -450,7 +447,7 @@ export async function getHomeLinks(userId: string) {
filter: `list = "${userHomeListId}"`,
sort: "name",
}),
]).catch(() => [[], []]);
]);

if (!records.length) return [];

Expand Down Expand Up @@ -501,12 +498,19 @@ async function getOrCreateHomeCollection(pb: any, userId: string) {
.catch(() => null);

if (!homeCollection) {
homeCollection = await pb.collection("linksLists").create({
user: userId,
type: "home",
name: "Home",
description: "",
});
const namedHomeCollection = await pb
.collection("linksLists")
.getFirstListItem(`user = "${userId}" && name = "Home"`)
.catch(() => null);

homeCollection = namedHomeCollection
? await pb.collection("linksLists").update(namedHomeCollection.id, { type: "home" })
: await pb.collection("linksLists").create({
user: userId,
type: "home",
name: "Home",
description: "",
});
}

return homeCollection;
Expand Down Expand Up @@ -547,7 +551,7 @@ async function getOrCreateFolderInList(
}

export async function getHomeLinkGroups(userId: string): Promise<string[]> {
const pb = getServerPB();
const pb = await getSuperuserPB();
const homeCollection = await pb
.collection("linksLists")
.getFirstListItem(`user = "${userId}" && type = "home"`)
Expand All @@ -572,7 +576,7 @@ export async function createHomeLinkGroup(
userId: string,
name: string,
): Promise<{ id: string; name: string }> {
const pb = getServerPB();
const pb = await getSuperuserPB();
const normalizedName = String(name || "").trim();

if (!normalizedName) {
Expand All @@ -597,7 +601,7 @@ export async function updateHomeLinkFolderIcon(
folderId: string,
data: { icon?: string },
): Promise<{ id: string; name: string; icon: string }> {
const pb = getServerPB();
const pb = await getSuperuserPB();
const folder = await pb.collection("linksFolders").getOne(folderId);
const list = await pb.collection("linksLists").getOne(folder.list);

Expand Down Expand Up @@ -640,7 +644,7 @@ export async function createHomeLinkItem(
folder?: string;
}
> {
const pb = getServerPB();
const pb = await getSuperuserPB();
const homeCollection = await getOrCreateHomeCollection(pb, userId);

const existingLinks = await pb.collection("linkItems").getFullList({
Expand Down Expand Up @@ -706,7 +710,7 @@ export async function setHomeLinks(
userId: string,
json: string,
): Promise<void> {
const pb = getServerPB();
const pb = await getSuperuserPB();
const inputs: HomeLinkInput[] = JSON.parse(json);

let homeCollection = await pb
Expand Down Expand Up @@ -793,7 +797,7 @@ export async function updateHomeLinkItem(
secondaryUrls?: SecondaryUrl[];
},
): Promise<void> {
const pb = getServerPB();
const pb = await getSuperuserPB();

const item = await pb.collection("linkItems").getOne(linkId);
const list = await pb.collection("linksLists").getOne(item.collection);
Expand Down Expand Up @@ -852,10 +856,41 @@ export async function reorderLinks(
userId: string,
items: { id: string; type: "link" | "folder"; position: number }[],
): Promise<void> {
const pb = getServerPB();
const pb = await getSuperuserPB();

const records = await Promise.all(items.map(async (item) => {
if (
!item ||
(item.type !== "link" && item.type !== "folder") ||
typeof item.id !== "string" ||
!item.id.trim() ||
!Number.isInteger(item.position)
) {
throw new ApiActionError("Invalid reorder item", 400, { error: "Invalid reorder item" });
}

const record = item.type === "link"
? await pb.collection("linkItems").getOne(item.id)
: await pb.collection("linksFolders").getOne(item.id);

return {
item,
listId: item.type === "link" ? String(record.collection || "") : String(record.list || ""),
};
}));

const listIds = Array.from(new Set(records.map((record) => record.listId).filter(Boolean)));
const lists = await Promise.all(listIds.map((listId) => pb.collection("linksLists").getOne(listId)));
const ownedListIds = new Set(
lists.filter((list: any) => list.user === userId).map((list: any) => String(list.id)),
);

if (records.some((record) => !ownedListIds.has(record.listId))) {
throw new ApiActionError("Not found", 404, { error: "Not found" });
}

await Promise.all(
items.map(async (item) => {
records.map(async ({ item }) => {
if (item.type === "link") {
await pb.collection("linkItems").update(item.id, { position: item.position });
} else {
Expand All @@ -869,7 +904,7 @@ export async function createCollection(
userId: string,
data: { name: string; description?: string; icon?: string },
): Promise<{ id: string; name: string; description: string; icon: string; type: string }> {
const pb = getServerPB();
const pb = await getSuperuserPB();

const record = await pb.collection("linksLists").create({
user: userId,
Expand All @@ -892,7 +927,7 @@ export async function deleteCollection(
userId: string,
listId: string,
): Promise<void> {
const pb = getServerPB();
const pb = await getSuperuserPB();

const list = await pb.collection("linksLists").getOne(listId);
if (list.user !== userId) throw new ApiActionError("Unauthorized", 401, { error: "Unauthorized" });
Expand Down Expand Up @@ -932,7 +967,7 @@ export async function deleteCollection(
export async function wipeUserLinks(
userId: string,
): Promise<{ deletedCollections: number; deletedFolders: number; deletedItems: number }> {
const pb = getServerPB();
const pb = await getSuperuserPB();
const lists = await pb.collection("linksLists").getFullList({
filter: `user = "${userId}"`,
});
Expand Down Expand Up @@ -1003,7 +1038,7 @@ export async function createLinkItem(data: {
created: string;
updated: string;
}> {
const pb = getServerPB();
const pb = await getSuperuserPB();

const record = await pb.collection("linkItems").create({
url: data.url,
Expand Down Expand Up @@ -1053,7 +1088,7 @@ export async function createCollectionLinkItem(
created: string;
updated: string;
}> {
const pb = getServerPB();
const pb = await getSuperuserPB();
const list = await pb.collection("linksLists").getOne(data.collection);

if (list.user !== userId) {
Expand Down Expand Up @@ -1083,7 +1118,7 @@ export async function updateLinkItem(
folder?: string;
}
> {
const pb = getServerPB();
const pb = await getSuperuserPB();

const record = await pb.collection("linkItems").update(itemId, {
...(data.url !== undefined && { url: data.url }),
Expand All @@ -1109,7 +1144,7 @@ export async function deleteLinkItem(
userId: string,
linkId: string,
): Promise<void> {
const pb = getServerPB();
const pb = await getSuperuserPB();

const item = await pb.collection("linkItems").getOne(linkId);
const list = await pb.collection("linksLists").getOne(item.collection);
Expand Down
2 changes: 2 additions & 0 deletions apps/backend/src/routes/auth.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "../lib/data/auth";
import type { UserPropertyValue } from "../lib/data/auth";
import { ensureBuiltinIntegrations } from "../lib/data/integrations";
import { setHomeLinks } from "../lib/data/links";
import { getSuperuserPB } from "../lib/pb/pocketbase";

import {
Expand Down Expand Up @@ -79,6 +80,7 @@ authRoute.get(
},
});

await setHomeLinks(result.user.id, JSON.stringify(links));
await ensureBuiltinIntegrations(result.user.id, await getSuperuserPB());

return result;
Expand Down
Loading
Loading