ContentMigration.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { WsProvider } from '@polkadot/api'
  2. import { QueryNodeApi } from './sumer-query-node/api'
  3. import { RuntimeApi } from '../RuntimeApi'
  4. import { VideosMigration } from './VideosMigration'
  5. import { ChannelMigration } from './ChannelsMigration'
  6. import { AssetsManager } from './AssetsManager'
  7. export type ContentMigrationConfig = {
  8. queryNodeUri: string
  9. wsProviderEndpointUri: string
  10. sudoUri: string
  11. channelIds: number[]
  12. dataDir: string
  13. channelBatchSize: number
  14. videoBatchSize: number
  15. forceChannelOwnerMemberId: number | undefined
  16. preferredDownloadSpEndpoints?: string[]
  17. uploadSpBucketId: number
  18. uploadSpEndpoint: string
  19. migrationStatePath: string
  20. }
  21. export class ContentMigration {
  22. private api: RuntimeApi
  23. private queryNodeApi: QueryNodeApi
  24. private config: ContentMigrationConfig
  25. constructor(config: ContentMigrationConfig) {
  26. const { queryNodeUri, wsProviderEndpointUri } = config
  27. const provider = new WsProvider(wsProviderEndpointUri)
  28. this.api = new RuntimeApi({ provider })
  29. this.queryNodeApi = new QueryNodeApi(queryNodeUri)
  30. this.config = config
  31. }
  32. private async getForcedChannelOwner(): Promise<{ id: string; controllerAccount: string } | undefined> {
  33. const { forceChannelOwnerMemberId } = this.config
  34. if (forceChannelOwnerMemberId !== undefined) {
  35. const ownerMember = await this.api.query.members.membershipById(forceChannelOwnerMemberId)
  36. if (ownerMember.isEmpty) {
  37. throw new Error(`Membership by id ${forceChannelOwnerMemberId} not found!`)
  38. }
  39. return {
  40. id: forceChannelOwnerMemberId.toString(),
  41. controllerAccount: ownerMember.controller_account.toString(),
  42. }
  43. }
  44. return undefined
  45. }
  46. public async run(): Promise<void> {
  47. const { api, queryNodeApi, config } = this
  48. await this.api.isReadyOrError
  49. const forcedChannelOwner = await this.getForcedChannelOwner()
  50. const assetsManager = await AssetsManager.create({
  51. api,
  52. queryNodeApi,
  53. config,
  54. })
  55. const { idsMap: channelsMap, videoIds } = await new ChannelMigration({
  56. api,
  57. queryNodeApi,
  58. config,
  59. forcedChannelOwner,
  60. assetsManager,
  61. }).run()
  62. await new VideosMigration({
  63. api,
  64. queryNodeApi,
  65. config,
  66. channelsMap,
  67. videoIds,
  68. forcedChannelOwner,
  69. assetsManager,
  70. }).run()
  71. }
  72. }