update.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. const kLicense = await db.get(KnownLicenseEntity, { where: { id } })
  74. if (!kLicense) throw Error(`KnownLicense not found ${id}`)
  75. const k = new 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.content = udl.content
  89. // Set the license type
  90. record.type = u
  91. }
  92. record.attribution = props.attribution || record.attribution
  93. await db.save<LicenseEntity>(record)
  94. }
  95. async function updateCategoryEntityPropertyValues(db: DB, where: IWhereCond, props: ICategory): Promise<void> {
  96. const record = await db.get(Category, where)
  97. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  98. Object.assign(record, props)
  99. await db.save<Category>(record)
  100. }
  101. async function updateChannelEntityPropertyValues(
  102. db: DB,
  103. where: IWhereCond,
  104. props: IChannel,
  105. entityIdBeforeTransaction: number
  106. ): Promise<void> {
  107. const record = await db.get(Channel, where)
  108. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  109. let lang: Language | undefined = record.language
  110. if (props.language) {
  111. const id = getEntityIdFromReferencedField(props.language, entityIdBeforeTransaction)
  112. lang = await db.get(Language, { where: { id } })
  113. if (lang === undefined) throw Error(`Language entity not found: ${id}`)
  114. props.language = undefined
  115. }
  116. Object.assign(record, props)
  117. record.language = lang
  118. await db.save<Channel>(record)
  119. }
  120. async function updateVideoMediaEntityPropertyValues(
  121. db: DB,
  122. where: IWhereCond,
  123. props: IVideoMedia,
  124. entityIdBeforeTransaction: number
  125. ): Promise<void> {
  126. const record = await db.get(VideoMedia, where)
  127. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  128. let enco: VideoMediaEncoding | undefined
  129. let mediaLoc: HttpMediaLocation | JoystreamMediaLocation = record.location
  130. const { encoding, location } = props
  131. if (encoding) {
  132. const id = getEntityIdFromReferencedField(encoding, entityIdBeforeTransaction)
  133. enco = await db.get(VideoMediaEncoding, { where: { id } })
  134. if (enco === undefined) throw Error(`VideoMediaEncoding entity not found: ${id}`)
  135. props.encoding = undefined
  136. }
  137. if (location) {
  138. const id = getEntityIdFromReferencedField(location, entityIdBeforeTransaction)
  139. const mLoc = await db.get(MediaLocationEntity, { where: { id } })
  140. if (!mLoc) throw Error(`MediaLocation entity not found: ${id}`)
  141. const { httpMediaLocation, joystreamMediaLocation } = mLoc
  142. if (httpMediaLocation) {
  143. mediaLoc = new HttpMediaLocation()
  144. mediaLoc.url = httpMediaLocation.url
  145. mediaLoc.port = httpMediaLocation.port
  146. }
  147. if (joystreamMediaLocation) {
  148. mediaLoc = new JoystreamMediaLocation()
  149. mediaLoc.dataObjectId = joystreamMediaLocation.dataObjectId
  150. }
  151. props.location = undefined
  152. }
  153. Object.assign(record, props)
  154. record.encoding = enco || record.encoding
  155. record.location = mediaLoc
  156. await db.save<VideoMedia>(record)
  157. }
  158. async function updateVideoEntityPropertyValues(
  159. db: DB,
  160. where: IWhereCond,
  161. props: IVideo,
  162. entityIdBeforeTransaction: number
  163. ): Promise<void> {
  164. const record = await db.get<Video>(Video, where)
  165. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  166. let chann: Channel | undefined
  167. let cat: Category | undefined
  168. let lang: Language | undefined
  169. let vMedia: VideoMedia | undefined
  170. const { channel, category, language, media, license } = props
  171. if (channel) {
  172. const id = getEntityIdFromReferencedField(channel, entityIdBeforeTransaction)
  173. chann = await db.get(Channel, { where: { id } })
  174. if (!chann) throw Error(`Channel entity not found: ${id}`)
  175. props.channel = undefined
  176. }
  177. if (category) {
  178. const id = getEntityIdFromReferencedField(category, entityIdBeforeTransaction)
  179. cat = await db.get(Category, { where: { id } })
  180. if (!cat) throw Error(`Category entity not found: ${id}`)
  181. props.category = undefined
  182. }
  183. if (media) {
  184. const id = getEntityIdFromReferencedField(media, entityIdBeforeTransaction)
  185. vMedia = await db.get(VideoMedia, { where: { id } })
  186. if (!vMedia) throw Error(`VideoMedia entity not found: ${id}`)
  187. props.media = undefined
  188. }
  189. if (license) {
  190. const id = getEntityIdFromReferencedField(license, entityIdBeforeTransaction)
  191. const licenseEntity = await db.get(LicenseEntity, { where: { id } })
  192. if (!licenseEntity) throw Error(`License entity not found: ${id}`)
  193. record.license = licenseEntity
  194. props.license = undefined
  195. }
  196. if (language) {
  197. const id = getEntityIdFromReferencedField(language, entityIdBeforeTransaction)
  198. lang = await db.get(Language, { where: { id } })
  199. if (!lang) throw Error(`Language entity not found: ${id}`)
  200. props.language = undefined
  201. }
  202. Object.assign(record, props)
  203. record.channel = chann || record.channel
  204. record.category = cat || record.category
  205. record.media = vMedia || record.media
  206. record.language = lang
  207. await db.save<Video>(record)
  208. }
  209. async function updateUserDefinedLicenseEntityPropertyValues(
  210. db: DB,
  211. where: IWhereCond,
  212. props: IUserDefinedLicense
  213. ): Promise<void> {
  214. const record = await db.get(UserDefinedLicenseEntity, where)
  215. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  216. Object.assign(record, props)
  217. await db.save<UserDefinedLicenseEntity>(record)
  218. }
  219. async function updateKnownLicenseEntityPropertyValues(db: DB, where: IWhereCond, props: IKnownLicense): Promise<void> {
  220. const record = await db.get(KnownLicenseEntity, where)
  221. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  222. Object.assign(record, props)
  223. await db.save<KnownLicenseEntity>(record)
  224. }
  225. async function updateHttpMediaLocationEntityPropertyValues(
  226. db: DB,
  227. where: IWhereCond,
  228. props: IHttpMediaLocation
  229. ): Promise<void> {
  230. const record = await db.get(HttpMediaLocationEntity, where)
  231. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  232. Object.assign(record, props)
  233. await db.save<HttpMediaLocationEntity>(record)
  234. }
  235. async function updateJoystreamMediaLocationEntityPropertyValues(
  236. db: DB,
  237. where: IWhereCond,
  238. props: IJoystreamMediaLocation
  239. ): Promise<void> {
  240. const record = await db.get(JoystreamMediaLocationEntity, where)
  241. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  242. Object.assign(record, props)
  243. await db.save<JoystreamMediaLocationEntity>(record)
  244. }
  245. async function updateLanguageEntityPropertyValues(db: DB, where: IWhereCond, props: ILanguage): Promise<void> {
  246. const record = await db.get(Language, where)
  247. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  248. Object.assign(record, props)
  249. await db.save<Language>(record)
  250. }
  251. async function updateVideoMediaEncodingEntityPropertyValues(
  252. db: DB,
  253. where: IWhereCond,
  254. props: IVideoMediaEncoding
  255. ): Promise<void> {
  256. const record = await db.get(VideoMediaEncoding, where)
  257. if (record === undefined) throw Error(`Entity not found: ${where.where.id}`)
  258. Object.assign(record, props)
  259. await db.save<VideoMediaEncoding>(record)
  260. }
  261. async function updateFeaturedVideoEntityPropertyValues(
  262. db: DB,
  263. where: IWhereCond,
  264. props: IFeaturedVideo,
  265. entityIdBeforeTransaction: number
  266. ): Promise<void> {
  267. const record = await db.get(FeaturedVideo, { ...where, relations: ['video'] })
  268. if (record === undefined) throw Error(`FeaturedVideo entity not found: ${where.where.id}`)
  269. if (props.video) {
  270. const id = getEntityIdFromReferencedField(props.video, entityIdBeforeTransaction)
  271. const video = await db.get(Video, { where: { id } })
  272. if (!video) throw Error(`Video entity not found: ${id}`)
  273. // Update old video isFeatured to false
  274. record.video.isFeatured = false
  275. await db.save<Video>(record.video)
  276. video.isFeatured = true
  277. record.video = video
  278. await db.save<Video>(video)
  279. await db.save<FeaturedVideo>(record)
  280. }
  281. }
  282. export {
  283. updateCategoryEntityPropertyValues,
  284. updateChannelEntityPropertyValues,
  285. updateVideoMediaEntityPropertyValues,
  286. updateVideoEntityPropertyValues,
  287. updateUserDefinedLicenseEntityPropertyValues,
  288. updateHttpMediaLocationEntityPropertyValues,
  289. updateJoystreamMediaLocationEntityPropertyValues,
  290. updateKnownLicenseEntityPropertyValues,
  291. updateLanguageEntityPropertyValues,
  292. updateVideoMediaEncodingEntityPropertyValues,
  293. updateLicenseEntityPropertyValues,
  294. updateMediaLocationEntityPropertyValues,
  295. updateFeaturedVideoEntityPropertyValues,
  296. }