Added elaborate template switching.
This commit is contained in:
95
src/users.js
95
src/users.js
@@ -1,13 +1,24 @@
|
||||
import { parse } from 'yaml';
|
||||
|
||||
import { knexOwner as knex } from './knex.js';
|
||||
import { curateStash } from './stashes.js';
|
||||
// import { curateStash } from './stashes.js';
|
||||
import { HttpError } from './errors.js';
|
||||
|
||||
export function curateUser(user, assets = {}) {
|
||||
function curateTemplate(template) {
|
||||
return {
|
||||
id: template.id,
|
||||
name: template.name,
|
||||
template: template.template,
|
||||
createdAt: template.created_at,
|
||||
};
|
||||
}
|
||||
|
||||
export function curateUser(user, _assets = {}) {
|
||||
if (!user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const curatedStashes = assets.stashes?.filter(Boolean).map((stash) => curateStash(stash)) || [];
|
||||
// const curatedStashes = assets.stashes?.filter(Boolean).map((stash) => curateStash(stash)) || [];
|
||||
|
||||
const curatedUser = {
|
||||
id: user.id,
|
||||
@@ -17,8 +28,6 @@ export function curateUser(user, assets = {}) {
|
||||
identityVerified: user.identity_verified,
|
||||
avatar: `/media/avatars/${user.id}_${user.username}.png`,
|
||||
createdAt: user.created_at,
|
||||
stashes: curatedStashes,
|
||||
primaryStash: curatedStashes.find((stash) => stash.isPrimary),
|
||||
};
|
||||
|
||||
return curatedUser;
|
||||
@@ -38,7 +47,7 @@ function whereUser(builder, userId, options = {}) {
|
||||
builder.where('users.id', Number(userId));
|
||||
}
|
||||
|
||||
export async function fetchUser(userId, options = {}, reqUser) {
|
||||
export async function fetchUser(userId, options = {}, _reqUser) {
|
||||
const user = await knex('users')
|
||||
.select(knex.raw('users.*, users_roles.abilities as role_abilities'))
|
||||
.modify((builder) => whereUser(builder, userId, options))
|
||||
@@ -50,19 +59,71 @@ export async function fetchUser(userId, options = {}, reqUser) {
|
||||
throw new HttpError(`User '${userId}' not found`, 404);
|
||||
}
|
||||
|
||||
const stashes = await knex('stashes')
|
||||
.select('stashes.*', 'stashes_meta.*')
|
||||
.leftJoin('stashes_meta', 'stashes_meta.stash_id', 'stashes.id')
|
||||
.where('user_id', user.id)
|
||||
.modify((builder) => {
|
||||
if (reqUser?.id !== user.id && !options.includeStashes) {
|
||||
builder.where('public', true);
|
||||
}
|
||||
});
|
||||
/*
|
||||
const [stashes, templates] = await Promise.all([
|
||||
knex('stashes')
|
||||
.select('stashes.*', 'stashes_meta.*')
|
||||
.leftJoin('stashes_meta', 'stashes_meta.stash_id', 'stashes.id')
|
||||
.where('user_id', user.id)
|
||||
.modify((builder) => {
|
||||
if (reqUser?.id !== user.id && !options.includeStashes) {
|
||||
builder.where('public', true);
|
||||
}
|
||||
}),
|
||||
options.includeTemplates
|
||||
? knex('users_templates').where('user_id', user.id)
|
||||
: null,
|
||||
]);
|
||||
*/
|
||||
|
||||
if (options.raw) {
|
||||
return { user, stashes };
|
||||
// return { user, stashes, templates };
|
||||
return { user };
|
||||
}
|
||||
|
||||
return curateUser(user, { stashes });
|
||||
// return curateUser(user, { stashes, templates });
|
||||
return curateUser(user, {});
|
||||
}
|
||||
|
||||
export async function fetchUserTemplates(reqUser) {
|
||||
const templates = await knex('users_templates')
|
||||
.where('user_id', reqUser.id)
|
||||
.orderBy('created_at', 'asc');
|
||||
|
||||
return templates.map((template) => curateTemplate(template));
|
||||
}
|
||||
|
||||
export async function createTemplate(template, reqUser) {
|
||||
if (!template.template) {
|
||||
throw new HttpError('No template specified', 400);
|
||||
}
|
||||
|
||||
if (!template.name) {
|
||||
throw new HttpError('No template name specified', 400);
|
||||
}
|
||||
|
||||
try {
|
||||
parse(template.template);
|
||||
} catch (error) {
|
||||
throw new HttpError(`Invalid YAML: ${error.message}`, 400);
|
||||
}
|
||||
|
||||
const [templateEntry] = await knex('users_templates')
|
||||
.insert({
|
||||
user_id: reqUser.id,
|
||||
name: template.name,
|
||||
template: template.template,
|
||||
})
|
||||
.onConflict(['name', 'user_id'])
|
||||
.merge()
|
||||
.returning('*');
|
||||
|
||||
return curateTemplate(templateEntry);
|
||||
}
|
||||
|
||||
export async function removeTemplate(templateId, reqUser) {
|
||||
await knex('users_templates')
|
||||
.where('id', templateId)
|
||||
.where('user_id', reqUser.id)
|
||||
.delete();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user