From 78654a62df07b3d949121b165b65adbecf0052f1 Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Sat, 21 Nov 2026 02:54:56 +0100 Subject: [PATCH] Added scene relations. --- components/edit/relations.vue | 238 ++++++++++++ components/edit/revisions.vue | 30 ++ components/scenes/relation.vue | 354 ++++++++++++++++++ components/scenes/tile.vue | 29 ++ .../admin/revisions/scenes/+onBeforeRender.js | 2 + pages/scenes/@sceneId/+Page.vue | 41 +- pages/scenes/@sceneId/+onBeforeRender.js | 13 +- pages/scenes/@sceneId/edit/+Page.vue | 33 ++ .../@sceneId/revisions/+onBeforeRender.js | 2 + pages/users/@username/+onBeforeRender.js | 2 + src/scenes.js | 192 +++++++++- utils/relation-labels.js | 20 + 12 files changed, 952 insertions(+), 4 deletions(-) create mode 100644 components/edit/relations.vue create mode 100644 components/scenes/relation.vue create mode 100644 utils/relation-labels.js diff --git a/components/edit/relations.vue b/components/edit/relations.vue new file mode 100644 index 0000000..30dc839 --- /dev/null +++ b/components/edit/relations.vue @@ -0,0 +1,238 @@ + + + + + diff --git a/components/edit/revisions.vue b/components/edit/revisions.vue index dbae454..d3822b6 100644 --- a/components/edit/revisions.vue +++ b/components/edit/revisions.vue @@ -10,6 +10,7 @@ import Pagination from '#/components/pagination/pagination.vue'; import { get, post } from '#/src/api.js'; import navigate from '#/src/navigate.js'; import { formatDuration } from '#/utils/format.js'; +import { getRelationLabel } from '#/utils/relation-labels.js'; defineProps({ context: { @@ -26,11 +27,28 @@ const actors = ref(pageContext.pageProps.actors); const tags = ref(pageContext.pageProps.tags); const movies = ref(pageContext.pageProps.movies); const avatars = ref(pageContext.pageProps.avatars); +const relationScenes = ref(pageContext.pageProps.relationScenes || []); // not provided for actor revisions const actorsById = computed(() => Object.fromEntries(actors.value.map((actor) => [actor.id, actor]))); const tagsById = computed(() => Object.fromEntries(tags.value.map((tag) => [tag.id, tag]))); const moviesById = computed(() => Object.fromEntries(movies.value.map((movie) => [movie.id, movie]))); const avatarsById = computed(() => Object.fromEntries(avatars.value.map((avatar) => [avatar.id, avatar]))); +const relationScenesById = computed(() => Object.fromEntries(relationScenes.value.map((scene) => [scene.id, scene]))); + +function getRelationKey(relation) { + return `${relation.direction}:${relation.type}:${relation.sceneId}`; +} + +function curateRelation(relation, modified) { + const label = getRelationLabel(relation.type, relation.direction); + const relatedScene = relationScenesById.value[relation.sceneId]; + + return { + id: getRelationKey(relation), + name: `${label.charAt(0).toLowerCase()}${label.slice(1)} ${relatedScene?.title || `#${relation.sceneId}`}`, + modified, + }; +} const feedbacks = ref({}); const showReviewed = ref(Object.hasOwn(pageContext.urlParsed.search, 'final')); @@ -90,6 +108,10 @@ const curatedRevisions = computed(() => revisions.value.map((revision) => { }))]; } + if (key === 'relations') { + return [key, value.map((relation) => curateRelation(relation, revision.deltas.some((delta) => delta.key === key && !delta.value.some((deltaRelation) => getRelationKey(deltaRelation) === getRelationKey(relation)))))]; + } + if (key === 'socials') { // new socials don't have IDs yet, so we need to compare the values return [key, value.map((item) => ({ @@ -134,6 +156,13 @@ const curatedRevisions = computed(() => revisions.value.map((revision) => { }; } + if (delta.key === 'relations') { + return { + ...delta, + value: delta.value.map((relation) => curateRelation(relation, !(revision.base.relations || []).some((baseRelation) => getRelationKey(baseRelation) === getRelationKey(relation)))), + }; + } + if (delta.key === 'socials') { // new socials don't have IDs yet, so we need to compare the values return { @@ -187,6 +216,7 @@ async function reloadRevisions() { actors.value = updatedRevisions.actors; tags.value = updatedRevisions.tags; movies.value = updatedRevisions.movies; + relationScenes.value = updatedRevisions.relationScenes || []; avatars.value = updatedRevisions.avatars; revisions.value = updatedRevisions.revisions; total.value = updatedRevisions.total; diff --git a/components/scenes/relation.vue b/components/scenes/relation.vue new file mode 100644 index 0000000..a90f459 --- /dev/null +++ b/components/scenes/relation.vue @@ -0,0 +1,354 @@ + + + + + diff --git a/components/scenes/tile.vue b/components/scenes/tile.vue index 8e84da8..d3d106d 100644 --- a/components/scenes/tile.vue +++ b/components/scenes/tile.vue @@ -11,6 +11,11 @@ const props = defineProps({ type: Object, default: null, }, + // short context shown over the poster, e.g. how the scene relates to the current page + label: { + type: String, + default: null, + }, }); const pageContext = inject('pageContext'); @@ -53,6 +58,11 @@ const favorited = ref(props.scene.stashes.some((sceneStash) => sceneStash.id === /> + {{ label }} + sceneStash.id === } } +/* top left, the stash heart sits top right */ +.label { + position: absolute; + top: 0; + left: 0; + max-width: calc(100% - 4rem); + box-sizing: border-box; + overflow: hidden; + padding: .2rem .4rem; + border-radius: 0 0 .25rem 0; + background: var(--shadow-strong-10); + color: var(--text-light); + font-size: .8rem; + font-weight: bold; + white-space: nowrap; + text-overflow: ellipsis; + pointer-events: none; +} + .thumbnail { width: 100%; height: 100%; diff --git a/pages/admin/revisions/scenes/+onBeforeRender.js b/pages/admin/revisions/scenes/+onBeforeRender.js index ef03d39..ae6e5cb 100644 --- a/pages/admin/revisions/scenes/+onBeforeRender.js +++ b/pages/admin/revisions/scenes/+onBeforeRender.js @@ -14,6 +14,7 @@ export async function onBeforeRender(pageContext) { actors, tags, movies, + relationScenes, } = await fetchSceneRevisions(null, { isFinalized: Object.hasOwn(pageContext.urlParsed.search, 'final'), limit: Number(pageContext.urlParsed.search.limit) || 100, @@ -29,6 +30,7 @@ export async function onBeforeRender(pageContext) { actors, tags, movies, + relationScenes, }, }, }; diff --git a/pages/scenes/@sceneId/+Page.vue b/pages/scenes/@sceneId/+Page.vue index 83cae8c..e7db481 100644 --- a/pages/scenes/@sceneId/+Page.vue +++ b/pages/scenes/@sceneId/+Page.vue @@ -10,6 +10,7 @@ import Banner from '#/components/media/banner.vue'; import MovieTile from '#/components/movies/tile.vue'; import Chapters from '#/components/scenes/chapters.vue'; +import SceneTile from '#/components/scenes/tile.vue'; import SerieTile from '#/components/series/tile.vue'; import Heart from '#/components/stashes/heart.vue'; import events from '#/src/events.js'; @@ -17,6 +18,7 @@ import getPath from '#/src/get-path.js'; import { formatDate, formatDuration } from '#/utils/format.js'; import processSummaryTemplate from '#/utils/process-summary-template.js'; +import { getRelationLabel } from '#/utils/relation-labels.js'; import verifyAbility from '#/utils/verify-ability.js'; const cookies = Cookies.withConverter({ @@ -31,7 +33,19 @@ const { env, } = inject('pageContext'); -const { scene } = pageProps; +const { scene, relations } = pageProps; + +// one tile per related scene, a scene may be related in more than one way +const relatedScenes = Object.values(relations.reduce((acc, relation) => { + acc[relation.scene.id] ??= { + scene: relation.scene, + labels: [], + }; + + acc[relation.scene.id].labels.push(getRelationLabel(relation.type, relation.direction)); + + return acc; +}, {})); const avatarFrameSize = 36; const avatarMargin = 2.75; @@ -571,6 +585,25 @@ function copySummary() { /> +
+

Related

+ + +
+

Added

@@ -937,6 +970,12 @@ function copySummary() { grid-template-columns: repeat(auto-fill, minmax(14rem, 1fr)); } +.related-scenes { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(20rem, 1fr)); + gap: .5rem; +} + .section { margin-bottom: 1rem; } diff --git a/pages/scenes/@sceneId/+onBeforeRender.js b/pages/scenes/@sceneId/+onBeforeRender.js index 52a46ff..587269f 100644 --- a/pages/scenes/@sceneId/+onBeforeRender.js +++ b/pages/scenes/@sceneId/+onBeforeRender.js @@ -1,7 +1,7 @@ import { redirect, render } from 'vike/abort'; import { getIdsBySlug } from '#/src/cache.js'; import { getRandomCampaigns } from '#/src/campaigns.js'; -import { fetchScenesById } from '#/src/scenes.js'; +import { fetchRelationTypes, fetchSceneRelations, fetchScenesById } from '#/src/scenes.js'; function getTitle(scene) { if (scene.title) { @@ -32,7 +32,7 @@ export async function onBeforeRender(pageContext) { throw render(404, `Cannot find scene '${pageContext.routeParams.sceneId}'.`); } - const [campaigns, tagIds] = await Promise.all([ + const [campaigns, tagIds, relations, relationTypes] = await Promise.all([ getRandomCampaigns([ { minRatio: 1.5, @@ -46,6 +46,13 @@ export async function onBeforeRender(pageContext) { 'dp', 'facial', ], 'tags', true), + fetchSceneRelations(scene.id, { + reqUser: pageContext.user, + restriction: pageContext.geo.restriction, + }), + pageContext.user // relations can be proposed through scene revisions + ? fetchRelationTypes() + : [], ]); return { @@ -54,6 +61,8 @@ export async function onBeforeRender(pageContext) { pageProps: { scene, tagIds, + relations, + relationTypes, }, campaigns: { scene: campaigns[0], diff --git a/pages/scenes/@sceneId/edit/+Page.vue b/pages/scenes/@sceneId/edit/+Page.vue index 9d47413..23b0e5c 100644 --- a/pages/scenes/@sceneId/edit/+Page.vue +++ b/pages/scenes/@sceneId/edit/+Page.vue @@ -4,6 +4,7 @@ import { computed, inject, ref, useId } from 'vue'; import EditActors from '#/components/edit/actors.vue'; import EditMovies from '#/components/edit/movies.vue'; +import EditRelations from '#/components/edit/relations.vue'; import EditTags from '#/components/edit/tags.vue'; import Checkbox from '#/components/form/checkbox.vue'; @@ -14,6 +15,7 @@ const pageContext = inject('pageContext'); const user = pageContext.user; const scene = ref(pageContext.pageProps.scene); +const relationTypes = pageContext.pageProps.relationTypes; const applyTooltipId = useId(); const fields = computed(() => [ @@ -80,6 +82,23 @@ const fields = computed(() => [ type: 'movies', value: scene.value.movies, }, + { + key: 'relations', + type: 'relations', + value: pageContext.pageProps.relations.map((relation) => ({ + sceneId: relation.scene.id, + type: relation.type, + direction: relation.direction, + scene: relation.scene, + })), + // only submit what identifies the relation, not the related scene's details + simplify: (relations) => relations.map((relation) => ({ + sceneId: relation.sceneId, + type: relation.type, + direction: relation.direction, + })), + note: 'Relations point from a rerelease, remaster or compilation to the original scene. Each relation is shown on both scenes.', + }, { key: 'comment', type: 'text', @@ -97,6 +116,10 @@ const fields = computed(() => [ ].filter((field) => field.enabled !== false)); function simplifyArray(field) { + if (typeof field.simplify === 'function') { + return field.simplify(field.value); + } + if (Array.isArray(field.value) && field.simplify !== false) { return field.value.map((item) => item.hash || item.id); } @@ -307,6 +330,16 @@ async function submit() { @movies="(movies) => { edits.movies = movies; }" /> + + (relation.source_scene_id === sceneId + ? relation.target_scene_id + : relation.source_scene_id)))); + + const relatedScenes = await fetchScenesById(relatedSceneIds, { reqUser, ...context }); + const relatedScenesById = Object.fromEntries(relatedScenes.map((scene) => [scene.id, scene])); + + return relations.map((relation) => { + const isSource = relation.source_scene_id === sceneId; + const relatedScene = relatedScenesById[isSource ? relation.target_scene_id : relation.source_scene_id]; + + if (!relatedScene) { + return null; + } + + return { + id: relation.id, + type: relation.type, + direction: isSource ? 'target' : 'source', // which side of the relation the related scene is on + scene: relatedScene, + }; + }).filter(Boolean); +} + +export async function fetchRelationTypes() { + return knex('relations_types').orderBy('type').pluck('type'); +} + +// relations in revisions are stored relative to the revised scene: { sceneId, type, direction } +// sceneId is the related scene, direction is the side of the relation the related scene is on +function getRelationKey(relation) { + return `${relation.direction}:${relation.type}:${relation.sceneId}`; +} + +async function fetchRevisionRelations(sceneId, trx) { + const relations = await knexOwner('scenes_relations') + .select('id', 'source_scene_id', 'target_scene_id', 'type') + .where((builder) => { + builder + .where('source_scene_id', sceneId) + .orWhere('target_scene_id', sceneId); + }) + .orderBy('created_at', 'asc') + .modify((builder) => { + if (trx) { + builder.transacting(trx); + } + }); + + return relations.map((relation) => ({ + id: relation.id, + sceneId: relation.source_scene_id === sceneId ? relation.target_scene_id : relation.source_scene_id, + type: relation.type, + direction: relation.source_scene_id === sceneId ? 'target' : 'source', + })); +} + +async function curateRelationsEdit(sceneId, relations) { + if (!Array.isArray(relations)) { + throw new HttpError('Relations must be a list', 400); + } + + const curatedRelations = Object.values(Object.fromEntries(relations.map((relation) => { + const curatedRelation = { + sceneId: Number(relation?.sceneId), + type: relation?.type, + direction: relation?.direction, + }; + + return [getRelationKey(curatedRelation), curatedRelation]; + }))); + + if (curatedRelations.some((relation) => !Number.isInteger(relation.sceneId) || relation.sceneId === sceneId)) { + throw new HttpError('Relations must refer to a different scene', 400); + } + + if (curatedRelations.some((relation) => !['source', 'target'].includes(relation.direction))) { + throw new HttpError('Invalid relation direction', 400); + } + + const relatedSceneIds = Array.from(new Set(curatedRelations.map((relation) => relation.sceneId))); + + const [types, existingSceneIds] = await Promise.all([ + fetchRelationTypes(), + relatedSceneIds.length > 0 + ? knexOwner('releases').whereIn('id', relatedSceneIds).pluck('id') + : [], + ]); + + const invalidTypes = curatedRelations.filter((relation) => !types.includes(relation.type)).map((relation) => relation.type); + + if (invalidTypes.length > 0) { + throw new HttpError(`Invalid relation types: ${Array.from(new Set(invalidTypes)).join(', ')}`, 400); + } + + const missingSceneIds = relatedSceneIds.filter((relatedSceneId) => !existingSceneIds.includes(relatedSceneId)); + + if (missingSceneIds.length > 0) { + throw new HttpError(`Related scenes do not exist: ${missingSceneIds.join(', ')}`, 404); + } + + return curatedRelations; +} + const sqlImplied = ['scenes_stashed']; function curateOptions(options) { @@ -919,10 +1035,19 @@ export async function fetchSceneRevisions(revisionId, filters = {}, reqUser) { const tagIds = Array.from(new Set(revisions.flatMap((revision) => [...revision.base.tags, ...(revision.deltas.find((delta) => delta.key === 'tags')?.value || [])].map((tag) => tag.id)))); const movieIds = Array.from(new Set(revisions.flatMap((revision) => [...revision.base.movies, ...(revision.deltas.find((delta) => delta.key === 'movies')?.value || [])]))); - const [actors, tags, movies] = await Promise.all([ + // revisions from before relations existed have no base relations + const relationSceneIds = Array.from(new Set(revisions.flatMap((revision) => [ + ...(revision.base.relations || []), + ...(revision.deltas.find((delta) => delta.key === 'relations')?.value || []), + ].map((relation) => relation.sceneId)))); + + const [actors, tags, movies, relationScenes] = await Promise.all([ fetchActorsById(actorIds), fetchTagsById(tagIds), fetchMoviesById(movieIds), + relationSceneIds.length > 0 + ? knexOwner('releases').select('id', 'title', 'slug').whereIn('id', relationSceneIds) + : [], ]); const curatedRevisions = revisions.map((revision) => curateRevision(revision)); @@ -934,6 +1059,7 @@ export async function fetchSceneRevisions(revisionId, filters = {}, reqUser) { actors, tags, movies, + relationScenes, }; } @@ -1016,6 +1142,40 @@ async function applySceneMoviesDelta(sceneId, delta, trx) { } } +// apply the difference between the revision's base and delta, so relations added or removed since are left alone +async function applySceneRelationsDelta(revision, delta, trx) { + const baseKeys = new Set((revision.base.relations || []).map((relation) => getRelationKey(relation))); + const deltaKeys = new Set(delta.value.map((relation) => getRelationKey(relation))); + + const removedKeys = new Set(Array.from(baseKeys).filter((key) => !deltaKeys.has(key))); + const addedRelations = delta.value.filter((relation) => !baseKeys.has(getRelationKey(relation))); + + if (removedKeys.size > 0) { + const currentRelations = await fetchRevisionRelations(revision.scene_id, trx); + const removedIds = currentRelations.filter((relation) => removedKeys.has(getRelationKey(relation))).map((relation) => relation.id); + + if (removedIds.length > 0) { + await knexOwner('scenes_relations') + .whereIn('id', removedIds) + .delete() + .transacting(trx); + } + } + + if (addedRelations.length > 0) { + await knexOwner('scenes_relations') + .insert(addedRelations.map((relation) => ({ + source_scene_id: relation.direction === 'target' ? revision.scene_id : relation.sceneId, + target_scene_id: relation.direction === 'target' ? relation.sceneId : revision.scene_id, + type: relation.type, + created_by: revision.user_id, + }))) + .onConflict(['source_scene_id', 'target_scene_id', 'type']) + .ignore() + .transacting(trx); + } +} + async function applySceneDeleteDelta(sceneId, _delta, trx, reqUser) { if (!reqUser.abilities.some((ability) => ability.subject === 'scene' && ability.action === 'delete')) { throw new HttpError('You are not privileged to delete scenes', 400); @@ -1068,6 +1228,10 @@ async function applySceneRevision(revisionIds, reqUser) { return applySceneMoviesDelta(revision.scene_id, delta, trx); } + if (delta.key === 'relations') { + return applySceneRelationsDelta(revision, delta, trx); + } + return null; })); @@ -1140,6 +1304,7 @@ export async function createSceneRevision(sceneId, { edits, comment, apply }, re const [ [scene], openRevisions, + baseRelations, ] = await Promise.all([ fetchScenesById([sceneId], { reqUser, @@ -1149,6 +1314,7 @@ export async function createSceneRevision(sceneId, { edits, comment, apply }, re knexOwner('scenes_revisions') .where('user_id', reqUser.id) .whereNull('approved'), + fetchRevisionRelations(sceneId), ]); if (!scene) { @@ -1192,11 +1358,35 @@ export async function createSceneRevision(sceneId, { edits, comment, apply }, re return [key, values]; }).filter(Boolean)); + baseScene.relations = baseRelations.map((relation) => ({ + sceneId: relation.sceneId, + type: relation.type, + direction: relation.direction, + })); + + const curatedRelations = edits.relations + ? await curateRelationsEdit(scene.id, edits.relations) + : null; + const deltas = Object.entries(edits).map(([key, value]) => { if (key === 'delete') { return { key: 'delete' }; } + if (key === 'relations') { + if (!curatedRelations) { + return null; + } + + const baseKeys = new Set(baseScene.relations.map((relation) => getRelationKey(relation))); + + if (curatedRelations.length === baseKeys.size && curatedRelations.every((relation) => baseKeys.has(getRelationKey(relation)))) { + return null; + } + + return { key, value: curatedRelations }; + } + if (baseScene[key] === value || typeof value === 'undefined') { return null; } diff --git a/utils/relation-labels.js b/utils/relation-labels.js new file mode 100644 index 0000000..3954201 --- /dev/null +++ b/utils/relation-labels.js @@ -0,0 +1,20 @@ +// labels per scene relation type, keyed by which side of the relation the related scene is on +// relations point from the derivation or compilation (source) to the original or compilation part (target) +const relationLabels = { + rerelease: { target: 'Rerelease of', source: 'Rereleased as' }, + remaster: { target: 'Remaster of', source: 'Remastered as' }, + compilation: { target: 'Compilation features', source: 'Featured in compilation' }, +}; + +export function getRelationLabel(type, direction) { + if (relationLabels[type]?.[direction]) { + return relationLabels[type][direction]; + } + + // fallback for types added later without a label + return direction === 'target' + ? `${type.charAt(0).toUpperCase()}${type.slice(1)} of` + : `Has ${type}`; +} + +export default relationLabels;