-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Move video to folder #1132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Move video to folder #1132
Changes from all commits
568e885
91415b3
a15335a
5d8d23c
ea8994c
c2d5606
0ab0856
b84ad4b
e360651
0e9aed0
b2abcbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| "use server"; | ||
|
|
||
| import { getCurrentUser } from "@cap/database/auth/session"; | ||
| import { CurrentUser } from "@cap/web-domain"; | ||
| import { Effect } from "effect"; | ||
| import { getAllFolders } from "../../lib/folder"; | ||
| import { runPromise } from "../../lib/server"; | ||
|
|
||
| export async function getAllFoldersAction( | ||
| root: | ||
| | { variant: "user" } | ||
| | { variant: "space"; spaceId: string } | ||
| | { variant: "org"; organizationId: string } | ||
| ) { | ||
| try { | ||
| const user = await getCurrentUser(); | ||
| if (!user || !user.activeOrganizationId) { | ||
| return { | ||
| success: false as const, | ||
| error: "Unauthorized or no active organization", | ||
| }; | ||
| } | ||
|
|
||
| const folders = await runPromise( | ||
| getAllFolders(root).pipe(Effect.provideService(CurrentUser, user)) | ||
| ); | ||
| return { success: true as const, folders }; | ||
| } catch (error) { | ||
| console.error("Error fetching folders:", error); | ||
| return { | ||
| success: false as const, | ||
| error: "Failed to fetch folders", | ||
| }; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| "use server"; | ||
|
|
||
| import { getCurrentUser } from "@cap/database/auth/session"; | ||
| import { CurrentUser, Video, Folder, Policy } from "@cap/web-domain"; | ||
| import { SpacesPolicy } from "@cap/web-backend"; | ||
| import { Effect } from "effect"; | ||
| import { moveVideosToFolder } from "../../lib/folder"; | ||
| import { runPromise } from "../../lib/server"; | ||
| import { revalidatePath } from "next/cache"; | ||
|
|
||
| interface MoveVideosToFolderParams { | ||
| videoIds: string[]; | ||
| targetFolderId: string | null; | ||
| spaceId?: string | null; | ||
| } | ||
|
|
||
| export async function moveVideosToFolderAction({ | ||
| videoIds, | ||
| targetFolderId, | ||
| spaceId, | ||
| }: MoveVideosToFolderParams) { | ||
| try { | ||
| const user = await getCurrentUser(); | ||
| if (!user || !user.activeOrganizationId) { | ||
| return { | ||
| success: false as const, | ||
| error: "Unauthorized or no active organization", | ||
| }; | ||
| } | ||
|
|
||
| const typedVideoIds = videoIds.map((id) => Video.VideoId.make(id)); | ||
| const typedTargetFolderId = targetFolderId | ||
| ? Folder.FolderId.make(targetFolderId) | ||
| : null; | ||
|
|
||
| const root = spaceId | ||
| ? { variant: "space" as const, spaceId } | ||
| : { variant: "org" as const, organizationId: user.activeOrganizationId }; | ||
|
|
||
| const moveVideosEffect = spaceId | ||
| ? Effect.gen(function* () { | ||
| const spacesPolicy = yield* SpacesPolicy; | ||
|
|
||
| return yield* moveVideosToFolder( | ||
| typedVideoIds, | ||
| typedTargetFolderId, | ||
| root | ||
| ).pipe(Policy.withPolicy(spacesPolicy.isMember(spaceId))); | ||
| }).pipe(Effect.provideService(CurrentUser, user)) | ||
| : moveVideosToFolder(typedVideoIds, typedTargetFolderId, root).pipe( | ||
| Effect.provideService(CurrentUser, user) | ||
| ); | ||
|
|
||
| const result = await runPromise(moveVideosEffect); | ||
|
|
||
| revalidatePath("/dashboard/caps"); | ||
|
|
||
| if (spaceId) { | ||
| revalidatePath(`/dashboard/spaces/${spaceId}`); | ||
| result.originalFolderIds.forEach((folderId) => { | ||
| if (folderId) { | ||
| revalidatePath(`/dashboard/spaces/${spaceId}/folder/${folderId}`); | ||
| } | ||
| }); | ||
| if (result.targetFolderId) { | ||
| revalidatePath( | ||
| `/dashboard/spaces/${spaceId}/folder/${result.targetFolderId}` | ||
| ); | ||
| } | ||
| } else { | ||
| result.originalFolderIds.forEach((folderId) => { | ||
| if (folderId) { | ||
| revalidatePath(`/dashboard/folder/${folderId}`); | ||
| } | ||
| }); | ||
| if (result.targetFolderId) { | ||
| revalidatePath(`/dashboard/folder/${result.targetFolderId}`); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| success: true as const, | ||
| message: `Successfully moved ${result.movedCount} video${ | ||
| result.movedCount !== 1 ? "s" : "" | ||
| } to ${result.targetFolderId ? "folder" : "root"}`, | ||
| movedCount: result.movedCount, | ||
| originalFolderIds: result.originalFolderIds, | ||
| targetFolderId: result.targetFolderId, | ||
| videoCountDeltas: result.videoCountDeltas, | ||
| }; | ||
| } catch (error) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: returning |
||
| console.error("Error moving videos to folder:", error); | ||
| return { | ||
| success: false as const, | ||
| error: error instanceof Error ? error.message : "Failed to move videos", | ||
| }; | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is currently empty (0 bytes). If it’s accidental, I’d drop it — especially since it differs from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For
root.variant === "space", it feels worth enforcing membership server-side (similar tomoveVideosToFolderActionwithSpacesPolicy.isMember(...)). As-is, any authenticated user could pass an arbitraryspaceIdand enumerate folder names/counts if they’re in the same org.Also,
organizationIdin theorgvariant doesn’t get used (everything is scoped touser.activeOrganizationId), so either validate it matches or drop it to avoid a misleading API.