diff --git a/app/(Main)/[username]/[url]/layout.tsx b/app/(Main)/[username]/[url]/layout.tsx index 1b355d2e..b45216b1 100644 --- a/app/(Main)/[username]/[url]/layout.tsx +++ b/app/(Main)/[username]/[url]/layout.tsx @@ -20,7 +20,8 @@ async function getPostData(username: string, url: string) { url: url, author: { username: decodedUsername.substring(1) - } + }, + published: true, }, include: { tags: { @@ -50,11 +51,11 @@ async function getPostData(username: string, url: string) { return { metadataBase: new URL(`${process.env.DOMAIN}/@${post.author.username}/${post.url}`), title: `${post.title} - FalseNotes`, - description: post.subtitle || markdownToText(post.content.slice(0, 100)), + description: post.subtitle || markdownToText(post.content?.slice(0, 100) || ''), keywords: post.tags.map((tag: any) => tag.tag.name).join(', '), openGraph: { title: `${post.title} - FalseNotes`, - description: post.subtitle || markdownToText(post.content.slice(0, 100)), + description: post.subtitle || markdownToText(post.content?.slice(0, 100) || ''), url: new URL(`${process.env.DOMAIN}/@${post.author.username}/${post.url}`), images: [ { @@ -68,8 +69,7 @@ async function getPostData(username: string, url: string) { twitter: { card: 'summary_large_image', title: `${post.title} - FalseNotes`, - description: post.subtitle || markdownToText(post.content.slice(0, 100)), - + description: post.subtitle || markdownToText(post.content?.slice(0, 100) || ''), }, } } catch (error) { @@ -105,7 +105,7 @@ export default async function PostLayout( url: { not: params.url }, - visibility: "public", + published: true, }, include: { _count: { select: { comments: true, savedUsers: true, likes: true } }, @@ -179,7 +179,7 @@ export default async function PostLayout( url: { not: post?.url }, - visibility: "public", + published: true, }, include: { _count: { select: { comments: true, savedUsers: true, likes: true } }, diff --git a/app/(Main)/[username]/[url]/page.tsx b/app/(Main)/[username]/[url]/page.tsx index 20fc7fed..dd5c0f59 100644 --- a/app/(Main)/[username]/[url]/page.tsx +++ b/app/(Main)/[username]/[url]/page.tsx @@ -4,6 +4,9 @@ import { notFound } from "next/navigation" import postgres from "@/lib/postgres" import Post from "@/components/blog/post" import { cookies } from 'next/headers' +import { ObjectId } from 'bson'; + +const id = new ObjectId().toHexString(); export default async function PostView({ params, searchParams }: { params: { username: string, url: string }, searchParams: { [key: string]: string | string[] | undefined } }) { const commentsOpen = typeof searchParams.commentsOpen === 'string' ? searchParams.commentsOpen : undefined @@ -67,7 +70,7 @@ export default async function PostView({ params, searchParams }: { params: { use const sessionUser = await getSessionUser() if (post?.authorId !== sessionUser?.id) { - if (post?.visibility !== "public") return notFound(); + if (post?.published) return notFound(); } const published = sessionUser?.id === post?.authorId && ( @@ -93,6 +96,7 @@ export default async function PostView({ params, searchParams }: { params: { use if (!hasReaded) { await postgres.readingHistory.create({ data: { + id, postId: post?.id, userId: sessionUser?.id } diff --git a/app/(Main)/[username]/page.tsx b/app/(Main)/[username]/page.tsx index a55d0a0e..adf8bc6f 100644 --- a/app/(Main)/[username]/page.tsx +++ b/app/(Main)/[username]/page.tsx @@ -52,7 +52,7 @@ export default async function Page({ params, searchParams }: { where: { OR: [ { - visibility: "public", + published: true, }, { authorId: sessionUserName?.id, diff --git a/app/(Main)/page.tsx b/app/(Main)/page.tsx index cd19a5d2..33741e33 100644 --- a/app/(Main)/page.tsx +++ b/app/(Main)/page.tsx @@ -22,7 +22,7 @@ export default async function Home() { const [latestPosts, tags, popularPosts] = await Promise.all([ postgres.post.findMany({ where: { - visibility: 'public', + published: true, }, orderBy: { createdAt: 'desc', diff --git a/app/(Main)/tags/[tagname]/page.tsx b/app/(Main)/tags/[tagname]/page.tsx index f33fdda2..c7aed88f 100644 --- a/app/(Main)/tags/[tagname]/page.tsx +++ b/app/(Main)/tags/[tagname]/page.tsx @@ -22,7 +22,7 @@ export default async function TagPage({ params }: { params: { tagname: string } const popularPosts = await postgres.post.findMany({ where: { - visibility: 'public', + published: true, tags: { some: { tagId: tag?.id @@ -51,7 +51,7 @@ export default async function TagPage({ params }: { params: { tagname: string } }); const latestPosts = await postgres.post.findMany({ where: { - visibility: 'public', + published: true, tags: { some: { tagId: tag?.id diff --git a/app/api/comments/[id]/route.ts b/app/api/comments/[id]/route.ts index 752e64f8..48db510d 100644 --- a/app/api/comments/[id]/route.ts +++ b/app/api/comments/[id]/route.ts @@ -1,5 +1,6 @@ import { create } from "@/lib/notifications/create-notification"; import postgres from "@/lib/postgres"; +import { Comment } from "@prisma/client"; import { NextRequest, NextResponse } from "next/server"; export async function POST(req: NextRequest, { params }: { params: { id: string } }) { @@ -13,7 +14,7 @@ export async function POST(req: NextRequest, { params }: { params: { id: string await postgres.comment.update({ where: { - id: Number(params.id), + id: params.id, }, data: { content: content, @@ -32,7 +33,7 @@ export async function DELETE(req: NextRequest, { params }: { params: { id: strin try { const replies = await postgres.comment.findMany({ where: { - parentId: Number(params.id), + parentId: params.id, }, select: { id: true, @@ -41,7 +42,7 @@ export async function DELETE(req: NextRequest, { params }: { params: { id: strin replies.forEach((reply) => deleteComment(reply)); - await deleteComment(Number(params.id)); + await deleteComment(params.id); return new NextResponse("Comment deleted", { status: 200 }); } catch (error) { @@ -50,10 +51,10 @@ export async function DELETE(req: NextRequest, { params }: { params: { id: strin } } -async function deleteComment(id: number) { +async function deleteComment(id: Comment["id"]) { await postgres.commentLike.deleteMany({ where: { - commentId: Number(id), + commentId: id, }, }) await postgres.commentLike.deleteMany({ @@ -61,7 +62,7 @@ async function deleteComment(id: number) { commentId: { in: await postgres.comment.findMany({ where: { - parentId: Number(id), + parentId: id, }, select: { id: true, @@ -72,14 +73,14 @@ async function deleteComment(id: number) { }) await postgres.comment.deleteMany({ where: { - parentId: Number(id), + parentId: id, }, }) await postgres.comment.delete({ where: { - id: Number(id), + id: id, }, }) } \ No newline at end of file diff --git a/app/api/comments/create/route.ts b/app/api/comments/create/route.ts index 92c008e8..acd936de 100644 --- a/app/api/comments/create/route.ts +++ b/app/api/comments/create/route.ts @@ -1,5 +1,6 @@ import { create } from "@/lib/notifications/create-notification"; import postgres from "@/lib/postgres"; +import { ObjectId } from "bson"; import { NextRequest, NextResponse } from "next/server"; export async function POST(req: NextRequest, res: NextResponse) { @@ -18,6 +19,7 @@ export async function POST(req: NextRequest, res: NextResponse) { // `; await postgres.comment.create({ data: { + id: new ObjectId().toHexString(), content: content, authorId: author, postId: post, diff --git a/app/api/comments/reply/route.ts b/app/api/comments/reply/route.ts index ec8c9987..1148052d 100644 --- a/app/api/comments/reply/route.ts +++ b/app/api/comments/reply/route.ts @@ -1,5 +1,6 @@ import { create } from "@/lib/notifications/create-notification"; import postgres from "@/lib/postgres"; +import { ObjectId } from "bson"; import { NextRequest, NextResponse } from "next/server"; export async function POST(req: NextRequest, res: NextResponse) { @@ -13,6 +14,7 @@ export async function POST(req: NextRequest, res: NextResponse) { await postgres.comment.create({ data: { + id: new ObjectId().toHexString(), content: content, authorId: author, postId: post, @@ -37,7 +39,6 @@ export async function POST(req: NextRequest, res: NextResponse) { author: { select: { username: true, - } } }, diff --git a/app/api/feed/route.ts b/app/api/feed/route.ts deleted file mode 100644 index f6b84302..00000000 --- a/app/api/feed/route.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { config } from '@/app/auth' -import postgres from '@/lib/postgres' -import { getFeed } from '@/lib/prisma/feed'; -import { getServerSession } from 'next-auth' -import { NextRequest, NextResponse } from 'next/server' - -export async function GET(req: NextRequest) { - const pageString = req.nextUrl.searchParams.get('page') || 0 - const page = Number(pageString) - const tag = req.nextUrl.searchParams.get('tag') as string | undefined - const session = await getServerSession(config) - if (!session) { - return NextResponse.json({ error: 'No user found' }, { status: 500 }) - } - const user = session?.user - const res = await postgres.user.findFirst({ - where: { image: user?.image }, - select: { id: true }, - }) - const id = res?.id - if (!id) { - return NextResponse.json({ error: 'No user found' }, { status: 500 }) - } - - const feed = await getFeed({ page, tab: tag, limit: 10 }); - if (feed?.error) { - return NextResponse.json({ error: feed.error }, { status: 500 }); - } else if (feed?.feed) { - return NextResponse.json({ feed: feed.feed }, { status: 200 }); - } -} \ No newline at end of file diff --git a/app/api/follow/route.ts b/app/api/follow/route.ts index 4db18016..ff1def35 100644 --- a/app/api/follow/route.ts +++ b/app/api/follow/route.ts @@ -1,6 +1,7 @@ import { NextRequest, NextResponse } from "next/server"; import postgres from "@/lib/postgres"; import { create } from "@/lib/notifications/create-notification"; +import { ObjectId } from "bson"; export async function GET(request: NextRequest) { try { @@ -33,6 +34,7 @@ export async function GET(request: NextRequest) { } else { await postgres.follow.create({ data: { + id: new ObjectId().toHexString(), followerId: followerId, followingId: followeeId, }, diff --git a/app/api/follow/tag/route.ts b/app/api/follow/tag/route.ts index 86c9f54f..6ec90cc1 100644 --- a/app/api/follow/tag/route.ts +++ b/app/api/follow/tag/route.ts @@ -1,8 +1,9 @@ import postgres from "@/lib/postgres" +import { ObjectId } from "bson"; import { NextRequest, NextResponse } from "next/server"; export async function GET(request: NextRequest){ - const tagid = Number(request.nextUrl.searchParams.get("tagId")) + const tagid = request.nextUrl.searchParams.get("tagId") const userid = request.nextUrl.searchParams.get("userId") try { @@ -37,6 +38,7 @@ export async function GET(request: NextRequest){ } else { await postgres.tagFollow.create({ data: { + id: new ObjectId().toHexString(), tagId: tagid, followerId: userid } diff --git a/app/api/notifications/route.ts b/app/api/notifications/route.ts index bed90bca..22047453 100644 --- a/app/api/notifications/route.ts +++ b/app/api/notifications/route.ts @@ -1,4 +1,5 @@ import postgres from "@/lib/postgres" +import { ObjectId } from "bson" import { NextRequest, NextResponse } from "next/server" export async function POST(request: NextRequest) { @@ -11,6 +12,7 @@ export async function POST(request: NextRequest) { await postgres.notification.create({ data: { + id: new ObjectId().toHexString(), content, receiverId, type, @@ -58,14 +60,14 @@ export async function GET(request: NextRequest) { // PUT /api/notifications?id export async function PUT(request: NextRequest) { - const id = await request.nextUrl.searchParams.get("id") + const id = await request.nextUrl.searchParams.get("id")?.toString() try { // await sql` // UPDATE notifications SET readat = ${new Date().toISOString()} WHERE id = ${id} // ` await postgres.notification.update({ where: { - id: Number(id) + id: id }, data: { read: true @@ -80,11 +82,11 @@ export async function PUT(request: NextRequest) { // DELETE /api/notifications?id export async function DELETE(request: NextRequest) { - const id = await request.nextUrl.searchParams.get("id") + const id = await request.nextUrl.searchParams.get("id")?.toString() try { await postgres.notification.delete({ where: { - id: Number(id) + id: id } }) return NextResponse.json({ status: 200, message: "Notification deleted" }) diff --git a/app/api/post/[postid]/drafts/route.ts b/app/api/post/[postid]/drafts/route.ts index 59fdc923..2cc8a9af 100644 --- a/app/api/post/[postid]/drafts/route.ts +++ b/app/api/post/[postid]/drafts/route.ts @@ -1,6 +1,7 @@ import { getSessionUser } from "@/components/get-session-user"; import { insertTag } from "@/lib/insert-tag"; import postgres from "@/lib/postgres"; +import { ObjectId } from "bson"; import { NextRequest } from "next/server"; import readingTime from "reading-time"; import { z } from "zod"; @@ -74,17 +75,16 @@ export async function PATCH( title, content, cover: coverImage || null, - readingTime: readTime, subtitle: subtitle || null, }, }); } else { await postgres.draftPost.create({ data: { + id: new ObjectId().toHexString(), title, content, cover: coverImage || null, - readingTime: readTime, url, subtitle: subtitle || null, postId: postid, diff --git a/app/api/post/[postid]/route.ts b/app/api/post/[postid]/route.ts index 7a7f4cff..b11622b3 100644 --- a/app/api/post/[postid]/route.ts +++ b/app/api/post/[postid]/route.ts @@ -24,7 +24,7 @@ export async function PATCH( title, content, coverImage, - visibility, + published, tags, url, authorId, @@ -45,9 +45,9 @@ export async function PATCH( id: postid, }, select: { - visibility: true, + published: true, }, - }) + }); await postgres.post.update({ where: { @@ -57,14 +57,14 @@ export async function PATCH( title: title, content: content, cover: coverImage || null, - visibility: visibility, + published: published, url: url, subtitle: subtitle || null, readingTime: readTime, - ...(oldData?.visibility === "draft" && - visibility === "public" && { createdAt: new Date() }), - ...(oldData?.visibility === "public" && - visibility === "public" && { updatedAt: new Date(), updated: true }), + ...(oldData?.published === false && + published === true && { publishedAt: new Date() }), + ...(oldData?.published === true && + published === true && { updatedAt: new Date(), updated: true }), }, }); diff --git a/app/api/posts/route.ts b/app/api/posts/route.ts index d721359c..8bb7cfac 100644 --- a/app/api/posts/route.ts +++ b/app/api/posts/route.ts @@ -4,6 +4,9 @@ import postgres from "@/lib/postgres"; import { getServerSession } from "next-auth"; import { NextRequest, NextResponse } from "next/server"; import { z } from "zod"; +import { ObjectId } from 'bson'; + +const id = new ObjectId().toHexString(); const baseQuery = { include: { @@ -54,9 +57,9 @@ export async function GET(req: NextRequest) { contains: search, mode: "insensitive", }, - visibility: "public", + published: true, } - : { visibility: "public" }, + : { published: true }, take: limit, skip: page * limit, }); @@ -93,11 +96,11 @@ export async function POST(req: Request) { const post = await postgres.post.create({ data: { + id: id, title: json.title, content: json.content, authorId: session.id, url: json.url, - visibility: json.visibility, }, select: { id: true, diff --git a/app/api/posts/submit/route.ts b/app/api/posts/submit/route.ts deleted file mode 100644 index d94bbeff..00000000 --- a/app/api/posts/submit/route.ts +++ /dev/null @@ -1,156 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; -import postgres from "@/lib/postgres" -import readingTime from "reading-time"; - -async function insertTag(tags: any, postid: any) { - if (tags) { - for (const tag of tags) { - // Check if tag exists - const tagExists = await postgres.tag.findFirst({ - where: { - name: tag.value.replace(/\s+/g, '-').toLowerCase(), - }, - }) - if (!tagExists) { - await postgres.tag.create({ - data: { - name: tag.value.replace(/\s+/g, '-').toLowerCase(), - } - }) - } - const tagId = await postgres.tag.findFirst({ - where: { - name: tag.value.replace(/\s+/g, '-').toLowerCase(), - }, - select: { - id: true, - } - }) - if (tagId) { - await postgres.postTag.create({ - data: { - tagId: tagId.id, - postId: postid, - } - }) - } - } - } -} - -export async function POST(req: NextRequest) { - try { - const data = await req.json(); - if (!data) { - return new Response("No data provided", { status: 400 }); - } - const postid = req.nextUrl.searchParams.get("postId"); - - const { title, content, coverImage, visibility, tags, url, authorId, subtitle } = data; - const stats = readingTime(content); - const readTime = stats.text; - - if (!title) { - return new Response("No title provided", { status: 400 }); - } - if (!content) { - return new Response("No content provided", { status: 400 }); - } - - if (!authorId) { - return new Response("No author provided", { status: 400 }); - } - - if (!url) { - return new Response("No url provided", { status: 400 }); - } - - if (postid) { - const postData = await postgres.post.findFirst({ - where: { - id: postid, - }, - }) - if (!postData) { - return new Response("Post does not exist", { status: 400 }); - } - - if (title !== postData.title || content !== postData.content || coverImage !== postData.cover || visibility !== postData.visibility || url !== postData.url || subtitle !== postData.subtitle, readTime !== postData.readingTime) { - - await postgres.post.update({ - where: { - id: postid, - }, - data: { - title: title, - content: content, - cover: coverImage, - visibility: visibility, - url: url, - subtitle: subtitle, - readingTime: readTime, - updated: true, - } - }) - - } - const postTagsData = await postgres.postTag.findMany({ - where: { - postId: postid, - }, - select: { - tag: { - select: { - name: true, - } - } - } - }) - if (postTagsData) { - await postgres.postTag.deleteMany({ - where: { - postId: postid, - } - }) - } - await insertTag(tags, postid); - - - return NextResponse.json({ body: "Post updated" }); - } - else { - await postgres.post.create({ - data: { - title: title, - content: content, - cover: coverImage ? coverImage : null, - visibility: visibility, - url: url, - subtitle: subtitle ? subtitle : null, - authorId: authorId, - readingTime: readTime, - } - }) - - const submittedPostId = await postgres.post.findFirst({ - where: { - url: url, - }, - select: { - id: true, - } - }) - - const postId = submittedPostId?.id; - - await insertTag(tags, postId); - - return NextResponse.json({ body: "Post submitted" }); - } - } catch (error) { - console.error("Error:", error); - return NextResponse.json({body: "Error processing data"}, - {status: 500}); - } - } - \ No newline at end of file diff --git a/app/api/user/[id]/posts/route.ts b/app/api/user/[id]/posts/route.ts index d076b464..ac1997c2 100644 --- a/app/api/user/[id]/posts/route.ts +++ b/app/api/user/[id]/posts/route.ts @@ -33,9 +33,9 @@ export async function GET(req: NextRequest, { params } : { params: { id: string const posts = await postgres.post.findMany({ ...baseQuery, where: { - ...(session?.id === userId ? {} : { visibility: "public" }), + ...(session?.id === userId ? {} : { published: true }), authorId: userId, - visibility: session?.id === userId ? undefined : "public", + published: session?.id === userId ? undefined : true, ...(search ? { title: { contains: search, mode: "insensitive" } } : {}), }, take: Number(limit), diff --git a/app/auth.ts b/app/auth.ts index dd235ad1..1d5e7f97 100644 --- a/app/auth.ts +++ b/app/auth.ts @@ -2,6 +2,9 @@ import postgres from "@/lib/postgres" import type { NextAuthOptions as NextAuthConfig } from "next-auth" import GitHub from "next-auth/providers/github" import { PrismaAdapter } from "@next-auth/prisma-adapter" +import { ObjectId } from 'bson'; + +const id = new ObjectId().toHexString(); export const config = { // https://next-auth.js.org/configuration/providers/oauth @@ -42,6 +45,7 @@ export const config = { try { const sessionUser = await postgres.user.create({ data: { + id: id, username: username, name: name, email: email, @@ -64,6 +68,7 @@ export const config = { if (!userSettingsExists) { await postgres.userSettings.create({ data: { + id: new ObjectId().toHexString(), userId: sessionUser.id } }) diff --git a/app/editor/[id]/page.tsx b/app/editor/[id]/page.tsx index 708664aa..4853df11 100644 --- a/app/editor/[id]/page.tsx +++ b/app/editor/[id]/page.tsx @@ -34,7 +34,6 @@ async function getPostForUser(postId: Post['id']) { post.title = draft.title post.subtitle = draft.subtitle post.cover = draft.cover - post.readingTime = draft.readingTime } return post diff --git a/components/blog/post.tsx b/components/blog/post.tsx index e0db8513..dec8178e 100644 --- a/components/blog/post.tsx +++ b/components/blog/post.tsx @@ -134,7 +134,7 @@ export default function SinglePost({ post: initialPost, author, sessionUser, tag
{post.readingTime}
-{post.readingTime}
+