2023-12-30 05:29:53 +00:00
|
|
|
import { differenceInYears } from 'date-fns';
|
2024-01-10 01:00:38 +00:00
|
|
|
import { unit } from 'mathjs';
|
2023-12-30 05:29:53 +00:00
|
|
|
|
|
|
|
import knex from './knex.js';
|
|
|
|
import { searchApi } from './manticore.js';
|
|
|
|
import { HttpError } from './errors.js';
|
2023-12-31 02:02:03 +00:00
|
|
|
import { fetchCountriesByAlpha2 } from './countries.js';
|
2023-12-30 05:29:53 +00:00
|
|
|
|
|
|
|
export function curateActor(actor, context = {}) {
|
|
|
|
return {
|
|
|
|
id: actor.id,
|
|
|
|
slug: actor.slug,
|
|
|
|
name: actor.name,
|
|
|
|
gender: actor.gender,
|
|
|
|
age: actor.age,
|
|
|
|
dateOfBirth: actor.date_of_birth,
|
|
|
|
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),
|
2024-01-10 01:00:38 +00:00
|
|
|
bust: actor.bust,
|
|
|
|
cup: actor.cup,
|
|
|
|
waist: actor.waist,
|
|
|
|
hip: actor.hip,
|
|
|
|
naturalBoobs: actor.naturalBoobs,
|
|
|
|
height: actor.height && {
|
|
|
|
metric: actor.height,
|
|
|
|
imperial: unit(actor.height, 'cm').splitUnit(['ft', 'in']).map((value) => Math.round(value.toNumber())),
|
2023-12-30 05:29:53 +00:00
|
|
|
},
|
2024-01-10 01:00:38 +00:00
|
|
|
weight: actor.weight && {
|
|
|
|
metric: actor.weight,
|
|
|
|
imperial: Math.round(unit(actor.weight, 'kg').toNumeric('lbs')),
|
|
|
|
},
|
|
|
|
eyes: actor.eyes,
|
|
|
|
hairColor: actor.hairColor,
|
|
|
|
hasTattoos: actor.has_tattoos,
|
|
|
|
tattoos: actor.tattoos,
|
|
|
|
hasPiercings: actor.has_piercings,
|
|
|
|
piercings: actor.piercings,
|
|
|
|
origin: {
|
|
|
|
country: actor.birth_country_alpha2 && {
|
|
|
|
alpha2: actor.birth_country_alpha2,
|
|
|
|
name: actor.birth_country_name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
residence: {
|
|
|
|
country: actor.residence_country_alpha2 && {
|
|
|
|
alpha2: actor.residence_country_alpha2,
|
|
|
|
name: actor.residence_country_name,
|
|
|
|
},
|
2023-12-30 05:29:53 +00:00
|
|
|
},
|
2024-01-05 23:30:30 +00:00
|
|
|
avatar: actor.avatar && {
|
|
|
|
id: actor.avatar.id,
|
|
|
|
path: actor.avatar.path,
|
|
|
|
thumbnail: actor.avatar.thumbnail,
|
|
|
|
lazy: actor.avatar.lazy,
|
|
|
|
isS3: actor.avatar.is_s3,
|
|
|
|
},
|
2024-01-10 01:00:38 +00:00
|
|
|
createdAt: actor.created_at,
|
|
|
|
updatedAt: actor.updated_at,
|
2024-01-05 23:30:30 +00:00
|
|
|
likes: actor.stashed,
|
2024-01-07 22:44:33 +00:00
|
|
|
...context.append?.[actor.id],
|
2023-12-30 05:29:53 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function sortActorsByGender(actors) {
|
|
|
|
if (!actors) {
|
|
|
|
return actors;
|
|
|
|
}
|
|
|
|
|
|
|
|
const alphaActors = actors.sort((actorA, actorB) => actorA.name.localeCompare(actorB.name, 'en'));
|
|
|
|
const genderActors = ['transsexual', 'female', 'male', undefined].flatMap((gender) => alphaActors.filter((actor) => actor.gender === gender));
|
|
|
|
|
|
|
|
return genderActors;
|
|
|
|
}
|
|
|
|
|
2024-01-07 05:13:40 +00:00
|
|
|
export async function fetchActorsById(actorIds, options = {}) {
|
2023-12-30 05:29:53 +00:00
|
|
|
const [actors] = await Promise.all([
|
2024-01-05 23:30:30 +00:00
|
|
|
knex('actors_meta')
|
|
|
|
.select('actors_meta.*')
|
2024-01-07 05:13:40 +00:00
|
|
|
.whereIn('actors_meta.id', actorIds)
|
|
|
|
.modify((builder) => {
|
|
|
|
if (options.order) {
|
|
|
|
builder.orderBy(...options.order);
|
|
|
|
}
|
|
|
|
}),
|
2023-12-30 05:29:53 +00:00
|
|
|
]);
|
|
|
|
|
2024-01-07 05:13:40 +00:00
|
|
|
if (options.order) {
|
2024-01-07 22:44:33 +00:00
|
|
|
return actors.map((actorEntry) => curateActor(actorEntry, { append: options.append }));
|
2024-01-07 05:13:40 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 00:49:16 +00:00
|
|
|
const curatedActors = actorIds.map((actorId) => {
|
2023-12-30 05:29:53 +00:00
|
|
|
const actor = actors.find((actorEntry) => actorEntry.id === actorId);
|
|
|
|
|
|
|
|
if (!actor) {
|
2024-01-08 01:21:57 +00:00
|
|
|
console.warn(`Can't match actor ${actorId}`);
|
2023-12-30 05:29:53 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-01-07 22:44:33 +00:00
|
|
|
return curateActor(actor, { append: options.append });
|
2023-12-30 05:29:53 +00:00
|
|
|
}).filter(Boolean);
|
2024-01-04 00:49:16 +00:00
|
|
|
|
|
|
|
return curatedActors;
|
2023-12-30 05:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function curateOptions(options) {
|
2024-01-04 00:49:16 +00:00
|
|
|
if (options?.limit > 120) {
|
|
|
|
throw new HttpError('Limit must be <= 120', 400);
|
2023-12-30 05:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
page: options?.page || 1,
|
|
|
|
limit: options?.limit || 30,
|
|
|
|
requireAvatar: options?.requireAvatar || false,
|
2024-01-05 23:30:30 +00:00
|
|
|
order: [options.order?.[0] || 'name', options.order?.[1] || 'asc'],
|
2023-12-30 05:29:53 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildQuery(filters) {
|
|
|
|
const query = {
|
|
|
|
bool: {
|
|
|
|
must: [],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-12-31 02:02:03 +00:00
|
|
|
const expressions = {
|
|
|
|
age: 'if(date_of_birth, floor((now() - date_of_birth) / 31556952), 0)',
|
|
|
|
};
|
|
|
|
|
2023-12-30 05:29:53 +00:00
|
|
|
if (filters.query) {
|
|
|
|
query.bool.must.push({
|
|
|
|
match: {
|
|
|
|
name: filters.query,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-03 01:52:41 +00:00
|
|
|
['gender', 'country'].forEach((attribute) => {
|
|
|
|
if (filters[attribute]) {
|
|
|
|
query.bool.must.push({
|
|
|
|
equals: {
|
|
|
|
[attribute]: filters[attribute],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2023-12-31 02:02:03 +00:00
|
|
|
|
2023-12-30 05:29:53 +00:00
|
|
|
['age', 'height', 'weight'].forEach((attribute) => {
|
|
|
|
if (filters[attribute]) {
|
|
|
|
query.bool.must.push({
|
|
|
|
range: {
|
|
|
|
[attribute]: {
|
|
|
|
gte: filters[attribute][0],
|
|
|
|
lte: filters[attribute][1],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-01-03 01:52:41 +00:00
|
|
|
if (filters.dateOfBirth && filters.dobType === 'dateOfBirth') {
|
|
|
|
query.bool.must.push({
|
|
|
|
equals: {
|
|
|
|
date_of_birth: Math.floor(filters.dateOfBirth.getTime() / 1000),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filters.dateOfBirth && filters.dobType === 'birthday') {
|
|
|
|
expressions.month_of_birth = 'month(date_of_birth)';
|
|
|
|
expressions.day_of_birth = 'day(date_of_birth)';
|
|
|
|
|
|
|
|
const month = filters.dateOfBirth.getMonth() + 1;
|
|
|
|
const day = filters.dateOfBirth.getDate();
|
|
|
|
|
|
|
|
query.bool.must.push({
|
|
|
|
bool: {
|
|
|
|
must: [
|
|
|
|
{
|
|
|
|
equals: {
|
|
|
|
month_of_birth: month,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
equals: {
|
|
|
|
day_of_birth: day,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-31 02:02:03 +00:00
|
|
|
if (filters.cup) {
|
|
|
|
expressions.cup_in_range = `regex(cup, '^[${filters.cup[0]}-${filters.cup[1]}]')`;
|
|
|
|
|
|
|
|
query.bool.must.push({
|
|
|
|
equals: {
|
|
|
|
cup_in_range: 1,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof filters.naturalBoobs === 'boolean') {
|
|
|
|
query.bool.must.push({
|
|
|
|
equals: {
|
|
|
|
natural_boobs: filters.naturalBoobs ? 2 : 1, // manticore boolean does not support null, so 0 = null, 1 = false (enhanced), 2 = true (natural)
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-30 05:29:53 +00:00
|
|
|
if (filters.requireAvatar) {
|
|
|
|
query.bool.must.push({
|
|
|
|
equals: {
|
|
|
|
has_avatar: 1,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-31 02:02:03 +00:00
|
|
|
return { query, expressions };
|
2023-12-30 05:29:53 +00:00
|
|
|
}
|
|
|
|
|
2024-01-05 23:30:30 +00:00
|
|
|
const sortMap = {
|
|
|
|
likes: 'stashed',
|
|
|
|
scenes: 'scenes',
|
2024-01-08 01:21:57 +00:00
|
|
|
relevance: '_score',
|
2024-01-05 23:30:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function getSort(order) {
|
|
|
|
if (order[0] === 'name') {
|
|
|
|
return [{
|
|
|
|
slug: order[1],
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
[sortMap[order[0]]]: order[1],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
slug: 'asc', // sort by name where primary order is equal
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-12-30 05:29:53 +00:00
|
|
|
export async function fetchActors(filters, rawOptions) {
|
|
|
|
const options = curateOptions(rawOptions);
|
2023-12-31 02:02:03 +00:00
|
|
|
const { query, expressions } = buildQuery(filters);
|
2023-12-30 05:29:53 +00:00
|
|
|
|
|
|
|
const result = await searchApi.search({
|
|
|
|
index: 'actors',
|
|
|
|
query,
|
2023-12-31 02:02:03 +00:00
|
|
|
expressions,
|
2023-12-30 05:29:53 +00:00
|
|
|
limit: options.limit,
|
|
|
|
offset: (options.page - 1) * options.limit,
|
2024-01-08 01:21:57 +00:00
|
|
|
sort: getSort(options.order, filters),
|
2023-12-31 02:02:03 +00:00
|
|
|
aggs: {
|
|
|
|
countries: {
|
|
|
|
terms: {
|
|
|
|
field: 'country',
|
|
|
|
size: 300,
|
|
|
|
},
|
|
|
|
sort: [{ country: { order: 'asc' } }],
|
|
|
|
},
|
|
|
|
},
|
2023-12-30 05:29:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const actorIds = result.hits.hits.map((hit) => Number(hit._id));
|
2023-12-31 02:02:03 +00:00
|
|
|
const [actors, countries] = await Promise.all([
|
|
|
|
fetchActorsById(actorIds),
|
|
|
|
fetchCountriesByAlpha2(result.aggregations.countries.buckets.map((bucket) => bucket.key)),
|
|
|
|
]);
|
2023-12-30 05:29:53 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
actors,
|
2023-12-31 02:02:03 +00:00
|
|
|
countries,
|
2023-12-30 05:29:53 +00:00
|
|
|
total: result.hits.total,
|
|
|
|
limit: options.limit,
|
|
|
|
};
|
|
|
|
}
|