|
|
|
@ -1,6 +1,6 @@
|
|
|
|
import { withMethods } from "@lib/api-middleware/with-methods"
|
|
|
|
import { withMethods } from "@lib/api-middleware/with-methods"
|
|
|
|
import { parseQueryParam } from "@lib/server/parse-query-param"
|
|
|
|
import { parseQueryParam } from "@lib/server/parse-query-param"
|
|
|
|
import { PostWithFiles, searchPosts } from "@lib/server/prisma"
|
|
|
|
import { searchPosts, ServerPostWithFiles } from "@lib/server/prisma"
|
|
|
|
import { NextApiRequest, NextApiResponse } from "next"
|
|
|
|
import { NextApiRequest, NextApiResponse } from "next"
|
|
|
|
import { getSession } from "next-auth/react"
|
|
|
|
import { getSession } from "next-auth/react"
|
|
|
|
|
|
|
|
|
|
|
|
@ -10,21 +10,21 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|
|
|
const session = await getSession()
|
|
|
|
const session = await getSession()
|
|
|
|
|
|
|
|
|
|
|
|
const query = parseQueryParam(q)
|
|
|
|
const query = parseQueryParam(q)
|
|
|
|
|
|
|
|
const user = parseQueryParam(userId)
|
|
|
|
if (!query) {
|
|
|
|
if (!query) {
|
|
|
|
res.status(400).json({ error: "Invalid query" })
|
|
|
|
res.status(400).json({ error: "Invalid query" })
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
let posts: PostWithFiles[]
|
|
|
|
let posts: ServerPostWithFiles[]
|
|
|
|
if (session?.user.id === userId || session?.user.role === "admin") {
|
|
|
|
if (session?.user.id === user || session?.user.role === "admin") {
|
|
|
|
posts = await searchPosts(query, {
|
|
|
|
posts = await searchPosts(query, {
|
|
|
|
userId: parseQueryParam(userId),
|
|
|
|
userId: user,
|
|
|
|
publicOnly: true
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
posts = await searchPosts(query, {
|
|
|
|
posts = await searchPosts(query, {
|
|
|
|
userId: parseQueryParam(userId),
|
|
|
|
userId: user,
|
|
|
|
publicOnly: true
|
|
|
|
publicOnly: true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|