From 99cd551fc07309ad7afe0d93df7715f8a25e5325 Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Fri, 4 Apr 2025 05:56:32 +0200 Subject: [PATCH] Resolving tag and entity slugs in alert API. --- src/alerts.js | 16 ++++++++++++++-- src/cache.js | 6 ++---- src/web/alerts.js | 6 ++++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/alerts.js b/src/alerts.js index ae1a5b3..8f295ce 100755 --- a/src/alerts.js +++ b/src/alerts.js @@ -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 diff --git a/src/cache.js b/src/cache.js index 8779729..bcce17d 100644 --- a/src/cache.js +++ b/src/cache.js @@ -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); diff --git a/src/web/alerts.js b/src/web/alerts.js index c664939..4d9dca8 100755 --- a/src/web/alerts.js +++ b/src/web/alerts.js @@ -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