2022-11-12 03:58:21 -05:00
|
|
|
import PostPage from "app/(posts)/post/[id]/components/post-page"
|
2022-11-18 02:39:52 -05:00
|
|
|
import { notFound, redirect } from "next/navigation"
|
2022-11-14 20:24:35 -05:00
|
|
|
import { getPostById, Post } from "@lib/server/prisma"
|
2022-11-14 02:02:31 -05:00
|
|
|
import { getCurrentUser } from "@lib/server/session"
|
2022-11-12 02:59:33 -05:00
|
|
|
|
|
|
|
export type PostProps = {
|
|
|
|
post: Post
|
|
|
|
isProtected?: boolean
|
|
|
|
}
|
|
|
|
|
2022-11-14 02:02:31 -05:00
|
|
|
// export async function generateStaticParams() {
|
|
|
|
// const posts = await getAllPosts({
|
|
|
|
// where: {
|
|
|
|
// visibility: "public"
|
|
|
|
// }
|
|
|
|
// })
|
2022-11-12 21:39:03 -05:00
|
|
|
|
2022-11-14 02:02:31 -05:00
|
|
|
// return posts.map((post) => ({
|
|
|
|
// id: post.id
|
|
|
|
// }))
|
|
|
|
// }
|
2022-11-12 21:39:03 -05:00
|
|
|
|
2022-11-12 02:59:33 -05:00
|
|
|
const getPost = async (id: string) => {
|
2022-11-14 04:28:40 -05:00
|
|
|
const post = await getPostById(id, {
|
|
|
|
withFiles: true,
|
2022-11-14 20:24:35 -05:00
|
|
|
withAuthor: true
|
2022-11-14 04:28:40 -05:00
|
|
|
})
|
2022-11-12 02:59:33 -05:00
|
|
|
const user = await getCurrentUser()
|
2022-11-12 21:39:03 -05:00
|
|
|
|
2022-11-12 02:59:33 -05:00
|
|
|
if (!post) {
|
|
|
|
return notFound()
|
|
|
|
}
|
|
|
|
|
2022-11-14 02:28:51 -05:00
|
|
|
const isAuthorOrAdmin = user?.id === post?.authorId || user?.role === "admin"
|
2022-11-12 02:59:33 -05:00
|
|
|
|
|
|
|
if (post.visibility === "public") {
|
2022-11-14 02:28:51 -05:00
|
|
|
return { post, isAuthor: isAuthorOrAdmin }
|
2022-11-12 02:59:33 -05:00
|
|
|
}
|
|
|
|
|
2022-11-18 01:36:53 -05:00
|
|
|
if (post.visibility === "private" && !isAuthorOrAdmin) {
|
2022-11-12 02:59:33 -05:00
|
|
|
return notFound()
|
|
|
|
}
|
|
|
|
|
2022-11-14 02:28:51 -05:00
|
|
|
if (post.visibility === "private" && !isAuthorOrAdmin) {
|
2022-11-12 02:59:33 -05:00
|
|
|
return notFound()
|
|
|
|
}
|
|
|
|
|
2022-11-14 02:28:51 -05:00
|
|
|
if (post.visibility === "protected" && !isAuthorOrAdmin) {
|
2022-11-12 02:59:33 -05:00
|
|
|
return {
|
2022-11-14 04:28:40 -05:00
|
|
|
// post,
|
2022-11-12 02:59:33 -05:00
|
|
|
isProtected: true,
|
2022-11-14 02:28:51 -05:00
|
|
|
isAuthor: isAuthorOrAdmin
|
2022-11-12 02:59:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 04:28:40 -05:00
|
|
|
// if expired
|
|
|
|
if (post.expiresAt && !isAuthorOrAdmin) {
|
|
|
|
const expirationDate = new Date(post.expiresAt)
|
|
|
|
if (expirationDate < new Date()) {
|
2022-11-18 02:39:52 -05:00
|
|
|
return redirect("/expired")
|
2022-11-14 04:28:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 02:28:51 -05:00
|
|
|
return { post, isAuthor: isAuthorOrAdmin }
|
2022-11-12 02:59:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const PostView = async ({
|
|
|
|
params
|
|
|
|
}: {
|
|
|
|
params: {
|
2022-11-12 04:28:06 -05:00
|
|
|
id: string
|
2022-11-12 02:59:33 -05:00
|
|
|
}
|
|
|
|
}) => {
|
2022-11-14 02:02:31 -05:00
|
|
|
const { post, isProtected, isAuthor } = await getPost(params.id)
|
|
|
|
// TODO: serialize dates in prisma middleware instead of passing as JSON
|
2022-11-14 02:28:51 -05:00
|
|
|
const stringifiedPost = JSON.stringify(post)
|
|
|
|
return (
|
|
|
|
<PostPage
|
|
|
|
isAuthor={isAuthor}
|
|
|
|
isProtected={isProtected}
|
|
|
|
post={stringifiedPost}
|
|
|
|
/>
|
|
|
|
)
|
2022-11-12 02:59:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default PostView
|