|
|
@ -240,6 +240,10 @@ posts.get(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
async (req: UserJwtRequest, res, next) => {
|
|
|
|
async (req: UserJwtRequest, res, next) => {
|
|
|
|
|
|
|
|
const isUserAuthor = (post: Post) => {
|
|
|
|
|
|
|
|
return req.user?.id && post.users?.map((user) => user.id).includes(req.user?.id)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const post = await Post.findByPk(req.params.id, {
|
|
|
|
const post = await Post.findByPk(req.params.id, {
|
|
|
|
include: [
|
|
|
|
include: [
|
|
|
@ -293,20 +297,30 @@ posts.get(
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else if (post.visibility === "private") {
|
|
|
|
} else if (post.visibility === "private") {
|
|
|
|
jwt(req as UserJwtRequest, res, () => {
|
|
|
|
jwt(req as UserJwtRequest, res, () => {
|
|
|
|
res.json(post)
|
|
|
|
if (isUserAuthor(post)) {
|
|
|
|
|
|
|
|
res.json(post)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
res.status(403).send()
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else if (post.visibility === "protected") {
|
|
|
|
} else if (post.visibility === "protected") {
|
|
|
|
const { password } = req.query
|
|
|
|
const { password } = req.query
|
|
|
|
if (!password || typeof password !== "string") {
|
|
|
|
if (!password || typeof password !== "string") {
|
|
|
|
return jwt(req as UserJwtRequest, res, () => {
|
|
|
|
return jwt(req as UserJwtRequest, res, () => {
|
|
|
|
res.json(post)
|
|
|
|
if (isUserAuthor(post)) {
|
|
|
|
|
|
|
|
res.json(post)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
res.status(403).send()
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const hash = crypto
|
|
|
|
const hash = crypto
|
|
|
|
.createHash("sha256")
|
|
|
|
.createHash("sha256")
|
|
|
|
.update(password)
|
|
|
|
.update(password)
|
|
|
|
.digest("hex")
|
|
|
|
.digest("hex")
|
|
|
|
.toString()
|
|
|
|
.toString()
|
|
|
|
|
|
|
|
|
|
|
|
if (hash !== post.password) {
|
|
|
|
if (hash !== post.password) {
|
|
|
|
return res.status(400).json({ error: "Incorrect password." })
|
|
|
|
return res.status(400).json({ error: "Incorrect password." })
|
|
|
|
}
|
|
|
|
}
|
|
|
|