traxxx-web/src/web/actors.js

172 lines
3.5 KiB
JavaScript

import Router from 'express-promise-router';
import {
fetchActors,
fetchActorsById,
fetchActorRevisions,
createActorRevision,
reviewActorRevision,
} from '../actors.js';
export function curateActorsQuery(query) {
return {
query: query.q,
gender: query.gender,
age: query.age?.split(',').map((age) => Number(age)),
dateOfBirth: query.dob && new Date(query.dob),
dobType: ({ dob: 'dateOfBirth', bd: 'birthday' })[query.dobt] || 'birthday',
cup: query.cup?.split(','),
country: query.c,
naturalBoobs: query.nb,
height: query.height?.split(',').map((height) => Number(height)),
weight: query.weight?.split(',').map((weight) => Number(weight)),
requireAvatar: query.avatar,
stashId: Number(query.stashId) || null,
};
}
export async function fetchActorsApi(req, res) {
const {
actors,
countries,
limit,
total,
} = await fetchActors(curateActorsQuery(req.query), {
page: Number(req.query.page) || 1,
limit: Number(req.query.limit) || 120,
order: req.query.order?.split('.') || ['likes', 'desc'],
}, req.user);
res.send({
actors,
countries,
limit,
total,
});
}
export const actorsSchema = `
extend type Query {
actors(
query: String
limit: Int! = 30
page: Int! = 1
order: [String!]
): ActorsResult
actor(
id: Int!
): Actor
actorsById(
ids: [Int!]!
): [Actor]
}
type Country {
alpha2: String
name: String
}
type Location {
country: Country
city: String
state: String
}
type ActorsResult {
nodes: [Actor!]!
total: Int
}
type Actor {
id: Int!
name: String
slug: String
gender: String
dateOfBirth: Date
age: Int
ageFromBirth: Int
ageThen: Int
origin: Location
residence: Location
height: Int
bust: String
hip: Int
waist: Int
naturalBoobs: Boolean
eyes: String
hairColor: String
hasPiercings: Boolean
hasTattoos: Boolean
tattoos: String
piercings: String
scenes: Int
likes: Int
}
`;
function curateGraphqlActor(actor) {
return {
...actor,
age: actor.ageFromBirth,
height: actor.height?.metric,
weight: actor.weight?.metric,
};
}
export async function fetchActorsGraphql(query, _req) {
const {
actors,
total,
} = await fetchActors(query, {
limit: query.limit,
page: query.page,
order: query.order,
aggregateCountries: false,
});
return {
nodes: actors.map((actor) => curateGraphqlActor(actor)),
total,
};
}
export async function fetchActorsByIdGraphql(query, _req, _info) {
const actors = await fetchActorsById([].concat(query.id, query.ids).filter(Boolean));
const curatedActors = actors.map((actor) => curateGraphqlActor(actor));
if (query.ids) {
return curatedActors;
}
return curatedActors[0];
}
async function fetchActorRevisionsApi(req, res) {
const revisions = await fetchActorRevisions(Number(req.params.revisionId) || null, req.query, req.user);
res.send(revisions);
}
async function createActorRevisionApi(req, res) {
await createActorRevision(Number(req.body.actorId), req.body, req.user);
res.status(204).send();
}
async function reviewActorRevisionApi(req, res) {
await reviewActorRevision(Number(req.params.revisionId), req.body.isApproved, req.body, req.user);
res.status(204).send();
}
export const actorsRouter = Router();
actorsRouter.get('/api/actors', fetchActorsApi);
actorsRouter.get('/api/revisions/actors', fetchActorRevisionsApi);
actorsRouter.get('/api/revisions/actors/:revisionId', fetchActorRevisionsApi);
actorsRouter.post('/api/revisions/actors', createActorRevisionApi);
actorsRouter.post('/api/revisions/actors/:revisionId/reviews', reviewActorRevisionApi);