remove.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 { IWhereCond } from '../../types'
  15. async function removeChannel(db: DB, where: IWhereCond): Promise<void> {
  16. const record = await db.get(Channel, where)
  17. if (record === undefined) throw Error(`Channel not found`)
  18. if (record.videos) record.videos.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
  19. await db.remove<Channel>(record)
  20. }
  21. async function removeCategory(db: DB, where: IWhereCond): Promise<void> {
  22. const record = await db.get(Category, where)
  23. if (record === undefined) throw Error(`Category not found`)
  24. if (record.videos) record.videos.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
  25. await db.remove<Category>(record)
  26. }
  27. async function removeVideoMedia(db: DB, where: IWhereCond): Promise<void> {
  28. const record = await db.get(VideoMedia, where)
  29. if (record === undefined) throw Error(`VideoMedia not found`)
  30. if (record.video) await db.remove<Video>(record.video)
  31. await db.remove<VideoMedia>(record)
  32. }
  33. async function removeVideo(db: DB, where: IWhereCond): Promise<void> {
  34. const record = await db.get(Video, where)
  35. if (record === undefined) throw Error(`Video not found`)
  36. await db.remove<Video>(record)
  37. }
  38. async function removeLicense(db: DB, where: IWhereCond): Promise<void> {
  39. const record = await db.get(LicenseEntity, where)
  40. if (record === undefined) throw Error(`License not found`)
  41. const { knownLicense, userdefinedLicense } = record
  42. let videos: Video[] = []
  43. if (knownLicense) {
  44. videos = await db.getMany(Video, {
  45. where: {
  46. license: {
  47. isTypeOf: 'KnownLicense',
  48. code: knownLicense.code,
  49. description: knownLicense.description,
  50. name: knownLicense.name,
  51. url: knownLicense.url,
  52. },
  53. },
  54. })
  55. }
  56. if (userdefinedLicense) {
  57. videos = await db.getMany(Video, {
  58. where: { license: { isTypeOf: 'UserDefinedLicense', content: userdefinedLicense.content } },
  59. })
  60. }
  61. // Remove all the videos under this license
  62. videos.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
  63. await db.remove<LicenseEntity>(record)
  64. }
  65. async function removeUserDefinedLicense(db: DB, where: IWhereCond): Promise<void> {
  66. const record = await db.get(UserDefinedLicenseEntity, where)
  67. if (record === undefined) throw Error(`UserDefinedLicense not found`)
  68. if (record.licenseentityuserdefinedLicense)
  69. record.licenseentityuserdefinedLicense.map(async (l) => await removeLicense(db, { where: { id: l.id } }))
  70. await db.remove<UserDefinedLicenseEntity>(record)
  71. }
  72. async function removeKnownLicense(db: DB, where: IWhereCond): Promise<void> {
  73. const record = await db.get(KnownLicenseEntity, where)
  74. if (record === undefined) throw Error(`KnownLicense not found`)
  75. if (record.licenseentityknownLicense)
  76. record.licenseentityknownLicense.map(async (k) => await removeLicense(db, { where: { id: k.id } }))
  77. await db.remove<KnownLicenseEntity>(record)
  78. }
  79. async function removeMediaLocation(db: DB, where: IWhereCond): Promise<void> {
  80. const record = await db.get(MediaLocationEntity, where)
  81. if (record === undefined) throw Error(`MediaLocation not found`)
  82. if (record.videoMedia) await removeVideo(db, { where: { id: record.videoMedia.id } })
  83. const { httpMediaLocation, joystreamMediaLocation } = record
  84. let videoMedia: VideoMedia | undefined
  85. if (httpMediaLocation) {
  86. videoMedia = await db.get(VideoMedia, {
  87. where: { location: { isTypeOf: 'HttpMediaLocation', url: httpMediaLocation.url, port: httpMediaLocation.port } },
  88. })
  89. }
  90. if (joystreamMediaLocation) {
  91. videoMedia = await db.get(VideoMedia, {
  92. where: { location: { isTypeOf: 'JoystreamMediaLocation', dataObjectId: joystreamMediaLocation.dataObjectId } },
  93. })
  94. }
  95. if (videoMedia) await db.remove<VideoMedia>(videoMedia)
  96. await db.remove<MediaLocationEntity>(record)
  97. }
  98. async function removeHttpMediaLocation(db: DB, where: IWhereCond): Promise<void> {
  99. const record = await db.get(HttpMediaLocationEntity, where)
  100. if (record === undefined) throw Error(`HttpMediaLocation not found`)
  101. if (record.medialocationentityhttpMediaLocation)
  102. record.medialocationentityhttpMediaLocation.map(async (v) => await removeMediaLocation(db, { where: { id: v.id } }))
  103. await db.remove<HttpMediaLocationEntity>(record)
  104. }
  105. async function removeJoystreamMediaLocation(db: DB, where: IWhereCond): Promise<void> {
  106. const record = await db.get(JoystreamMediaLocationEntity, where)
  107. if (record === undefined) throw Error(`JoystreamMediaLocation not found`)
  108. if (record.medialocationentityjoystreamMediaLocation)
  109. record.medialocationentityjoystreamMediaLocation.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
  110. await db.remove<JoystreamMediaLocationEntity>(record)
  111. }
  112. async function removeLanguage(db: DB, where: IWhereCond): Promise<void> {
  113. const record = await db.get(Language, where)
  114. if (record === undefined) throw Error(`Language not found`)
  115. if (record.channellanguage) record.channellanguage.map(async (c) => await removeChannel(db, { where: { id: c.id } }))
  116. if (record.videolanguage) record.videolanguage.map(async (v) => await removeVideo(db, { where: { id: v.id } }))
  117. await db.remove<Language>(record)
  118. }
  119. async function removeVideoMediaEncoding(db: DB, where: IWhereCond): Promise<void> {
  120. const record = await db.get(VideoMediaEncoding, where)
  121. if (record === undefined) throw Error(`Language not found`)
  122. await db.remove<VideoMediaEncoding>(record)
  123. }
  124. export {
  125. removeCategory,
  126. removeChannel,
  127. removeVideoMedia,
  128. removeVideo,
  129. removeUserDefinedLicense,
  130. removeKnownLicense,
  131. removeHttpMediaLocation,
  132. removeJoystreamMediaLocation,
  133. removeLanguage,
  134. removeVideoMediaEncoding,
  135. removeMediaLocation,
  136. removeLicense,
  137. }