remove.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import Debug from 'debug'
  2. import { DB } from '../../../generated/indexer'
  3. import { Channel } from '../../../generated/graphql-server/src/modules/channel/channel.model'
  4. import { Category } from '../../../generated/graphql-server/src/modules/category/category.model'
  5. import { KnownLicenseEntity } from '../../../generated/graphql-server/src/modules/known-license-entity/known-license-entity.model'
  6. import { UserDefinedLicenseEntity } from '../../../generated/graphql-server/src/modules/user-defined-license-entity/user-defined-license-entity.model'
  7. import { JoystreamMediaLocationEntity } from '../../../generated/graphql-server/src/modules/joystream-media-location-entity/joystream-media-location-entity.model'
  8. import { HttpMediaLocationEntity } from '../../../generated/graphql-server/src/modules/http-media-location-entity/http-media-location-entity.model'
  9. import { VideoMedia } from '../../../generated/graphql-server/src/modules/video-media/video-media.model'
  10. import { Video } from '../../../generated/graphql-server/src/modules/video/video.model'
  11. import { Language } from '../../../generated/graphql-server/src/modules/language/language.model'
  12. import { VideoMediaEncoding } from '../../../generated/graphql-server/src/modules/video-media-encoding/video-media-encoding.model'
  13. import { LicenseEntity } from '../../../generated/graphql-server/src/modules/license-entity/license-entity.model'
  14. import { MediaLocationEntity } from '../../../generated/graphql-server/src/modules/media-location-entity/media-location-entity.model'
  15. import { FeaturedVideo } from '../../../generated/graphql-server/src/modules/featured-video/featured-video.model'
  16. import { IWhereCond } from '../../types'
  17. const debug = Debug('mappings:remove-entity')
  18. async function removeChannel(db: DB, where: IWhereCond): Promise<void> {
  19. const record = await db.get(Channel, where)
  20. if (record === undefined) throw Error(`Channel not found`)
  21. if (record.videos) record.videos.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
  22. await db.remove<Channel>(record)
  23. }
  24. async function removeCategory(db: DB, where: IWhereCond): Promise<void> {
  25. const record = await db.get(Category, where)
  26. if (record === undefined) throw Error(`Category not found`)
  27. if (record.videos) record.videos.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
  28. await db.remove<Category>(record)
  29. }
  30. async function removeVideoMedia(db: DB, where: IWhereCond): Promise<void> {
  31. const record = await db.get(VideoMedia, where)
  32. if (record === undefined) throw Error(`VideoMedia not found`)
  33. if (record.video) await db.remove<Video>(record.video)
  34. await db.remove<VideoMedia>(record)
  35. }
  36. async function removeVideo(db: DB, where: IWhereCond): Promise<void> {
  37. const record = await db.get(Video, where)
  38. if (record === undefined) throw Error(`Video not found`)
  39. await db.remove<Video>(record)
  40. }
  41. async function removeLicense(db: DB, where: IWhereCond): Promise<void> {
  42. const record = await db.get(LicenseEntity, where)
  43. if (record === undefined) throw Error(`License not found`)
  44. const { knownLicense, userdefinedLicense } = record
  45. let videos: Video[] = []
  46. if (knownLicense) {
  47. videos = await db.getMany(Video, {
  48. where: {
  49. license: {
  50. isTypeOf: 'KnownLicense',
  51. code: knownLicense.code,
  52. description: knownLicense.description,
  53. name: knownLicense.name,
  54. url: knownLicense.url,
  55. },
  56. },
  57. })
  58. }
  59. if (userdefinedLicense) {
  60. videos = await db.getMany(Video, {
  61. where: { license: { isTypeOf: 'UserDefinedLicense', content: userdefinedLicense.content } },
  62. })
  63. }
  64. // Remove all the videos under this license
  65. videos.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
  66. await db.remove<LicenseEntity>(record)
  67. }
  68. async function removeUserDefinedLicense(db: DB, where: IWhereCond): Promise<void> {
  69. const record = await db.get(UserDefinedLicenseEntity, where)
  70. if (record === undefined) throw Error(`UserDefinedLicense not found`)
  71. if (record.licenseentityuserdefinedLicense)
  72. record.licenseentityuserdefinedLicense.map(async (l) => await removeLicense(db, { where: { id: l.id } }))
  73. await db.remove<UserDefinedLicenseEntity>(record)
  74. }
  75. async function removeKnownLicense(db: DB, where: IWhereCond): Promise<void> {
  76. const record = await db.get(KnownLicenseEntity, where)
  77. if (record === undefined) throw Error(`KnownLicense not found`)
  78. if (record.licenseentityknownLicense)
  79. record.licenseentityknownLicense.map(async (k) => await removeLicense(db, { where: { id: k.id } }))
  80. await db.remove<KnownLicenseEntity>(record)
  81. }
  82. async function removeMediaLocation(db: DB, where: IWhereCond): Promise<void> {
  83. const record = await db.get(MediaLocationEntity, where)
  84. if (record === undefined) throw Error(`MediaLocation not found`)
  85. if (record.videoMedia) await removeVideo(db, { where: { id: record.videoMedia.id } })
  86. const { httpMediaLocation, joystreamMediaLocation } = record
  87. let videoMedia: VideoMedia | undefined
  88. if (httpMediaLocation) {
  89. videoMedia = await db.get(VideoMedia, {
  90. where: { location: { isTypeOf: 'HttpMediaLocation', url: httpMediaLocation.url, port: httpMediaLocation.port } },
  91. })
  92. }
  93. if (joystreamMediaLocation) {
  94. videoMedia = await db.get(VideoMedia, {
  95. where: { location: { isTypeOf: 'JoystreamMediaLocation', dataObjectId: joystreamMediaLocation.dataObjectId } },
  96. })
  97. }
  98. if (videoMedia) await db.remove<VideoMedia>(videoMedia)
  99. await db.remove<MediaLocationEntity>(record)
  100. }
  101. async function removeHttpMediaLocation(db: DB, where: IWhereCond): Promise<void> {
  102. const record = await db.get(HttpMediaLocationEntity, where)
  103. if (record === undefined) throw Error(`HttpMediaLocation not found`)
  104. if (record.medialocationentityhttpMediaLocation)
  105. record.medialocationentityhttpMediaLocation.map(async (v) => await removeMediaLocation(db, { where: { id: v.id } }))
  106. await db.remove<HttpMediaLocationEntity>(record)
  107. }
  108. async function removeJoystreamMediaLocation(db: DB, where: IWhereCond): Promise<void> {
  109. const record = await db.get(JoystreamMediaLocationEntity, where)
  110. if (record === undefined) throw Error(`JoystreamMediaLocation not found`)
  111. if (record.medialocationentityjoystreamMediaLocation)
  112. record.medialocationentityjoystreamMediaLocation.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
  113. await db.remove<JoystreamMediaLocationEntity>(record)
  114. }
  115. async function removeLanguage(db: DB, where: IWhereCond): Promise<void> {
  116. const record = await db.get(Language, where)
  117. if (record === undefined) throw Error(`Language not found`)
  118. if (record.channellanguage) record.channellanguage.map(async (c) => await removeChannel(db, { where: { id: c.id } }))
  119. if (record.videolanguage) record.videolanguage.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
  120. await db.remove<Language>(record)
  121. }
  122. async function removeVideoMediaEncoding(db: DB, where: IWhereCond): Promise<void> {
  123. const record = await db.get(VideoMediaEncoding, where)
  124. if (record === undefined) throw Error(`Language not found`)
  125. await db.remove<VideoMediaEncoding>(record)
  126. }
  127. async function removeFeaturedVideo(db: DB, where: IWhereCond): Promise<void> {
  128. const record = await db.get(FeaturedVideo, { ...where, relations: ['video'] })
  129. if (!record) throw Error(`FeaturedVideo not found. id: ${where.where.id}`)
  130. record.video.isFeatured = false
  131. record.video.featured = undefined
  132. await db.save<Video>(record.video)
  133. await db.remove<FeaturedVideo>(record)
  134. }
  135. export {
  136. removeCategory,
  137. removeChannel,
  138. removeVideoMedia,
  139. removeVideo,
  140. removeUserDefinedLicense,
  141. removeKnownLicense,
  142. removeHttpMediaLocation,
  143. removeJoystreamMediaLocation,
  144. removeLanguage,
  145. removeVideoMediaEncoding,
  146. removeMediaLocation,
  147. removeLicense,
  148. removeFeaturedVideo,
  149. }