Skip to content
Open
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
97 changes: 48 additions & 49 deletions apps/web/actions/caps/share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,34 +76,33 @@ export async function shareCap({
.from(sharedVideos)
.where(eq(sharedVideos.videoId, capId));

for (const sharedOrganization of currentSharedOrganizations) {
if (!organizationIds.includes(sharedOrganization.organizationId)) {
await db()
.delete(sharedVideos)
.where(
and(
eq(sharedVideos.videoId, capId),
eq(
sharedVideos.organizationId,
sharedOrganization.organizationId,
),
),
);
}
const orgIdsToRemove = currentSharedOrganizations
.filter((s) => !organizationIds.includes(s.organizationId))
.map((s) => s.organizationId);
if (orgIdsToRemove.length > 0) {
await db()
.delete(sharedVideos)
.where(
and(
eq(sharedVideos.videoId, capId),
inArray(sharedVideos.organizationId, orgIdsToRemove),
),
);
}

for (const organizationId of organizationIds) {
const existingShare = currentSharedOrganizations.find(
(share) => share.organizationId === organizationId,
);
if (!existingShare) {
await db().insert(sharedVideos).values({
id: nanoId(),
videoId: capId,
organizationId: organizationId,
sharedByUserId: user.id,
});
}
const existingOrgIds = new Set(
currentSharedOrganizations.map((s) => s.organizationId),
);
const newOrgEntries = organizationIds
.filter((id) => !existingOrgIds.has(id))
.map((organizationId) => ({
id: nanoId(),
videoId: capId,
organizationId,
sharedByUserId: user.id,
}));
if (newOrgEntries.length > 0) {
await db().insert(sharedVideos).values(newOrgEntries);
}

const spacesIds = spacesData.map((space) => space.id);
Expand All @@ -113,31 +112,31 @@ export async function shareCap({
.from(spaceVideos)
.where(eq(spaceVideos.videoId, capId));

for (const spaceVideo of currentSpaceVideos) {
if (!spacesIds.includes(spaceVideo.spaceId)) {
await db()
.delete(spaceVideos)
.where(
and(
eq(spaceVideos.videoId, capId),
eq(spaceVideos.spaceId, spaceVideo.spaceId),
),
);
}
const spaceIdsToRemove = currentSpaceVideos
.filter((s) => !spacesIds.includes(s.spaceId))
.map((s) => s.spaceId);
if (spaceIdsToRemove.length > 0) {
await db()
.delete(spaceVideos)
.where(
and(
eq(spaceVideos.videoId, capId),
inArray(spaceVideos.spaceId, spaceIdsToRemove),
),
);
}

for (const spaceId of spacesIds) {
const existingSpaceShare = currentSpaceVideos.find(
(share) => share.spaceId === spaceId,
);
if (!existingSpaceShare) {
await db().insert(spaceVideos).values({
id: nanoId(),
videoId: capId,
spaceId: spaceId,
addedById: user.id,
});
}
const existingSpaceIds = new Set(currentSpaceVideos.map((s) => s.spaceId));
const newSpaceEntries = spacesIds
.filter((id) => !existingSpaceIds.has(id))
.map((spaceId) => ({
id: nanoId(),
videoId: capId,
spaceId,
addedById: user.id,
}));
if (newSpaceEntries.length > 0) {
await db().insert(spaceVideos).values(newSpaceEntries);
}

// Update public status if provided
Expand Down
Loading