Browse Source

query-node: add 'attribution' field to License entity

metmirr 4 years ago
parent
commit
82090e21f5

+ 1 - 0
query-node/mappings/content-directory/content-dir-consts.ts

@@ -50,6 +50,7 @@ export const channelPropertyNamesWithId: IPropertyWithId = {
 export const licensePropertyNamesWithId: IPropertyWithId = {
   0: { name: 'knownLicense', type: 'number', required: false },
   1: { name: 'userDefinedLicense', type: 'number', required: false },
+  2: { name: 'attribution', type: 'string', required: false },
 }
 
 export const knownLicensePropertyNamesWIthId: IPropertyWithId = {

+ 27 - 31
query-node/mappings/content-directory/entity/create.ts

@@ -254,28 +254,9 @@ async function createVideo(
       nextEntityIdBeforeTransaction
     )
   }
-  if (license !== undefined) {
-    const { knownLicense, userdefinedLicense } = await getOrCreate.license(
-      { db, block, id },
-      classEntityMap,
-      license,
-      nextEntityIdBeforeTransaction
-    )
-    if (knownLicense) {
-      const lic = new KnownLicense()
-      lic.code = knownLicense.code
-      lic.description = knownLicense.description
-      lic.isTypeOf = 'KnownLicense'
-      lic.name = knownLicense.name
-      lic.url = knownLicense.url
-      video.license = lic
-    }
-    if (userdefinedLicense) {
-      const lic = new UserDefinedLicense()
-      lic.content = userdefinedLicense.content
-      lic.isTypeOf = 'UserDefinedLicense'
-      video.license = lic
-    }
+  if (license) {
+    const lic = await getOrCreate.license({ db, block, id }, classEntityMap, license, nextEntityIdBeforeTransaction)
+    video.license = lic
   }
   if (category !== undefined) {
     video.category = await getOrCreate.category(
@@ -337,27 +318,42 @@ async function createLicense(
   const record = await db.get(LicenseEntity, { where: { id } })
   if (record) return record
 
-  const { knownLicense, userDefinedLicense } = p
-
   const license = new LicenseEntity()
-  license.id = id
-  if (knownLicense !== undefined) {
-    license.knownLicense = await getOrCreate.knownLicense(
+
+  if (p.knownLicense) {
+    const kLicense = await getOrCreate.knownLicense(
       { db, block, id },
       classEntityMap,
-      knownLicense,
+      p.knownLicense,
       nextEntityIdBeforeTransaction
     )
+    const k = new KnownLicense()
+    k.isTypeOf = 'KnownLicense'
+    k.code = kLicense.code
+    k.description = kLicense.description
+    k.name = kLicense.name
+    k.url = kLicense.url
+    // Set the license type
+    license.type = k
   }
-  if (userDefinedLicense !== undefined) {
-    license.userdefinedLicense = await getOrCreate.userDefinedLicense(
+  if (p.userDefinedLicense) {
+    const { content } = await getOrCreate.userDefinedLicense(
       { db, block, id },
       classEntityMap,
-      userDefinedLicense,
+      p.userDefinedLicense,
       nextEntityIdBeforeTransaction
     )
+    const u = new UserDefinedLicense()
+    u.isTypeOf = 'UserDefinedLicense'
+    u.content = content
+    // Set the license type
+    license.type = u
   }
+
+  license.id = id
+  license.attribution = p.attribution
   license.happenedIn = await createBlockOrGetFromDatabase(db, block)
+
   await db.save<LicenseEntity>(license)
   return license
 }

+ 5 - 26
query-node/mappings/content-directory/entity/remove.ts

@@ -42,43 +42,22 @@ async function removeLicense(db: DB, where: IWhereCond): Promise<void> {
   const record = await db.get(LicenseEntity, where)
   if (record === undefined) throw Error(`License not found`)
 
-  const { knownLicense, userdefinedLicense } = record
-  let videos: Video[] = []
-
-  if (knownLicense) {
-    videos = await db.getMany(Video, {
-      where: {
-        license: {
-          isTypeOf: 'KnownLicense',
-          code: knownLicense.code,
-          description: knownLicense.description,
-          name: knownLicense.name,
-          url: knownLicense.url,
-        },
-      },
-    })
-  }
-  if (userdefinedLicense) {
-    videos = await db.getMany(Video, {
-      where: { license: { isTypeOf: 'UserDefinedLicense', content: userdefinedLicense.content } },
-    })
+  if (record.videolicense) {
+    // Remove all the videos under this license
+    record.videolicense.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
   }
-  // Remove all the videos under this license
-  videos.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
   await db.remove<LicenseEntity>(record)
 }
+
 async function removeUserDefinedLicense(db: DB, where: IWhereCond): Promise<void> {
   const record = await db.get(UserDefinedLicenseEntity, where)
   if (record === undefined) throw Error(`UserDefinedLicense not found`)
-  if (record.licenseentityuserdefinedLicense)
-    record.licenseentityuserdefinedLicense.map(async (l) => await removeLicense(db, { where: { id: l.id } }))
   await db.remove<UserDefinedLicenseEntity>(record)
 }
+
 async function removeKnownLicense(db: DB, where: IWhereCond): Promise<void> {
   const record = await db.get(KnownLicenseEntity, where)
   if (record === undefined) throw Error(`KnownLicense not found`)
-  if (record.licenseentityknownLicense)
-    record.licenseentityknownLicense.map(async (k) => await removeLicense(db, { where: { id: k.id } }))
   await db.remove<KnownLicenseEntity>(record)
 }
 async function removeMediaLocation(db: DB, where: IWhereCond): Promise<void> {

+ 29 - 18
query-node/mappings/content-directory/entity/update.ts

@@ -34,6 +34,7 @@ import {
   KnownLicense,
   UserDefinedLicense,
 } from '../../../generated/graphql-server/src/modules/variants/variants.model'
+import { videoPropertyNamesWithId } from '../content-dir-consts'
 
 function getEntityIdFromReferencedField(ref: IReference, entityIdBeforeTransaction: number): string {
   const { entityId, existing } = ref
@@ -74,11 +75,28 @@ async function updateLicenseEntityPropertyValues(
   const { knownLicense, userDefinedLicense } = props
   if (knownLicense) {
     const id = getEntityIdFromReferencedField(knownLicense, entityIdBeforeTransaction)
-    record.knownLicense = await db.get(KnownLicenseEntity, { where: { id } })
+    const kLicense = await db.get(KnownLicenseEntity, { where: { id } })
+    if (!kLicense) throw Error(`KnownLicense not found ${id}`)
+
+    const k = new KnownLicense()
+    k.isTypeOf = '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)
-    record.userdefinedLicense = await db.get(UserDefinedLicenseEntity, { where: { id } })
+    const udl = await db.get(UserDefinedLicenseEntity, { where: { id } })
+    if (!udl) throw Error(`UserDefinedLicense not found ${id}`)
+
+    const u = new UserDefinedLicense()
+    u.isTypeOf = 'UserDefinedLicense'
+    u.content = udl.content
+    // Set the license type
+    record.type = u
   }
   await db.save<LicenseEntity>(record)
 }
@@ -89,6 +107,7 @@ async function updateCategoryEntityPropertyValues(db: DB, where: IWhereCond, pro
   Object.assign(record, props)
   await db.save<Category>(record)
 }
+
 async function updateChannelEntityPropertyValues(
   db: DB,
   where: IWhereCond,
@@ -110,6 +129,7 @@ async function updateChannelEntityPropertyValues(
   record.language = lang
   await db.save<Channel>(record)
 }
+
 async function updateVideoMediaEntityPropertyValues(
   db: DB,
   where: IWhereCond,
@@ -154,6 +174,7 @@ async function updateVideoMediaEntityPropertyValues(
   record.location = mediaLoc
   await db.save<VideoMedia>(record)
 }
+
 async function updateVideoEntityPropertyValues(
   db: DB,
   where: IWhereCond,
@@ -167,7 +188,6 @@ async function updateVideoEntityPropertyValues(
   let cat: Category | undefined
   let lang: Language | undefined
   let vMedia: VideoMedia | undefined
-  let lic: KnownLicense | UserDefinedLicense = record.license
 
   const { channel, category, language, media, license } = props
   if (channel) {
@@ -195,20 +215,7 @@ async function updateVideoEntityPropertyValues(
       relations: ['knownLicense', 'userdefinedLicense'],
     })
     if (!licenseEntity) throw Error(`License entity not found: ${id}`)
-    const { knownLicense, userdefinedLicense } = licenseEntity
-    if (knownLicense) {
-      lic = new KnownLicense()
-      lic.code = knownLicense.code
-      lic.description = knownLicense.description
-      lic.isTypeOf = 'KnownLicense'
-      lic.name = knownLicense.name
-      lic.url = knownLicense.url
-    }
-    if (userdefinedLicense) {
-      lic = new UserDefinedLicense()
-      lic.content = userdefinedLicense.content
-      lic.isTypeOf = 'UserDefinedLicense'
-    }
+    record.license = licenseEntity
     props.license = undefined
   }
   if (language) {
@@ -223,11 +230,11 @@ async function updateVideoEntityPropertyValues(
   record.channel = chann || record.channel
   record.category = cat || record.category
   record.media = vMedia || record.media
-  record.license = lic
   record.language = lang
 
   await db.save<Video>(record)
 }
+
 async function updateUserDefinedLicenseEntityPropertyValues(
   db: DB,
   where: IWhereCond,
@@ -238,12 +245,14 @@ async function updateUserDefinedLicenseEntityPropertyValues(
   Object.assign(record, props)
   await db.save<UserDefinedLicenseEntity>(record)
 }
+
 async function updateKnownLicenseEntityPropertyValues(db: DB, where: IWhereCond, props: IKnownLicense): Promise<void> {
   const record = await db.get(KnownLicenseEntity, where)
   if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
   Object.assign(record, props)
   await db.save<KnownLicenseEntity>(record)
 }
+
 async function updateHttpMediaLocationEntityPropertyValues(
   db: DB,
   where: IWhereCond,
@@ -265,12 +274,14 @@ async function updateJoystreamMediaLocationEntityPropertyValues(
   Object.assign(record, props)
   await db.save<JoystreamMediaLocationEntity>(record)
 }
+
 async function updateLanguageEntityPropertyValues(db: DB, where: IWhereCond, props: ILanguage): Promise<void> {
   const record = await db.get(Language, where)
   if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
   Object.assign(record, props)
   await db.save<Language>(record)
 }
+
 async function updateVideoMediaEncodingEntityPropertyValues(
   db: DB,
   where: IWhereCond,

+ 1 - 0
query-node/mappings/types.ts

@@ -115,6 +115,7 @@ export interface IVideo {
 export interface ILicense {
   knownLicense?: IReference
   userDefinedLicense?: IReference
+  attribution?: string
 }
 
 export interface IMediaLocation {

+ 15 - 18
query-node/schema.graphql

@@ -163,21 +163,6 @@ type UserDefinedLicenseEntity @entity {
   happenedIn: Block!
 }
 
-type LicenseEntity @entity {
-  "Runtime entity identifier (EntityId)"
-  id: ID!
-
-  # One of the following field will be non-null
-
-  "Reference to a known license"
-  knownLicense: KnownLicenseEntity
-
-  "Reference to user-defined license"
-  userdefinedLicense: UserDefinedLicenseEntity
-
-  happenedIn: Block!
-}
-
 type MediaLocationEntity @entity {
   "Runtime entity identifier (EntityId)"
   id: ID!
@@ -290,7 +275,7 @@ type Video @entity {
   "Whether the Video contains explicit material."
   isExplicit: Boolean!
 
-  license: License!
+  license: LicenseEntity!
 
   happenedIn: Block!
 }
@@ -308,6 +293,8 @@ type HttpMediaLocation @variant {
   port: Int
 }
 
+union MediaLocation = HttpMediaLocation | JoystreamMediaLocation
+
 type KnownLicense @variant {
   "Short, commonly recognized code of the licence (ie. CC_BY_SA)"
   code: String!
@@ -327,6 +314,16 @@ type UserDefinedLicense @variant {
   content: String!
 }
 
-union MediaLocation = HttpMediaLocation | JoystreamMediaLocation
-
 union License = KnownLicense | UserDefinedLicense
+
+type LicenseEntity @entity {
+  "Runtime entity identifier (EntityId)"
+  id: ID!
+
+  type: License!
+
+  "Attribution (if required by the license)"
+  attribution: String
+
+  happenedIn: Block!
+}