next/lib/server/middlewareButNotReally/storyCheck.ts
2023-10-02 15:44:55 -04:00

29 lines
832 B
TypeScript

import type { H3Event, EventHandlerRequest } from "h3";
import type { Document } from "mongoose";
import { isFicmasHidden } from "~/lib/functions";
import { IStory } from "~/models/stories";
export default async function (
event: H3Event<EventHandlerRequest>,
story: IStory,
) {
let ret: any = {};
let num: number = event.context.chapterIndex;
if (story.ficmas != null) {
if (isFicmasHidden(story)) {
ret = {
statusCode: 423,
message: `TOP SECRET! This story is part of an ongoing challenge. You'll be able to read it after the challenge's reveal date.`,
};
}
}
if (
story.chapters[num]?.hidden ||
(event.context.currentUser._id !== story.author._id &&
!event.context.currentUser.isAdmin)
) {
ret.statusCode = 403;
ret.message = "Forbidden";
}
return !!Object.keys(ret).length ? ret : null;
}