2023-10-03 00:46:57 -04:00
|
|
|
import { H3Event, EventHandlerRequest } from "h3";
|
2023-12-29 20:11:07 -05:00
|
|
|
import { Document } from "mongoose";
|
2023-12-20 17:23:31 -05:00
|
|
|
import { norm, stringifyStream } from "@functions";
|
|
|
|
import { IStory } from "@models/stories";
|
|
|
|
import { IChapter } from "@models/stories/chapter";
|
2023-10-03 00:46:57 -04:00
|
|
|
import getBucket from "../storyHelpers/getBucket";
|
2023-12-20 17:23:31 -05:00
|
|
|
import { SingleChapterResult } from "@client/types/slightlyDifferentStory";
|
2023-10-03 00:46:57 -04:00
|
|
|
|
|
|
|
export default async function (
|
|
|
|
story: Document<number, {}, IStory> & IStory,
|
|
|
|
event: H3Event<EventHandlerRequest>,
|
2023-12-01 17:57:17 -05:00
|
|
|
cindex: number,
|
|
|
|
): Promise<SingleChapterResult> {
|
2023-10-03 00:46:57 -04:00
|
|
|
const finObj: any = story.toObject();
|
|
|
|
const cloned: any & { chapters: IChapter[] } = { ...finObj };
|
|
|
|
delete finObj.chapters;
|
|
|
|
const bucket = getBucket();
|
2023-12-29 20:11:07 -05:00
|
|
|
let ds = bucket.openDownloadStreamByName(`/stories/${cloned.chapters[cindex || event.context.chapterIndex || 0].id}.txt`);
|
2023-10-03 00:46:57 -04:00
|
|
|
let stream = await stringifyStream(ds);
|
|
|
|
finObj.currentChapter = {
|
2023-10-11 16:37:18 -04:00
|
|
|
...cloned.chapters[cindex || event.context.chapterIndex || 0],
|
2023-12-11 21:13:22 -05:00
|
|
|
text: norm(stream),
|
2023-10-03 00:46:57 -04:00
|
|
|
};
|
|
|
|
finObj.totalChapters = story.chapters.length;
|
2023-12-01 17:57:17 -05:00
|
|
|
finObj.chapterNames = story.chapters.map((a) => a.title);
|
2023-10-03 00:46:57 -04:00
|
|
|
return finObj;
|
|
|
|
}
|