Added affiliate parameters to scene URL.

This commit is contained in:
2024-06-22 22:51:57 +02:00
parent c2edf72081
commit 9ce9cfbb0c
8 changed files with 97 additions and 13 deletions

View File

@@ -19,6 +19,11 @@ export function curateEntity(entity, context) {
hasLogo: entity.has_logo,
parent: curateEntity(entity.parent, context),
children: context?.children?.filter((child) => child.parent_id === entity.id).map((child) => curateEntity({ ...child, parent: entity }, { parent: entity })) || [],
affiliate: entity.affiliate ? {
id: entity.affiliate.id,
url: entity.affiliate.url,
parameters: entity.affiliate.parameters,
} : null,
...context?.append?.[entity.id],
};
}
@@ -66,15 +71,20 @@ export async function fetchEntities(options) {
export async function fetchEntitiesById(entityIds, options = {}) {
const [entities, children] = await Promise.all([
knex('entities')
.select('entities.*', knex.raw('row_to_json(parents) as parent'))
.select(
'entities.*',
knex.raw('row_to_json(parents) as parent'),
knex.raw('row_to_json(affiliates) as affiliate'),
)
.whereIn('entities.id', entityIds)
.leftJoin('entities as parents', 'parents.id', 'entities.parent_id')
.leftJoin('affiliates', knex.raw('affiliates.entity_id in (entities.id, parents.id)'))
.modify((builder) => {
if (options.order) {
builder.orderBy(...options.order);
}
})
.groupBy('entities.id', 'parents.id'),
.groupBy('entities.id', 'parents.id', 'affiliates.id'),
options.includeChildren ? knex('entities')
.whereIn('entities.parent_id', entityIds)
.orderBy('slug') : [],

View File

@@ -12,12 +12,47 @@ import { curateMedia } from './media.js';
import escape from '../utils/escape-manticore.js';
import promiseProps from '../utils/promise-props.js';
function getWatchUrl(scene) {
if (scene.url) {
return scene.url;
}
if (scene.channel && (scene.channel.isIndependent || scene.channel.type === 'network')) {
return scene.channel.url;
}
if (scene.network) {
return scene.network.url;
}
return null;
}
function getAffiliateUrl(scene) {
const watchUrl = getWatchUrl(scene);
if (!watchUrl) {
return null;
}
if (!scene.affiliate?.parameters) {
return scene.url;
}
const newParams = new URLSearchParams({
...Object.fromEntries(new URL(watchUrl).searchParams),
...Object.fromEntries(new URLSearchParams(scene.affiliate.parameters)),
});
return `${watchUrl}?${newParams.toString()}`;
}
function curateScene(rawScene, assets) {
if (!rawScene) {
return null;
}
return {
const curatedScene = {
id: rawScene.id,
title: rawScene.title,
slug: rawScene.slug,
@@ -43,6 +78,11 @@ function curateScene(rawScene, assets) {
type: assets.channel.network_type,
hasLogo: assets.channel.has_logo,
} : null,
affiliate: assets.channel.affiliate ? {
id: assets.channel.affiliate.id,
url: assets.channel.affiliate.url,
parameters: assets.channel.affiliate.parameters,
} : null,
actors: sortActorsByGender(assets.actors.map((actor) => curateActor(actor, {
sceneDate: rawScene.effective_date,
stashes: assets.actorStashes.filter((actorStash) => actorStash.actor_id === actor.id),
@@ -80,6 +120,10 @@ function curateScene(rawScene, assets) {
updatedBatchId: rawScene.updated_batch_id,
isNew: assets.lastBatchId === rawScene.created_batch_id,
};
curatedScene.watchUrl = getAffiliateUrl(curatedScene);
return curatedScene;
}
export async function fetchScenesById(sceneIds, { reqUser, ...context } = {}) {
@@ -101,11 +145,19 @@ export async function fetchScenesById(sceneIds, { reqUser, ...context } = {}) {
} = await promiseProps({
scenes: knex('releases').whereIn('releases.id', sceneIds),
channels: knex('releases')
.select('channels.*', 'networks.id as network_id', 'networks.slug as network_slug', 'networks.name as network_name', 'networks.type as network_type')
.select(
'channels.*',
'networks.id as network_id',
'networks.slug as network_slug',
'networks.name as network_name',
'networks.type as network_type',
knex.raw('row_to_json(affiliates) as affiliate'),
)
.whereIn('releases.id', sceneIds)
.leftJoin('entities as channels', 'channels.id', 'releases.entity_id')
.leftJoin('entities as networks', 'networks.id', 'channels.parent_id')
.groupBy('channels.id', 'networks.id'),
.leftJoin('affiliates', knex.raw('affiliates.entity_id in (channels.id, networks.id)'))
.groupBy('channels.id', 'networks.id', 'affiliates.id'),
actors: knex('releases_actors')
.select(
'actors.*',

View File

@@ -24,8 +24,6 @@ export async function curateScenesQuery(query) {
notEntityIds: await getIdsBySlug(splitEntities.filter((entity) => entity.charAt(0) === '!').map((entity) => entity.slice(1)), 'entities'),
});
console.log('QUERY', query);
return {
scope: query.scope || 'latest',
query: query.q,