2023-12-20 17:23:31 -05:00
|
|
|
import { Band } from "@models/band";
|
|
|
|
import { Challenge } from "@models/challenges/gen";
|
|
|
|
import { IStory, Story } from "@models/stories";
|
2023-10-10 22:25:25 -04:00
|
|
|
import { log } from "../logger";
|
2023-12-20 17:23:31 -05:00
|
|
|
import { H3Event, EventHandlerRequest, H3EventContext } from "h3";
|
2023-10-03 00:30:22 -04:00
|
|
|
|
2023-12-06 21:31:50 -05:00
|
|
|
export default async function (
|
2023-12-20 17:23:31 -05:00
|
|
|
query: any,
|
|
|
|
context: H3EventContext,
|
2023-12-06 21:31:50 -05:00
|
|
|
ev: H3Event<EventHandlerRequest>,
|
|
|
|
limit: number = 0,
|
2023-12-20 17:23:31 -05:00
|
|
|
sort?: any,
|
2023-12-06 21:31:50 -05:00
|
|
|
): Promise<{ stories: IStory[]; total: number }> {
|
|
|
|
const q = getQuery(ev);
|
2023-12-20 17:23:31 -05:00
|
|
|
let skipAmt = limit * (parseInt((q.page as string) || "1") - 1);
|
2023-12-06 21:31:50 -05:00
|
|
|
if (skipAmt < 0) skipAmt = 0;
|
2023-10-03 00:30:22 -04:00
|
|
|
query["chapters.hidden"] = false;
|
|
|
|
if (context.currentUser) {
|
|
|
|
if (!query.author) query.author = {};
|
|
|
|
if (!query["chapters.bands"]) query["chapters.bands"] = {};
|
2023-10-10 22:25:25 -04:00
|
|
|
query["chapters.bands"]["$nin"] = context.currentUser.hiddenBands;
|
|
|
|
query["author"]["$nin"] = context.currentUser.hiddenAuthors;
|
2023-10-03 00:30:22 -04:00
|
|
|
}
|
|
|
|
query["ficmas"] = {
|
2023-12-20 17:23:31 -05:00
|
|
|
$nin: context.ficmasarray_raw!.map((a) => a._id),
|
2023-10-03 00:30:22 -04:00
|
|
|
};
|
|
|
|
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" },
|
|
|
|
})
|
2023-10-11 15:31:02 -04:00
|
|
|
.populate("coAuthor", "username _id")
|
2023-10-03 00:30:22 -04:00
|
|
|
.populate("chapters.bands")
|
|
|
|
.populate({ path: "challenge", model: Challenge })
|
|
|
|
.populate("author", "username _id")
|
|
|
|
.exec();
|
2023-12-06 21:31:50 -05:00
|
|
|
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),
|
|
|
|
};
|
2023-10-03 00:30:22 -04:00
|
|
|
}
|