2021-03-15 02:30:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
2024-01-25 00:15:42 +00:00
|
|
|
const config = require('config');
|
|
|
|
|
2021-03-15 02:30:47 +00:00
|
|
|
const knex = require('./knex');
|
|
|
|
const { HttpError } = require('./errors');
|
2021-03-20 01:49:17 +00:00
|
|
|
const slugify = require('./utils/slugify');
|
2024-01-25 00:15:42 +00:00
|
|
|
const logger = require('./logger')(__filename);
|
|
|
|
|
|
|
|
let lastActorsViewRefresh = 0;
|
2021-03-15 02:30:47 +00:00
|
|
|
|
|
|
|
function curateStash(stash) {
|
2021-03-17 04:11:17 +00:00
|
|
|
if (!stash) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-03-15 02:30:47 +00:00
|
|
|
const curatedStash = {
|
|
|
|
id: stash.id,
|
|
|
|
name: stash.name,
|
|
|
|
slug: stash.slug,
|
2021-03-21 02:23:58 +00:00
|
|
|
primary: stash.primary,
|
2021-03-15 02:30:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return curatedStash;
|
|
|
|
}
|
|
|
|
|
2021-03-20 01:49:17 +00:00
|
|
|
function curateStashEntry(stash, user) {
|
|
|
|
const curatedStashEntry = {
|
|
|
|
user_id: user.id,
|
|
|
|
name: stash.name,
|
|
|
|
slug: slugify(stash.name),
|
|
|
|
public: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
return curatedStashEntry;
|
|
|
|
}
|
|
|
|
|
2021-03-15 02:30:47 +00:00
|
|
|
async function fetchStash(stashId, sessionUser) {
|
|
|
|
if (!sessionUser) {
|
|
|
|
throw new HttpError('You are not authenthicated', 401);
|
|
|
|
}
|
|
|
|
|
|
|
|
const stash = await knex('stashes')
|
|
|
|
.where({
|
|
|
|
id: stashId,
|
|
|
|
user_id: sessionUser.id,
|
|
|
|
})
|
|
|
|
.first();
|
|
|
|
|
2021-03-20 01:03:30 +00:00
|
|
|
if (!stash) {
|
|
|
|
throw new HttpError('You are not authorized to access this stash', 403);
|
|
|
|
}
|
|
|
|
|
|
|
|
return curateStash(stash);
|
|
|
|
}
|
|
|
|
|
2021-03-21 02:23:58 +00:00
|
|
|
async function fetchStashes(domain, itemId, sessionUser) {
|
|
|
|
const stashes = await knex(`stashes_${domain}s`)
|
|
|
|
.select('stashes.*')
|
|
|
|
.where({
|
|
|
|
[`${domain}_id`]: itemId,
|
|
|
|
user_id: sessionUser.id,
|
|
|
|
})
|
|
|
|
.leftJoin('stashes', 'stashes.id', `stashes_${domain}s.stash_id`);
|
|
|
|
|
2021-11-20 22:59:15 +00:00
|
|
|
return stashes.map((stash) => curateStash(stash));
|
2021-03-21 02:23:58 +00:00
|
|
|
}
|
|
|
|
|
2021-03-20 01:49:17 +00:00
|
|
|
async function createStash(newStash, sessionUser) {
|
|
|
|
if (!sessionUser) {
|
|
|
|
throw new HttpError('You are not authenthicated', 401);
|
|
|
|
}
|
|
|
|
|
2023-06-08 02:19:37 +00:00
|
|
|
try {
|
|
|
|
const stash = await knex('stashes')
|
|
|
|
.insert(curateStashEntry(newStash, sessionUser))
|
|
|
|
.returning('*');
|
|
|
|
|
|
|
|
return curateStash(stash);
|
|
|
|
} catch (error) {
|
|
|
|
if (error.routine === '_bt_check_unique') {
|
|
|
|
throw new HttpError('Stash name should be unique', 409);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
2021-03-20 01:49:17 +00:00
|
|
|
}
|
|
|
|
|
2021-03-20 01:03:30 +00:00
|
|
|
async function updateStash(stashId, newStash, sessionUser) {
|
|
|
|
if (!sessionUser) {
|
|
|
|
throw new HttpError('You are not authenthicated', 401);
|
|
|
|
}
|
|
|
|
|
|
|
|
const stash = await knex('stashes')
|
|
|
|
.where({
|
|
|
|
id: stashId,
|
|
|
|
user_id: sessionUser.id,
|
|
|
|
})
|
|
|
|
.update(newStash)
|
|
|
|
.returning('*');
|
|
|
|
|
2021-03-15 02:30:47 +00:00
|
|
|
if (!stash) {
|
|
|
|
throw new HttpError('You are not authorized to modify this stash', 403);
|
|
|
|
}
|
|
|
|
|
2021-03-20 01:03:30 +00:00
|
|
|
return curateStash(stash);
|
2021-03-15 02:30:47 +00:00
|
|
|
}
|
|
|
|
|
2021-03-20 22:03:13 +00:00
|
|
|
async function removeStash(stashId, sessionUser) {
|
|
|
|
if (!sessionUser) {
|
|
|
|
throw new HttpError('You are not authenthicated', 401);
|
|
|
|
}
|
|
|
|
|
|
|
|
const removed = await knex('stashes')
|
|
|
|
.where({
|
|
|
|
id: stashId,
|
|
|
|
user_id: sessionUser.id,
|
2021-03-21 02:23:58 +00:00
|
|
|
primary: false,
|
2021-03-20 22:03:13 +00:00
|
|
|
})
|
|
|
|
.delete();
|
|
|
|
|
|
|
|
if (removed === 0) {
|
|
|
|
throw new HttpError('Unable to remove this stash', 400);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-25 00:15:42 +00:00
|
|
|
async function refreshActorsView() {
|
|
|
|
if (new Date() - lastActorsViewRefresh > config.stashes.viewRefreshCooldown * 60000) {
|
|
|
|
// don't refresh actors view more than once an hour
|
|
|
|
lastActorsViewRefresh = new Date();
|
|
|
|
|
|
|
|
logger.debug('Refreshing actors view');
|
|
|
|
|
|
|
|
return knex.schema.refreshMaterializedView('actors_meta');
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.silly('Skipping actors view refresh');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-15 02:30:47 +00:00
|
|
|
async function stashActor(actorId, stashId, sessionUser) {
|
|
|
|
const stash = await fetchStash(stashId, sessionUser);
|
|
|
|
|
|
|
|
await knex('stashes_actors')
|
|
|
|
.insert({
|
|
|
|
stash_id: stash.id,
|
|
|
|
actor_id: actorId,
|
|
|
|
});
|
2021-03-21 02:23:58 +00:00
|
|
|
|
2024-01-25 00:15:42 +00:00
|
|
|
refreshActorsView();
|
|
|
|
|
2021-03-21 02:23:58 +00:00
|
|
|
return fetchStashes('actor', actorId, sessionUser);
|
2021-03-15 02:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function stashScene(sceneId, stashId, sessionUser) {
|
|
|
|
const stash = await fetchStash(stashId, sessionUser);
|
|
|
|
|
|
|
|
await knex('stashes_scenes')
|
|
|
|
.insert({
|
|
|
|
stash_id: stash.id,
|
2021-03-17 01:09:34 +00:00
|
|
|
scene_id: sceneId,
|
2021-03-15 02:30:47 +00:00
|
|
|
});
|
2021-03-21 02:23:58 +00:00
|
|
|
|
|
|
|
return fetchStashes('scene', sceneId, sessionUser);
|
2021-03-15 02:30:47 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 04:11:17 +00:00
|
|
|
async function stashMovie(movieId, stashId, sessionUser) {
|
|
|
|
const stash = await fetchStash(stashId, sessionUser);
|
|
|
|
|
|
|
|
await knex('stashes_movies')
|
|
|
|
.insert({
|
|
|
|
stash_id: stash.id,
|
|
|
|
movie_id: movieId,
|
|
|
|
});
|
2021-03-21 02:23:58 +00:00
|
|
|
|
|
|
|
return fetchStashes('movie', movieId, sessionUser);
|
2021-03-17 04:11:17 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 02:30:47 +00:00
|
|
|
async function unstashActor(actorId, stashId, sessionUser) {
|
|
|
|
await knex
|
2021-03-17 01:09:34 +00:00
|
|
|
.from('stashes_actors AS deletable')
|
|
|
|
.where('deletable.actor_id', actorId)
|
|
|
|
.where('deletable.stash_id', stashId)
|
2021-03-17 04:11:17 +00:00
|
|
|
.whereExists(knex('stashes_actors') // verify user owns this stash, complimentary to row-level security
|
2021-03-15 02:30:47 +00:00
|
|
|
.leftJoin('stashes', 'stashes.id', 'stashes_actors.stash_id')
|
2021-03-17 01:09:34 +00:00
|
|
|
.where('stashes_actors.stash_id', knex.raw('deletable.stash_id'))
|
|
|
|
.where('stashes.user_id', sessionUser.id))
|
|
|
|
.delete();
|
2021-03-21 02:23:58 +00:00
|
|
|
|
2024-01-25 00:15:42 +00:00
|
|
|
refreshActorsView();
|
|
|
|
|
2021-03-21 02:23:58 +00:00
|
|
|
return fetchStashes('actor', actorId, sessionUser);
|
2021-03-17 01:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function unstashScene(sceneId, stashId, sessionUser) {
|
|
|
|
await knex
|
|
|
|
.from('stashes_scenes AS deletable')
|
|
|
|
.where('deletable.scene_id', sceneId)
|
|
|
|
.where('deletable.stash_id', stashId)
|
2021-03-17 04:11:17 +00:00
|
|
|
.whereExists(knex('stashes_scenes') // verify user owns this stash, complimentary to row-level security
|
2021-03-17 01:09:34 +00:00
|
|
|
.leftJoin('stashes', 'stashes.id', 'stashes_scenes.stash_id')
|
|
|
|
.where('stashes_scenes.stash_id', knex.raw('deletable.stash_id'))
|
|
|
|
.where('stashes.user_id', sessionUser.id))
|
2021-03-15 02:30:47 +00:00
|
|
|
.delete();
|
2021-03-21 02:23:58 +00:00
|
|
|
|
|
|
|
return fetchStashes('scene', sceneId, sessionUser);
|
2021-03-15 02:30:47 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 04:11:17 +00:00
|
|
|
async function unstashMovie(movieId, stashId, sessionUser) {
|
|
|
|
await knex
|
|
|
|
.from('stashes_movies AS deletable')
|
|
|
|
.where('deletable.movie_id', movieId)
|
|
|
|
.where('deletable.stash_id', stashId)
|
|
|
|
.whereExists(knex('stashes_movies') // verify user owns this stash, complimentary to row-level security
|
|
|
|
.leftJoin('stashes', 'stashes.id', 'stashes_movies.stash_id')
|
|
|
|
.where('stashes_movies.stash_id', knex.raw('deletable.stash_id'))
|
|
|
|
.where('stashes.user_id', sessionUser.id))
|
|
|
|
.delete();
|
2021-03-21 02:23:58 +00:00
|
|
|
|
|
|
|
return fetchStashes('movie', movieId, sessionUser);
|
2021-03-17 04:11:17 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 02:30:47 +00:00
|
|
|
module.exports = {
|
2021-03-20 01:49:17 +00:00
|
|
|
createStash,
|
2021-03-15 02:30:47 +00:00
|
|
|
curateStash,
|
2021-03-20 22:03:13 +00:00
|
|
|
removeStash,
|
2021-03-15 02:30:47 +00:00
|
|
|
stashActor,
|
|
|
|
stashScene,
|
2021-03-17 04:11:17 +00:00
|
|
|
stashMovie,
|
2021-03-17 01:09:34 +00:00
|
|
|
unstashScene,
|
2021-03-15 02:30:47 +00:00
|
|
|
unstashActor,
|
2021-03-17 04:11:17 +00:00
|
|
|
unstashMovie,
|
2021-03-20 01:03:30 +00:00
|
|
|
updateStash,
|
2021-03-15 02:30:47 +00:00
|
|
|
};
|