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; }"
/>
+ { edits.relations = relations; }"
+ />
+
(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;