update.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 { 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. IWhereCond,
  31. } from '../../types'
  32. import {
  33. HttpMediaLocation,
  34. JoystreamMediaLocation,
  35. KnownLicense,
  36. UserDefinedLicense,
  37. } from '../../../generated/graphql-server/src/modules/variants/variants.model'
  38. function getEntityIdFromReferencedField(ref: IReference, entityIdBeforeTransaction: number): string {
  39. const { entityId, existing } = ref
  40. const id = existing ? entityId : entityIdBeforeTransaction + entityId
  41. return id.toString()
  42. }
  43. async function updateMediaLocationEntityPropertyValues(
  44. db: DB,
  45. where: IWhereCond,
  46. props: IMediaLocation,
  47. entityIdBeforeTransaction: number
  48. ): Promise<void> {
  49. const { httpMediaLocation, joystreamMediaLocation } = props
  50. const record = await db.get(MediaLocationEntity, where)
  51. if (record === undefined) throw Error(`MediaLocation entity not found: ${where.where.id}`)
  52. if (httpMediaLocation) {
  53. const id = getEntityIdFromReferencedField(httpMediaLocation, entityIdBeforeTransaction)
  54. record.httpMediaLocation = await db.get(HttpMediaLocationEntity, { where: { id } })
  55. }
  56. if (joystreamMediaLocation) {
  57. const id = getEntityIdFromReferencedField(joystreamMediaLocation, entityIdBeforeTransaction)
  58. record.joystreamMediaLocation = await db.get(JoystreamMediaLocationEntity, { where: { id } })
  59. }
  60. await db.save<MediaLocationEntity>(record)
  61. }
  62. async function updateLicenseEntityPropertyValues(
  63. db: DB,
  64. where: IWhereCond,
  65. props: ILicense,
  66. entityIdBeforeTransaction: number
  67. ): Promise<void> {
  68. const record = await db.get(LicenseEntity, where)
  69. if (record === undefined) throw Error(`License entity not found: ${where.where.id}`)
  70. const { knownLicense, userDefinedLicense } = props
  71. if (knownLicense) {
  72. const id = getEntityIdFromReferencedField(knownLicense, entityIdBeforeTransaction)
  73. record.knownLicense = await db.get(KnownLicenseEntity, { where: { id } })
  74. }
  75. if (userDefinedLicense) {
  76. const id = getEntityIdFromReferencedField(userDefinedLicense, entityIdBeforeTransaction)
  77. record.userdefinedLicense = await db.get(UserDefinedLicenseEntity, { where: { id } })
  78. }
  79. await db.save<LicenseEntity>(record)
  80. }
  81. async function updateCategoryEntityPropertyValues(db: DB, where: IWhereCond, props: ICategory): Promise<void> {
  82. const record = await db.get(Category, where)
  83. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  84. Object.assign(record, props)
  85. await db.save<Category>(record)
  86. }
  87. async function updateChannelEntityPropertyValues(
  88. db: DB,
  89. where: IWhereCond,
  90. props: IChannel,
  91. entityIdBeforeTransaction: number
  92. ): Promise<void> {
  93. const record = await db.get(Channel, where)
  94. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  95. let lang: Language | undefined = record.language
  96. if (props.language) {
  97. const id = getEntityIdFromReferencedField(props.language, entityIdBeforeTransaction)
  98. lang = await db.get(Language, { where: { id } })
  99. if (lang === undefined) throw Error(`Language entity not found: ${id}`)
  100. props.language = undefined
  101. }
  102. Object.assign(record, props)
  103. record.language = lang
  104. await db.save<Channel>(record)
  105. }
  106. async function updateVideoMediaEntityPropertyValues(
  107. db: DB,
  108. where: IWhereCond,
  109. props: IVideoMedia,
  110. entityIdBeforeTransaction: number
  111. ): Promise<void> {
  112. const record = await db.get(VideoMedia, where)
  113. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  114. let enco: VideoMediaEncoding | undefined
  115. let mediaLoc: HttpMediaLocation | JoystreamMediaLocation = record.location
  116. const { encoding, location } = props
  117. if (encoding) {
  118. const id = getEntityIdFromReferencedField(encoding, entityIdBeforeTransaction)
  119. enco = await db.get(VideoMediaEncoding, { where: { id } })
  120. if (enco === undefined) throw Error(`VideoMediaEncoding entity not found: ${id}`)
  121. props.encoding = undefined
  122. }
  123. if (location) {
  124. const id = getEntityIdFromReferencedField(location, entityIdBeforeTransaction)
  125. const mLoc = await db.get(MediaLocationEntity, { where: { id } })
  126. if (!mLoc) throw Error(`MediaLocation entity not found: ${id}`)
  127. const { httpMediaLocation, joystreamMediaLocation } = mLoc
  128. if (httpMediaLocation) {
  129. mediaLoc = new HttpMediaLocation()
  130. mediaLoc.isTypeOf = typeof HttpMediaLocation
  131. mediaLoc.url = httpMediaLocation.url
  132. mediaLoc.port = httpMediaLocation.port
  133. }
  134. if (joystreamMediaLocation) {
  135. mediaLoc = new JoystreamMediaLocation()
  136. mediaLoc.isTypeOf = typeof JoystreamMediaLocation
  137. mediaLoc.dataObjectId = joystreamMediaLocation.dataObjectId
  138. }
  139. props.location = undefined
  140. }
  141. Object.assign(record, props)
  142. record.encoding = enco || record.encoding
  143. record.location = mediaLoc
  144. await db.save<VideoMedia>(record)
  145. }
  146. async function updateVideoEntityPropertyValues(
  147. db: DB,
  148. where: IWhereCond,
  149. props: IVideo,
  150. entityIdBeforeTransaction: number
  151. ): Promise<void> {
  152. const record = await db.get<Video>(Video, where)
  153. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  154. let chann: Channel | undefined
  155. let cat: Category | undefined
  156. let lang: Language | undefined
  157. let vMedia: VideoMedia | undefined
  158. let lic: KnownLicense | UserDefinedLicense = record.license
  159. const { channel, category, language, media, license } = props
  160. if (channel) {
  161. const id = getEntityIdFromReferencedField(channel, entityIdBeforeTransaction)
  162. chann = await db.get(Channel, { where: { id } })
  163. if (!chann) throw Error(`Channel entity not found: ${id}`)
  164. props.channel = undefined
  165. }
  166. if (category) {
  167. const id = getEntityIdFromReferencedField(category, entityIdBeforeTransaction)
  168. cat = await db.get(Category, { where: { id } })
  169. if (!cat) throw Error(`Category entity not found: ${id}`)
  170. props.category = undefined
  171. }
  172. if (media) {
  173. const id = getEntityIdFromReferencedField(media, entityIdBeforeTransaction)
  174. vMedia = await db.get(VideoMedia, { where: { id } })
  175. if (!vMedia) throw Error(`VideoMedia entity not found: ${id}`)
  176. props.media = undefined
  177. }
  178. if (license) {
  179. const id = getEntityIdFromReferencedField(license, entityIdBeforeTransaction)
  180. const licenseEntity = await db.get(LicenseEntity, {
  181. where: { id },
  182. relations: ['knownLicense', 'userdefinedLicense'],
  183. })
  184. if (!licenseEntity) throw Error(`License entity not found: ${id}`)
  185. const { knownLicense, userdefinedLicense } = licenseEntity
  186. if (knownLicense) {
  187. lic = new KnownLicense()
  188. lic.code = knownLicense.code
  189. lic.description = knownLicense.description
  190. lic.isTypeOf = 'KnownLicense'
  191. lic.name = knownLicense.name
  192. lic.url = knownLicense.url
  193. }
  194. if (userdefinedLicense) {
  195. lic = new UserDefinedLicense()
  196. lic.content = userdefinedLicense.content
  197. lic.isTypeOf = 'UserDefinedLicense'
  198. }
  199. props.license = undefined
  200. }
  201. if (language) {
  202. const id = getEntityIdFromReferencedField(language, entityIdBeforeTransaction)
  203. lang = await db.get(Language, { where: { id } })
  204. if (!lang) throw Error(`Language entity not found: ${id}`)
  205. props.language = undefined
  206. }
  207. Object.assign(record, props)
  208. record.channel = chann || record.channel
  209. record.category = cat || record.category
  210. record.media = vMedia || record.media
  211. record.license = lic
  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. async function updateFeaturedVideoEntityPropertyValues(
  268. db: DB,
  269. where: IWhereCond,
  270. props: IFeaturedVideo,
  271. entityIdBeforeTransaction: number
  272. ): Promise<void> {
  273. const record = await db.get(FeaturedVideo, { ...where, relations: ['video'] })
  274. if (record === undefined) throw Error(`FeaturedVideo entity not found: ${where.where.id}`)
  275. if (props.video) {
  276. const id = getEntityIdFromReferencedField(props.video, entityIdBeforeTransaction)
  277. const video = await db.get(Video, { where: { id } })
  278. if (!video) throw Error(`Video entity not found: ${id}`)
  279. // Update old video isFeatured to false
  280. record.video.isFeatured = false
  281. await db.save<Video>(record.video)
  282. video.isFeatured = true
  283. record.video = video
  284. await db.save<Video>(video)
  285. await db.save<FeaturedVideo>(record)
  286. }
  287. }
  288. export {
  289. updateCategoryEntityPropertyValues,
  290. updateChannelEntityPropertyValues,
  291. updateVideoMediaEntityPropertyValues,
  292. updateVideoEntityPropertyValues,
  293. updateUserDefinedLicenseEntityPropertyValues,
  294. updateHttpMediaLocationEntityPropertyValues,
  295. updateJoystreamMediaLocationEntityPropertyValues,
  296. updateKnownLicenseEntityPropertyValues,
  297. updateLanguageEntityPropertyValues,
  298. updateVideoMediaEncodingEntityPropertyValues,
  299. updateLicenseEntityPropertyValues,
  300. updateMediaLocationEntityPropertyValues,
  301. updateFeaturedVideoEntityPropertyValues,
  302. }