Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Fix videos.id error (#801)

🐈 Yun Wangbf22a03801a7
diff --git a/apps/web/actions/folders/getVideosByFolderId.ts b/apps/web/actions/folders/getVideosByFolderId.ts
index 64cc73410..64997e617 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<string, Array<{
+ id: string;
+ name: string;
+ organizationId: string;
+ iconUrl: string;
+ isOrg: boolean;
+ }>> = {};
+
+ // 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<string>`
COALESCE(
@@ -117,16 +140,22 @@ 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;
return {
...videoWithoutEffectiveDate,
- sharedOrganizations: video.sharedOrganizations.filter(
- (organization) => organization.id !== null
- ),
- sharedSpaces: video.sharedSpaces.filter((space) => space.id !== null),
+ 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 e9e2b536b..d5a014051 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<string, Array<{
+ id: string;
+ name: string;
+ organizationId: string;
+ iconUrl: string;
+ isOrg: boolean;
+ }>> = {};
+
+ // 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<string>`
COALESCE(
@@ -190,16 +211,22 @@ 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;
return {
...videoWithoutEffectiveDate,
foldersData,
- sharedOrganizations: video.sharedOrganizations.filter(
- (organization) => organization.id !== null
- ),
- sharedSpaces: video.sharedSpaces.filter((space) => space.id !== null),
+ 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
| {