Added poster to API scene overview.

This commit is contained in:
DebaucheryLibrarian 2020-12-19 01:03:15 +01:00
parent 2b808025f9
commit 67ed249239
2 changed files with 29 additions and 12 deletions

View File

@ -8,4 +8,5 @@ module.exports = knex({
connection: config.database,
// performance overhead, don't use asyncStackTraces in production
asyncStackTraces: process.env.NODE_ENV === 'development',
debug: process.env.NODE_ENV === 'development',
});

View File

@ -6,7 +6,7 @@ const logger = require('./logger')(__filename);
const knex = require('./knex');
const { flushOrphanedMedia } = require('./media');
function curateRelease(release, withMedia = false) {
function curateRelease(release, withMedia = false, withPoster = true) {
if (!release) {
return null;
}
@ -43,29 +43,35 @@ function curateRelease(release, withMedia = false) {
name: tag.name,
slug: tag.slug,
})),
...(withMedia && {
...((withMedia || withPoster) && {
poster: release.poster ? {
id: release.poster.id,
path: release.poster.path,
thumbnail: release.poster.thumbnail,
width: release.poster.width,
height: release.poster.height,
size: release.poster.size,
source: release.poster.source,
} : null,
}),
...(withMedia && {
photos: (release.photos || []).map(photo => ({
id: photo.id,
path: photo.path,
thumbnail: release.poster.thumbnail,
width: photo.width,
height: photo.height,
size: photo.size,
source: photo.source,
})),
trailer: release.trailer ? {
id: release.trailer.id,
path: release.trailer.path,
} : null,
}),
createdAt: release.created_at,
};
}
function withRelations(queryBuilder, withMedia = false) {
function withRelations(queryBuilder, withMedia = false, withPoster = true) {
queryBuilder
.select(knex.raw(`
releases.id, releases.entry_id, releases.shoot_id, releases.title, releases.url, releases.date, releases.description, releases.duration, releases.created_at,
@ -85,24 +91,34 @@ function withRelations(queryBuilder, withMedia = false) {
entities.id, parents.id
`));
if (withMedia) {
if (withMedia || withPoster) {
queryBuilder
.select(knex.raw(`
row_to_json(posters) as poster,
COALESCE(json_agg(DISTINCT photos) FILTER (WHERE photos.id IS NOT NULL), '[]') as photos
row_to_json(posters) as poster
`))
.leftJoin('releases_posters', 'releases_posters.release_id', 'releases.id')
.leftJoin('media as posters', 'posters.id', 'releases_posters.media_id')
.groupBy('posters.id');
}
if (withMedia) {
queryBuilder
.select(knex.raw(`
row_to_json(trailers) as trailer,
COALESCE(json_agg(DISTINCT photos) FILTER (WHERE photos.id IS NOT NULL), '[]') as photos
`))
.leftJoin('releases_photos', 'releases_photos.release_id', 'releases.id')
.leftJoin('media as photos', 'photos.id', 'releases_photos.media_id')
.groupBy('posters.id');
.leftJoin('releases_trailers', 'releases_trailers.release_id', 'releases.id')
.leftJoin('media as trailers', 'trailers.id', 'releases_trailers.media_id')
.groupBy('posters.id', 'trailers.id');
}
}
async function fetchScene(releaseId) {
const release = await knex('releases')
.where('releases.id', releaseId)
.modify(withRelations, true)
.modify(withRelations, true, true)
.first();
return curateRelease(release, true);
@ -110,7 +126,7 @@ async function fetchScene(releaseId) {
async function fetchScenes(limit = 100) {
const releases = await knex('releases')
.modify(withRelations, false)
.modify(withRelations, false, true)
.limit(Math.min(limit, 1000000));
return releases.map(release => curateRelease(release));
@ -119,7 +135,7 @@ async function fetchScenes(limit = 100) {
async function searchScenes(query, limit = 100) {
const releases = await knex
.from(knex.raw('search_releases(?) as releases', [query]))
.modify(withRelations, false)
.modify(withRelations, false, true)
.limit(Math.min(limit, 1000000));
return releases.map(release => curateRelease(release));