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')
.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()

View File

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

View File

@ -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),

View File

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