|
|
|
|
@@ -1,11 +1,13 @@
|
|
|
|
|
import config from 'config';
|
|
|
|
|
|
|
|
|
|
import { knexQuery as knex, knexManticore } from './knex.js';
|
|
|
|
|
import { knexQuery as knex, knexOwner, knexManticore } from './knex.js';
|
|
|
|
|
import { utilsApi } from './manticore.js';
|
|
|
|
|
import { HttpError } from './errors.js';
|
|
|
|
|
import { fetchActorsById, curateActor, sortActorsByGender } from './actors.js';
|
|
|
|
|
import { fetchTagsById } from './tags.js';
|
|
|
|
|
import { fetchEntitiesById } from './entities.js';
|
|
|
|
|
import { curateStash } from './stashes.js';
|
|
|
|
|
import promiseProps from '../utils/promise-props.js';
|
|
|
|
|
|
|
|
|
|
function curateMedia(media) {
|
|
|
|
|
if (!media) {
|
|
|
|
|
@@ -23,21 +25,21 @@ function curateMedia(media) {
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function curateMovie(rawScene, assets) {
|
|
|
|
|
if (!rawScene) {
|
|
|
|
|
function curateMovie(rawMovie, assets) {
|
|
|
|
|
if (!rawMovie) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id: rawScene.id,
|
|
|
|
|
title: rawScene.title,
|
|
|
|
|
slug: rawScene.slug,
|
|
|
|
|
url: rawScene.url,
|
|
|
|
|
date: rawScene.date,
|
|
|
|
|
createdAt: rawScene.created_at,
|
|
|
|
|
effectiveDate: rawScene.effective_date,
|
|
|
|
|
description: rawScene.description,
|
|
|
|
|
duration: rawScene.duration,
|
|
|
|
|
id: rawMovie.id,
|
|
|
|
|
title: rawMovie.title,
|
|
|
|
|
slug: rawMovie.slug,
|
|
|
|
|
url: rawMovie.url,
|
|
|
|
|
date: rawMovie.date,
|
|
|
|
|
createdAt: rawMovie.created_at,
|
|
|
|
|
effectiveDate: rawMovie.effective_date,
|
|
|
|
|
description: rawMovie.description,
|
|
|
|
|
duration: rawMovie.duration,
|
|
|
|
|
channel: {
|
|
|
|
|
id: assets.channel.id,
|
|
|
|
|
slug: assets.channel.slug,
|
|
|
|
|
@@ -53,7 +55,7 @@ function curateMovie(rawScene, assets) {
|
|
|
|
|
type: assets.channel.network_type,
|
|
|
|
|
hasLogo: assets.channel.has_logo,
|
|
|
|
|
} : null,
|
|
|
|
|
actors: sortActorsByGender(assets.actors.map((actor) => curateActor(actor, { sceneDate: rawScene.effective_date }))),
|
|
|
|
|
actors: sortActorsByGender(assets.actors.map((actor) => curateActor(actor, { sceneDate: rawMovie.effective_date }))),
|
|
|
|
|
directors: assets.directors.map((director) => ({
|
|
|
|
|
id: director.id,
|
|
|
|
|
slug: director.slug,
|
|
|
|
|
@@ -67,23 +69,36 @@ function curateMovie(rawScene, assets) {
|
|
|
|
|
poster: curateMedia(assets.poster),
|
|
|
|
|
covers: assets.covers.map((cover) => curateMedia(cover)),
|
|
|
|
|
photos: assets.photos.map((photo) => curateMedia(photo)),
|
|
|
|
|
createdBatchId: rawScene.created_batch_id,
|
|
|
|
|
updatedBatchId: rawScene.updated_batch_id,
|
|
|
|
|
stashes: assets.stashes?.map((stash) => curateStash(stash)) || [],
|
|
|
|
|
createdBatchId: rawMovie.created_batch_id,
|
|
|
|
|
updatedBatchId: rawMovie.updated_batch_id,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function fetchMoviesById(movieIds) {
|
|
|
|
|
const [movies, channels, actors, directors, tags, covers, photos] = await Promise.all([
|
|
|
|
|
knex('movies').whereIn('id', movieIds),
|
|
|
|
|
// channels
|
|
|
|
|
knex('movies')
|
|
|
|
|
export async function fetchMoviesById(movieIds, reqUser) {
|
|
|
|
|
const {
|
|
|
|
|
movies,
|
|
|
|
|
channels,
|
|
|
|
|
actors,
|
|
|
|
|
directors,
|
|
|
|
|
tags,
|
|
|
|
|
covers,
|
|
|
|
|
photos,
|
|
|
|
|
stashes,
|
|
|
|
|
} = await promiseProps({
|
|
|
|
|
movies: knex('movies')
|
|
|
|
|
.select('movies.*', knex.raw('sum(releases.duration) as duration'))
|
|
|
|
|
.leftJoin('movies_scenes', 'movies_scenes.movie_id', 'movies.id')
|
|
|
|
|
.leftJoin('releases', 'releases.id', 'movies_scenes.scene_id')
|
|
|
|
|
.whereIn('movies.id', movieIds)
|
|
|
|
|
.groupBy('movies.id'),
|
|
|
|
|
channels: knex('movies')
|
|
|
|
|
.select('channels.*', 'networks.id as network_id', 'networks.slug as network_slug', 'networks.name as network_name', 'networks.type as network_type')
|
|
|
|
|
.whereIn('movies.id', movieIds)
|
|
|
|
|
.leftJoin('entities as channels', 'channels.id', 'movies.entity_id')
|
|
|
|
|
.leftJoin('entities as networks', 'networks.id', 'channels.parent_id')
|
|
|
|
|
.groupBy('channels.id', 'networks.id'),
|
|
|
|
|
// actors
|
|
|
|
|
knex('movies')
|
|
|
|
|
actors: knex('movies')
|
|
|
|
|
.select(
|
|
|
|
|
'actors.*',
|
|
|
|
|
'actors_meta.*',
|
|
|
|
|
@@ -97,14 +112,12 @@ export async function fetchMoviesById(movieIds) {
|
|
|
|
|
.leftJoin('releases_actors', 'releases_actors.release_id', 'movies_scenes.scene_id')
|
|
|
|
|
.leftJoin('actors', 'actors.id', 'releases_actors.actor_id')
|
|
|
|
|
.leftJoin('actors_meta', 'actors_meta.actor_id', 'actors.id'),
|
|
|
|
|
// directors
|
|
|
|
|
knex('movies')
|
|
|
|
|
directors: knex('movies')
|
|
|
|
|
.whereIn('movies.id', movieIds)
|
|
|
|
|
.leftJoin('movies_scenes', 'movies_scenes.movie_id', 'movies.id')
|
|
|
|
|
.leftJoin('releases_directors', 'releases_directors.release_id', 'movies_scenes.scene_id')
|
|
|
|
|
.leftJoin('actors as directors', 'directors.id', 'releases_directors.director_id'),
|
|
|
|
|
// tags
|
|
|
|
|
knex('movies')
|
|
|
|
|
tags: knex('movies')
|
|
|
|
|
.select('tags.id', 'tags.slug', 'tags.name', 'tags.priority', 'movies.id as movie_id')
|
|
|
|
|
.distinct()
|
|
|
|
|
.whereIn('movies.id', movieIds)
|
|
|
|
|
@@ -113,17 +126,21 @@ export async function fetchMoviesById(movieIds) {
|
|
|
|
|
.leftJoin('releases_tags', 'releases_tags.release_id', 'movies_scenes.scene_id')
|
|
|
|
|
.leftJoin('tags', 'tags.id', 'releases_tags.tag_id')
|
|
|
|
|
.orderBy('priority', 'desc'),
|
|
|
|
|
// covers
|
|
|
|
|
knex('movies_covers')
|
|
|
|
|
covers: knex('movies_covers')
|
|
|
|
|
.whereIn('movie_id', movieIds)
|
|
|
|
|
.leftJoin('media', 'media.id', 'movies_covers.media_id'),
|
|
|
|
|
// photos
|
|
|
|
|
knex('movies')
|
|
|
|
|
photos: knex('movies')
|
|
|
|
|
.whereIn('movies.id', movieIds)
|
|
|
|
|
.leftJoin('movies_scenes', 'movies_scenes.movie_id', 'movies.id')
|
|
|
|
|
.leftJoin('releases_photos', 'releases_photos.release_id', 'movies_scenes.scene_id')
|
|
|
|
|
.leftJoin('media', 'media.id', 'releases_photos.media_id'),
|
|
|
|
|
]);
|
|
|
|
|
stashes: reqUser
|
|
|
|
|
? knexOwner('stashes_movies')
|
|
|
|
|
.leftJoin('stashes', 'stashes.id', 'stashes_movies.stash_id')
|
|
|
|
|
.where('stashes.user_id', reqUser.id)
|
|
|
|
|
.whereIn('stashes_movies.movie_id', movieIds)
|
|
|
|
|
: [],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return movieIds.map((movieId) => {
|
|
|
|
|
const movie = movies.find((movieEntry) => movieEntry.id === movieId);
|
|
|
|
|
@@ -139,6 +156,7 @@ export async function fetchMoviesById(movieIds) {
|
|
|
|
|
const movieTags = tags.filter((tag) => tag.movie_id === movieId);
|
|
|
|
|
const movieCovers = covers.filter((cover) => cover.movie_id === movieId);
|
|
|
|
|
const moviePhotos = photos.filter((photo) => photo.release_id === movieId);
|
|
|
|
|
const movieStashes = stashes.filter((stash) => stash.movie_id === movieId);
|
|
|
|
|
|
|
|
|
|
return curateMovie(movie, {
|
|
|
|
|
channel: movieChannel,
|
|
|
|
|
@@ -147,6 +165,7 @@ export async function fetchMoviesById(movieIds) {
|
|
|
|
|
tags: movieTags,
|
|
|
|
|
covers: movieCovers,
|
|
|
|
|
photos: moviePhotos,
|
|
|
|
|
stashes: movieStashes,
|
|
|
|
|
});
|
|
|
|
|
}).filter(Boolean);
|
|
|
|
|
}
|
|
|
|
|
@@ -499,7 +518,7 @@ function countAggregations(buckets) {
|
|
|
|
|
return Object.fromEntries(buckets.map((bucket) => [bucket.key, { count: bucket.doc_count }]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function fetchMovies(filters, rawOptions) {
|
|
|
|
|
export async function fetchMovies(filters, rawOptions, reqUser) {
|
|
|
|
|
const options = curateOptions(rawOptions);
|
|
|
|
|
|
|
|
|
|
console.log(options);
|
|
|
|
|
@@ -517,11 +536,8 @@ export async function fetchMovies(filters, rawOptions) {
|
|
|
|
|
options.aggregateChannels ? fetchEntitiesById(result.aggregations.channelIds.map((bucket) => bucket.key), { order: ['name', 'asc'], append: channelCounts }) : [],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
console.log(result.aggregations);
|
|
|
|
|
console.log(aggActors);
|
|
|
|
|
|
|
|
|
|
const movieIds = result.movies.map((movie) => Number(movie.id));
|
|
|
|
|
const movies = await fetchMoviesById(movieIds);
|
|
|
|
|
const movies = await fetchMoviesById(movieIds, reqUser);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
movies,
|
|
|
|
|
|