Generalized tags to allow them to be assigned to anything.

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

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