From 222260d3cf84e9aec902cad3f720df3dd7b3ea46 Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Sat, 7 Dec 2019 04:41:16 +0100 Subject: [PATCH] Generalized tags to allow them to be assigned to anything. --- migrations/20190325001339_releases.js | 12 +++--------- seeds/03_tags.js | 4 ++-- src/releases.js | 13 +++++++++---- src/tags.js | 17 ++++++++++++++--- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/migrations/20190325001339_releases.js b/migrations/20190325001339_releases.js index 901697bd..fcfd1368 100644 --- a/migrations/20190325001339_releases.js +++ b/migrations/20190325001339_releases.js @@ -285,16 +285,10 @@ exports.up = knex => Promise.resolve() .references('id') .inTable('tags'); - table.integer('site_id', 12) - .references('id') - .inTable('sites'); + table.string('domain'); + table.integer('target_id', 16); - table.integer('release_id', 16) - .references('id') - .inTable('releases'); - - table.unique(['release_id', 'tag_id']); - table.unique(['site_id', 'tag_id']); + table.unique(['domain', 'tag_id', 'target_id']); })); exports.down = knex => Promise.resolve() diff --git a/seeds/03_tags.js b/seeds/03_tags.js index e7356a13..67fc8fe9 100644 --- a/seeds/03_tags.js +++ b/seeds/03_tags.js @@ -1545,13 +1545,13 @@ exports.seed = knex => Promise.resolve() return upsert('tags_groups', groups, duplicatesBySlug, 'slug', knex); }) .then(async () => { - const [duplicates, groups] = await Promise.all([ + const [duplicates, groupEntries] = await Promise.all([ knex('tags').select('*'), knex('tags_groups').select('*'), ]); const duplicatesBySlug = duplicates.reduce((acc, tag) => ({ ...acc, [tag.slug]: tag }), {}); - const groupsMap = groups.reduce((acc, { id, slug }) => ({ ...acc, [slug]: id }), {}); + const groupsMap = groupEntries.reduce((acc, { id, slug }) => ({ ...acc, [slug]: id }), {}); const tags = getTags(groupsMap); diff --git a/src/releases.js b/src/releases.js index e5b17fe5..08961586 100644 --- a/src/releases.js +++ b/src/releases.js @@ -29,7 +29,10 @@ async function curateRelease(release) { .orderBy('actors.gender'), knex('tags_associated') .select('tags.name', 'tags.slug') - .where({ release_id: release.id }) + .where({ + domain: 'releases', + target_id: release.id, + }) .leftJoin('tags', 'tags.id', 'tags_associated.tag_id') .orderBy('tags.priority', 'desc'), knex('media') @@ -162,7 +165,8 @@ function commonQuery(queryBuilder, { .from('tags_associated') .leftJoin('tags', 'tags_associated.tag_id', 'tags.id') .whereIn('tags.slug', finalFilter) - .andWhereRaw('tags_associated.release_id = releases.id'); + .where('tags_associated.domain', 'releases') + .whereRaw('tags_associated.target_id = releases.id'); }) .andWhere('date', '>', after) .andWhere('date', '<=', before) @@ -209,12 +213,13 @@ async function fetchActorReleases(queryObject, options = {}) { async function fetchTagReleases(queryObject, options = {}) { const releases = await knex('tags_associated') - .leftJoin('releases', 'tags_associated.release_id', 'releases.id') + .leftJoin('releases', 'tags_associated.target_id', 'releases.id') .leftJoin('tags', 'tags_associated.tag_id', 'tags.id') .select( 'tags.name as tag_name', ) .modify(commonQuery, options) + .where('tags_associated.domain', 'releases') .where(builder => whereOr(queryObject, 'tags', builder)); return curateReleases(releases); @@ -225,7 +230,7 @@ async function storeReleaseAssets(release, releaseId) { try { await Promise.all([ - // associateTags(release, releaseId), + associateTags(release, releaseId), storePhotos(release, releaseId), storePoster(release, releaseId), storeTrailer(release, releaseId), diff --git a/src/tags.js b/src/tags.js index 9f0cd0a7..a4c95e85 100644 --- a/src/tags.js +++ b/src/tags.js @@ -64,11 +64,22 @@ async function associateTags(release, releaseId) { const tags = release.tags.some(tag => typeof tag === 'string') ? await matchTags(release.tags) // scraper returned raw tags - : release.tags; // tags already matched by scraper + : release.tags; // tags already matched by (outdated) scraper - await knex('tags_associated').insert(tags.map(tagId => ({ + const associationEntries = await knex('tags_associated') + .where({ + domain: 'releases', + target_id: releaseId, + }) + .whereIn('tag_id', tags); + + const existingAssociations = new Set(associationEntries.map(association => association.tag_id)); + const newAssociations = tags.filter(tagId => !existingAssociations.has(tagId)); + + await knex('tags_associated').insert(newAssociations.map(tagId => ({ tag_id: tagId, - release_id: releaseId, + domain: 'releases', + target_id: releaseId, }))); }