next/lib/server/middlewareButNotReally/storyCheck.ts

31 lines
888 B
TypeScript

import type { H3Event, EventHandlerRequest } from "h3";
import { isFicmasHidden } from "~/lib/functions";
import { IStory } from "~/models/stories";
import { messages } from "../constants";
export default async function (
event: H3Event<EventHandlerRequest>,
story: IStory,
idx: number,
) {
let ret: any = {};
if (!story) {
ret.statusCode = 404;
ret.message = messages[404];
} else 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.`,
};
}
} else if (
story.chapters[idx]?.hidden &&
event.context.currentUser?._id !== story.author._id &&
!event.context.currentUser?.profile.isAdmin
) {
ret.statusCode = 403;
ret.message = messages[403];
}
return !!Object.keys(ret).length ? ret : null;
}