Resolving tag and entity slugs in alert API.

This commit is contained in:
DebaucheryLibrarian 2025-04-04 05:56:32 +02:00
parent c37a2ce1a1
commit 99cd551fc0
3 changed files with 20 additions and 8 deletions

View File

@ -2,6 +2,7 @@ import escapeRegexp from 'escape-string-regexp';
import { knexOwner as knex } from './knex.js';
import { fetchScenesById } from './scenes.js';
import { getIdsBySlug } from './cache.js';
import promiseProps from '../utils/promise-props.js';
import { HttpError } from './errors.js';
@ -159,12 +160,23 @@ export async function createAlert(alert, reqUser) {
})
.returning('id');
const tagIds = (await getIdsBySlug(alert.tags, 'tags') || []).concat(alert.tagIds || []);
const entityIds = (await getIdsBySlug(alert.entities || [], 'entities')).concat(alert.entityIds || []);
if (tagIds.length < (alert.tags?.length || 0) + (alert.tagIds?.length || 0)) {
throw new HttpError('Failed to resolve all tags');
}
if (entityIds.length < (alert.entities?.length || 0) + (alert.entityIds?.length || 0)) {
throw new HttpError('Failed to resolve all entities');
}
await Promise.all([
alert.actors?.length > 0 && knex('alerts_actors').insert(alert.actors.map((actorId) => ({
alert_id: alertId,
actor_id: actorId,
}))),
alert.tags?.length > 0 && knex('alerts_tags').insert(alert.tags.map((tagId) => ({
tagIds?.length > 0 && knex('alerts_tags').insert(tagIds.map((tagId) => ({
alert_id: alertId,
tag_id: tagId,
}))),
@ -179,7 +191,7 @@ export async function createAlert(alert, reqUser) {
alert_id: alertId,
stash_id: stashId,
}))),
alert.entities?.length > 0 && knex('alerts_entities').insert(alert.entities.map((entityId) => ({
entityIds?.length > 0 && knex('alerts_entities').insert(entityIds.map((entityId) => ({
alert_id: alertId,
entity_id: entityId,
})).slice(0, alert.allEntities ? 1 : Infinity)), // one scene can never match multiple entities in AND mode

View File

@ -10,11 +10,9 @@ export async function getIdsBySlug(slugs, domain, toMap) {
return null;
}
/* this, naturally, fails if the slug is 69 etc.
if (Number(slug)) {
return Number(slug); // already an ID or missing
if (typeof slug === 'number') {
return slug; // already an ID, tags like 69 should be a string at this stage
}
*/
const id = await redis.hGet(`traxxx:${domain}:id_by_slug`, slug);

View File

@ -29,8 +29,10 @@ export const alertsSchema = `
allTags: Boolean = true
allMatches: Boolean = true
actors: [Int!]
tags: [Int!]
entities: [Int!]
tags: [String!]
tagIds: [Int!]
entities: [String!]
entityIds: [Int!]
matches: [AlertMatchInput!]
notify: Boolean = true
email: Boolean = false