diff --git a/src/manticore.js b/src/manticore.js new file mode 100644 index 00000000..c7002904 --- /dev/null +++ b/src/manticore.js @@ -0,0 +1,12 @@ +const config = require('config'); +const manticore = require('manticoresearch'); + +const mantiClient = new manticore.ApiClient(); + +mantiClient.basePath = `http://${config.database.manticore.host}:${config.database.manticore.httpPort}`; + +module.exports = { + searchApi: new manticore.SearchApi(mantiClient), + indexApi: new manticore.IndexApi(mantiClient), + utilsApi: new manticore.UtilsApi(), +}; diff --git a/src/stashes.js b/src/stashes.js index a4f1dbbc..65dc4a5d 100755 --- a/src/stashes.js +++ b/src/stashes.js @@ -3,6 +3,7 @@ const config = require('config'); const knex = require('./knex'); +const { indexApi } = require('./manticore'); const { HttpError } = require('./errors'); const slugify = require('./utils/slugify'); const logger = require('./logger')(__filename); @@ -142,11 +143,23 @@ async function refreshActorsView() { async function stashActor(actorId, stashId, sessionUser) { const stash = await fetchStash(stashId, sessionUser); - await knex('stashes_actors') + const [stashed] = await knex('stashes_actors') .insert({ stash_id: stash.id, actor_id: actorId, - }); + }) + .returning(['id', 'created_at']); + + await indexApi.replace({ + index: 'actors_stashed', + id: stashed.id, + doc: { + actor_id: actorId, + user_id: sessionUser.id, + stash_id: stashId, + created_at: Math.round(stashed.created_at.getTime() / 1000), + }, + }); refreshActorsView(); @@ -156,11 +169,24 @@ async function stashActor(actorId, stashId, sessionUser) { async function stashScene(sceneId, stashId, sessionUser) { const stash = await fetchStash(stashId, sessionUser); - await knex('stashes_scenes') + const [stashed] = await knex('stashes_scenes') .insert({ stash_id: stash.id, scene_id: sceneId, - }); + }) + .returning(['id', 'created_at']); + + await indexApi.replace({ + index: 'scenes_stashed', + id: stashed.id, + doc: { + // ...doc.replace.doc, + scene_id: sceneId, + user_id: sessionUser.id, + stash_id: stashId, + created_at: Math.round(stashed.created_at.getTime() / 1000), + }, + }); return fetchStashes('scene', sceneId, sessionUser); } @@ -168,11 +194,23 @@ async function stashScene(sceneId, stashId, sessionUser) { async function stashMovie(movieId, stashId, sessionUser) { const stash = await fetchStash(stashId, sessionUser); - await knex('stashes_movies') + const [stashed] = await knex('stashes_movies') .insert({ stash_id: stash.id, movie_id: movieId, - }); + }) + .returning(['id', 'created_at']); + + await indexApi.replace({ + index: 'movies_stashed', + id: stashed.id, + doc: { + movie_id: movieId, + user_id: sessionUser.id, + stash_id: stashId, + created_at: Math.round(stashed.created_at.getTime() / 1000), + }, + }); return fetchStashes('movie', movieId, sessionUser); } @@ -188,6 +226,19 @@ async function unstashActor(actorId, stashId, sessionUser) { .where('stashes.user_id', sessionUser.id)) .delete(); + await indexApi.callDelete({ + index: 'actors_stashed', + query: { + bool: { + must: [ + { equals: { actor_id: actorId } }, + { equals: { stash_id: stashId } }, + { equals: { user_id: sessionUser.id } }, + ], + }, + }, + }); + refreshActorsView(); return fetchStashes('actor', actorId, sessionUser); @@ -204,6 +255,19 @@ async function unstashScene(sceneId, stashId, sessionUser) { .where('stashes.user_id', sessionUser.id)) .delete(); + await indexApi.callDelete({ + index: 'scenes_stashed', + query: { + bool: { + must: [ + { equals: { scene_id: sceneId } }, + { equals: { stash_id: stashId } }, + { equals: { user_id: sessionUser.id } }, + ], + }, + }, + }); + return fetchStashes('scene', sceneId, sessionUser); } @@ -218,6 +282,19 @@ async function unstashMovie(movieId, stashId, sessionUser) { .where('stashes.user_id', sessionUser.id)) .delete(); + await indexApi.callDelete({ + index: 'movies_stashed', + query: { + bool: { + must: [ + { equals: { movie_id: movieId } }, + { equals: { stash_id: stashId } }, + { equals: { user_id: sessionUser.id } }, + ], + }, + }, + }); + return fetchStashes('movie', movieId, sessionUser); } diff --git a/src/web/stashes.js b/src/web/stashes.js index ce2e90be..62640d90 100755 --- a/src/web/stashes.js +++ b/src/web/stashes.js @@ -31,37 +31,37 @@ async function removeStashApi(req, res) { } async function stashActorApi(req, res) { - const stashes = await stashActor(req.body.actorId, req.params.stashId, req.session.user); + const stashes = await stashActor(req.body.actorId, Number(req.params.stashId), req.session.user); res.send(stashes); } async function stashSceneApi(req, res) { - const stashes = await stashScene(req.body.sceneId, req.params.stashId, req.session.user); + const stashes = await stashScene(req.body.sceneId, Number(req.params.stashId), req.session.user); res.send(stashes); } async function stashMovieApi(req, res) { - const stashes = await stashMovie(req.body.movieId, req.params.stashId, req.session.user); + const stashes = await stashMovie(req.body.movieId, Number(req.params.stashId), req.session.user); res.send(stashes); } async function unstashActorApi(req, res) { - const stashes = await unstashActor(req.params.actorId, req.params.stashId, req.session.user); + const stashes = await unstashActor(Number(req.params.actorId), Number(req.params.stashId), req.session.user); res.send(stashes); } async function unstashSceneApi(req, res) { - const stashes = await unstashScene(req.params.sceneId, req.params.stashId, req.session.user); + const stashes = await unstashScene(Number(req.params.sceneId), Number(req.params.stashId), req.session.user); res.send(stashes); } async function unstashMovieApi(req, res) { - const stashes = await unstashMovie(req.params.movieId, req.params.stashId, req.session.user); + const stashes = await unstashMovie(Number(req.params.movieId), Number(req.params.stashId), req.session.user); res.send(stashes); }