Skip to content

Commit

Permalink
Fix visibility and add bson dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
thebkht committed Nov 26, 2023
1 parent 6bbbd1a commit de4df68
Show file tree
Hide file tree
Showing 50 changed files with 320 additions and 1,436 deletions.
14 changes: 7 additions & 7 deletions app/(Main)/[username]/[url]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ async function getPostData(username: string, url: string) {
url: url,
author: {
username: decodedUsername.substring(1)
}
},
published: true,
},
include: {
tags: {
Expand Down Expand Up @@ -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: [
{
Expand All @@ -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) {
Expand Down Expand Up @@ -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 } },
Expand Down Expand Up @@ -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 } },
Expand Down
6 changes: 5 additions & 1 deletion app/(Main)/[username]/[url]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 && (
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion app/(Main)/[username]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default async function Page({ params, searchParams }: {
where: {
OR: [
{
visibility: "public",
published: true,
},
{
authorId: sessionUserName?.id,
Expand Down
2 changes: 1 addition & 1 deletion app/(Main)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions app/(Main)/tags/[tagname]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions app/api/comments/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -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 } }) {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -50,18 +51,18 @@ 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({
where: {
commentId: {
in: await postgres.comment.findMany({
where: {
parentId: Number(id),
parentId: id,
},
select: {
id: true,
Expand All @@ -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,
},
})
}
2 changes: 2 additions & 0 deletions app/api/comments/create/route.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion app/api/comments/reply/route.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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,
Expand All @@ -37,7 +39,6 @@ export async function POST(req: NextRequest, res: NextResponse) {
author: {
select: {
username: true,

}
}
},
Expand Down
31 changes: 0 additions & 31 deletions app/api/feed/route.ts

This file was deleted.

2 changes: 2 additions & 0 deletions app/api/follow/route.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -33,6 +34,7 @@ export async function GET(request: NextRequest) {
} else {
await postgres.follow.create({
data: {
id: new ObjectId().toHexString(),
followerId: followerId,
followingId: followeeId,
},
Expand Down
4 changes: 3 additions & 1 deletion app/api/follow/tag/route.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -37,6 +38,7 @@ export async function GET(request: NextRequest){
} else {
await postgres.tagFollow.create({
data: {
id: new ObjectId().toHexString(),
tagId: tagid,
followerId: userid
}
Expand Down
10 changes: 6 additions & 4 deletions app/api/notifications/route.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -11,6 +12,7 @@ export async function POST(request: NextRequest) {

await postgres.notification.create({
data: {
id: new ObjectId().toHexString(),
content,
receiverId,
type,
Expand Down Expand Up @@ -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
Expand All @@ -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" })
Expand Down
4 changes: 2 additions & 2 deletions app/api/post/[postid]/drafts/route.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit de4df68

Please sign in to comment.