metadata.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { DatabaseManager } from '@joystream/hydra-common'
  2. import {
  3. DistributionBucketFamilyMetadata,
  4. DistributionBucketOperatorMetadata,
  5. StorageBucketOperatorMetadata,
  6. GeoCoordinates,
  7. NodeLocationMetadata,
  8. } from 'query-node/dist/model'
  9. import { deserializeMetadata } from '../common'
  10. import { Bytes } from '@polkadot/types'
  11. import {
  12. DistributionBucketOperatorMetadata as DistributionBucketOperatorMetadataProto,
  13. StorageBucketOperatorMetadata as StorageBucketOperatorMetadataProto,
  14. DistributionBucketFamilyMetadata as DistributionBucketFamilyMetadataProto,
  15. INodeLocationMetadata,
  16. } from '@joystream/metadata-protobuf'
  17. import { isSet, isEmptyObject, isValidCountryCode } from '@joystream/metadata-protobuf/utils'
  18. async function processNodeLocationMetadata(
  19. store: DatabaseManager,
  20. current: NodeLocationMetadata | undefined,
  21. meta: INodeLocationMetadata
  22. ): Promise<NodeLocationMetadata> {
  23. const nodeLocation = current || new NodeLocationMetadata()
  24. if (isSet(meta.city)) {
  25. nodeLocation.city = meta.city
  26. }
  27. if (isSet(meta.coordinates)) {
  28. if (isEmptyObject(meta.coordinates)) {
  29. nodeLocation.coordinates = null as any
  30. } else {
  31. const coordinates = current?.coordinates || new GeoCoordinates()
  32. coordinates.latitude = meta.coordinates.latitude || coordinates.latitude || 0
  33. coordinates.longitude = meta.coordinates.longitude || coordinates.longitude || 0
  34. await store.save<GeoCoordinates>(coordinates)
  35. nodeLocation.coordinates = coordinates
  36. }
  37. }
  38. if (isSet(meta.countryCode)) {
  39. if (isValidCountryCode(meta.countryCode)) {
  40. nodeLocation.countryCode = meta.countryCode
  41. } else {
  42. console.warn(`Invalid country code: ${meta.countryCode}`)
  43. nodeLocation.countryCode = null as any
  44. }
  45. }
  46. await store.save<NodeLocationMetadata>(nodeLocation)
  47. return nodeLocation
  48. }
  49. export async function processDistributionOperatorMetadata(
  50. store: DatabaseManager,
  51. current: DistributionBucketOperatorMetadata | undefined,
  52. metadataBytes: Bytes
  53. ): Promise<DistributionBucketOperatorMetadata | undefined> {
  54. const meta = deserializeMetadata(DistributionBucketOperatorMetadataProto, metadataBytes)
  55. if (!meta) {
  56. return current
  57. }
  58. const metadataEntity = current || new DistributionBucketOperatorMetadata()
  59. if (isSet(meta.endpoint)) {
  60. metadataEntity.nodeEndpoint = meta.endpoint
  61. }
  62. if (isSet(meta.location)) {
  63. metadataEntity.nodeLocation = isEmptyObject(meta.location)
  64. ? (null as any)
  65. : await processNodeLocationMetadata(store, metadataEntity.nodeLocation, meta.location)
  66. }
  67. if (isSet(meta.extra)) {
  68. metadataEntity.extra = meta.extra
  69. }
  70. await store.save<DistributionBucketOperatorMetadata>(metadataEntity)
  71. return metadataEntity
  72. }
  73. export async function processStorageOperatorMetadata(
  74. store: DatabaseManager,
  75. current: StorageBucketOperatorMetadata | undefined,
  76. metadataBytes: Bytes
  77. ): Promise<StorageBucketOperatorMetadata | undefined> {
  78. const meta = deserializeMetadata(StorageBucketOperatorMetadataProto, metadataBytes)
  79. if (!meta) {
  80. return current
  81. }
  82. const metadataEntity = current || new StorageBucketOperatorMetadata()
  83. if (isSet(meta.endpoint)) {
  84. metadataEntity.nodeEndpoint = meta.endpoint || (null as any)
  85. }
  86. if (isSet(meta.location)) {
  87. metadataEntity.nodeLocation = isEmptyObject(meta.location)
  88. ? (null as any)
  89. : await processNodeLocationMetadata(store, metadataEntity.nodeLocation, meta.location)
  90. }
  91. if (isSet(meta.extra)) {
  92. metadataEntity.extra = meta.extra || (null as any)
  93. }
  94. await store.save<StorageBucketOperatorMetadata>(metadataEntity)
  95. return metadataEntity
  96. }
  97. export async function processDistributionBucketFamilyMetadata(
  98. store: DatabaseManager,
  99. current: DistributionBucketFamilyMetadata | undefined,
  100. metadataBytes: Bytes
  101. ): Promise<DistributionBucketFamilyMetadata | undefined> {
  102. const meta = deserializeMetadata(DistributionBucketFamilyMetadataProto, metadataBytes)
  103. if (!meta) {
  104. return current
  105. }
  106. const metadataEntity = current || new DistributionBucketFamilyMetadata()
  107. if (isSet(meta.region)) {
  108. metadataEntity.region = meta.region || (null as any)
  109. }
  110. if (isSet(meta.description)) {
  111. metadataEntity.description = meta.description || (null as any)
  112. }
  113. await store.save<DistributionBucketOperatorMetadata>(metadataEntity)
  114. // Update boundary after metadata is saved (since we need an id to reference)
  115. if (isSet(meta.boundary)) {
  116. await Promise.all((metadataEntity.boundary || []).map((coords) => store.remove<GeoCoordinates>(coords)))
  117. await Promise.all(
  118. meta.boundary
  119. .filter((c) => !isEmptyObject(c))
  120. .map(({ latitude, longitude }) =>
  121. store.save<GeoCoordinates>(
  122. new GeoCoordinates({
  123. latitude: latitude || 0,
  124. longitude: longitude || 0,
  125. boundarySourceBucketFamilyMeta: metadataEntity,
  126. })
  127. )
  128. )
  129. )
  130. }
  131. return metadataEntity
  132. }