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
139 changes: 84 additions & 55 deletions apps/web/actions/folders/getVideosByFolderId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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(
Expand Down Expand Up @@ -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
| {
Expand Down
139 changes: 83 additions & 56 deletions apps/web/app/(org)/dashboard/caps/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
}: {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
| {
Expand Down