Skip to content

Commit

Permalink
Fix post deletion and user history retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
thebkht committed Nov 27, 2023
1 parent 6e341f7 commit 1823bc3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
37 changes: 29 additions & 8 deletions app/api/post/[postid]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,50 +92,71 @@ export async function DELETE(
) {
try {
const { postid } = params;

const post = await postgres.post.findUnique({
where: {
id: postid,
},
});

if (!post) {
throw new Error('Post not found');
}

if (!(await verifyCurrentUserHasAccessToPost(postid))) {
const authorId = post.authorId;

if (!(await verifyCurrentUserHasAccessToPost(post.id))) {
return new Response(null, { status: 403 });
}



// Disconnect all connections of the post
await postgres.postTag.deleteMany({
where: {
postId: postid,
postId: post.id,
},
})

await postgres.comment.deleteMany({
where: {
postId: postid,
postId: post.id,
},
})

await postgres.like.deleteMany({
where: {
postId: postid,
postId: post.id,
},
})

await postgres.bookmark.deleteMany({
where: {
postId: postid,
postId: post.id,
},
})

await postgres.readingHistory.deleteMany({
where: {
postId: postid,
userId: authorId,
},
});

await postgres.readingHistory.deleteMany({
where: {
postId: post.id,
},
})

await postgres.draftPost.deleteMany({
where: {
postId: postid,
postId: post.id,
},
})
// Delete the post.
await postgres.post.delete({
where: {
id: postid,
id: post.id,
},
})

Expand Down
2 changes: 1 addition & 1 deletion components/editor/post-editor-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ export function PostEditorForm(props: { post: any, user: any }) {
<Link href={`/@${props.user?.username}`} className="flex align-items-center">
<Avatar className="h-8 w-8 mr-1 border">
<AvatarImage src={ props.user?.image } alt={ props.user?.name || props.user?.username } />
<AvatarFallback>{props.user?.name.charAt(0)}</AvatarFallback>
<AvatarFallback>{props.user?.name ? props.user?.name.charAt(0) : props.user?.username.charAt(0)}</AvatarFallback>

</Avatar>
<Button variant="ghost" size={"sm"} className="hidden md:flex" asChild>
Expand Down
2 changes: 1 addition & 1 deletion components/tags/post-card-v2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default function PostCard(
)
}
<span className="!text-muted-foreground text-sm">
{dateFormat(props.post.publishedAt)}
{props.post.published ? dateFormat(props.post.publishedAt) : dateFormat(props.post.createdAt)}
</span>
</div>
</CardHeader>
Expand Down
2 changes: 1 addition & 1 deletion lib/prisma/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const tagCounts = tags.reduce((counts, tag) => {
// Sort the tags by their count in descending order
const sortedTagIds = Object.entries(tagCounts)
.sort((a, b) => b[1] - a[1])
.map(([tagId]) => Number(tagId).toString()); // Convert numbers to strings
.map(([tagId]) => tagId)

const posts = await postgres.post.findMany({
where: { tags: { some: { tagId: { in: sortedTagIds.slice(0, 5) } } } },
Expand Down
14 changes: 10 additions & 4 deletions lib/prisma/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ export const getBookmarks = async ({ id, limit = 5, page = 0 }: { id: string | u
export const getHistory = async ({ id, limit = 5, page = 0 }: { id: string | undefined, limit?: number, page?: number }) => {
const user = await postgres.user.findFirst({
where: { id },
include: {
select: {
id: true,
readinghistory: {
include: {
post: {
Expand All @@ -186,10 +187,15 @@ export const getHistory = async ({ id, limit = 5, page = 0 }: { id: string | und
},
_count: { select: { readinghistory: true } },
},
}
);
});

if (!user) {
throw new Error('User not found');
}

const history = user.readinghistory.filter((historyItem : any) => historyItem.post !== null);

return { history: JSON.parse(JSON.stringify(user?.readinghistory)), historyCount: user?._count?.readinghistory };
return { history: JSON.parse(JSON.stringify(history)), historyCount: user._count.readinghistory };
}

export const getNotifications = async ({ id }: { id: string | undefined }) => {
Expand Down

0 comments on commit 1823bc3

Please sign in to comment.