addSchema.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import { DB } from '../../../generated/indexer'
  2. import { Channel } from '../../../generated/graphql-server/src/modules/channel/channel.model'
  3. import { Category } from '../../../generated/graphql-server/src/modules/category/category.model'
  4. import { KnownLicenseEntity } from '../../../generated/graphql-server/src/modules/known-license-entity/known-license-entity.model'
  5. import { UserDefinedLicenseEntity } from '../../../generated/graphql-server/src/modules/user-defined-license-entity/user-defined-license-entity.model'
  6. import { JoystreamMediaLocationEntity } from '../../../generated/graphql-server/src/modules/joystream-media-location-entity/joystream-media-location-entity.model'
  7. import { HttpMediaLocationEntity } from '../../../generated/graphql-server/src/modules/http-media-location-entity/http-media-location-entity.model'
  8. import { VideoMedia } from '../../../generated/graphql-server/src/modules/video-media/video-media.model'
  9. import { Video } from '../../../generated/graphql-server/src/modules/video/video.model'
  10. import { Language } from '../../../generated/graphql-server/src/modules/language/language.model'
  11. import { VideoMediaEncoding } from '../../../generated/graphql-server/src/modules/video-media-encoding/video-media-encoding.model'
  12. import { LicenseEntity } from '../../../generated/graphql-server/src/modules/license-entity/license-entity.model'
  13. import { MediaLocationEntity } from '../../../generated/graphql-server/src/modules/media-location-entity/media-location-entity.model'
  14. import { FeaturedVideo } from '../../../generated/graphql-server/src/modules/featured-video/featured-video.model'
  15. import {
  16. ICategory,
  17. IChannel,
  18. IFeaturedVideo,
  19. IHttpMediaLocation,
  20. IJoystreamMediaLocation,
  21. IKnownLicense,
  22. ILanguage,
  23. ILicense,
  24. IMediaLocation,
  25. IReference,
  26. IUserDefinedLicense,
  27. IVideo,
  28. IVideoMedia,
  29. IVideoMediaEncoding,
  30. } from '../../types'
  31. import {
  32. HttpMediaLocation,
  33. JoystreamMediaLocation,
  34. KnownLicense,
  35. UserDefinedLicense,
  36. } from '../../../generated/graphql-server/src/modules/variants/variants.model'
  37. type SchemaSupport<T> = { db: DB; entityId: number; props: T; nextEntityId: number }
  38. function getEntityIdFromRef(ref: IReference, nextEntityId: number) {
  39. const { entityId, existing } = ref
  40. return (existing ? entityId : entityId + nextEntityId).toString()
  41. }
  42. export async function addSchemaToChannel(param: SchemaSupport<IChannel>): Promise<void> {
  43. const { entityId, nextEntityId, props, db } = param
  44. const c = await db.get(Channel, { where: { id: entityId.toString() } })
  45. if (!c) throw Error(`Channel(${entityId}) has never been created!`)
  46. c.handle = props.handle || c.handle
  47. c.description = props.description || c.description
  48. c.isCurated = !!props.isCurated
  49. c.isPublic = props.isPublic || c.isPublic
  50. c.coverPhotoUrl = props.coverPhotoUrl || c.coverPhotoUrl
  51. c.avatarPhotoUrl = props.avatarPhotoUrl || c.avatarPhotoUrl
  52. const { language } = props
  53. if (language) {
  54. c.language = await db.get(Language, { where: { id: getEntityIdFromRef(language, nextEntityId) } })
  55. }
  56. await db.save<Channel>(c)
  57. }
  58. export async function addSchemaToCategory(param: SchemaSupport<ICategory>): Promise<void> {
  59. const { entityId, props, db } = param
  60. const c = await db.get(Category, { where: { id: entityId.toString() } })
  61. if (!c) throw Error(`Category(${entityId}) has never been created!`)
  62. c.name = props.name || c.name
  63. c.description = props.description || c.description
  64. await db.save<Category>(c)
  65. }
  66. export async function addSchemaToHttpMediaLocation(param: SchemaSupport<IHttpMediaLocation>): Promise<void> {
  67. const { entityId, props, db } = param
  68. const c = await db.get(HttpMediaLocationEntity, { where: { id: entityId.toString() } })
  69. if (!c) throw Error(`HttpMediaLocationEntity(${entityId}) has never been created!`)
  70. c.url = props.url || c.url
  71. c.port = props.port || c.port
  72. await db.save<HttpMediaLocationEntity>(c)
  73. }
  74. export async function addSchemaToJoystreamMediaLocation(param: SchemaSupport<IJoystreamMediaLocation>): Promise<void> {
  75. const { entityId, props, db } = param
  76. const j = await db.get(JoystreamMediaLocationEntity, { where: { id: entityId.toString() } })
  77. if (!j) throw Error(`JoystreamMediaLocationEntity(${entityId}) has never been created!`)
  78. j.dataObjectId = props.dataObjectId || j.dataObjectId
  79. await db.save<JoystreamMediaLocationEntity>(j)
  80. }
  81. export async function addSchemaToKnownLicense(param: SchemaSupport<IKnownLicense>): Promise<void> {
  82. const { entityId, props, db } = param
  83. const k = await db.get(KnownLicenseEntity, { where: { id: entityId.toString() } })
  84. if (!k) throw Error(`KnownLicenseEntity(${entityId}) has never been created!`)
  85. k.code = props.code || k.code
  86. k.description = props.description || k.description
  87. k.name = props.name || k.name
  88. k.url = props.url || k.url
  89. await db.save<KnownLicenseEntity>(k)
  90. }
  91. export async function addSchemaToLanguage(param: SchemaSupport<ILanguage>): Promise<void> {
  92. const { entityId, props, db } = param
  93. const l = await db.get(Language, { where: { id: entityId.toString() } })
  94. if (!l) throw Error(`Language(${entityId}) has never been created!`)
  95. l.code = props.code || l.code
  96. l.name = props.name || l.name
  97. await db.save<Language>(l)
  98. }
  99. export async function addSchemaToLicense(param: SchemaSupport<ILicense>): Promise<void> {
  100. const { entityId, props, db, nextEntityId } = param
  101. const l = await db.get(LicenseEntity, { where: { id: entityId.toString() } })
  102. if (!l) throw Error(`LicenseEntity(${entityId}) has never been created!`)
  103. const { knownLicense, userDefinedLicense } = props
  104. let licenseEntityId
  105. if (knownLicense) {
  106. licenseEntityId = getEntityIdFromRef(knownLicense, nextEntityId)
  107. const k = await db.get(KnownLicenseEntity, { where: { id: licenseEntityId } })
  108. if (!k) throw Error(`KnownLicenseEntity(${licenseEntityId}) not found`)
  109. const licenseType = new KnownLicense()
  110. licenseType.code = k.code
  111. licenseType.description = k.description
  112. licenseType.name = k.name
  113. licenseType.url = k.url
  114. l.type = licenseType
  115. }
  116. if (userDefinedLicense) {
  117. licenseEntityId = getEntityIdFromRef(userDefinedLicense, nextEntityId)
  118. const u = await db.get(UserDefinedLicenseEntity, {
  119. where: { id: licenseEntityId },
  120. })
  121. if (!u) throw Error(`UserDefinedLicenseEntity(${licenseEntityId}) not found`)
  122. const licenseType = new UserDefinedLicense()
  123. licenseType.content = u.content
  124. l.type = licenseType
  125. }
  126. l.attribution = props.attribution || l.attribution
  127. await db.save<LicenseEntity>(l)
  128. }
  129. export async function addSchemaToMediaLocation(param: SchemaSupport<IMediaLocation>): Promise<void> {
  130. const { entityId, props, db, nextEntityId } = param
  131. const m = await db.get(MediaLocationEntity, { where: { id: entityId.toString() } })
  132. if (!m) throw Error(`MediaLocationEntity(${entityId}) has never been created!`)
  133. const { httpMediaLocation, joystreamMediaLocation } = props
  134. if (httpMediaLocation) {
  135. m.httpMediaLocation =
  136. (await db.get(HttpMediaLocationEntity, {
  137. where: { id: getEntityIdFromRef(httpMediaLocation, nextEntityId) },
  138. })) || m.httpMediaLocation
  139. }
  140. if (joystreamMediaLocation) {
  141. m.joystreamMediaLocation =
  142. (await db.get(JoystreamMediaLocationEntity, {
  143. where: { id: getEntityIdFromRef(joystreamMediaLocation, nextEntityId) },
  144. })) || m.joystreamMediaLocation
  145. }
  146. await db.save<MediaLocationEntity>(m)
  147. }
  148. export async function addSchemaToUserDefinedLicense(param: SchemaSupport<IUserDefinedLicense>): Promise<void> {
  149. const { entityId, props, db } = param
  150. const u = await db.get(UserDefinedLicenseEntity, { where: { id: entityId.toString() } })
  151. if (!u) throw Error(`UserDefinedLicenseEntity(${entityId}) has never been created!`)
  152. u.content = props.content || u.content
  153. await db.save<UserDefinedLicenseEntity>(u)
  154. }
  155. export async function addSchemaToVideoMedia(param: SchemaSupport<IVideoMedia>): Promise<void> {
  156. const { entityId, props, db, nextEntityId } = param
  157. const v = await db.get(VideoMedia, { where: { id: entityId.toString() } })
  158. if (!v) throw Error(`VideoMedia(${entityId}) has never been created!`)
  159. v.pixelHeight = props.pixelHeight || v.pixelHeight
  160. v.pixelWidth = props.pixelWidth || v.pixelWidth
  161. v.size = props.size || v.size
  162. const { location, encoding } = props
  163. if (encoding) {
  164. const encodingId = getEntityIdFromRef(encoding, nextEntityId)
  165. const encod = await db.get(VideoMediaEncoding, { where: { id: encodingId } })
  166. if (!encod) throw Error(`VideoMediaEncoding(${encodingId}) has never been created!`)
  167. v.encoding = encod
  168. }
  169. if (location) {
  170. const mediaLocation = await db.get(MediaLocationEntity, {
  171. where: { id: getEntityIdFromRef(location, nextEntityId) },
  172. relations: ['httpMediaLocation', 'joystreamMediaLocation'],
  173. })
  174. v.locationEntity = mediaLocation
  175. if (mediaLocation) {
  176. const { httpMediaLocation, joystreamMediaLocation } = mediaLocation
  177. if (httpMediaLocation) {
  178. const mediaLoc = new HttpMediaLocation()
  179. mediaLoc.port = httpMediaLocation.port
  180. mediaLoc.url = httpMediaLocation.url
  181. v.location = mediaLoc
  182. }
  183. if (joystreamMediaLocation) {
  184. const mediaLoc = new JoystreamMediaLocation()
  185. mediaLoc.dataObjectId = joystreamMediaLocation.dataObjectId
  186. v.location = mediaLoc
  187. }
  188. }
  189. }
  190. await db.save<VideoMedia>(v)
  191. }
  192. export async function addSchemaToVideoMediaEncoding(param: SchemaSupport<IVideoMediaEncoding>): Promise<void> {
  193. const { entityId, props, db } = param
  194. const e = await db.get(VideoMediaEncoding, { where: { id: entityId.toString() } })
  195. if (!e) throw Error(`VideoMediaEncoding(${entityId}) has never been created!`)
  196. e.name = props.name || e.name
  197. await db.save<VideoMediaEncoding>(e)
  198. }
  199. export async function addSchemaToFeaturedVideo(param: SchemaSupport<IFeaturedVideo>): Promise<void> {
  200. const { entityId, props, db, nextEntityId } = param
  201. const f = await db.get(FeaturedVideo, { where: { id: entityId.toString() } })
  202. if (!f) throw Error(`FeaturedVideo(${entityId}) has never been created!`)
  203. if (props.video) {
  204. const videoId = getEntityIdFromRef(props.video, nextEntityId)
  205. const v = await db.get(Video, { where: { id: videoId } })
  206. if (!v) throw Error(`Video(${videoId}) has never been created!`)
  207. f.video = v
  208. }
  209. await db.save<FeaturedVideo>(f)
  210. }
  211. export async function addSchemaToVideo(param: SchemaSupport<IVideo>): Promise<void> {
  212. const { entityId, nextEntityId, props, db } = param
  213. const v = await db.get(Video, { where: { id: entityId.toString() } })
  214. if (!v) throw Error(`Video(${entityId}) has never been created!`)
  215. v.title = props.title || v.title
  216. v.description = props.description || v.description
  217. v.duration = props.duration || v.duration
  218. v.hasMarketing = props.hasMarketing || v.hasMarketing
  219. v.isCurated = !!props.isCurated
  220. v.isExplicit = props.isExplicit || v.isExplicit
  221. v.isPublic = props.isPublic || v.isPublic
  222. v.publishedBeforeJoystream = props.publishedBeforeJoystream || v.publishedBeforeJoystream
  223. v.skippableIntroDuration = props.skippableIntroDuration || v.skippableIntroDuration
  224. v.thumbnailUrl = props.thumbnailUrl || v.thumbnailUrl
  225. v.isFeatured = !!v.isFeatured
  226. const { language, license, category, channel, media } = props
  227. if (language) {
  228. v.language = await db.get(Language, { where: { id: getEntityIdFromRef(language, nextEntityId) } })
  229. }
  230. if (license) {
  231. const licenseId = getEntityIdFromRef(license, nextEntityId)
  232. const l = await db.get(LicenseEntity, { where: { id: licenseId } })
  233. if (!l) throw Error(`LicenseEntity(${licenseId}) has never been created!`)
  234. v.license = l
  235. }
  236. if (category) {
  237. const categoryId = getEntityIdFromRef(category, nextEntityId)
  238. const c = await db.get(Category, { where: { id: categoryId } })
  239. if (!c) throw Error(`Category(${categoryId}) has never been created!`)
  240. v.category = c
  241. }
  242. if (channel) {
  243. const channelId = getEntityIdFromRef(channel, nextEntityId)
  244. const c = await db.get(Channel, { where: { id: channelId } })
  245. if (!c) throw Error(`Channel(${channelId}) has never been created!`)
  246. v.channel = c
  247. }
  248. if (media) {
  249. v.media = await db.get(VideoMedia, { where: { id: getEntityIdFromRef(media, nextEntityId) } })
  250. }
  251. /* eslint-enable @typescript-eslint/no-non-null-assertion */
  252. await db.save<Video>(v)
  253. }