update.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 { VideoMedia } from '../../../generated/graphql-server/src/modules/video-media/video-media.model'
  7. import { Video } from '../../../generated/graphql-server/src/modules/video/video.model'
  8. import { Language } from '../../../generated/graphql-server/src/modules/language/language.model'
  9. import { VideoMediaEncoding } from '../../../generated/graphql-server/src/modules/video-media-encoding/video-media-encoding.model'
  10. import { LicenseEntity } from '../../../generated/graphql-server/src/modules/license-entity/license-entity.model'
  11. import { MediaLocationEntity } from '../../../generated/graphql-server/src/modules/media-location-entity/media-location-entity.model'
  12. import { HttpMediaLocationEntity } from '../../../generated/graphql-server/src/modules/http-media-location-entity/http-media-location-entity.model'
  13. import { JoystreamMediaLocationEntity } from '../../../generated/graphql-server/src/modules/joystream-media-location-entity/joystream-media-location-entity.model'
  14. import {
  15. ICategory,
  16. IChannel,
  17. IHttpMediaLocation,
  18. IJoystreamMediaLocation,
  19. IKnownLicense,
  20. ILanguage,
  21. ILicense,
  22. IMediaLocation,
  23. IReference,
  24. IUserDefinedLicense,
  25. IVideo,
  26. IVideoMedia,
  27. IVideoMediaEncoding,
  28. IWhereCond,
  29. } from '../../types'
  30. import {
  31. HttpMediaLocation,
  32. JoystreamMediaLocation,
  33. KnownLicense,
  34. UserDefinedLicense,
  35. } from '../../../generated/graphql-server/src/modules/variants/variants.model'
  36. import { videoPropertyNamesWithId } from '../content-dir-consts'
  37. function getEntityIdFromReferencedField(ref: IReference, entityIdBeforeTransaction: number): string {
  38. const { entityId, existing } = ref
  39. const id = existing ? entityId : entityIdBeforeTransaction + entityId
  40. return id.toString()
  41. }
  42. async function updateMediaLocationEntityPropertyValues(
  43. db: DB,
  44. where: IWhereCond,
  45. props: IMediaLocation,
  46. entityIdBeforeTransaction: number
  47. ): Promise<void> {
  48. const { httpMediaLocation, joystreamMediaLocation } = props
  49. const record = await db.get(MediaLocationEntity, where)
  50. if (record === undefined) throw Error(`MediaLocation entity not found: ${where.where.id}`)
  51. if (httpMediaLocation) {
  52. const id = getEntityIdFromReferencedField(httpMediaLocation, entityIdBeforeTransaction)
  53. record.httpMediaLocation = await db.get(HttpMediaLocationEntity, { where: { id } })
  54. }
  55. if (joystreamMediaLocation) {
  56. const id = getEntityIdFromReferencedField(joystreamMediaLocation, entityIdBeforeTransaction)
  57. record.joystreamMediaLocation = await db.get(JoystreamMediaLocationEntity, { where: { id } })
  58. }
  59. await db.save<MediaLocationEntity>(record)
  60. }
  61. async function updateLicenseEntityPropertyValues(
  62. db: DB,
  63. where: IWhereCond,
  64. props: ILicense,
  65. entityIdBeforeTransaction: number
  66. ): Promise<void> {
  67. const record = await db.get(LicenseEntity, where)
  68. if (record === undefined) throw Error(`License entity not found: ${where.where.id}`)
  69. const { knownLicense, userDefinedLicense } = props
  70. if (knownLicense) {
  71. const id = getEntityIdFromReferencedField(knownLicense, entityIdBeforeTransaction)
  72. const kLicense = await db.get(KnownLicenseEntity, { where: { id } })
  73. if (!kLicense) throw Error(`KnownLicense not found ${id}`)
  74. const k = new KnownLicense()
  75. k.isTypeOf = 'KnownLicense'
  76. k.code = kLicense.code
  77. k.description = kLicense.description
  78. k.name = kLicense.name
  79. k.url = kLicense.url
  80. // Set the license type
  81. record.type = k
  82. }
  83. if (userDefinedLicense) {
  84. const id = getEntityIdFromReferencedField(userDefinedLicense, entityIdBeforeTransaction)
  85. const udl = await db.get(UserDefinedLicenseEntity, { where: { id } })
  86. if (!udl) throw Error(`UserDefinedLicense not found ${id}`)
  87. const u = new UserDefinedLicense()
  88. u.isTypeOf = 'UserDefinedLicense'
  89. u.content = udl.content
  90. // Set the license type
  91. record.type = u
  92. }
  93. record.attribution = props.attribution || record.attribution
  94. await db.save<LicenseEntity>(record)
  95. }
  96. async function updateCategoryEntityPropertyValues(db: DB, where: IWhereCond, props: ICategory): Promise<void> {
  97. const record = await db.get(Category, where)
  98. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  99. Object.assign(record, props)
  100. await db.save<Category>(record)
  101. }
  102. async function updateChannelEntityPropertyValues(
  103. db: DB,
  104. where: IWhereCond,
  105. props: IChannel,
  106. entityIdBeforeTransaction: number
  107. ): Promise<void> {
  108. const record = await db.get(Channel, where)
  109. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  110. let lang: Language | undefined = record.language
  111. if (props.language) {
  112. const id = getEntityIdFromReferencedField(props.language, entityIdBeforeTransaction)
  113. lang = await db.get(Language, { where: { id } })
  114. if (lang === undefined) throw Error(`Language entity not found: ${id}`)
  115. props.language = undefined
  116. }
  117. Object.assign(record, props)
  118. record.language = lang
  119. await db.save<Channel>(record)
  120. }
  121. async function updateVideoMediaEntityPropertyValues(
  122. db: DB,
  123. where: IWhereCond,
  124. props: IVideoMedia,
  125. entityIdBeforeTransaction: number
  126. ): Promise<void> {
  127. const record = await db.get(VideoMedia, where)
  128. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  129. let enco: VideoMediaEncoding | undefined
  130. let mediaLoc: HttpMediaLocation | JoystreamMediaLocation = record.location
  131. const { encoding, location } = props
  132. if (encoding) {
  133. const id = getEntityIdFromReferencedField(encoding, entityIdBeforeTransaction)
  134. enco = await db.get(VideoMediaEncoding, { where: { id } })
  135. if (enco === undefined) throw Error(`VideoMediaEncoding entity not found: ${id}`)
  136. props.encoding = undefined
  137. }
  138. if (location) {
  139. const id = getEntityIdFromReferencedField(location, entityIdBeforeTransaction)
  140. const mLoc = await db.get(MediaLocationEntity, { where: { id } })
  141. if (!mLoc) throw Error(`MediaLocation entity not found: ${id}`)
  142. const { httpMediaLocation, joystreamMediaLocation } = mLoc
  143. if (httpMediaLocation) {
  144. mediaLoc = new HttpMediaLocation()
  145. mediaLoc.isTypeOf = typeof HttpMediaLocation
  146. mediaLoc.url = httpMediaLocation.url
  147. mediaLoc.port = httpMediaLocation.port
  148. }
  149. if (joystreamMediaLocation) {
  150. mediaLoc = new JoystreamMediaLocation()
  151. mediaLoc.isTypeOf = typeof JoystreamMediaLocation
  152. mediaLoc.dataObjectId = joystreamMediaLocation.dataObjectId
  153. }
  154. props.location = undefined
  155. }
  156. Object.assign(record, props)
  157. record.encoding = enco || record.encoding
  158. record.location = mediaLoc
  159. await db.save<VideoMedia>(record)
  160. }
  161. async function updateVideoEntityPropertyValues(
  162. db: DB,
  163. where: IWhereCond,
  164. props: IVideo,
  165. entityIdBeforeTransaction: number
  166. ): Promise<void> {
  167. const record = await db.get<Video>(Video, where)
  168. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  169. let chann: Channel | undefined
  170. let cat: Category | undefined
  171. let lang: Language | undefined
  172. let vMedia: VideoMedia | undefined
  173. const { channel, category, language, media, license } = props
  174. if (channel) {
  175. const id = getEntityIdFromReferencedField(channel, entityIdBeforeTransaction)
  176. chann = await db.get(Channel, { where: { id } })
  177. if (!chann) throw Error(`Channel entity not found: ${id}`)
  178. props.channel = undefined
  179. }
  180. if (category) {
  181. const id = getEntityIdFromReferencedField(category, entityIdBeforeTransaction)
  182. cat = await db.get(Category, { where: { id } })
  183. if (!cat) throw Error(`Category entity not found: ${id}`)
  184. props.category = undefined
  185. }
  186. if (media) {
  187. const id = getEntityIdFromReferencedField(media, entityIdBeforeTransaction)
  188. vMedia = await db.get(VideoMedia, { where: { id } })
  189. if (!vMedia) throw Error(`VideoMedia entity not found: ${id}`)
  190. props.media = undefined
  191. }
  192. if (license) {
  193. const id = getEntityIdFromReferencedField(license, entityIdBeforeTransaction)
  194. const licenseEntity = await db.get(LicenseEntity, {
  195. where: { id },
  196. relations: ['knownLicense', 'userdefinedLicense'],
  197. })
  198. if (!licenseEntity) throw Error(`License entity not found: ${id}`)
  199. record.license = licenseEntity
  200. props.license = undefined
  201. }
  202. if (language) {
  203. const id = getEntityIdFromReferencedField(language, entityIdBeforeTransaction)
  204. lang = await db.get(Language, { where: { id } })
  205. if (!lang) throw Error(`Language entity not found: ${id}`)
  206. props.language = undefined
  207. }
  208. Object.assign(record, props)
  209. record.channel = chann || record.channel
  210. record.category = cat || record.category
  211. record.media = vMedia || record.media
  212. record.language = lang
  213. await db.save<Video>(record)
  214. }
  215. async function updateUserDefinedLicenseEntityPropertyValues(
  216. db: DB,
  217. where: IWhereCond,
  218. props: IUserDefinedLicense
  219. ): Promise<void> {
  220. const record = await db.get(UserDefinedLicenseEntity, where)
  221. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  222. Object.assign(record, props)
  223. await db.save<UserDefinedLicenseEntity>(record)
  224. }
  225. async function updateKnownLicenseEntityPropertyValues(db: DB, where: IWhereCond, props: IKnownLicense): Promise<void> {
  226. const record = await db.get(KnownLicenseEntity, where)
  227. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  228. Object.assign(record, props)
  229. await db.save<KnownLicenseEntity>(record)
  230. }
  231. async function updateHttpMediaLocationEntityPropertyValues(
  232. db: DB,
  233. where: IWhereCond,
  234. props: IHttpMediaLocation
  235. ): Promise<void> {
  236. const record = await db.get(HttpMediaLocationEntity, where)
  237. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  238. Object.assign(record, props)
  239. await db.save<HttpMediaLocationEntity>(record)
  240. }
  241. async function updateJoystreamMediaLocationEntityPropertyValues(
  242. db: DB,
  243. where: IWhereCond,
  244. props: IJoystreamMediaLocation
  245. ): Promise<void> {
  246. const record = await db.get(JoystreamMediaLocationEntity, where)
  247. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  248. Object.assign(record, props)
  249. await db.save<JoystreamMediaLocationEntity>(record)
  250. }
  251. async function updateLanguageEntityPropertyValues(db: DB, where: IWhereCond, props: ILanguage): Promise<void> {
  252. const record = await db.get(Language, where)
  253. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  254. Object.assign(record, props)
  255. await db.save<Language>(record)
  256. }
  257. async function updateVideoMediaEncodingEntityPropertyValues(
  258. db: DB,
  259. where: IWhereCond,
  260. props: IVideoMediaEncoding
  261. ): Promise<void> {
  262. const record = await db.get(VideoMediaEncoding, where)
  263. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  264. Object.assign(record, props)
  265. await db.save<VideoMediaEncoding>(record)
  266. }
  267. export {
  268. updateCategoryEntityPropertyValues,
  269. updateChannelEntityPropertyValues,
  270. updateVideoMediaEntityPropertyValues,
  271. updateVideoEntityPropertyValues,
  272. updateUserDefinedLicenseEntityPropertyValues,
  273. updateHttpMediaLocationEntityPropertyValues,
  274. updateJoystreamMediaLocationEntityPropertyValues,
  275. updateKnownLicenseEntityPropertyValues,
  276. updateLanguageEntityPropertyValues,
  277. updateVideoMediaEncodingEntityPropertyValues,
  278. updateLicenseEntityPropertyValues,
  279. updateMediaLocationEntityPropertyValues,
  280. }