Added favorites button to actor page.
This commit is contained in:
13
src/auth.js
13
src/auth.js
@@ -4,7 +4,7 @@ const util = require('util');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const knex = require('./knex');
|
||||
const { curateUser } = require('./users');
|
||||
const { curateUser, fetchUser } = require('./users');
|
||||
const { HttpError } = require('./errors');
|
||||
|
||||
const scrypt = util.promisify(crypto.scrypt);
|
||||
@@ -21,12 +21,7 @@ async function verifyPassword(password, storedPassword) {
|
||||
}
|
||||
|
||||
async function login(credentials) {
|
||||
const user = await knex('users')
|
||||
.select('users.*', 'users_roles.abilities as role_abilities')
|
||||
.where('username', credentials.username)
|
||||
.orWhere('email', credentials.username)
|
||||
.leftJoin('users_roles', 'users_roles.role', 'users.role')
|
||||
.first();
|
||||
const user = await fetchUser(credentials.username, true);
|
||||
|
||||
if (!user) {
|
||||
throw new HttpError('Username or password incorrect', 401);
|
||||
@@ -69,7 +64,7 @@ async function signup(credentials) {
|
||||
email: credentials.email,
|
||||
password: storedPassword,
|
||||
})
|
||||
.returning('*');
|
||||
.returning('id');
|
||||
|
||||
await knex('stashes').insert({
|
||||
user_id: user.id,
|
||||
@@ -78,7 +73,7 @@ async function signup(credentials) {
|
||||
public: false,
|
||||
});
|
||||
|
||||
return curateUser(user);
|
||||
return fetchUser(user.id);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
73
src/stashes.js
Normal file
73
src/stashes.js
Normal file
@@ -0,0 +1,73 @@
|
||||
'use strict';
|
||||
|
||||
const knex = require('./knex');
|
||||
const { HttpError } = require('./errors');
|
||||
|
||||
function curateStash(stash) {
|
||||
const curatedStash = {
|
||||
id: stash.id,
|
||||
name: stash.name,
|
||||
slug: stash.slug,
|
||||
};
|
||||
|
||||
return curatedStash;
|
||||
}
|
||||
|
||||
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 modify this stash', 403);
|
||||
}
|
||||
|
||||
return stash;
|
||||
}
|
||||
|
||||
async function stashActor(actorId, stashId, sessionUser) {
|
||||
const stash = await fetchStash(stashId, sessionUser);
|
||||
|
||||
await knex('stashes_actors')
|
||||
.insert({
|
||||
stash_id: stash.id,
|
||||
actor_id: actorId,
|
||||
});
|
||||
}
|
||||
|
||||
async function stashScene(sceneId, stashId, sessionUser) {
|
||||
const stash = await fetchStash(stashId, sessionUser);
|
||||
|
||||
await knex('stashes_scenes')
|
||||
.insert({
|
||||
stash_id: stash.id,
|
||||
actor_id: sceneId,
|
||||
});
|
||||
}
|
||||
|
||||
async function unstashActor(actorId, stashId, sessionUser) {
|
||||
await knex
|
||||
.from('stashes_actors')
|
||||
.whereIn('stashes_actors.id', knex('stashes_actors')
|
||||
.select('stashes_actors.id')
|
||||
.leftJoin('stashes', 'stashes.id', 'stashes_actors.stash_id')
|
||||
.where('stashes.user_id', sessionUser.id) // verify user owns this stash
|
||||
.where('stashes_actors.actor_id', actorId)
|
||||
.where('stashes_actors.stash_id', stashId))
|
||||
.delete();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
curateStash,
|
||||
stashActor,
|
||||
stashScene,
|
||||
// unstashScene,
|
||||
unstashActor,
|
||||
};
|
||||
26
src/users.js
26
src/users.js
@@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const knex = require('./knex');
|
||||
const { curateStash } = require('./stashes');
|
||||
|
||||
function curateUser(user) {
|
||||
if (!user) {
|
||||
@@ -17,20 +18,35 @@ function curateUser(user) {
|
||||
identityVerified: user.identity_verified,
|
||||
ability,
|
||||
createdAt: user.created_at,
|
||||
stashes: user.stashes?.map(stash => curateStash(stash)) || [],
|
||||
};
|
||||
|
||||
return curatedUser;
|
||||
}
|
||||
|
||||
async function fetchUser(userId) {
|
||||
async function fetchUser(userId, raw) {
|
||||
const user = await knex('users')
|
||||
.select('users.*', 'users_roles.abilities as role_abilities')
|
||||
.where('id', userId)
|
||||
.orWhere('username', userId)
|
||||
.orWhere('email', userId)
|
||||
.select(knex.raw('users.*, users_roles.abilities as role_abilities, json_agg(stashes) as stashes'))
|
||||
.modify((builder) => {
|
||||
if (typeof userId === 'number') {
|
||||
builder.where('users.id', userId);
|
||||
}
|
||||
|
||||
if (typeof userId === 'string') {
|
||||
builder
|
||||
.where('users.username', userId)
|
||||
.orWhere('users.email', userId);
|
||||
}
|
||||
})
|
||||
.leftJoin('users_roles', 'users_roles.role', 'users.role')
|
||||
.leftJoin('stashes', 'stashes.user_id', 'users.id')
|
||||
.groupBy('users.id', 'users_roles.role')
|
||||
.first();
|
||||
|
||||
if (raw) {
|
||||
return user;
|
||||
}
|
||||
|
||||
return curateUser(user);
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ async function fetchMeApi(req, res) {
|
||||
async function signupApi(req, res) {
|
||||
const user = await signup(req.body);
|
||||
|
||||
req.session.user = user;
|
||||
res.send(user);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ module.exports = postgraphile(
|
||||
'public',
|
||||
{
|
||||
// watchPg: true,
|
||||
disableDefaultMutations: true,
|
||||
dynamicJson: true,
|
||||
graphiql: true,
|
||||
enhanceGraphiql: true,
|
||||
|
||||
@@ -43,6 +43,13 @@ const {
|
||||
fetchTags,
|
||||
} = require('./tags');
|
||||
|
||||
const {
|
||||
stashActor,
|
||||
stashScene,
|
||||
unstashActor,
|
||||
unstashScene,
|
||||
} = require('./stashes');
|
||||
|
||||
async function initServer() {
|
||||
const app = express();
|
||||
const router = Router();
|
||||
@@ -74,6 +81,12 @@ async function initServer() {
|
||||
|
||||
router.post('/api/users', signup);
|
||||
|
||||
router.post('/api/stashes/:stashId/actors', stashActor);
|
||||
router.post('/api/stashes/:stashId/scenes', stashScene);
|
||||
|
||||
router.delete('/api/stashes/:stashId/actors/:actorId', unstashActor);
|
||||
router.delete('/api/stashes/:stashId/scenes/:sceneId', unstashScene);
|
||||
|
||||
router.get('/api/scenes', fetchScenes);
|
||||
router.get('/api/scenes/:releaseId', fetchScene);
|
||||
router.get('/api/scenes/:releaseId/poster', fetchScenePoster);
|
||||
|
||||
34
src/web/stashes.js
Normal file
34
src/web/stashes.js
Normal file
@@ -0,0 +1,34 @@
|
||||
'use strict';
|
||||
|
||||
const { stashActor, stashScene, unstashActor, unstashScene } = require('../stashes');
|
||||
|
||||
async function stashActorApi(req, res) {
|
||||
await stashActor(req.body.actorId, req.params.stashId, req.session.user);
|
||||
|
||||
res.status(201).send();
|
||||
}
|
||||
|
||||
async function stashSceneApi(req, res) {
|
||||
await stashScene(req.body.sceneId, req.params.stashId, req.session.user);
|
||||
|
||||
res.status(201).send();
|
||||
}
|
||||
|
||||
async function unstashActorApi(req, res) {
|
||||
await unstashActor(req.params.actorId, req.params.stashId, req.session.user);
|
||||
|
||||
res.status(204).send();
|
||||
}
|
||||
|
||||
async function unstashSceneApi(req, res) {
|
||||
await unstashScene(req.params.sceneId, req.params.stashId, req.session.user);
|
||||
|
||||
res.status(204).send();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
stashActor: stashActorApi,
|
||||
stashScene: stashSceneApi,
|
||||
unstashActor: unstashActorApi,
|
||||
unstashScene: unstashSceneApi,
|
||||
};
|
||||
Reference in New Issue
Block a user