Added stash GraphQL mutations. Added movies to GraphQL queries. Moved key management to profile page, only for approved users.

This commit is contained in:
2025-03-31 06:14:56 +02:00
parent 09bba4fe1e
commit 1025285796
14 changed files with 721 additions and 235 deletions

View File

@@ -8,6 +8,8 @@ import {
reviewActorRevision,
} from '../actors.js';
import { fetchStashByUsernameAndSlug } from '../stashes.js';
export function curateActorsQuery(query) {
return {
query: query.q,
@@ -49,6 +51,7 @@ export const actorsSchema = `
extend type Query {
actors(
query: String
stash: String
limit: Int! = 30
page: Int! = 1
order: [String!]
@@ -115,14 +118,37 @@ function curateGraphqlActor(actor) {
};
}
export async function fetchActorsGraphql(query, _req) {
function getOrder(query) {
if (query.order) {
return query.order;
}
if (query.query) {
return ['results', 'desc'];
}
if (query.stash) {
return ['stashed', 'desc'];
}
return ['likes', 'desc'];
}
export async function fetchActorsGraphql(query, req) {
const stash = query.stash && req.user
? await fetchStashByUsernameAndSlug(req.user.id, query.stash, req.user)
: null;
const {
actors,
total,
} = await fetchActors(query, {
} = await fetchActors({
query: query.query,
stashId: stash?.id,
}, {
limit: query.limit,
page: query.page,
order: query.order,
order: getOrder(query),
aggregateCountries: false,
});

View File

@@ -13,6 +13,12 @@ import {
fetchScenesByIdGraphql,
} from './scenes.js';
import {
moviesSchema,
fetchMoviesGraphql,
fetchMoviesByIdGraphql,
} from './movies.js';
import {
entitiesSchema,
fetchEntitiesGraphql,
@@ -25,20 +31,39 @@ import {
fetchActorsByIdGraphql,
} from './actors.js';
import {
stashesSchema,
fetchUserStashesGraphql,
fetchStashGraphql,
createStashGraphql,
updateStashGraphql,
removeStashGraphql,
stashSceneGraphql,
unstashSceneGraphql,
stashActorGraphql,
unstashActorGraphql,
stashMovieGraphql,
unstashMovieGraphql,
} from './stashes.js';
import { verifyKey } from '../auth.js';
const schema = buildSchema(`
type Query {
movies(
limit: Int = 30
): ReleasesResult
_: Boolean
}
type Mutation {
_: Boolean
}
scalar Date
${scenesSchema}
${moviesSchema}
${actorsSchema}
${entitiesSchema}
${stashesSchema}
`);
const DateTimeScalar = new GraphQLScalarType({
@@ -71,6 +96,10 @@ export async function graphqlApi(req, res) {
await verifyKey(req.headers['api-user'], req.headers['api-key'], req);
req.user = { // eslint-disable-line no-param-reassign
id: Number(req.headers['api-user']),
};
const data = await graphql({
schema,
source: req.body.query,
@@ -80,9 +109,13 @@ export async function graphqlApi(req, res) {
DateScalar,
},
rootValue: {
// queries
scenes: async (query) => fetchScenesGraphql(query, req),
scene: async (query) => fetchScenesByIdGraphql(query, req),
scenesById: async (query) => fetchScenesByIdGraphql(query, req),
movies: async (query) => fetchMoviesGraphql(query, req),
movie: async (query) => fetchMoviesByIdGraphql(query, req),
moviesById: async (query) => fetchMoviesByIdGraphql(query, req),
actors: async (query) => fetchActorsGraphql(query, req),
actor: async (query, args, info) => fetchActorsByIdGraphql(query, req, info),
actorsById: async (query, args, info) => fetchActorsByIdGraphql(query, req, info),
@@ -90,6 +123,18 @@ export async function graphqlApi(req, res) {
entity: async (query, args, info) => fetchEntitiesByIdGraphql(query, req, info),
entitiesBySlug: async (query, args, info) => fetchEntitiesByIdGraphql(query, req, info),
entitiesById: async (query, args, info) => fetchEntitiesByIdGraphql(query, req, info),
stashes: async (query) => fetchUserStashesGraphql(query, req),
stash: async (query) => fetchStashGraphql(query, req),
// mutation
createStash: async (query) => createStashGraphql(query, req),
updateStash: async (query) => updateStashGraphql(query, req),
removeStash: async (query) => removeStashGraphql(query, req),
stashScene: async (query) => stashSceneGraphql(query, req),
unstashScene: async (query) => unstashSceneGraphql(query, req),
stashActor: async (query) => stashActorGraphql(query, req),
unstashActor: async (query) => unstashActorGraphql(query, req),
stashMovie: async (query) => stashMovieGraphql(query, req),
unstashMovie: async (query) => unstashMovieGraphql(query, req),
},
});

View File

@@ -1,8 +1,12 @@
import { stringify } from '@brillout/json-serializer/stringify'; /* eslint-disable-line import/extensions */
import { fetchMovies } from '../movies.js';
import { fetchMovies, fetchMoviesById } from '../movies.js';
import { fetchStashByUsernameAndSlug } from '../stashes.js';
import { parseActorIdentifier } from '../query.js';
import { getIdsBySlug } from '../cache.js';
import slugify from '../../utils/slugify.js';
import promiseProps from '../../utils/promise-props.js';
import { HttpError } from '../errors.js';
export async function curateMoviesQuery(query) {
return {
@@ -41,3 +45,112 @@ export async function fetchMoviesApi(req, res) {
total,
}));
}
export async function fetchMovieApi(req, res) {
const [movie] = await fetchMoviesById([Number(req.params.movieId)], { reqUser: req.user });
if (!movie) {
throw new HttpError(`No movie with ID ${req.params.movieId} found`, 404);
}
res.send(movie);
}
export const moviesSchema = `
extend type Query {
movies(
query: String
scope: String
entities: [String!]
actorIds: [String!]
tags: [String!]
stash: String
limit: Int! = 30
page: Int! = 1
): ReleasesResult
movie(
id: Int!
): Release
moviesById(
ids: [Int!]!
): [Release]
}
`;
function getScope(query) {
if (query.scope) {
return query.scope;
}
if (query.query) {
return 'results';
}
if (query.stash) {
return 'stashed';
}
return 'latest';
}
export async function fetchMoviesGraphql(query, req) {
const mainEntity = query.entities?.find((entity) => entity.charAt(0) !== '!');
const stash = query.stash && req.user
? await fetchStashByUsernameAndSlug(req.user.id, query.stash, req.user)
: null;
if (query.stash && !stash) {
throw new HttpError(`Could not find stash '${query.stash}'`, 404);
}
const {
tagIds,
notTagIds,
entityId,
notEntityIds,
} = await promiseProps({
tagIds: getIdsBySlug(query.tags?.filter((tag) => tag.charAt(0) !== '!'), 'tags'),
notTagIds: getIdsBySlug(query.tags?.filter((tag) => tag.charAt(0) === '!').map((tag) => tag.slice(1)).map((tag) => slugify(tag)), 'tags'),
entityId: getIdsBySlug([mainEntity], 'entities').then(([id]) => id),
notEntityIds: getIdsBySlug(query.entities?.filter((entity) => entity.charAt(0) === '!').map((entity) => entity.slice(1)), 'entities'),
});
const {
movies,
total,
} = await fetchMovies({
query: query.query, // query query query query
tagIds,
// not- currently not implemented by movies module, pass here anyway for when it is
notTagIds,
entityId,
notEntityIds,
actorIds: query.actorIds?.filter((actorId) => actorId.charAt(0) !== '!').map((actorId) => Number(actorId)),
notActorIds: query.actorIds?.filter((actorId) => actorId.charAt(0) === '!').map((actorId) => Number(actorId.slice(1))),
stashId: stash?.id,
scope: getScope(query),
isShowcased: null,
}, {
page: query.page || 1,
limit: query.limit || 30,
aggregate: false,
}, req.user);
return {
nodes: movies,
total,
};
}
export async function fetchMoviesByIdGraphql(query, req) {
const movies = await fetchMoviesById([].concat(query.id, query.ids).filter(Boolean), req.user);
if (query.ids) {
return movies;
}
return movies[0];
}

View File

@@ -9,6 +9,8 @@ import {
reviewSceneRevision,
} from '../scenes.js';
import { fetchStashByUsernameAndSlug } from '../stashes.js';
import { parseActorIdentifier } from '../query.js';
import { getIdsBySlug } from '../cache.js';
import slugify from '../../utils/slugify.js';
@@ -87,6 +89,8 @@ export const scenesSchema = `
entities: [String!]
actorIds: [String!]
tags: [String!]
movieId: Int
stash: String
limit: Int! = 30
page: Int! = 1
): ReleasesResult
@@ -152,9 +156,33 @@ export const scenesSchema = `
}
`;
function getScope(query) {
if (query.scope) {
return query.scope;
}
if (query.query) {
return 'results';
}
if (query.stash) {
return 'stashed';
}
return 'latest';
}
export async function fetchScenesGraphql(query, req) {
const mainEntity = query.entities?.find((entity) => entity.charAt(0) !== '!');
const stash = query.stash && req.user
? await fetchStashByUsernameAndSlug(req.user.id, query.stash, req.user)
: null;
if (query.stash && !stash) {
throw new HttpError(`Could not find stash '${query.stash}'`, 404);
}
const {
tagIds,
notTagIds,
@@ -183,9 +211,9 @@ export async function fetchScenesGraphql(query, req) {
notEntityIds,
actorIds: query.actorIds?.filter((actorId) => actorId.charAt(0) !== '!').map((actorId) => Number(actorId)),
notActorIds: query.actorIds?.filter((actorId) => actorId.charAt(0) === '!').map((actorId) => Number(actorId.slice(1))),
scope: query.query && !query.scope
? 'results'
: query.scope,
movieId: query.movieId,
stashId: stash?.id,
scope: getScope(query),
isShowcased: null,
}, {
page: query.page || 1,

View File

@@ -133,7 +133,7 @@ export default async function initServer() {
// API KEYS
router.get('/api/me/keys', fetchUserKeysApi);
router.post('/api/keys', createKeyApi);
router.post('/api/me/keys', createKeyApi);
router.delete('/api/me/keys/:keyIdentifier', removeUserKeyApi);
router.delete('/api/me/keys', flushUserKeysApi);

View File

@@ -11,66 +11,210 @@ import {
unstashScene,
unstashMovie,
updateStash,
fetchStashByUsernameAndSlug,
} from '../stashes.js';
export const stashesSchema = `
extend type Query {
stashes(username: String): [Stash]
stash(
username: String
stash: String!
): Stash
}
extend type Mutation {
createStash(
name: String!
isPublic: Boolean
): Stash
updateStash(
stash: String!
name: String
isPublic: Boolean
): Stash
removeStash(
stash: String!
): Stash
stashScene(
sceneId: Int!
stash: String!
): Stashed
unstashScene(
sceneId: Int!
stash: String!
): Stashed
stashActor(
actorId: Int!
stash: String!
): Stashed
unstashActor(
actorId: Int!
stash: String!
): Stashed
stashMovie(
movieId: Int!
stash: String!
): Stashed
unstashMovie(
movieId: Int!
stash: String!
): Stashed
}
type Stash {
id: Int
name: String
slug: String
isPublic: Boolean
isPrimary: Boolean
createdAt: Date
}
type Stashed {
id: Int
stashId: Int
sceneId: Int
actorId: Int
movieId: Int
serieId: Int
createdAt: Date
}
`;
export async function fetchUserStashesApi(req, res) {
const stashes = await fetchUserStashes(req.user.id, req.user);
res.send(stashes);
}
export async function fetchUserStashesGraphql(query, req) {
const stashes = await fetchUserStashes(query.username || req.userId, req.user);
return stashes;
}
export async function fetchStashGraphql(query, req) {
const stashes = await fetchStashByUsernameAndSlug(query.username || req.userId, query.stash, req.user);
return stashes;
}
export async function createStashApi(req, res) {
const stash = await createStash(req.body, req.user);
res.send(stash);
}
export async function createStashGraphql(query, req) {
const stash = await createStash(query, req.user);
return stash;
}
export async function updateStashApi(req, res) {
const stash = await updateStash(Number(req.params.stashId), req.body, req.user);
res.send(stash);
}
export async function updateStashGraphql(query, req) {
const stash = await updateStash(query.stash, query, req.user);
return stash;
}
export async function removeStashApi(req, res) {
await removeStash(Number(req.params.stashId), req.user);
res.status(204).send();
}
export async function removeStashGraphql(query, req) {
const removedId = await removeStash(query.stash, req.user);
return removedId;
}
export async function stashActorApi(req, res) {
const stashes = await stashActor(req.body.actorId, Number(req.params.stashId), req.user);
const stashed = await stashActor(req.body.actorId, Number(req.params.stashId), req.user);
res.send(stashes);
res.send(stashed);
}
export async function stashSceneApi(req, res) {
const stashes = await stashScene(req.body.sceneId, Number(req.params.stashId), req.user);
export async function stashActorGraphql(query, req) {
const stashed = await stashActor(query.actorId, query.stash, req.user);
res.send(stashes);
}
export async function stashMovieApi(req, res) {
const stashes = await stashMovie(req.body.movieId, Number(req.params.stashId), req.user);
res.send(stashes);
return stashed;
}
export async function unstashActorApi(req, res) {
const stashes = await unstashActor(Number(req.params.actorId), Number(req.params.stashId), req.user);
const unstashed = await unstashActor(Number(req.params.actorId), Number(req.params.stashId), req.user);
res.send(stashes);
res.send(unstashed);
}
export async function unstashActorGraphql(query, req) {
const stashes = await unstashActor(query.actorId, query.stash, req.user);
return stashes;
}
export async function stashSceneApi(req, res) {
const stashed = await stashScene(req.body.sceneId, Number(req.params.stashId), req.user);
res.send(stashed);
}
export async function stashSceneGraphql(query, req) {
const stashed = await stashScene(query.sceneId, query.stash, req.user);
return stashed;
}
export async function unstashSceneApi(req, res) {
const stashes = await unstashScene(Number(req.params.sceneId), Number(req.params.stashId), req.user);
const unstashed = await unstashScene(Number(req.params.sceneId), Number(req.params.stashId), req.user);
res.send(stashes);
res.send(unstashed);
}
export async function unstashSceneGraphql(query, req) {
const unstashed = await unstashScene(query.sceneId, query.stash, req.user);
return unstashed;
}
export async function stashMovieApi(req, res) {
const stashed = await stashMovie(req.body.movieId, Number(req.params.stashId), req.user);
res.send(stashed);
}
export async function stashMovieGraphql(query, req) {
const stashed = await stashMovie(query.movieId, query.stash, req.user);
return stashed;
}
export async function unstashMovieApi(req, res) {
const stashes = await unstashMovie(Number(req.params.movieId), Number(req.params.stashId), req.user);
const unstashed = await unstashMovie(Number(req.params.movieId), Number(req.params.stashId), req.user);
res.send(stashes);
res.send(unstashed);
}
export async function unstashMovieGraphql(query, req) {
const unstashed = await unstashMovie(query.movieId, query.stash, req.user);
return unstashed;
}
export const router = Router();