import { Band } from "@models/band"; import { Challenge } from "@models/challenges/gen"; import { IStory, Story } from "@models/stories"; import { log } from "../logger"; import { H3Event, EventHandlerRequest, H3EventContext } from "h3"; export default async function ( query: any, context: H3EventContext, ev: H3Event, limit: number = 0, sort?: any, ): Promise<{ stories: IStory[]; total: number }> { const q = getQuery(ev); let skipAmt = limit * (parseInt((q.page as string) || "1") - 1); if (skipAmt < 0) skipAmt = 0; query["chapters.hidden"] = false; if (context.currentUser) { if (!query.author) query.author = {}; if (!query["chapters.bands"]) query["chapters.bands"] = {}; query["chapters.bands"]["$nin"] = context.currentUser.hiddenBands; query["author"]["$nin"] = context.currentUser.hiddenAuthors; } query["ficmas"] = { $nin: context.ficmasarray_raw!.map((a) => a._id), }; let stories = await Story.find(query, null) .collation({ locale: "en" }) .sort(sort ? sort : { "chapters.posted": -1 }) .populate({ path: "ficmas", populate: { path: "wisher", model: "User", select: "username _id" }, }) .populate("coAuthor", "username _id profile") .populate("chapters.bands") .populate({ path: "challenge", model: Challenge }) .populate("author", "username _id profile") .exec(); let oro = stories.filter((a) => a.author != null); log.debug(JSON.stringify(query), { label: "list query" }); return { total: oro.length, stories: oro.slice(skipAmt, limit == 0 ? oro.length : skipAmt + limit), }; }