From f74054e4dc372a82d9be87e3f1818eda63b1d240 Mon Sep 17 00:00:00 2001 From: mogita Date: Wed, 23 Jul 2025 22:18:35 +0800 Subject: [PATCH 1/4] fix: use raw sql with proper correlation --- .github/workflows/docker-build-web.yml | 6 ++--- .../actions/folders/getVideosByFolderId.ts | 24 +++++++++---------- apps/web/app/(org)/dashboard/caps/page.tsx | 24 +++++++++---------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/.github/workflows/docker-build-web.yml b/.github/workflows/docker-build-web.yml index 01c1ad29445..86b02287d74 100644 --- a/.github/workflows/docker-build-web.yml +++ b/.github/workflows/docker-build-web.yml @@ -55,7 +55,7 @@ jobs: file: apps/web/Dockerfile platforms: linux/${{ matrix.platform }} push: true - outputs: type=image,name=ghcr.io/capsoftware/cap-web,push-by-digest=true + outputs: type=image,name=ghcr.io/mogita/cap-web,push-by-digest=true cache-from: type=gha,scope=buildx-${{ matrix.platform }} cache-to: type=gha,mode=max,scope=buildx-${{ matrix.platform }} @@ -101,5 +101,5 @@ jobs: - name: Create Image Manifest run: | - docker buildx imagetools create -t ghcr.io/capsoftware/cap-web:${{ inputs.tag || 'latest' }} \ - $(find /tmp/digests -type f -not -path "*/\.*" -exec basename {} \; | xargs -I {} echo "ghcr.io/capsoftware/cap-web@sha256:{}") + docker buildx imagetools create -t ghcr.io/mogita/cap-web:${{ inputs.tag || 'latest' }} \ + $(find /tmp/digests -type f -not -path "*/\.*" -exec basename {} \; | xargs -I {} echo "ghcr.io/mogita/cap-web@sha256:{}") diff --git a/apps/web/actions/folders/getVideosByFolderId.ts b/apps/web/actions/folders/getVideosByFolderId.ts index 64cc7341086..561e5a4d9a9 100644 --- a/apps/web/actions/folders/getVideosByFolderId.ts +++ b/apps/web/actions/folders/getVideosByFolderId.ts @@ -58,30 +58,30 @@ export async function getVideosByFolderId(folderId: string) { ) FROM ( -- Include spaces where the video is directly added via space_videos - SELECT DISTINCT - s.id, - s.name, - s.organizationId, + SELECT DISTINCT + s.id, + s.name, + s.organizationId, o.iconUrl as iconUrl, FALSE as isOrg FROM space_videos sv JOIN spaces s ON sv.spaceId = s.id JOIN organizations o ON s.organizationId = o.id - WHERE sv.videoId = ${videos.id} - + WHERE sv.videoId = videos.id + UNION - + -- For organization-level sharing, include the organization details -- and mark it as an organization with isOrg=TRUE - SELECT DISTINCT - o.id as id, - o.name as name, - o.id as organizationId, + SELECT DISTINCT + o.id as id, + o.name as name, + o.id as organizationId, o.iconUrl as iconUrl, TRUE as isOrg FROM shared_videos sv JOIN organizations o ON sv.organizationId = o.id - WHERE sv.videoId = ${videos.id} + WHERE sv.videoId = videos.id ) AS s ), JSON_ARRAY() diff --git a/apps/web/app/(org)/dashboard/caps/page.tsx b/apps/web/app/(org)/dashboard/caps/page.tsx index e9e2b536b09..512d57ebc35 100644 --- a/apps/web/app/(org)/dashboard/caps/page.tsx +++ b/apps/web/app/(org)/dashboard/caps/page.tsx @@ -110,30 +110,30 @@ export default async function CapsPage({ ) FROM ( -- Include spaces where the video is directly added via space_videos - SELECT DISTINCT - s.id, - s.name, - s.organizationId, + SELECT DISTINCT + s.id, + s.name, + s.organizationId, o.iconUrl as iconUrl, FALSE as isOrg FROM space_videos sv JOIN spaces s ON sv.spaceId = s.id JOIN organizations o ON s.organizationId = o.id - WHERE sv.videoId = ${videos.id} - + WHERE sv.videoId = videos.id + UNION - + -- For organization-level sharing, include the organization details -- and mark it as an organization with isOrg=TRUE - SELECT DISTINCT - o.id as id, - o.name as name, - o.id as organizationId, + SELECT DISTINCT + o.id as id, + o.name as name, + o.id as organizationId, o.iconUrl as iconUrl, TRUE as isOrg FROM shared_videos sv JOIN organizations o ON sv.organizationId = o.id - WHERE sv.videoId = ${videos.id} + WHERE sv.videoId = videos.id ) AS s ), JSON_ARRAY() From b24a52640ac473a8dde79ee110e1f67e3d3924ae Mon Sep 17 00:00:00 2001 From: mogita Date: Wed, 23 Jul 2025 23:15:37 +0800 Subject: [PATCH 2/4] fix: separate queries in attempt to fix the dashboard issue --- .../actions/folders/getVideosByFolderId.ts | 131 +++++++++++------- apps/web/app/(org)/dashboard/caps/page.tsx | 131 +++++++++++------- 2 files changed, 157 insertions(+), 105 deletions(-) diff --git a/apps/web/actions/folders/getVideosByFolderId.ts b/apps/web/actions/folders/getVideosByFolderId.ts index 561e5a4d9a9..a1a50ffb4d1 100644 --- a/apps/web/actions/folders/getVideosByFolderId.ts +++ b/apps/web/actions/folders/getVideosByFolderId.ts @@ -7,11 +7,84 @@ import { users, organizations, sharedVideos, + spaceVideos, + spaces, } from "@cap/database/schema"; import { eq, desc } from "drizzle-orm"; import { sql } from "drizzle-orm/sql"; import { revalidatePath } from "next/cache"; +// Helper function to fetch shared spaces data for videos +async function getSharedSpacesForVideos(videoIds: string[]) { + if (videoIds.length === 0) return {}; + + // Fetch space-level sharing + const spaceSharing = await db() + .select({ + videoId: spaceVideos.videoId, + id: spaces.id, + name: spaces.name, + organizationId: spaces.organizationId, + iconUrl: organizations.iconUrl, + }) + .from(spaceVideos) + .innerJoin(spaces, eq(spaceVideos.spaceId, spaces.id)) + .innerJoin(organizations, eq(spaces.organizationId, organizations.id)) + .where(sql`${spaceVideos.videoId} IN (${sql.join(videoIds.map(id => sql`${id}`), sql`, `)})`); + + // Fetch organization-level sharing + const orgSharing = await db() + .select({ + videoId: sharedVideos.videoId, + id: organizations.id, + name: organizations.name, + organizationId: organizations.id, + iconUrl: organizations.iconUrl, + }) + .from(sharedVideos) + .innerJoin(organizations, eq(sharedVideos.organizationId, organizations.id)) + .where(sql`${sharedVideos.videoId} IN (${sql.join(videoIds.map(id => sql`${id}`), sql`, `)})`); + + // Combine and group by videoId + const sharedSpacesMap: Record> = {}; + + // Add space-level sharing + spaceSharing.forEach(space => { + if (!sharedSpacesMap[space.videoId]) { + sharedSpacesMap[space.videoId] = []; + } + sharedSpacesMap[space.videoId].push({ + id: space.id, + name: space.name, + organizationId: space.organizationId, + iconUrl: space.iconUrl || '', + isOrg: false, + }); + }); + + // Add organization-level sharing + orgSharing.forEach(org => { + if (!sharedSpacesMap[org.videoId]) { + sharedSpacesMap[org.videoId] = []; + } + sharedSpacesMap[org.videoId].push({ + id: org.id, + name: org.name, + organizationId: org.organizationId, + iconUrl: org.iconUrl || '', + isOrg: true, + }); + }); + + return sharedSpacesMap; +} + export async function getVideosByFolderId(folderId: string) { if (!folderId) throw new Error("Folder ID is required"); @@ -36,57 +109,7 @@ export async function getVideosByFolderId(folderId: string) { JSON_ARRAY() ) `, - sharedSpaces: sql< - { - id: string; - name: string; - organizationId: string; - iconUrl: string; - isOrg: boolean; - }[] - >` - COALESCE( - ( - SELECT JSON_ARRAYAGG( - JSON_OBJECT( - 'id', s.id, - 'name', s.name, - 'organizationId', s.organizationId, - 'iconUrl', s.iconUrl, - 'isOrg', s.isOrg - ) - ) - FROM ( - -- Include spaces where the video is directly added via space_videos - SELECT DISTINCT - s.id, - s.name, - s.organizationId, - o.iconUrl as iconUrl, - FALSE as isOrg - FROM space_videos sv - JOIN spaces s ON sv.spaceId = s.id - JOIN organizations o ON s.organizationId = o.id - WHERE sv.videoId = videos.id - - UNION - - -- For organization-level sharing, include the organization details - -- and mark it as an organization with isOrg=TRUE - SELECT DISTINCT - o.id as id, - o.name as name, - o.id as organizationId, - o.iconUrl as iconUrl, - TRUE as isOrg - FROM shared_videos sv - JOIN organizations o ON sv.organizationId = o.id - WHERE sv.videoId = videos.id - ) AS s - ), - JSON_ARRAY() - ) - `, + ownerName: users.name, effectiveDate: sql` COALESCE( @@ -117,6 +140,10 @@ export async function getVideosByFolderId(folderId: string) { )`) ); + // Fetch shared spaces data for all videos + const videoIds = videoData.map(video => video.id); + const sharedSpacesMap = await getSharedSpacesForVideos(videoIds); + // Process the video data to match the expected format const processedVideoData = videoData.map((video) => { const { effectiveDate, ...videoWithoutEffectiveDate } = video; @@ -126,7 +153,7 @@ export async function getVideosByFolderId(folderId: string) { sharedOrganizations: video.sharedOrganizations.filter( (organization) => organization.id !== null ), - sharedSpaces: video.sharedSpaces.filter((space) => space.id !== null), + sharedSpaces: sharedSpacesMap[video.id] || [], ownerName: video.ownerName ?? "", metadata: video.metadata as | { diff --git a/apps/web/app/(org)/dashboard/caps/page.tsx b/apps/web/app/(org)/dashboard/caps/page.tsx index 512d57ebc35..77dc74f7152 100644 --- a/apps/web/app/(org)/dashboard/caps/page.tsx +++ b/apps/web/app/(org)/dashboard/caps/page.tsx @@ -3,7 +3,7 @@ import { getCurrentUser } from "@cap/database/auth/session"; import { comments, folders, organizations, - sharedVideos, users, + sharedVideos, spaceVideos, spaces, users, videos } from "@cap/database/schema"; import { and, count, desc, eq, isNull, sql } from "drizzle-orm"; @@ -16,6 +16,77 @@ export const metadata: Metadata = { title: "My Caps — Cap", }; +// Helper function to fetch shared spaces data for videos +async function getSharedSpacesForVideos(videoIds: string[]) { + if (videoIds.length === 0) return {}; + + // Fetch space-level sharing + const spaceSharing = await db() + .select({ + videoId: spaceVideos.videoId, + id: spaces.id, + name: spaces.name, + organizationId: spaces.organizationId, + iconUrl: organizations.iconUrl, + }) + .from(spaceVideos) + .innerJoin(spaces, eq(spaceVideos.spaceId, spaces.id)) + .innerJoin(organizations, eq(spaces.organizationId, organizations.id)) + .where(sql`${spaceVideos.videoId} IN (${sql.join(videoIds.map(id => sql`${id}`), sql`, `)})`); + + // Fetch organization-level sharing + const orgSharing = await db() + .select({ + videoId: sharedVideos.videoId, + id: organizations.id, + name: organizations.name, + organizationId: organizations.id, + iconUrl: organizations.iconUrl, + }) + .from(sharedVideos) + .innerJoin(organizations, eq(sharedVideos.organizationId, organizations.id)) + .where(sql`${sharedVideos.videoId} IN (${sql.join(videoIds.map(id => sql`${id}`), sql`, `)})`); + + // Combine and group by videoId + const sharedSpacesMap: Record> = {}; + + // Add space-level sharing + spaceSharing.forEach(space => { + if (!sharedSpacesMap[space.videoId]) { + sharedSpacesMap[space.videoId] = []; + } + sharedSpacesMap[space.videoId].push({ + id: space.id, + name: space.name, + organizationId: space.organizationId, + iconUrl: space.iconUrl || '', + isOrg: false, + }); + }); + + // Add organization-level sharing + orgSharing.forEach(org => { + if (!sharedSpacesMap[org.videoId]) { + sharedSpacesMap[org.videoId] = []; + } + sharedSpacesMap[org.videoId].push({ + id: org.id, + name: org.name, + organizationId: org.organizationId, + iconUrl: org.iconUrl || '', + isOrg: true, + }); + }); + + return sharedSpacesMap; +} + export default async function CapsPage({ searchParams, }: { @@ -88,57 +159,7 @@ export default async function CapsPage({ JSON_ARRAY() ) `, - sharedSpaces: sql< - { - id: string; - name: string; - organizationId: string; - iconUrl: string; - isOrg: boolean; - }[] - >` - COALESCE( - ( - SELECT JSON_ARRAYAGG( - JSON_OBJECT( - 'id', s.id, - 'name', s.name, - 'organizationId', s.organizationId, - 'iconUrl', s.iconUrl, - 'isOrg', s.isOrg - ) - ) - FROM ( - -- Include spaces where the video is directly added via space_videos - SELECT DISTINCT - s.id, - s.name, - s.organizationId, - o.iconUrl as iconUrl, - FALSE as isOrg - FROM space_videos sv - JOIN spaces s ON sv.spaceId = s.id - JOIN organizations o ON s.organizationId = o.id - WHERE sv.videoId = videos.id - - UNION - - -- For organization-level sharing, include the organization details - -- and mark it as an organization with isOrg=TRUE - SELECT DISTINCT - o.id as id, - o.name as name, - o.id as organizationId, - o.iconUrl as iconUrl, - TRUE as isOrg - FROM shared_videos sv - JOIN organizations o ON sv.organizationId = o.id - WHERE sv.videoId = videos.id - ) AS s - ), - JSON_ARRAY() - ) - `, + ownerName: users.name, effectiveDate: sql` COALESCE( @@ -190,6 +211,10 @@ export default async function CapsPage({ ) ); + // Fetch shared spaces data for all videos + const videoIds = videoData.map(video => video.id); + const sharedSpacesMap = await getSharedSpacesForVideos(videoIds); + const processedVideoData = videoData.map((video) => { const { effectiveDate, ...videoWithoutEffectiveDate } = video; @@ -199,7 +224,7 @@ export default async function CapsPage({ sharedOrganizations: video.sharedOrganizations.filter( (organization) => organization.id !== null ), - sharedSpaces: video.sharedSpaces.filter((space) => space.id !== null), + sharedSpaces: sharedSpacesMap[video.id] || [], ownerName: video.ownerName ?? "", metadata: video.metadata as | { From 261531719999d25fad4cfaf598368ef2db852421 Mon Sep 17 00:00:00 2001 From: mogita Date: Wed, 23 Jul 2025 23:54:20 +0800 Subject: [PATCH 3/4] fix: defensive code for shared organizations --- apps/web/actions/folders/getVideosByFolderId.ts | 10 ++++++---- apps/web/app/(org)/dashboard/caps/page.tsx | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/apps/web/actions/folders/getVideosByFolderId.ts b/apps/web/actions/folders/getVideosByFolderId.ts index a1a50ffb4d1..64997e6173f 100644 --- a/apps/web/actions/folders/getVideosByFolderId.ts +++ b/apps/web/actions/folders/getVideosByFolderId.ts @@ -150,10 +150,12 @@ export async function getVideosByFolderId(folderId: string) { return { ...videoWithoutEffectiveDate, - sharedOrganizations: video.sharedOrganizations.filter( - (organization) => organization.id !== null - ), - sharedSpaces: sharedSpacesMap[video.id] || [], + sharedOrganizations: Array.isArray(video.sharedOrganizations) + ? video.sharedOrganizations.filter((organization) => organization.id !== null) + : [], + sharedSpaces: Array.isArray(sharedSpacesMap[video.id]) + ? sharedSpacesMap[video.id] + : [], ownerName: video.ownerName ?? "", metadata: video.metadata as | { diff --git a/apps/web/app/(org)/dashboard/caps/page.tsx b/apps/web/app/(org)/dashboard/caps/page.tsx index 77dc74f7152..d5a01405139 100644 --- a/apps/web/app/(org)/dashboard/caps/page.tsx +++ b/apps/web/app/(org)/dashboard/caps/page.tsx @@ -221,10 +221,12 @@ export default async function CapsPage({ return { ...videoWithoutEffectiveDate, foldersData, - sharedOrganizations: video.sharedOrganizations.filter( - (organization) => organization.id !== null - ), - sharedSpaces: sharedSpacesMap[video.id] || [], + sharedOrganizations: Array.isArray(video.sharedOrganizations) + ? video.sharedOrganizations.filter((organization) => organization.id !== null) + : [], + sharedSpaces: Array.isArray(sharedSpacesMap[video.id]) + ? sharedSpacesMap[video.id] + : [], ownerName: video.ownerName ?? "", metadata: video.metadata as | { From 571f77921d4c5ff82eed8decba9e40f8c8c1fe8d Mon Sep 17 00:00:00 2001 From: mogita Date: Thu, 24 Jul 2025 22:55:48 +0800 Subject: [PATCH 4/4] fix: revert docker registry to original capsoftware repo --- .github/workflows/docker-build-web.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/docker-build-web.yml b/.github/workflows/docker-build-web.yml index 86b02287d74..01c1ad29445 100644 --- a/.github/workflows/docker-build-web.yml +++ b/.github/workflows/docker-build-web.yml @@ -55,7 +55,7 @@ jobs: file: apps/web/Dockerfile platforms: linux/${{ matrix.platform }} push: true - outputs: type=image,name=ghcr.io/mogita/cap-web,push-by-digest=true + outputs: type=image,name=ghcr.io/capsoftware/cap-web,push-by-digest=true cache-from: type=gha,scope=buildx-${{ matrix.platform }} cache-to: type=gha,mode=max,scope=buildx-${{ matrix.platform }} @@ -101,5 +101,5 @@ jobs: - name: Create Image Manifest run: | - docker buildx imagetools create -t ghcr.io/mogita/cap-web:${{ inputs.tag || 'latest' }} \ - $(find /tmp/digests -type f -not -path "*/\.*" -exec basename {} \; | xargs -I {} echo "ghcr.io/mogita/cap-web@sha256:{}") + docker buildx imagetools create -t ghcr.io/capsoftware/cap-web:${{ inputs.tag || 'latest' }} \ + $(find /tmp/digests -type f -not -path "*/\.*" -exec basename {} \; | xargs -I {} echo "ghcr.io/capsoftware/cap-web@sha256:{}")