Improved actor editing units.

This commit is contained in:
2024-10-30 02:25:19 +01:00
parent 2293e10af7
commit 855a698eae
3 changed files with 262 additions and 127 deletions

View File

@@ -59,6 +59,12 @@ export function curateActor(actor, context = {}) {
name: actor.name,
gender: actor.gender,
age: actor.age,
ethnicity: actor.ethnicity,
entity: actor.entity && {
id: actor.entity.id,
slug: actor.entity.slug,
name: actor.entity.name,
},
...Object.fromEntries(Object.entries(keyMap).map(([key, entryKey]) => [key, actor[entryKey]])),
ageFromBirth: actor.date_of_birth && differenceInYears(Date.now(), actor.date_of_birth),
ageThen: context.sceneDate && actor.date_of_birth && differenceInYears(context.sceneDate, actor.date_of_birth),
@@ -166,11 +172,13 @@ export async function fetchActorsById(actorIds, options = {}, reqUser) {
knex.raw('COALESCE(birth_countries.alias, birth_countries.name) as birth_country_name'),
'residence_countries.alpha2 as residence_country_alpha2',
knex.raw('COALESCE(residence_countries.alias, residence_countries.name) as residence_country_name'),
knex.raw('row_to_json(entities) as entity'),
)
.leftJoin('actors_meta', 'actors_meta.actor_id', 'actors.id')
.leftJoin('countries as birth_countries', 'birth_countries.alpha2', 'actors.birth_country_alpha2')
.leftJoin('countries as residence_countries', 'residence_countries.alpha2', 'actors.residence_country_alpha2')
.leftJoin('media as avatars', 'avatars.id', 'actors.avatar_media_id')
.leftJoin('entities', 'entities.id', 'actors.entity_id')
.whereIn('actors.id', actorIds)
.modify((builder) => {
if (options.order) {
@@ -199,8 +207,8 @@ export async function fetchActorsById(actorIds, options = {}, reqUser) {
)
.whereIn('actor_id', actorIds)
.leftJoin('media', 'media.id', 'actors_avatars.media_id')
.groupBy('media.id', 'actors_avatars.actor_id', 'actors_avatars.created_at')
.orderBy('actors_avatars.created_at', 'desc'),
.groupBy('media.id', 'actors_avatars.actor_id')
.orderBy(knex.raw('max(actors_avatars.created_at)'), 'desc'),
reqUser
? knex('stashes_actors')
.leftJoin('stashes', 'stashes.id', 'stashes_actors.stash_id')
@@ -549,34 +557,6 @@ async function fetchMainProfile(actorId, wasCreated = false) {
return fetchMainProfile(actorId, true);
}
/*
async function applyMainProfile(actorId) {
const [actorEntry, mainProfile] = await Promise.all([
knex('actors')
.where('id', actorId)
.first(),
fetchMainProfile(actorId),
]);
if (!actorEntry) {
throw new HttpError('No actor profile found to apply main profile to', 404);
}
const preservedKeys = ['id'];
// we start iterating from the actor entry so we don't include keys that are not yet supported by the actors table
const mergedProfile = Object.fromEntries(Object.entries(actorEntry)
.filter(([key]) => Object.hasOwn(mainProfile, key))
.map(([key, value]) => [key, mainProfile[key] === null || preservedKeys.includes(key)
? value
: mainProfile[key]]));
await knex('actors')
.where('id', actorId)
.update(mergedProfile);
}
*/
async function applyActorRevision(revisionIds, reqUser) {
const revisions = await knex('actors_revisions')
.whereIn('id', revisionIds)
@@ -600,6 +580,7 @@ async function applyActorRevision(revisionIds, reqUser) {
'residenceCountry',
'residenceState',
'residenceCity',
'ethnicity',
'height',
'weight',
'bust',
@@ -755,6 +736,27 @@ function convertFigure(domain = 'cup', rawValue, units) {
return usValue;
}
function convertHeight(height, units) {
if (units === 'metric' || !Array.isArray(height)) {
return Number(height) || null;
}
if (height.length !== 2) {
return null;
}
// 12 inches in a foot
return Math.round(((height[0] * 12) + height[1]) * 2.54);
}
function convertWeight(weight, units) {
if (units === 'imperial') {
return Math.round(unit(weight, 'lbs').toNumeric('kg'));
}
return Number(weight) || null;
}
export async function createActorRevision(actorId, {
edits,
comment,
@@ -819,15 +821,9 @@ export async function createActorRevision(actorId, {
return null;
}
if (Array.isArray(value)) {
const valueSet = new Set(value);
const baseSet = new Set(baseActor[key]);
if (valueSet.size === baseSet.size && baseActor[key].every((id) => valueSet.has(id))) {
return null;
}
return { key, value: Array.from(valueSet) };
if (['originPlace', 'residencePlay'].includes(key)) {
console.log(key, value);
throw new Error('must be converted first!');
}
if (['cup', 'bust', 'waist', 'hip'].includes(key)) {
@@ -844,9 +840,49 @@ export async function createActorRevision(actorId, {
};
}
if (['height'].includes(key)) {
const convertedValue = convertHeight(value, options.sizeUnits);
if (baseActor[key] === convertedValue) {
return null;
}
const conversionComment = !value || convertedValue === value
? null
: `${key} converted from ${value[0]} in ${value[1]} ft to ${convertedValue} cm`;
return {
key,
value: convertedValue,
comment: conversionComment,
};
}
if (['weight'].includes(key)) {
const convertedValue = convertWeight(value, options.sizeUnits);
if (baseActor[key] === convertedValue) {
return null;
}
const conversionComment = !value || convertedValue === value
? null
: `${key} converted from ${value} lbs to ${convertedValue} kg`;
return {
key,
value: convertedValue,
comment: conversionComment,
};
}
if (['penisLength', 'penisGirth'].includes(key) && options.penisUnits === 'imperial') {
const convertedValue = Math.round(convert(value, 'inches').to('cm'));
if (baseActor[key] === convertedValue) {
return null;
}
return {
key,
value: convertedValue,
@@ -854,6 +890,17 @@ export async function createActorRevision(actorId, {
};
}
if (Array.isArray(value)) {
const valueSet = new Set(value);
const baseSet = new Set(baseActor[key]);
if (valueSet.size === baseSet.size && baseActor[key].every((id) => valueSet.has(id))) {
return null;
}
return { key, value: Array.from(valueSet) };
}
return { key, value };
}).filter(Boolean);