2024-01-25 02:07:26 +00:00
|
|
|
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';
|
2024-06-12 15:09:53 +00:00
|
|
|
import { getRandomCampaigns } from '#/src/campaigns.js';
|
2024-01-25 02:07:26 +00:00
|
|
|
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}'.`);
|
|
|
|
}
|
|
|
|
|
2024-06-12 15:09:53 +00:00
|
|
|
const [
|
|
|
|
[entity],
|
|
|
|
entityScenes,
|
|
|
|
] = await Promise.all([
|
2024-01-25 02:07:26 +00:00
|
|
|
fetchEntitiesById([Number(entityId)], { includeChildren: true }),
|
|
|
|
fetchScenes(await curateScenesQuery({
|
|
|
|
...pageContext.urlQuery,
|
|
|
|
scope: pageContext.routeParams.scope || 'latest',
|
|
|
|
entityId: Number(entityId),
|
2024-04-02 03:55:53 +00:00
|
|
|
tagFilter: pageContext.tagFilter,
|
2024-01-25 02:07:26 +00:00
|
|
|
}), {
|
|
|
|
page: Number(pageContext.routeParams.page) || 1,
|
|
|
|
limit: Number(pageContext.urlParsed.search.limit) || 30,
|
|
|
|
aggregate: true,
|
2024-03-29 03:23:36 +00:00
|
|
|
}, pageContext.user),
|
2024-01-25 02:07:26 +00:00
|
|
|
]);
|
|
|
|
|
2024-06-12 15:09:53 +00:00
|
|
|
const campaigns = await getRandomCampaigns([
|
|
|
|
{
|
|
|
|
entityIds: [entity.id, entity.parent?.id].filter(Boolean),
|
|
|
|
minRatio: 1.5,
|
2024-06-16 23:28:20 +00:00
|
|
|
allowRandomFallback: false,
|
2024-06-12 15:09:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
entityIds: [entity.id, entity.parent?.id].filter(Boolean),
|
|
|
|
minRatio: 0.75,
|
|
|
|
maxRatio: 1.25,
|
2024-06-16 23:28:20 +00:00
|
|
|
allowRandomFallback: false,
|
2024-06-12 15:09:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
entityIds: [entity.id, entity.parent?.id].filter(Boolean),
|
|
|
|
parentEntityId: entity.parent?.id,
|
|
|
|
minRatio: 1.5,
|
2024-06-16 23:28:20 +00:00
|
|
|
allowRandomFallback: false,
|
2024-06-12 15:09:53 +00:00
|
|
|
},
|
2024-06-13 00:26:34 +00:00
|
|
|
], { tagFilter: pageContext.tagFilter });
|
2024-06-12 15:09:53 +00:00
|
|
|
|
2024-01-25 02:07:26 +00:00
|
|
|
const {
|
|
|
|
scenes,
|
|
|
|
aggActors,
|
|
|
|
aggTags,
|
|
|
|
aggChannels,
|
|
|
|
total,
|
|
|
|
limit,
|
|
|
|
} = entityScenes;
|
|
|
|
|
2024-06-12 15:09:53 +00:00
|
|
|
const campaignIndex = Math.floor((Math.random() * (0.5 - 0.2) + 0.2) * scenes.length);
|
|
|
|
const [metaCampaign, sceneCampaign, paginationCampaign] = campaigns;
|
|
|
|
|
2024-01-25 02:07:26 +00:00
|
|
|
return {
|
|
|
|
pageContext: {
|
|
|
|
title: entity.name,
|
|
|
|
pageProps: {
|
|
|
|
entity,
|
|
|
|
scenes,
|
|
|
|
aggActors,
|
|
|
|
aggTags,
|
|
|
|
aggChannels,
|
|
|
|
total,
|
|
|
|
limit,
|
|
|
|
},
|
2024-06-12 15:09:53 +00:00
|
|
|
campaigns: {
|
|
|
|
index: campaignIndex,
|
|
|
|
meta: metaCampaign,
|
|
|
|
scenes: scenes.length > 5 && sceneCampaign,
|
|
|
|
pagination: paginationCampaign,
|
|
|
|
},
|
2024-01-25 02:07:26 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|