import { DB } from '../../../generated/indexer' import { Channel } from '../../../generated/graphql-server/src/modules/channel/channel.model' import { Category } from '../../../generated/graphql-server/src/modules/category/category.model' import { KnownLicenseEntity } from '../../../generated/graphql-server/src/modules/known-license-entity/known-license-entity.model' import { UserDefinedLicenseEntity } from '../../../generated/graphql-server/src/modules/user-defined-license-entity/user-defined-license-entity.model' import { VideoMedia } from '../../../generated/graphql-server/src/modules/video-media/video-media.model' import { Video } from '../../../generated/graphql-server/src/modules/video/video.model' import { Language } from '../../../generated/graphql-server/src/modules/language/language.model' import { VideoMediaEncoding } from '../../../generated/graphql-server/src/modules/video-media-encoding/video-media-encoding.model' import { LicenseEntity } from '../../../generated/graphql-server/src/modules/license-entity/license-entity.model' import { MediaLocationEntity } from '../../../generated/graphql-server/src/modules/media-location-entity/media-location-entity.model' import { HttpMediaLocationEntity } from '../../../generated/graphql-server/src/modules/http-media-location-entity/http-media-location-entity.model' import { JoystreamMediaLocationEntity } from '../../../generated/graphql-server/src/modules/joystream-media-location-entity/joystream-media-location-entity.model' import { FeaturedVideo } from '../../../generated/graphql-server/src/modules/featured-video/featured-video.model' import { ICategory, IChannel, IFeaturedVideo, IHttpMediaLocation, IJoystreamMediaLocation, IKnownLicense, ILanguage, ILicense, IMediaLocation, IReference, IUserDefinedLicense, IVideo, IVideoMedia, IVideoMediaEncoding, IWhereCond, } from '../../types' import { HttpMediaLocation, JoystreamMediaLocation, KnownLicense, UserDefinedLicense, } from '../../../generated/graphql-server/src/modules/variants/variants.model' function getEntityIdFromReferencedField(ref: IReference, entityIdBeforeTransaction: number): string { const { entityId, existing } = ref const id = existing ? entityId : entityIdBeforeTransaction + entityId return id.toString() } async function updateMediaLocationEntityPropertyValues( db: DB, where: IWhereCond, props: IMediaLocation, entityIdBeforeTransaction: number ): Promise { const { httpMediaLocation, joystreamMediaLocation } = props const record = await db.get(MediaLocationEntity, where) if (record === undefined) throw Error(`MediaLocation entity not found: ${where.where.id}`) if (httpMediaLocation) { const id = getEntityIdFromReferencedField(httpMediaLocation, entityIdBeforeTransaction) record.httpMediaLocation = await db.get(HttpMediaLocationEntity, { where: { id } }) } if (joystreamMediaLocation) { const id = getEntityIdFromReferencedField(joystreamMediaLocation, entityIdBeforeTransaction) record.joystreamMediaLocation = await db.get(JoystreamMediaLocationEntity, { where: { id } }) } await db.save(record) } async function updateLicenseEntityPropertyValues( db: DB, where: IWhereCond, props: ILicense, entityIdBeforeTransaction: number ): Promise { const record = await db.get(LicenseEntity, where) if (record === undefined) throw Error(`License entity not found: ${where.where.id}`) const { knownLicense, userDefinedLicense } = props if (knownLicense) { const id = getEntityIdFromReferencedField(knownLicense, entityIdBeforeTransaction) const kLicense = await db.get(KnownLicenseEntity, { where: { id } }) if (!kLicense) throw Error(`KnownLicense not found ${id}`) const k = new KnownLicense() k.code = kLicense.code k.description = kLicense.description k.name = kLicense.name k.url = kLicense.url // Set the license type record.type = k } if (userDefinedLicense) { const id = getEntityIdFromReferencedField(userDefinedLicense, entityIdBeforeTransaction) const udl = await db.get(UserDefinedLicenseEntity, { where: { id } }) if (!udl) throw Error(`UserDefinedLicense not found ${id}`) const u = new UserDefinedLicense() u.content = udl.content // Set the license type record.type = u } record.attribution = props.attribution || record.attribution await db.save(record) } async function updateCategoryEntityPropertyValues(db: DB, where: IWhereCond, props: ICategory): Promise { const record = await db.get(Category, where) if (record === undefined) throw Error(`Entity not found: ${where.where.id}`) Object.assign(record, props) await db.save(record) } async function updateChannelEntityPropertyValues( db: DB, where: IWhereCond, props: IChannel, entityIdBeforeTransaction: number ): Promise { const record = await db.get(Channel, where) if (record === undefined) throw Error(`Entity not found: ${where.where.id}`) let lang: Language | undefined = record.language if (props.language) { const id = getEntityIdFromReferencedField(props.language, entityIdBeforeTransaction) lang = await db.get(Language, { where: { id } }) if (lang === undefined) throw Error(`Language entity not found: ${id}`) props.language = undefined } Object.assign(record, props) record.language = lang await db.save(record) } async function updateVideoMediaEntityPropertyValues( db: DB, where: IWhereCond, props: IVideoMedia, entityIdBeforeTransaction: number ): Promise { const record = await db.get(VideoMedia, where) if (record === undefined) throw Error(`Entity not found: ${where.where.id}`) let enco: VideoMediaEncoding | undefined let mediaLoc: HttpMediaLocation | JoystreamMediaLocation = record.location const { encoding, location } = props if (encoding) { const id = getEntityIdFromReferencedField(encoding, entityIdBeforeTransaction) enco = await db.get(VideoMediaEncoding, { where: { id } }) if (enco === undefined) throw Error(`VideoMediaEncoding entity not found: ${id}`) props.encoding = undefined } if (location) { const id = getEntityIdFromReferencedField(location, entityIdBeforeTransaction) const mLoc = await db.get(MediaLocationEntity, { where: { id } }) if (!mLoc) throw Error(`MediaLocation entity not found: ${id}`) const { httpMediaLocation, joystreamMediaLocation } = mLoc if (httpMediaLocation) { mediaLoc = new HttpMediaLocation() mediaLoc.url = httpMediaLocation.url mediaLoc.port = httpMediaLocation.port } if (joystreamMediaLocation) { mediaLoc = new JoystreamMediaLocation() mediaLoc.dataObjectId = joystreamMediaLocation.dataObjectId } props.location = undefined } Object.assign(record, props) record.encoding = enco || record.encoding record.location = mediaLoc await db.save(record) } async function updateVideoEntityPropertyValues( db: DB, where: IWhereCond, props: IVideo, entityIdBeforeTransaction: number ): Promise { const record = await db.get