traxxx-web/pages/entities/@entitySlug/+onBeforeRender.js

92 lines
2.8 KiB
JavaScript
Raw Normal View History

import { render } from 'vike/abort'; /* eslint-disable-line import/extensions */
import { fetchEntitiesById } from '#/src/entities.js';
import { fetchScenes } from '#/src/scenes.js';
import { fetchMovies } from '#/src/movies.js';
import { curateScenesQuery } from '#/src/web/scenes.js';
import { curateMoviesQuery } from '#/src/web/movies.js';
import { getRandomCampaigns, getCampaignIndex } from '#/src/campaigns.js';
import redis from '#/src//redis.js';
async function fetchReleases(pageContext, entityId) {
if (pageContext.routeParams.domain === 'movies') {
return fetchMovies(await curateMoviesQuery({
...pageContext.urlQuery,
scope: pageContext.routeParams.scope || 'latest',
entityId: Number(entityId),
tagFilter: pageContext.tagFilter,
}), {
page: Number(pageContext.routeParams.page) || 1,
limit: Number(pageContext.urlParsed.search.limit) || 30,
aggregate: true,
}, pageContext.user);
}
return fetchScenes(await curateScenesQuery({
...pageContext.urlQuery,
scope: pageContext.routeParams.scope || 'latest',
entityId: Number(entityId),
tagFilter: pageContext.tagFilter,
}), {
page: Number(pageContext.routeParams.page) || 1,
limit: Number(pageContext.urlParsed.search.limit) || 30,
aggregate: true,
}, pageContext.user);
}
export async function onBeforeRender(pageContext) {
const entityId = await redis.hGet('traxxx:entities:id_by_slug', pageContext.routeParams.entityType === 'network' ? `_${pageContext.routeParams.entitySlug}` : pageContext.routeParams.entitySlug);
if (!entityId) {
throw render(404, `Cannot find ${pageContext.routeParams.entityType} '${pageContext.routeParams.entitySlug}'.`);
}
const [
[entity],
entityReleases,
] = await Promise.all([
fetchEntitiesById([Number(entityId)], { includeChildren: true }),
fetchReleases(pageContext, entityId),
]);
const campaigns = await getRandomCampaigns([
{
entityIds: [entity.id, entity.parent?.id].filter(Boolean),
minRatio: 1.5,
allowRandomFallback: false,
},
{
entityIds: [entity.id, entity.parent?.id].filter(Boolean),
minRatio: 1.5,
allowRandomFallback: false,
},
pageContext.routeParams.domain === 'scenes' ? {
entityIds: [entity.id, entity.parent?.id].filter(Boolean),
minRatio: 0.75,
maxRatio: 1.25,
allowRandomFallback: false,
} : null,
].filter(Boolean), { tagFilter: pageContext.tagFilter });
const releases = entityReleases.scenes || entityReleases.movies;
const campaignIndex = getCampaignIndex(releases.length);
const [metaCampaign, paginationCampaign, sceneCampaign] = campaigns;
return {
pageContext: {
title: entity.name,
pageProps: {
entity,
...entityReleases,
},
campaigns: {
index: campaignIndex,
meta: metaCampaign,
scenes: releases.length > 5 && sceneCampaign,
pagination: paginationCampaign,
},
},
};
}