Syncing stash actions to manticore.

This commit is contained in:
DebaucheryLibrarian 2024-03-27 02:39:16 +01:00
parent 837f675e4f
commit e019648a87
3 changed files with 101 additions and 12 deletions

12
src/manticore.js Normal file
View File

@ -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(),
};

View File

@ -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,10 +143,22 @@ 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,10 +169,23 @@ 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,10 +194,22 @@ 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);
}

View File

@ -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);
}