Generalized tags to allow them to be assigned to anything.

This commit is contained in:
ThePendulum 2019-12-07 04:41:16 +01:00
parent 85c2654add
commit 222260d3cf
4 changed files with 28 additions and 18 deletions

View File

@ -285,16 +285,10 @@ exports.up = knex => Promise.resolve()
.references('id') .references('id')
.inTable('tags'); .inTable('tags');
table.integer('site_id', 12) table.string('domain');
.references('id') table.integer('target_id', 16);
.inTable('sites');
table.integer('release_id', 16) table.unique(['domain', 'tag_id', 'target_id']);
.references('id')
.inTable('releases');
table.unique(['release_id', 'tag_id']);
table.unique(['site_id', 'tag_id']);
})); }));
exports.down = knex => Promise.resolve() exports.down = knex => Promise.resolve()

View File

@ -1545,13 +1545,13 @@ exports.seed = knex => Promise.resolve()
return upsert('tags_groups', groups, duplicatesBySlug, 'slug', knex); return upsert('tags_groups', groups, duplicatesBySlug, 'slug', knex);
}) })
.then(async () => { .then(async () => {
const [duplicates, groups] = await Promise.all([ const [duplicates, groupEntries] = await Promise.all([
knex('tags').select('*'), knex('tags').select('*'),
knex('tags_groups').select('*'), knex('tags_groups').select('*'),
]); ]);
const duplicatesBySlug = duplicates.reduce((acc, tag) => ({ ...acc, [tag.slug]: tag }), {}); 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); const tags = getTags(groupsMap);

View File

@ -29,7 +29,10 @@ async function curateRelease(release) {
.orderBy('actors.gender'), .orderBy('actors.gender'),
knex('tags_associated') knex('tags_associated')
.select('tags.name', 'tags.slug') .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') .leftJoin('tags', 'tags.id', 'tags_associated.tag_id')
.orderBy('tags.priority', 'desc'), .orderBy('tags.priority', 'desc'),
knex('media') knex('media')
@ -162,7 +165,8 @@ function commonQuery(queryBuilder, {
.from('tags_associated') .from('tags_associated')
.leftJoin('tags', 'tags_associated.tag_id', 'tags.id') .leftJoin('tags', 'tags_associated.tag_id', 'tags.id')
.whereIn('tags.slug', finalFilter) .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', '>', after)
.andWhere('date', '<=', before) .andWhere('date', '<=', before)
@ -209,12 +213,13 @@ async function fetchActorReleases(queryObject, options = {}) {
async function fetchTagReleases(queryObject, options = {}) { async function fetchTagReleases(queryObject, options = {}) {
const releases = await knex('tags_associated') 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') .leftJoin('tags', 'tags_associated.tag_id', 'tags.id')
.select( .select(
'tags.name as tag_name', 'tags.name as tag_name',
) )
.modify(commonQuery, options) .modify(commonQuery, options)
.where('tags_associated.domain', 'releases')
.where(builder => whereOr(queryObject, 'tags', builder)); .where(builder => whereOr(queryObject, 'tags', builder));
return curateReleases(releases); return curateReleases(releases);
@ -225,7 +230,7 @@ async function storeReleaseAssets(release, releaseId) {
try { try {
await Promise.all([ await Promise.all([
// associateTags(release, releaseId), associateTags(release, releaseId),
storePhotos(release, releaseId), storePhotos(release, releaseId),
storePoster(release, releaseId), storePoster(release, releaseId),
storeTrailer(release, releaseId), storeTrailer(release, releaseId),

View File

@ -64,11 +64,22 @@ async function associateTags(release, releaseId) {
const tags = release.tags.some(tag => typeof tag === 'string') const tags = release.tags.some(tag => typeof tag === 'string')
? await matchTags(release.tags) // scraper returned raw tags ? 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, tag_id: tagId,
release_id: releaseId, domain: 'releases',
target_id: releaseId,
}))); })));
} }