You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

307 lines
11 KiB

import Knex from "knex";
import { isConstructorDeclaration } from "typescript";
import * as api from '../../client/src/api/api';
import { Tag, TagParentId, TagDetails, Id, Name } from "../../client/src/api/api";
import { DBError, DBErrorKind } from "../endpoints/types";
import { makeNotFoundError } from "./common";
export async function getTagChildrenRecursive(id: number,
userId: number,
trx: any,
visited: number[] = [], // internal, for cycle detection
): Promise<number[]> {
// check for cycles, these are not allowed.
// a cycle would be if the same ID occurs more than once in the visited set.
if ((new Set<number>(visited)).size < visited.length) {
throw new Error('cyclic tag dependency')
}
const directChildren = (await trx.select('id')
.from('tags')
.where({ 'user': userId })
.where({ 'parentId': id })).map((r: any) => r.id);
const indirectChildrenPromises = directChildren.map(
(child: number) => getTagChildrenRecursive(child, userId, trx, [...visited, id])
);
const indirectChildrenNested = await Promise.all(indirectChildrenPromises);
const indirectChildren = indirectChildrenNested.flat();
return [
...directChildren,
...indirectChildren,
]
}
// Returns the id of the created tag.
export async function createTag(userId: number, tag: (Tag & Name & TagParentId), knex: Knex): Promise<number> {
return await knex.transaction(async (trx) => {
// If applicable, retrieve the parent tag.
const maybeMatches: any[] | null =
tag.parentId ?
(await trx.select('id')
.from('tags')
.where({ 'user': userId })
.where({ 'id': tag.parentId })) :
null;
// Check if the parent was found, if applicable.
if (tag.parentId && maybeMatches && !maybeMatches.length) {
throw makeNotFoundError();
}
// Create the new tag.
var newTag: any = {
name: tag.name,
user: userId,
};
if (tag.parentId) {
newTag['parentId'] = tag.parentId;
}
const tagId = (await trx('tags')
.insert(newTag)
.returning('id') // Needed for Postgres
)[0];
console.log('created tag', tag, ', ID ', tagId);
return tagId;
})
}
export async function deleteTag(userId: number, tagId: number, knex: Knex) {
await knex.transaction(async (trx) => {
// Start retrieving any child tags.
const childTagsPromise =
getTagChildrenRecursive(tagId, userId, trx);
// Start retrieving the tag itself.
const tagPromise = trx.select('id')
.from('tags')
.where({ 'user': userId })
.where({ id: tagId })
.then((r: any) => (r && r[0]) ? r[0]['id'] : undefined)
// Wait for the requests to finish.
var [tag, children] = await Promise.all([tagPromise, childTagsPromise]);
// Merge all IDs.
const toDelete = [tag, ...children];
// Check that we found all objects we need.
if (!tag) {
throw makeNotFoundError();
}
// Start deleting artist associations with the tag.
const deleteArtistsPromise: Promise<any> =
trx.delete()
.from('artists_tags')
.whereIn('tagId', toDelete);
// Start deleting album associations with the tag.
const deleteAlbumsPromise: Promise<any> =
trx.delete()
.from('albums_tags')
.whereIn('tagId', toDelete);
// Start deleting track associations with the tag.
const deleteTracksPromise: Promise<any> =
trx.delete()
.from('tracks_tags')
.whereIn('tagId', toDelete);
// Start deleting the tag and its children.
const deleteTags: Promise<any> = trx('tags')
.where({ 'user': userId })
.whereIn('id', toDelete)
.del();
await Promise.all([deleteArtistsPromise, deleteAlbumsPromise, deleteTracksPromise, deleteTags])
})
}
export async function getTag(userId: number, tagId: number, knex: Knex): Promise<(Tag & TagDetails & Name)> {
const tagPromise: Promise<(Tag & Id & Name & TagParentId) | null> =
knex.select(['id', 'name', 'parentId'])
.from('tags')
.where({ 'user': userId })
.where({ 'id': tagId })
.then((r: (Id & Name & TagParentId)[] | undefined) => r ? r[0] : null)
.then((r: (Id & Name & TagParentId) | null) => {
if (r) {
return { ...r, mbApi_typename: 'tag'};
}
return null;
})
const parentPromise: Promise<(Tag & Id & Name & TagDetails) | null> =
tagPromise
.then((r: (Tag & Id & Name & TagParentId) | null) =>
(r && r.parentId) ? (
getTag(userId, r.parentId, knex)
.then((rr: (Tag & Name & TagDetails) | null) =>
rr ? { ...rr, id: r.parentId || 0 } : null)
) : null
)
const [maybeTag, maybeParent] = await Promise.all([tagPromise, parentPromise]);
if (maybeTag) {
let result: (Tag & Name & TagDetails) = {
mbApi_typename: "tag",
name: maybeTag.name,
parent: maybeParent,
}
return result;
} else {
throw makeNotFoundError();
}
}
export async function modifyTag(userId: number, tagId: number, tag: Tag, knex: Knex): Promise<void> {
await knex.transaction(async (trx) => {
// Start retrieving the parent tag.
const parentTagIdPromise: Promise<number | undefined | null> = tag.parentId ?
trx.select('id')
.from('tags')
.where({ 'user': userId })
.where({ 'id': tag.parentId })
.then((ts: any) => ts.length ? ts.map((t: any) => t['tagId']) : null) :
(async () => { return null })();
// Start retrieving the tag itself.
const tagPromise = trx.select('id')
.from('tags')
.where({ 'user': userId })
.where({ id: tagId })
.then((r: any) => (r && r[0]) ? r[0]['id'] : undefined)
// Start retrieving all current children. This is to prevent
// cycles.
const childrenPromise = getTagChildrenRecursive(tagId, userId, trx);
// Wait for the requests to finish.
var [dbTag, parent, children] = await Promise.all([tagPromise, parentTagIdPromise, childrenPromise]);
// Check that modifying this will not cause a dependency cycle.
if (tag.parentId && [...children, tagId].includes(tag.parentId)) {
const e: DBError = {
name: "DBError",
kind: DBErrorKind.ResourceConflict,
message: 'Modifying this tag would cause a tag parent cycle.',
};
throw e;
}
// Check that we found all objects we need.
if ((tag.parentId && !parent) ||
!dbTag) {
throw makeNotFoundError();
}
// Modify the tag.
await trx('tags')
.where({ 'user': userId })
.where({ 'id': tagId })
.update({
name: tag.name,
parentId: tag.parentId || null,
})
})
}
export async function mergeTag(userId: number, fromId: number, toId: number, knex: Knex): Promise<void> {
await knex.transaction(async (trx) => {
// Start retrieving the "from" tag.
const fromTagIdPromise = trx.select('id')
.from('tags')
.where({ 'user': userId })
.where({ id: fromId })
.then((r: any) => (r && r[0]) ? r[0]['id'] : undefined)
// Start retrieving the "to" tag.
const toTagIdPromise = trx.select('id')
.from('tags')
.where({ 'user': userId })
.where({ id: toId })
.then((r: any) => (r && r[0]) ? r[0]['id'] : undefined)
// Start retrieving any children of the 'from' tag
const childrenPromise = getTagChildrenRecursive(fromId, userId, trx);
// Wait for the requests to finish.
var [fromTagId, toTagId, fromChildren] = await Promise.all([fromTagIdPromise, toTagIdPromise, childrenPromise]);
// Check that we found all objects we need.
if (!fromTagId || !toTagId) {
throw makeNotFoundError();
}
// Check that we are not merging to itself and not merging with its own children
if (fromTagId === toTagId) {
const e: DBError = {
name: "DBError",
kind: DBErrorKind.ResourceConflict,
message: 'Cannot merge a tag into itself',
};
throw e;
}
if (fromChildren.includes(toId)) {
const e: DBError = {
name: "DBError",
kind: DBErrorKind.ResourceConflict,
message: 'Cannot merge a tag with one of its children.',
};
throw e;
}
// Move any child tags under the new tag.
const cPromise = trx('tags')
.where({ 'user': userId })
.where({ 'parentId': fromId })
.update({ 'parentId': toId });
// Assign new tag ID to any objects referencing the to-be-merged tag.
let doReplacement = async (table: string, otherIdField: string) => {
// Store the items referencing the old tag.
let referencesFrom = await trx(table)
.select([otherIdField])
.where({ 'tagId': fromId })
.then((r: any) => r.map((result: any) => result[otherIdField]))
// Store the items referencing the new tag.
let referencesTo = await trx(table)
.select([otherIdField])
.where({ 'tagId': toId })
.then((r: any) => r.map((result: any) => result[otherIdField]))
let referencesEither = [...referencesFrom, ...referencesTo];
let referencesBoth = referencesEither.filter((id: number) => referencesFrom.includes(id) && referencesTo.includes(id));
let referencesOnlyFrom = referencesEither.filter((id: number) => referencesFrom.includes(id) && !referencesTo.includes(id));
// For items referencing only the from tag, update to the to tag.
await trx(table)
.whereIn(otherIdField, referencesOnlyFrom)
.where({ 'tagId': fromId })
.update({ 'tagId': toId });
// For items referencing both, just remove the reference to the from tag.
await trx(table)
.whereIn(otherIdField, referencesBoth)
.where({ 'tagId': fromId })
.delete();
}
const sPromise = doReplacement('tracks_tags', 'trackId');
const arPromise = doReplacement('artists_tags', 'artistId');
const alPromise = doReplacement('albums_tags', 'albumId');
await Promise.all([sPromise, arPromise, alPromise, cPromise]);
// Delete the original tag.
await trx('tags')
.where({ 'user': userId })
.where({ 'id': fromId })
.del();
})
}