traxxx/src/stashes.js

236 lines
5.4 KiB
JavaScript
Raw Normal View History

2021-03-15 02:30:47 +00:00
'use strict';
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');
const logger = require('./logger')(__filename);
let lastActorsViewRefresh = 0;
2021-03-15 02:30:47 +00:00
function curateStash(stash) {
if (!stash) {
return null;
}
2021-03-15 02:30:47 +00:00
const curatedStash = {
id: stash.id,
name: stash.name,
slug: stash.slug,
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();
if (!stash) {
throw new HttpError('You are not authorized to access this stash', 403);
}
return curateStash(stash);
}
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-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
}
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);
}
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,
primary: false,
2021-03-20 22:03:13 +00:00
})
.delete();
if (removed === 0) {
throw new HttpError('Unable to remove this stash', 400);
}
}
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,
});
refreshActorsView();
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
});
return fetchStashes('scene', sceneId, sessionUser);
2021-03-15 02:30:47 +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,
});
return fetchStashes('movie', movieId, sessionUser);
}
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)
.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();
refreshActorsView();
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)
.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();
return fetchStashes('scene', sceneId, sessionUser);
2021-03-15 02:30:47 +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();
return fetchStashes('movie', movieId, sessionUser);
}
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,
stashMovie,
2021-03-17 01:09:34 +00:00
unstashScene,
2021-03-15 02:30:47 +00:00
unstashActor,
unstashMovie,
updateStash,
2021-03-15 02:30:47 +00:00
};