67 lines
2.3 KiB
JavaScript
67 lines
2.3 KiB
JavaScript
import { stringify } from '@brillout/json-serializer/stringify'; /* eslint-disable-line import/extensions */
|
|
|
|
import { fetchScenes } from '../scenes.js';
|
|
import { parseActorIdentifier } from '../query.js';
|
|
import { getIdsBySlug } from '../cache.js';
|
|
import slugify from '../../utils/slugify.js';
|
|
import promiseProps from '../../utils/promise-props.js';
|
|
|
|
export async function curateScenesQuery(query) {
|
|
const splitTags = query.tags?.split(',') || [];
|
|
const splitActors = query.actors?.split(',') || [];
|
|
const splitEntities = query.e?.split(',') || [];
|
|
const mainEntity = splitEntities.find((entity) => entity.charAt(0) !== '!');
|
|
|
|
const {
|
|
tagIds,
|
|
notTagIds,
|
|
entityId,
|
|
notEntityIds,
|
|
} = await promiseProps({
|
|
tagIds: getIdsBySlug([query.tagSlug, ...splitTags.filter((tag) => tag.charAt(0) !== '!')], 'tags'),
|
|
notTagIds: getIdsBySlug([...(query.tagFilter || []), ...(splitTags.filter((tag) => tag.charAt(0) === '!').map((tag) => tag.slice(1)) || [])].map((tag) => slugify(tag)), 'tags'),
|
|
entityId: mainEntity ? await getIdsBySlug([mainEntity], 'entities').then(([id]) => id) : query.entityId,
|
|
notEntityIds: await getIdsBySlug(splitEntities.filter((entity) => entity.charAt(0) === '!').map((entity) => entity.slice(1)), 'entities'),
|
|
});
|
|
|
|
return {
|
|
scope: query.scope || 'latest',
|
|
query: query.q,
|
|
actorIds: [query.actorId, ...splitActors.filter((actor) => actor.charAt(0) !== '!').map((identifier) => parseActorIdentifier(identifier)?.id)].filter(Boolean),
|
|
notActorIds: splitActors.filter((actor) => actor.charAt(0) === '!').map((identifier) => parseActorIdentifier(identifier.slice(1))?.id).filter(Boolean),
|
|
tagIds,
|
|
notTagIds: notTagIds.filter((tagId) => !tagIds.includes(tagId)), // included tags get priority over excluded tags
|
|
entityId,
|
|
notEntityIds,
|
|
movieId: Number(query.movieId) || null,
|
|
stashId: Number(query.stashId) || null,
|
|
isShowcased: typeof query.isShowcased === 'boolean' ? query.isShowcased : null,
|
|
};
|
|
}
|
|
|
|
export async function fetchScenesApi(req, res) {
|
|
const {
|
|
scenes,
|
|
aggActors,
|
|
aggTags,
|
|
aggChannels,
|
|
limit,
|
|
total,
|
|
} = await fetchScenes(await curateScenesQuery({
|
|
...req.query,
|
|
tagFilter: req.tagFilter,
|
|
}), {
|
|
page: Number(req.query.page) || 1,
|
|
limit: Number(req.query.limit) || 30,
|
|
}, req.user);
|
|
|
|
res.send(stringify({
|
|
scenes,
|
|
aggActors,
|
|
aggTags,
|
|
aggChannels,
|
|
limit,
|
|
total,
|
|
}));
|
|
}
|