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

@@ -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.*',