import { render } from 'vike/abort'; /* eslint-disable-line import/extensions */ import { fetchEntitiesById } from '#/src/entities.js'; import { fetchScenes } from '#/src/scenes.js'; import { curateScenesQuery } from '#/src/web/scenes.js'; import { getRandomCampaigns } from '#/src/campaigns.js'; import redis from '#/src//redis.js'; 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], entityScenes, ] = await Promise.all([ fetchEntitiesById([Number(entityId)], { includeChildren: true }), 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), ]); 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: 0.75, maxRatio: 1.25, allowRandomFallback: false, }, { entityIds: [entity.id, entity.parent?.id].filter(Boolean), minRatio: 1.5, allowRandomFallback: false, }, ], { tagFilter: pageContext.tagFilter }); const { scenes, aggActors, aggTags, aggChannels, total, limit, } = entityScenes; const campaignIndex = Math.floor((Math.random() * (0.5 - 0.2) + 0.2) * scenes.length); const [metaCampaign, sceneCampaign, paginationCampaign] = campaigns; return { pageContext: { title: entity.name, pageProps: { entity, scenes, aggActors, aggTags, aggChannels, total, limit, }, campaigns: { index: campaignIndex, meta: metaCampaign, scenes: scenes.length > 5 && sceneCampaign, pagination: paginationCampaign, }, }, }; }