next/lib/server/dbHelpers/listQuerier.ts

33 lines
1.1 KiB
TypeScript

import { Band } from "~/models/band";
import { Challenge } from "~/models/challenges/gen";
import { Story } from "~/models/stories";
import { log } from "../logger";
export default async function (query, context, limit?: number, sort?) {
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),
};
log.debug(query, { label: "list query" });
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")
.populate("chapters.bands")
.populate({ path: "challenge", model: Challenge })
.populate("author", "username _id")
.limit(limit || Infinity)
.exec();
return stories.filter((a) => a.author != null);
}