transaction.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import Debug from 'debug'
  2. import { DB, SubstrateEvent } from '../../generated/indexer'
  3. import { NextEntityId } from '../../generated/graphql-server/src/modules/next-entity-id/next-entity-id.model'
  4. import { ClassEntity } from '../../generated/graphql-server/src/modules/class-entity/class-entity.model'
  5. import { decode } from './decode'
  6. import {
  7. ClassEntityMap,
  8. IBatchOperation,
  9. ICategory,
  10. IChannel,
  11. ICreateEntityOperation,
  12. IDBBlockId,
  13. IEntity,
  14. IFeaturedVideo,
  15. IHttpMediaLocation,
  16. IJoystreamMediaLocation,
  17. IKnownLicense,
  18. ILanguage,
  19. ILicense,
  20. IMediaLocation,
  21. IProperty,
  22. IUserDefinedLicense,
  23. IVideo,
  24. IVideoMedia,
  25. IVideoMediaEncoding,
  26. IWhereCond,
  27. } from '../types'
  28. import {
  29. categoryPropertyNamesWithId,
  30. channelPropertyNamesWithId,
  31. knownLicensePropertyNamesWIthId,
  32. userDefinedLicensePropertyNamesWithId,
  33. joystreamMediaLocationPropertyNamesWithId,
  34. httpMediaLocationPropertyNamesWithId,
  35. videoMediaPropertyNamesWithId,
  36. videoMediaEncodingPropertyNamesWithId,
  37. videoPropertyNamesWithId,
  38. languagePropertyNamesWIthId,
  39. ContentDirectoryKnownClasses,
  40. licensePropertyNamesWithId,
  41. mediaLocationPropertyNamesWithId,
  42. featuredVideoPropertyNamesWithId,
  43. } from './content-dir-consts'
  44. import { getClassName, createBlockOrGetFromDatabase } from './entity/create'
  45. import { getOrCreate } from './get-or-create'
  46. import {
  47. addSchemaToCategory,
  48. addSchemaToChannel,
  49. addSchemaToFeaturedVideo,
  50. addSchemaToHttpMediaLocation,
  51. addSchemaToJoystreamMediaLocation,
  52. addSchemaToKnownLicense,
  53. addSchemaToLanguage,
  54. addSchemaToLicense,
  55. addSchemaToMediaLocation,
  56. addSchemaToUserDefinedLicense,
  57. addSchemaToVideo,
  58. addSchemaToVideoMedia,
  59. addSchemaToVideoMediaEncoding,
  60. } from './entity/addSchema'
  61. import { createDefaultSchema } from './default-schemas'
  62. const debug = Debug('mappings:cd:transaction')
  63. async function getNextEntityId(db: DB): Promise<number> {
  64. const e = await db.get(NextEntityId, { where: { id: '1' } })
  65. // Entity creation happens before addSchemaSupport so this should never happen
  66. if (!e) throw Error(`NextEntityId table doesn't have any record`)
  67. return e.nextId
  68. }
  69. // eslint-disable-next-line @typescript-eslint/naming-convention
  70. export async function contentDirectory_TransactionFailed(db: DB, event: SubstrateEvent): Promise<void> {
  71. debug(`TransactionFailed event: ${JSON.stringify(event)}`)
  72. const failedOperationIndex = event.params[1].value as number
  73. const operations = decode.getOperations(event)
  74. const successfulOperations = operations.toArray().slice(0, failedOperationIndex)
  75. if (!successfulOperations.length) return // No succesfull operations
  76. await applyOperations(decode.getOperationsByTypes(successfulOperations), db, event)
  77. }
  78. // eslint-disable-next-line @typescript-eslint/naming-convention
  79. export async function contentDirectory_TransactionCompleted(db: DB, event: SubstrateEvent): Promise<void> {
  80. debug(`TransactionCompleted event: ${JSON.stringify(event)}`)
  81. const operations = decode.getOperations(event)
  82. await applyOperations(decode.getOperationsByTypes(operations), db, event)
  83. }
  84. async function applyOperations(operations: IBatchOperation, db: DB, event: SubstrateEvent) {
  85. const { addSchemaSupportToEntityOperations, createEntityOperations, updatePropertyValuesOperations } = operations
  86. // Create entities before adding schema support
  87. // We need this to know which entity belongs to which class(we will need to know to update/create
  88. // Channel, Video etc.). For example if there is a property update operation there is no class id
  89. await batchCreateClassEntities(db, event.blockNumber, createEntityOperations)
  90. await batchAddSchemaSupportToEntity(db, createEntityOperations, addSchemaSupportToEntityOperations, event.blockNumber)
  91. await batchUpdatePropertyValue(db, createEntityOperations, updatePropertyValuesOperations)
  92. }
  93. async function batchCreateClassEntities(db: DB, block: number, operations: ICreateEntityOperation[]): Promise<void> {
  94. const nextEntityIdFromDb = await getOrCreate.nextEntityId(db)
  95. let entityId = nextEntityIdFromDb.nextId
  96. for (const { classId } of operations) {
  97. const c = new ClassEntity({
  98. id: entityId.toString(),
  99. classId: classId,
  100. version: block,
  101. happenedIn: await createBlockOrGetFromDatabase(db, block),
  102. })
  103. await db.save<ClassEntity>(c)
  104. // Create default schema for the entity
  105. await createDefaultSchema(db, c)
  106. entityId++
  107. }
  108. // Update database for next entity id
  109. nextEntityIdFromDb.nextId = entityId
  110. await db.save<NextEntityId>(nextEntityIdFromDb)
  111. }
  112. /**
  113. *
  114. * @param db database connection
  115. * @param createEntityOperations: Entity creations with in the same transaction
  116. * @param entities List of entities that schema support is added for
  117. * @param block block number
  118. */
  119. async function batchAddSchemaSupportToEntity(
  120. db: DB,
  121. createEntityOperations: ICreateEntityOperation[],
  122. entities: IEntity[],
  123. block: number
  124. ) {
  125. const classEntityMap: ClassEntityMap = new Map<string, IEntity[]>()
  126. for (const entity of entities) {
  127. const className = await getClassName(db, entity, createEntityOperations)
  128. if (className !== undefined) {
  129. const es = classEntityMap.get(className)
  130. classEntityMap.set(className, es ? [...es, entity] : [entity])
  131. }
  132. }
  133. // This is a copy of classEntityMap, we will use it to keep track of items.
  134. // We will remove items from this list whenever we insert them into db
  135. // const doneList: ClassEntityMap = new Map(classEntityMap.entries())
  136. const nextEntityIdBeforeTransaction = (await getNextEntityId(db)) - createEntityOperations.length
  137. for (const [className, entities] of classEntityMap) {
  138. for (const entity of entities) {
  139. const { entityId, indexOf, properties } = entity
  140. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  141. const id = entityId !== undefined ? entityId : indexOf! + nextEntityIdBeforeTransaction
  142. // const arg: IDBBlockId = { db, block, id: id.toString() }
  143. await addSchemaSupportToEntity(db, className, id, nextEntityIdBeforeTransaction, properties)
  144. }
  145. }
  146. }
  147. /**
  148. * Batch update operations for entity properties values update
  149. * @param db database connection
  150. * @param createEntityOperations Entity creations with in the same transaction
  151. * @param entities list of entities those properties values updated
  152. */
  153. async function batchUpdatePropertyValue(db: DB, createEntityOperations: ICreateEntityOperation[], entities: IEntity[]) {
  154. const entityIdBeforeTransaction = (await getNextEntityId(db)) - createEntityOperations.length
  155. for (const entity of entities) {
  156. const { entityId, indexOf, properties } = entity
  157. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  158. const id = entityId !== undefined ? entityId : entityIdBeforeTransaction - indexOf!
  159. // const where: IWhereCond = { where: { id: id.toString() } }
  160. const className = await getClassName(db, entity, createEntityOperations)
  161. if (!className) {
  162. debug(`Can not update entity properties values. Unknown class name`)
  163. return
  164. }
  165. await addSchemaSupportToEntity(db, className, id, entityIdBeforeTransaction, properties)
  166. }
  167. }
  168. async function addSchemaSupportToEntity(
  169. db: DB,
  170. className: string,
  171. entityId: number,
  172. nextEntityId: number,
  173. properties: IProperty[]
  174. ) {
  175. switch (className) {
  176. case ContentDirectoryKnownClasses.CATEGORY:
  177. await addSchemaToCategory({
  178. db,
  179. entityId,
  180. nextEntityId,
  181. props: decode.setEntityPropertyValues<ICategory>(properties, categoryPropertyNamesWithId),
  182. })
  183. break
  184. case ContentDirectoryKnownClasses.CHANNEL:
  185. await addSchemaToChannel({
  186. db,
  187. entityId,
  188. nextEntityId,
  189. props: decode.setEntityPropertyValues<IChannel>(properties, channelPropertyNamesWithId),
  190. })
  191. break
  192. case ContentDirectoryKnownClasses.KNOWNLICENSE:
  193. await addSchemaToKnownLicense({
  194. db,
  195. entityId,
  196. nextEntityId,
  197. props: decode.setEntityPropertyValues<IKnownLicense>(properties, knownLicensePropertyNamesWIthId),
  198. })
  199. break
  200. case ContentDirectoryKnownClasses.USERDEFINEDLICENSE:
  201. await addSchemaToUserDefinedLicense({
  202. db,
  203. entityId,
  204. nextEntityId,
  205. props: decode.setEntityPropertyValues<IUserDefinedLicense>(properties, userDefinedLicensePropertyNamesWithId),
  206. })
  207. break
  208. case ContentDirectoryKnownClasses.JOYSTREAMMEDIALOCATION:
  209. await addSchemaToJoystreamMediaLocation({
  210. db,
  211. entityId,
  212. nextEntityId,
  213. props: decode.setEntityPropertyValues<IJoystreamMediaLocation>(
  214. properties,
  215. joystreamMediaLocationPropertyNamesWithId
  216. ),
  217. })
  218. break
  219. case ContentDirectoryKnownClasses.HTTPMEDIALOCATION:
  220. await addSchemaToHttpMediaLocation({
  221. db,
  222. entityId,
  223. nextEntityId,
  224. props: decode.setEntityPropertyValues<IHttpMediaLocation>(properties, httpMediaLocationPropertyNamesWithId),
  225. })
  226. break
  227. case ContentDirectoryKnownClasses.VIDEOMEDIA:
  228. await addSchemaToVideoMedia({
  229. db,
  230. entityId,
  231. nextEntityId,
  232. props: decode.setEntityPropertyValues<IVideoMedia>(properties, videoMediaPropertyNamesWithId),
  233. })
  234. break
  235. case ContentDirectoryKnownClasses.VIDEO:
  236. await addSchemaToVideo({
  237. db,
  238. entityId,
  239. nextEntityId,
  240. props: decode.setEntityPropertyValues<IVideo>(properties, videoPropertyNamesWithId),
  241. })
  242. break
  243. case ContentDirectoryKnownClasses.LANGUAGE:
  244. await addSchemaToLanguage({
  245. db,
  246. entityId,
  247. nextEntityId,
  248. props: decode.setEntityPropertyValues<ILanguage>(properties, languagePropertyNamesWIthId),
  249. })
  250. break
  251. case ContentDirectoryKnownClasses.VIDEOMEDIAENCODING:
  252. await addSchemaToVideoMediaEncoding({
  253. db,
  254. entityId,
  255. nextEntityId,
  256. props: decode.setEntityPropertyValues<IVideoMediaEncoding>(properties, videoMediaEncodingPropertyNamesWithId),
  257. })
  258. break
  259. case ContentDirectoryKnownClasses.LICENSE:
  260. await addSchemaToLicense({
  261. db,
  262. entityId,
  263. nextEntityId,
  264. props: decode.setEntityPropertyValues<ILicense>(properties, licensePropertyNamesWithId),
  265. })
  266. break
  267. case ContentDirectoryKnownClasses.MEDIALOCATION:
  268. await addSchemaToMediaLocation({
  269. db,
  270. entityId,
  271. nextEntityId,
  272. props: decode.setEntityPropertyValues<IMediaLocation>(properties, mediaLocationPropertyNamesWithId),
  273. })
  274. break
  275. case ContentDirectoryKnownClasses.FEATUREDVIDEOS:
  276. await addSchemaToFeaturedVideo({
  277. db,
  278. entityId,
  279. nextEntityId,
  280. props: decode.setEntityPropertyValues<IFeaturedVideo>(properties, featuredVideoPropertyNamesWithId),
  281. })
  282. break
  283. default:
  284. debug(`Unknown class name: ${className}`)
  285. break
  286. }
  287. }