Added tag pages.
This commit is contained in:
@@ -4,7 +4,7 @@ import initLogger from './logger.js';
|
||||
|
||||
const logger = initLogger();
|
||||
|
||||
function curateEntity(entity, context) {
|
||||
export function curateEntity(entity, context) {
|
||||
if (!entity) {
|
||||
return null;
|
||||
}
|
||||
|
||||
70
src/tags.js
70
src/tags.js
@@ -2,6 +2,8 @@ import knex from './knex.js';
|
||||
import redis from './redis.js';
|
||||
import initLogger from './logger.js';
|
||||
|
||||
import { curateEntity } from './entities.js';
|
||||
|
||||
const logger = initLogger();
|
||||
|
||||
function curateTag(tag, context) {
|
||||
@@ -9,35 +11,89 @@ function curateTag(tag, context) {
|
||||
id: tag.id,
|
||||
name: tag.name,
|
||||
slug: tag.slug,
|
||||
description: tag.description,
|
||||
priority: tag.priority,
|
||||
...context.append?.[tag.id],
|
||||
poster: tag.poster && {
|
||||
id: tag.poster.id,
|
||||
path: tag.poster.path,
|
||||
thumbnail: tag.poster.thumbnail,
|
||||
lazy: tag.poster.lazy,
|
||||
isS3: tag.poster.is_s3,
|
||||
comment: tag.poster.comment,
|
||||
entity: tag.poster.entity && curateEntity({
|
||||
...tag.poster.entity,
|
||||
parent: tag.poster.entity_parent,
|
||||
}),
|
||||
},
|
||||
...context?.append?.[tag.id],
|
||||
};
|
||||
}
|
||||
|
||||
export async function fetchTagsById(tagIds, options = {}) {
|
||||
const [tags] = await Promise.all([
|
||||
export async function fetchTags(options = {}) {
|
||||
const [tags, posters] = await Promise.all([
|
||||
knex('tags')
|
||||
.whereIn('tags.id', tagIds)
|
||||
.select('tags.*', 'tags_posters.media_id as poster_id')
|
||||
.modify((builder) => {
|
||||
if (!options.includeAliases) {
|
||||
builder.whereNull('alias_for');
|
||||
}
|
||||
}),
|
||||
knex('tags_posters')
|
||||
.select('tags_posters.tag_id', 'media.*', knex.raw('row_to_json(entities) as entity'), knex.raw('row_to_json(parents) as entity_parent'))
|
||||
.leftJoin('media', 'media.id', 'tags_posters.media_id')
|
||||
.leftJoin('entities', 'entities.id', 'media.entity_id')
|
||||
.leftJoin('entities as parents', 'entities.id', 'entities.parent_id'),
|
||||
]);
|
||||
|
||||
const postersByTagId = Object.fromEntries(posters.map((poster) => [poster.tag_id, poster]));
|
||||
|
||||
return tags.map((tagEntry) => curateTag({
|
||||
...tagEntry,
|
||||
poster: postersByTagId[tagEntry.id],
|
||||
}));
|
||||
}
|
||||
|
||||
export async function fetchTagsById(tagIds, options = {}) {
|
||||
const [tags, posters] = await Promise.all([
|
||||
knex('tags')
|
||||
.whereIn('tags.id', tagIds.filter((tagId) => typeof tagId === 'number'))
|
||||
.orWhereIn('tags.slug', tagIds.filter((tagId) => typeof tagId === 'string'))
|
||||
.modify((builder) => {
|
||||
if (options.order) {
|
||||
builder.orderBy(...options.order);
|
||||
}
|
||||
}),
|
||||
knex('tags_posters')
|
||||
.select('tags_posters.tag_id', 'media.*', knex.raw('row_to_json(entities) as entity'), knex.raw('row_to_json(parents) as entity_parent'))
|
||||
.leftJoin('tags', 'tags.id', 'tags_posters.tag_id')
|
||||
.leftJoin('media', 'media.id', 'tags_posters.media_id')
|
||||
.leftJoin('entities', 'entities.id', 'media.entity_id')
|
||||
.leftJoin('entities as parents', 'parents.id', 'entities.parent_id')
|
||||
.whereIn('tags.id', tagIds.filter((tagId) => typeof tagId === 'number'))
|
||||
.orWhereIn('tags.slug', tagIds.filter((tagId) => typeof tagId === 'string')),
|
||||
]);
|
||||
|
||||
const postersByTagId = Object.fromEntries(posters.map((poster) => [poster.tag_id, poster]));
|
||||
|
||||
if (options.order) {
|
||||
return tags.map((tagEntry) => curateTag(tagEntry, { append: options.append }));
|
||||
return tags.map((tagEntry) => curateTag({
|
||||
...tagEntry,
|
||||
poster: postersByTagId[tagEntry.id],
|
||||
}, { append: options.append }));
|
||||
}
|
||||
|
||||
const curatedTags = tagIds.map((tagId) => {
|
||||
const tag = tags.find((tagEntry) => tagEntry.id === tagId);
|
||||
const tag = tags.find((tagEntry) => (typeof tagId === 'string' ? tagEntry.slug === tagId : tagEntry.id === tagId));
|
||||
|
||||
if (!tag) {
|
||||
console.warn(`Can't match tag ${tagId}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
return curateTag(tag, { append: options.append });
|
||||
return curateTag({
|
||||
...tag,
|
||||
poster: postersByTagId[tag.id],
|
||||
}, { append: options.append });
|
||||
}).filter(Boolean);
|
||||
|
||||
return curatedTags;
|
||||
|
||||
@@ -26,7 +26,7 @@ export async function curateScenesQuery(query) {
|
||||
return {
|
||||
scope: query.scope || 'latest',
|
||||
actorIds: [query.actorId, ...(query.actors?.split(',') || []).map((identifier) => parseActorIdentifier(identifier)?.id)].filter(Boolean),
|
||||
tagIds: await getIdsBySlug([query.tagId, ...(query.tags?.split(',') || [])], 'tags'),
|
||||
tagIds: await getIdsBySlug([query.tagSlug, ...(query.tags?.split(',') || [])], 'tags'),
|
||||
entityId: query.e ? await getIdsBySlug([query.e], 'entities').then(([id]) => id) : query.entityId,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user