Added filterable stash pages.
This commit is contained in:
110
src/stashes.js
110
src/stashes.js
@@ -1,6 +1,7 @@
|
||||
import config from 'config';
|
||||
|
||||
import { knexOwner as knex } from './knex.js';
|
||||
import { indexApi } from './manticore.js';
|
||||
import { HttpError } from './errors.js';
|
||||
import slugify from './utils/slugify.js';
|
||||
import initLogger from './logger.js';
|
||||
@@ -9,7 +10,7 @@ const logger = initLogger();
|
||||
|
||||
let lastActorsViewRefresh = 0;
|
||||
|
||||
export function curateStash(stash) {
|
||||
export function curateStash(stash, assets = {}) {
|
||||
if (!stash) {
|
||||
return null;
|
||||
}
|
||||
@@ -24,6 +25,12 @@ export function curateStash(stash) {
|
||||
stashedScenes: stash.stashed_scenes || null,
|
||||
stashedMovies: stash.stashed_movies || null,
|
||||
stashedActors: stash.stashed_actors || null,
|
||||
user: assets.user ? {
|
||||
id: assets.user.id,
|
||||
username: assets.user.username,
|
||||
avatar: `/media/avatars/${assets.user.id}_${assets.user.username}.png`,
|
||||
createdAt: assets.user.created_at,
|
||||
} : null,
|
||||
};
|
||||
|
||||
return curatedStash;
|
||||
@@ -40,23 +47,39 @@ function curateStashEntry(stash, user) {
|
||||
return curatedStashEntry;
|
||||
}
|
||||
|
||||
export async function fetchStash(stashId, sessionUser) {
|
||||
if (!sessionUser) {
|
||||
throw new HttpError('You are not authenthicated', 401);
|
||||
function verifyStashAccess(stash, sessionUser) {
|
||||
if (!stash || (!stash.public && stash.user_id !== sessionUser?.id)) {
|
||||
throw new HttpError('This stash does not exist, or you are not allowed access.', 404);
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchStashById(stashId, sessionUser) {
|
||||
const stash = await knex('stashes')
|
||||
.where('id', stashId)
|
||||
.first();
|
||||
|
||||
verifyStashAccess(stash, sessionUser);
|
||||
|
||||
return curateStash(stash);
|
||||
}
|
||||
|
||||
export async function fetchStashByUsernameAndSlug(username, stashSlug, sessionUser) {
|
||||
const user = await knex('users').where('username', username).first();
|
||||
|
||||
if (!user) {
|
||||
throw new HttpError('This user does not exist.', 404);
|
||||
}
|
||||
|
||||
const stash = await knex('stashes')
|
||||
.where({
|
||||
id: stashId,
|
||||
user_id: sessionUser.id,
|
||||
})
|
||||
.select('stashes.*', 'stashes_meta.*')
|
||||
.leftJoin('stashes_meta', 'stashes_meta.stash_id', 'stashes.id')
|
||||
.where('slug', stashSlug)
|
||||
.where('user_id', user.id)
|
||||
.first();
|
||||
|
||||
if (!stash) {
|
||||
throw new HttpError('You are not authorized to access this stash', 403);
|
||||
}
|
||||
verifyStashAccess(stash, sessionUser);
|
||||
|
||||
return curateStash(stash);
|
||||
return curateStash(stash, { user });
|
||||
}
|
||||
|
||||
export async function fetchStashes(domain, itemId, sessionUser) {
|
||||
@@ -145,7 +168,7 @@ export async function refreshActorsView() {
|
||||
}
|
||||
|
||||
export async function stashActor(actorId, stashId, sessionUser) {
|
||||
const stash = await fetchStash(stashId, sessionUser);
|
||||
const stash = await fetchStashById(stashId, sessionUser);
|
||||
|
||||
await knex('stashes_actors')
|
||||
.insert({
|
||||
@@ -158,30 +181,6 @@ export async function stashActor(actorId, stashId, sessionUser) {
|
||||
return fetchStashes('actor', actorId, sessionUser);
|
||||
}
|
||||
|
||||
export async function stashScene(sceneId, stashId, sessionUser) {
|
||||
const stash = await fetchStash(stashId, sessionUser);
|
||||
|
||||
await knex('stashes_scenes')
|
||||
.insert({
|
||||
stash_id: stash.id,
|
||||
scene_id: sceneId,
|
||||
});
|
||||
|
||||
return fetchStashes('scene', sceneId, sessionUser);
|
||||
}
|
||||
|
||||
export 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);
|
||||
}
|
||||
|
||||
export async function unstashActor(actorId, stashId, sessionUser) {
|
||||
await knex
|
||||
.from('stashes_actors AS deletable')
|
||||
@@ -198,6 +197,18 @@ export async function unstashActor(actorId, stashId, sessionUser) {
|
||||
return fetchStashes('actor', actorId, sessionUser);
|
||||
}
|
||||
|
||||
export async function stashScene(sceneId, stashId, sessionUser) {
|
||||
const stash = await fetchStashById(stashId, sessionUser);
|
||||
|
||||
await knex('stashes_scenes')
|
||||
.insert({
|
||||
stash_id: stash.id,
|
||||
scene_id: sceneId,
|
||||
});
|
||||
|
||||
return fetchStashes('scene', sceneId, sessionUser);
|
||||
}
|
||||
|
||||
export async function unstashScene(sceneId, stashId, sessionUser) {
|
||||
await knex
|
||||
.from('stashes_scenes AS deletable')
|
||||
@@ -209,9 +220,34 @@ export async function unstashScene(sceneId, stashId, sessionUser) {
|
||||
.where('stashes.user_id', sessionUser.id))
|
||||
.delete();
|
||||
|
||||
await indexApi.callDelete({
|
||||
index: 'scenes_stashed',
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{ equals: { id: sceneId } },
|
||||
{ equals: { stash_id: stashId } },
|
||||
{ equals: { user_id: sessionUser.id } },
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return fetchStashes('scene', sceneId, sessionUser);
|
||||
}
|
||||
|
||||
export async function stashMovie(movieId, stashId, sessionUser) {
|
||||
const stash = await fetchStashById(stashId, sessionUser);
|
||||
|
||||
await knex('stashes_movies')
|
||||
.insert({
|
||||
stash_id: stash.id,
|
||||
movie_id: movieId,
|
||||
});
|
||||
|
||||
return fetchStashes('movie', movieId, sessionUser);
|
||||
}
|
||||
|
||||
export async function unstashMovie(movieId, stashId, sessionUser) {
|
||||
await knex
|
||||
.from('stashes_movies AS deletable')
|
||||
|
||||
Reference in New Issue
Block a user