Added stash menu with remove and rename.

This commit is contained in:
2024-03-27 00:06:03 +01:00
parent e4638324f7
commit c018f54a12
22 changed files with 620 additions and 623 deletions

View File

@@ -19,7 +19,7 @@ export function curateStash(stash, assets = {}) {
id: stash.id,
name: stash.name,
slug: stash.slug,
primary: stash.primary,
isPrimary: stash.primary,
public: stash.public,
createdAt: stash.created_at,
stashedScenes: stash.stashed_scenes ?? null,
@@ -38,10 +38,10 @@ export function curateStash(stash, assets = {}) {
function curateStashEntry(stash, user) {
const curatedStashEntry = {
user_id: user.id,
name: stash.name,
slug: slugify(stash.name),
public: false,
user_id: user?.id || undefined,
name: stash.name || undefined,
slug: slugify(stash.name) || undefined,
public: stash.public ?? false,
};
return curatedStashEntry;
@@ -94,26 +94,30 @@ export async function fetchStashes(domain, itemId, sessionUser) {
return stashes.map((stash) => curateStash(stash));
}
function verifyStashName(stash) {
if (!stash.name) {
throw new HttpError('Stash name required', 400);
}
if (stash.name.length < config.stashes.nameLength[0]) {
throw new HttpError('Stash name is too short', 400);
}
if (stash.name.length > config.stashes.nameLength[1]) {
throw new HttpError('Stash name is too long', 400);
}
if (!config.stashes.namePattern.test(stash.name)) {
throw new HttpError('Stash name contains invalid characters', 400);
}
}
export async function createStash(newStash, sessionUser) {
if (!sessionUser) {
throw new HttpError('You are not authenthicated', 401);
}
if (!newStash.name) {
throw new HttpError('Stash name required', 400);
}
if (newStash.name.length < config.stashes.nameLength[0]) {
throw new HttpError('Stash name is too short', 400);
}
if (newStash.name.length > config.stashes.nameLength[1]) {
throw new HttpError('Stash name is too long', 400);
}
if (!config.stashes.namePattern.test(newStash.name)) {
throw new HttpError('Stash name contains invalid characters', 400);
}
verifyStashName(newStash);
try {
const stash = await knex('stashes')
@@ -130,24 +134,36 @@ export async function createStash(newStash, sessionUser) {
}
}
export async function updateStash(stashId, newStash, sessionUser) {
export async function updateStash(stashId, updatedStash, 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('*');
if (!stash) {
throw new HttpError('You are not authorized to modify this stash', 403);
if (updatedStash.name) {
verifyStashName(updatedStash);
}
return curateStash(stash);
try {
const stash = await knex('stashes')
.where({
id: stashId,
user_id: sessionUser.id,
})
.update(curateStashEntry(updatedStash))
.returning('*');
if (!stash) {
throw new HttpError('You are not authorized to modify this stash', 403);
}
return curateStash(stash);
} catch (error) {
if (error.routine === '_bt_check_unique') {
throw new HttpError('Stash name should be unique', 409);
}
throw error;
}
}
export async function removeStash(stashId, sessionUser) {