mirror of https://github.com/MaxLeiter/Drift
				
				
				
			post/[id]: move function to new file to avoid invalid-segment-export
							parent
							
								
									b848aa9e40
								
							
						
					
					
						commit
						604f5d64d0
					
				@ -0,0 +1,66 @@
 | 
			
		||||
import { getPostById } from "@lib/server/prisma"
 | 
			
		||||
import { getCurrentUser } from "@lib/server/session"
 | 
			
		||||
import { notFound, redirect } from "next/navigation"
 | 
			
		||||
import { cache } from "react"
 | 
			
		||||
 | 
			
		||||
export const getPost = cache(async (id: string) => {
 | 
			
		||||
	const post = await getPostById(id, {
 | 
			
		||||
		select: {
 | 
			
		||||
			visibility: true,
 | 
			
		||||
			authorId: true,
 | 
			
		||||
			title: true,
 | 
			
		||||
			description: true,
 | 
			
		||||
			id: true,
 | 
			
		||||
			createdAt: true,
 | 
			
		||||
			expiresAt: true,
 | 
			
		||||
			parentId: true,
 | 
			
		||||
			author: {
 | 
			
		||||
				select: {
 | 
			
		||||
					displayName: true,
 | 
			
		||||
					image: true
 | 
			
		||||
				}
 | 
			
		||||
			},
 | 
			
		||||
			files: {
 | 
			
		||||
				select: {
 | 
			
		||||
					id: true,
 | 
			
		||||
					content: true,
 | 
			
		||||
					updatedAt: true,
 | 
			
		||||
					title: true,
 | 
			
		||||
					html: true
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	if (!post) {
 | 
			
		||||
		return notFound()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (post.expiresAt && new Date(post.expiresAt) < new Date()) {
 | 
			
		||||
		return redirect("/expired")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (post.visibility === "public" || post.visibility === "unlisted") {
 | 
			
		||||
		return { post }
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (post.visibility === "private") {
 | 
			
		||||
		const user = await getCurrentUser()
 | 
			
		||||
		if (user?.id === post.authorId || user?.role === "admin") {
 | 
			
		||||
			return { post }
 | 
			
		||||
		}
 | 
			
		||||
		return redirect("/new")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (post.visibility === "protected") {
 | 
			
		||||
		return {
 | 
			
		||||
			post: {
 | 
			
		||||
				visibility: "protected",
 | 
			
		||||
				authorId: post.authorId,
 | 
			
		||||
				id: post.id
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return { post }
 | 
			
		||||
})
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue