| 1 | "use server"; |
| 2 | |
| 3 | import { auth } from "@/server/auth"; |
| 4 | import { db } from "@/server/db"; |
| 5 | |
| 6 | /** |
| 7 | * Get a public presentation without requiring authentication |
| 8 | * This is used for the shared presentation view |
| 9 | */ |
| 10 | export async function getSharedPresentation(id: string) { |
| 11 | try { |
| 12 | const presentation = await db.baseDocument.findUnique({ |
| 13 | where: { |
| 14 | id, |
| 15 | isPublic: true, // Only fetch public presentations |
| 16 | }, |
| 17 | include: { |
| 18 | presentation: { |
| 19 | select: { |
| 20 | id: true, |
| 21 | content: true, |
| 22 | theme: true, |
| 23 | outline: true, |
| 24 | presentationStyle: true, |
| 25 | language: true, |
| 26 | }, |
| 27 | }, |
| 28 | user: { |
| 29 | select: { |
| 30 | name: true, |
| 31 | image: true, |
| 32 | }, |
| 33 | }, |
| 34 | }, |
| 35 | }); |
| 36 | |
| 37 | if (!presentation) { |
| 38 | return { |
| 39 | success: false, |
| 40 | message: "Presentation not found or not public", |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | return { |
| 45 | success: true, |
| 46 | presentation, |
| 47 | }; |
| 48 | } catch (error) { |
| 49 | console.error("Error fetching shared presentation:", error); |
| 50 | return { |
| 51 | success: false, |
| 52 | message: "Failed to fetch presentation", |
| 53 | }; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Toggle the public status of a presentation |
| 59 | */ |
| 60 | export async function togglePresentationPublicStatus( |
| 61 | id: string, |
| 62 | isPublic: boolean, |
| 63 | ) { |
| 64 | const session = await auth(); |
| 65 | if (!session?.user) { |
| 66 | return { |
| 67 | success: false, |
| 68 | message: "Unauthorized", |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | try { |
| 73 | // This requires auth and ownership verification |
| 74 | const presentation = await db.baseDocument.update({ |
| 75 | where: { |
| 76 | id, |
| 77 | userId: session.user.id, // Only the owner can change the public status |
| 78 | }, |
| 79 | data: { isPublic }, |
| 80 | }); |
| 81 | |
| 82 | return { |
| 83 | success: true, |
| 84 | message: isPublic |
| 85 | ? "Presentation is now publicly accessible" |
| 86 | : "Presentation is now private", |
| 87 | presentation, |
| 88 | }; |
| 89 | } catch (error) { |
| 90 | console.error("Error updating presentation public status:", error); |
| 91 | return { |
| 92 | success: false, |
| 93 | message: "Failed to update presentation public status", |
| 94 | }; |
| 95 | } |
| 96 | } |
| 97 |