2023-10-03 00:21:14 -04:00
|
|
|
import san from "sanitize-html";
|
2023-12-20 17:23:31 -05:00
|
|
|
import { messages } from "@server/constants";
|
2023-12-29 16:32:32 -05:00
|
|
|
import { isLoggedIn } from "@server/middlewareButNotReally";
|
2023-12-20 17:23:31 -05:00
|
|
|
import { Story } from "@models/stories";
|
|
|
|
import { Review } from "@models/stories/review";
|
2023-12-29 19:06:55 -05:00
|
|
|
import { IUser } from "@models/user";
|
2023-10-03 00:21:14 -04:00
|
|
|
|
|
|
|
export default eventHandler(async (ev) => {
|
|
|
|
isLoggedIn(ev);
|
|
|
|
const revid = parseInt(getRouterParam(ev, "revid")!);
|
|
|
|
let replyingTo = await Review.findOne({
|
|
|
|
_id: revid,
|
|
|
|
})
|
|
|
|
.populate("author", "username _id blocked")
|
|
|
|
.exec();
|
|
|
|
if (!replyingTo) {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 404,
|
|
|
|
message: messages[404],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (
|
2023-12-29 20:53:29 -05:00
|
|
|
(replyingTo?.author as IUser).blocked.includes(ev.context.currentUser!._id) ||
|
2023-12-29 19:06:55 -05:00
|
|
|
ev.context.currentUser!.blocked.includes((replyingTo?.author as IUser)._id)
|
2023-10-03 00:21:14 -04:00
|
|
|
) {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 403,
|
|
|
|
message: "That didn't work",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const body = await readBody(ev);
|
|
|
|
const newReply = new Review({
|
|
|
|
author: ev.context.currentUser!._id,
|
|
|
|
replyingTo: revid,
|
|
|
|
text: san(body.content),
|
|
|
|
leftOn: replyingTo?.leftOn,
|
|
|
|
whichChapter: replyingTo.whichChapter,
|
|
|
|
datePosted: new Date(),
|
|
|
|
});
|
2023-12-09 17:39:28 -05:00
|
|
|
const { _id } = await newReply.save();
|
2023-12-29 20:53:29 -05:00
|
|
|
const nrs = (await Review.findOne({ _id }).populate("author", "username _id blocked").exec())!;
|
2023-10-03 00:21:14 -04:00
|
|
|
replyingTo.replies.push(nrs._id);
|
|
|
|
await replyingTo.save();
|
|
|
|
const story = await Story.findById(replyingTo.leftOn);
|
2023-12-09 17:39:28 -05:00
|
|
|
if (!story) {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 404,
|
|
|
|
});
|
|
|
|
}
|
2023-10-03 00:21:14 -04:00
|
|
|
return {
|
2023-12-29 20:53:29 -05:00
|
|
|
back: `/story/${replyingTo.leftOn}/${story!.chapters.findIndex((x) => x.id === nrs.whichChapter) + 1}`,
|
2023-10-03 00:21:14 -04:00
|
|
|
data: nrs.toObject(),
|
|
|
|
success: true,
|
|
|
|
};
|
|
|
|
});
|