default-schemas.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import Debug from 'debug'
  2. import { nanoid } from 'nanoid'
  3. import { DB } from '../../generated/indexer'
  4. import { Channel } from '../../generated/graphql-server/src/modules/channel/channel.model'
  5. import { Language } from '../../generated/graphql-server/src/modules/language/language.model'
  6. import { ClassEntity } from '../../generated/graphql-server/src/modules/class-entity/class-entity.model'
  7. import { Video } from '../../generated/graphql-server/src/modules/video/video.model'
  8. import { Category } from '../../generated/graphql-server/src/modules/category/category.model'
  9. import { VideoMedia } from '../../generated/graphql-server/src/modules/video-media/video-media.model'
  10. import { LicenseEntity } from '../../generated/graphql-server/src/modules/license-entity/license-entity.model'
  11. import { VideoMediaEncoding } from '../../generated/graphql-server/src/modules/video-media-encoding/video-media-encoding.model'
  12. import { MediaLocationEntity } from '../../generated/graphql-server/src/modules/media-location-entity/media-location-entity.model'
  13. import { HttpMediaLocationEntity } from '../../generated/graphql-server/src/modules/http-media-location-entity/http-media-location-entity.model'
  14. import { JoystreamMediaLocationEntity } from '../../generated/graphql-server/src/modules/joystream-media-location-entity/joystream-media-location-entity.model'
  15. import { KnownLicenseEntity } from '../../generated/graphql-server/src/modules/known-license-entity/known-license-entity.model'
  16. import { UserDefinedLicenseEntity } from '../../generated/graphql-server/src/modules/user-defined-license-entity/user-defined-license-entity.model'
  17. import { FeaturedVideo } from '../../generated/graphql-server/src/modules/featured-video/featured-video.model'
  18. import { JoystreamMediaLocation } from '../../generated/graphql-server/src/modules/variants/variants.model'
  19. import { contentDirectoryClassNamesWithId, ContentDirectoryKnownClasses } from './content-dir-consts'
  20. const debug = Debug('mappings:default-schema')
  21. /**
  22. * Inserts a default schema for every newly created entity
  23. * @param db DB
  24. * @param ce ClassEntity
  25. */
  26. export async function createDefaultSchema(db: DB, ce: ClassEntity): Promise<void> {
  27. const knownClass = contentDirectoryClassNamesWithId.find((k) => k.classId === ce.classId)
  28. // Its not a known class for query node so we simply return
  29. if (!knownClass) {
  30. debug(`Not a known class. ClassId: ${ce.classId} EntityId: ${ce.id}`)
  31. return
  32. }
  33. switch (knownClass.name) {
  34. case ContentDirectoryKnownClasses.CHANNEL:
  35. await channelDefaultSchema(db, ce)
  36. break
  37. case ContentDirectoryKnownClasses.CATEGORY:
  38. await categoryDefaultSchema(db, ce)
  39. break
  40. case ContentDirectoryKnownClasses.HTTPMEDIALOCATION:
  41. await httpMediaLocationDefaultSchema(db, ce)
  42. break
  43. case ContentDirectoryKnownClasses.JOYSTREAMMEDIALOCATION:
  44. await joystreamMediaLocationDefaultSchema(db, ce)
  45. break
  46. case ContentDirectoryKnownClasses.KNOWNLICENSE:
  47. await knownLicenseDefaultSchema(db, ce)
  48. break
  49. case ContentDirectoryKnownClasses.LANGUAGE:
  50. await languageDefaultSchema(db, ce)
  51. break
  52. case ContentDirectoryKnownClasses.LICENSE:
  53. await licenseDefaultSchema(db, ce)
  54. break
  55. case ContentDirectoryKnownClasses.MEDIALOCATION:
  56. await mediaLocationDefaultSchema(db, ce)
  57. break
  58. case ContentDirectoryKnownClasses.USERDEFINEDLICENSE:
  59. await userDefinedLicenseDefaultSchema(db, ce)
  60. break
  61. case ContentDirectoryKnownClasses.VIDEOMEDIAENCODING:
  62. await videoMediaEncodingDefaultSchema(db, ce)
  63. break
  64. case ContentDirectoryKnownClasses.FEATUREDVIDEOS:
  65. await featuredVideoDefaultSchema(db, ce)
  66. break
  67. case ContentDirectoryKnownClasses.VIDEO:
  68. await videoDefaultSchema(db, ce)
  69. break
  70. case ContentDirectoryKnownClasses.VIDEOMEDIA:
  71. await videoMediaDefaultSchema(db, ce)
  72. break
  73. default:
  74. break
  75. }
  76. }
  77. function commonProperties(ce: ClassEntity) {
  78. return { id: ce.id, happenedIn: ce.happenedIn, version: ce.happenedIn.block }
  79. }
  80. export async function categoryDefaultSchema(db: DB, ce: ClassEntity): Promise<void> {
  81. await db.save<Category>(
  82. new Category({
  83. ...commonProperties(ce),
  84. name: ce.id,
  85. })
  86. )
  87. }
  88. export async function channelDefaultSchema(db: DB, ce: ClassEntity): Promise<void> {
  89. const defaultSchemaFromDb = await db.get(Channel, { where: { id: '0' } })
  90. if (!defaultSchemaFromDb) throw Error(`Channel(0) not found`)
  91. const c = new Channel({
  92. ...commonProperties(ce),
  93. handle: `${ce.id} - ${nanoid()}`,
  94. description: `${ce.id}`,
  95. isPublic: defaultSchemaFromDb.isPublic,
  96. isCurated: defaultSchemaFromDb.isCurated,
  97. })
  98. await db.save<Channel>(c)
  99. }
  100. export async function httpMediaLocationDefaultSchema(db: DB, ce: ClassEntity): Promise<void> {
  101. const defaultSchemaFromDb = await db.get(HttpMediaLocationEntity, { where: { id: '0' } })
  102. if (!defaultSchemaFromDb) throw Error(`HttpMediaLocationEntity(0) not found`)
  103. await db.save<HttpMediaLocationEntity>(
  104. new HttpMediaLocationEntity({
  105. ...commonProperties(ce),
  106. url: defaultSchemaFromDb.url,
  107. })
  108. )
  109. }
  110. export async function joystreamMediaLocationDefaultSchema(db: DB, ce: ClassEntity): Promise<void> {
  111. const defaultSchemaFromDb = await db.get(JoystreamMediaLocationEntity, { where: { id: '0' } })
  112. if (!defaultSchemaFromDb) throw Error(`JoystreamMediaLocationEntity(0) not found`)
  113. await db.save<JoystreamMediaLocationEntity>(
  114. new JoystreamMediaLocationEntity({
  115. ...commonProperties(ce),
  116. dataObjectId: defaultSchemaFromDb.dataObjectId,
  117. })
  118. )
  119. }
  120. export async function knownLicenseDefaultSchema(db: DB, ce: ClassEntity): Promise<void> {
  121. const defaultSchemaFromDb = await db.get(KnownLicenseEntity, { where: { id: '0' } })
  122. if (!defaultSchemaFromDb) throw Error(`KnownLicenseEntity(0) not found`)
  123. await db.save<KnownLicenseEntity>(
  124. new KnownLicenseEntity({
  125. ...commonProperties(ce),
  126. code: `${ce.id}-${defaultSchemaFromDb.code}`,
  127. name: defaultSchemaFromDb.name,
  128. })
  129. )
  130. }
  131. export async function userDefinedLicenseDefaultSchema(db: DB, ce: ClassEntity): Promise<void> {
  132. await db.save<UserDefinedLicenseEntity>(
  133. new UserDefinedLicenseEntity({
  134. ...commonProperties(ce),
  135. content: `${ce.id}`,
  136. })
  137. )
  138. }
  139. export async function languageDefaultSchema(db: DB, ce: ClassEntity): Promise<void> {
  140. await db.save<Language>(
  141. new Language({
  142. ...commonProperties(ce),
  143. code: `${ce.id}`,
  144. name: `${ce.id}`,
  145. })
  146. )
  147. }
  148. export async function licenseDefaultSchema(db: DB, ce: ClassEntity): Promise<void> {
  149. const defaultSchemaFromDb = await db.get(LicenseEntity, { where: { id: '0' } })
  150. if (!defaultSchemaFromDb) throw Error(`LicenseEntity(0) not found`)
  151. await db.save<LicenseEntity>(
  152. new LicenseEntity({
  153. ...commonProperties(ce),
  154. type: defaultSchemaFromDb.type,
  155. })
  156. )
  157. }
  158. export async function mediaLocationDefaultSchema(db: DB, ce: ClassEntity): Promise<void> {
  159. const defaultSchemaFromDb = await db.get(MediaLocationEntity, {
  160. where: { id: '0' },
  161. relations: ['joystreamMediaLocation'],
  162. })
  163. if (!defaultSchemaFromDb) throw Error(`MediaLocationEntity(0) not found`)
  164. const m = new MediaLocationEntity()
  165. m.joystreamMediaLocation = defaultSchemaFromDb.joystreamMediaLocation
  166. await db.save<MediaLocationEntity>(
  167. new MediaLocationEntity({
  168. ...commonProperties(ce),
  169. joystreamMediaLocation: defaultSchemaFromDb.joystreamMediaLocation,
  170. })
  171. )
  172. }
  173. export async function videoMediaEncodingDefaultSchema(db: DB, ce: ClassEntity): Promise<void> {
  174. const defaultSchemaFromDb = await db.get(VideoMediaEncoding, { where: { id: '0' } })
  175. if (!defaultSchemaFromDb) throw Error(`VideoMediaEncoding(0) not found`)
  176. await db.save<VideoMediaEncoding>(
  177. new VideoMediaEncoding({
  178. ...commonProperties(ce),
  179. name: defaultSchemaFromDb.name,
  180. })
  181. )
  182. }
  183. export async function featuredVideoDefaultSchema(db: DB, ce: ClassEntity): Promise<void> {
  184. const video = await db.get(Video, { where: { id: '0' } })
  185. if (!video) throw Error(`Video(0) not found for FeaturedVideo(${ce.id}) creation`)
  186. await db.save<FeaturedVideo>(
  187. new FeaturedVideo({
  188. ...commonProperties(ce),
  189. video,
  190. })
  191. )
  192. }
  193. export async function videoDefaultSchema(db: DB, ce: ClassEntity): Promise<void> {
  194. const defaultSchemaFromDb = await db.get(Video, { where: { id: '0' } })
  195. if (!defaultSchemaFromDb) throw Error(`Video(0) not found`)
  196. const v = new Video({
  197. ...commonProperties(ce),
  198. })
  199. // ///// default relations ///////
  200. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  201. const filmAnimation = await db.get(Category, { where: { name: 'Film & Animation' } })
  202. v.category = filmAnimation || (await db.get(Category, { where: { id: '0' } }))!
  203. v.channel = (await db.get(Channel, { where: { id: '0' } }))!
  204. v.license = (await db.get(LicenseEntity, { where: { id: '0' } }))!
  205. // const media = (await db.get(VideoMedia, { where: { id: '0' } }))!
  206. // const encoding = new VideoMediaEncoding({
  207. // name: `${Date.now()}`,
  208. // ...commonProperties(ce),
  209. // id: `${Date.now()}`,
  210. // })
  211. // await db.save<VideoMediaEncoding>(encoding)
  212. const encoding = await db.get(VideoMediaEncoding, { where: { id: 0 } })
  213. const mediaLoc = await db.get(MediaLocationEntity, { where: { id: '0' }, relations: ['joystreamMediaLocation'] })
  214. if (!mediaLoc) throw Error(`MediaLocationEntity(0) not found while creating default schema for video`)
  215. const location = new JoystreamMediaLocation()
  216. location.dataObjectId = mediaLoc.joystreamMediaLocation!.dataObjectId
  217. const media = await db.get(VideoMedia, { where: { id: '0' } })
  218. if (!media) throw Error(`VideoMedia(0) not found while creating default schema for video`)
  219. const newMedia = new VideoMedia({
  220. ...commonProperties(ce),
  221. location,
  222. encoding,
  223. id: `${Date.now()}`, // override id
  224. pixelHeight: media.pixelHeight,
  225. pixelWidth: media.pixelWidth,
  226. })
  227. await db.save<VideoMedia>(newMedia)
  228. v.media = newMedia
  229. // ///// default relations ///////
  230. // Get default schema for video entity
  231. // const defaultSchemaFromDb = (await db.get(Video, { where: { id: '0' } }))!
  232. /* eslint-enable @typescript-eslint/no-non-null-assertion */
  233. v.title = ce.id
  234. v.description = ce.id
  235. v.duration = defaultSchemaFromDb.duration
  236. v.thumbnailUrl = defaultSchemaFromDb.thumbnailUrl
  237. v.isPublic = defaultSchemaFromDb.isPublic
  238. v.isCurated = defaultSchemaFromDb.isCurated
  239. v.isExplicit = defaultSchemaFromDb.isExplicit
  240. v.isFeatured = defaultSchemaFromDb.isFeatured
  241. await db.save<Video>(v)
  242. }
  243. export async function videoMediaDefaultSchema(db: DB, ce: ClassEntity): Promise<void> {
  244. const media = await db.get(VideoMedia, { where: { id: '0' } })
  245. if (!media) throw Error(`VideoMedia(0) not found`)
  246. const encoding = await db.get(VideoMediaEncoding, { where: { id: '0' } })
  247. const mediaLoc = await db.get(MediaLocationEntity, { where: { id: '0' }, relations: ['joystreamMediaLocation'] })
  248. if (!mediaLoc) throw Error(`MediaLocationEntity(0) not found while creating default schema for video`)
  249. const location = new JoystreamMediaLocation()
  250. location.dataObjectId = mediaLoc.joystreamMediaLocation!.dataObjectId
  251. const vm = new VideoMedia({
  252. ...commonProperties(ce),
  253. pixelHeight: media.pixelHeight,
  254. pixelWidth: media.pixelWidth,
  255. encoding,
  256. location,
  257. })
  258. await db.save<VideoMedia>(vm)
  259. }