utils.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. import { DatabaseManager, EventContext, StoreContext } from '@joystream/hydra-common'
  2. import { FindConditions } from 'typeorm'
  3. import {
  4. IVideoMetadata,
  5. IPublishedBeforeJoystream,
  6. ILicense,
  7. IMediaType,
  8. IChannelMetadata,
  9. } from '@joystream/metadata-protobuf'
  10. import { integrateMeta, isSet, isValidLanguageCode } from '@joystream/metadata-protobuf/utils'
  11. import { invalidMetadata, inconsistentState, logger, deterministicEntityId } from '../common'
  12. import {
  13. // primary entities
  14. CuratorGroup,
  15. Channel,
  16. Video,
  17. VideoCategory,
  18. // secondary entities
  19. Language,
  20. License,
  21. VideoMediaMetadata,
  22. // asset
  23. Membership,
  24. VideoMediaEncoding,
  25. ChannelCategory,
  26. StorageDataObject,
  27. DataObjectTypeChannelAvatar,
  28. DataObjectTypeChannelCoverPhoto,
  29. DataObjectTypeVideoMedia,
  30. DataObjectTypeVideoThumbnail,
  31. } from 'query-node/dist/model'
  32. // Joystream types
  33. import { ContentActor, StorageAssets } from '@joystream/types/augment'
  34. import { DecodedMetadataObject } from '@joystream/metadata-protobuf/types'
  35. import BN from 'bn.js'
  36. import { getMostRecentlyCreatedDataObjects } from '../storage/utils'
  37. const ASSET_TYPES = {
  38. channel: [
  39. {
  40. DataObjectTypeConstructor: DataObjectTypeChannelCoverPhoto,
  41. metaFieldName: 'coverPhoto',
  42. schemaFieldName: 'coverPhoto',
  43. },
  44. {
  45. DataObjectTypeConstructor: DataObjectTypeChannelAvatar,
  46. metaFieldName: 'avatarPhoto',
  47. schemaFieldName: 'avatarPhoto',
  48. },
  49. ],
  50. video: [
  51. {
  52. DataObjectTypeConstructor: DataObjectTypeVideoMedia,
  53. metaFieldName: 'video',
  54. schemaFieldName: 'media',
  55. },
  56. {
  57. DataObjectTypeConstructor: DataObjectTypeVideoThumbnail,
  58. metaFieldName: 'thumbnailPhoto',
  59. schemaFieldName: 'thumbnailPhoto',
  60. },
  61. ],
  62. } as const
  63. async function processChannelAssets(
  64. { event, store }: EventContext & StoreContext,
  65. assets: StorageDataObject[],
  66. channel: Channel,
  67. meta: DecodedMetadataObject<IChannelMetadata>
  68. ) {
  69. await Promise.all(
  70. ASSET_TYPES.channel.map(async ({ metaFieldName, schemaFieldName, DataObjectTypeConstructor }) => {
  71. const newAssetIndex = meta[metaFieldName]
  72. const currentAsset = channel[schemaFieldName]
  73. if (isSet(newAssetIndex)) {
  74. const asset = findAssetByIndex(assets, newAssetIndex)
  75. if (asset) {
  76. if (currentAsset) {
  77. currentAsset.unsetAt = new Date(event.blockTimestamp)
  78. await store.save<StorageDataObject>(currentAsset)
  79. }
  80. const dataObjectType = new DataObjectTypeConstructor()
  81. dataObjectType.channelId = channel.id
  82. asset.type = dataObjectType
  83. channel[schemaFieldName] = asset
  84. await store.save<StorageDataObject>(asset)
  85. }
  86. }
  87. })
  88. )
  89. }
  90. async function processVideoAssets(
  91. { event, store }: EventContext & StoreContext,
  92. assets: StorageDataObject[],
  93. video: Video,
  94. meta: DecodedMetadataObject<IVideoMetadata>
  95. ) {
  96. await Promise.all(
  97. ASSET_TYPES.video.map(async ({ metaFieldName, schemaFieldName, DataObjectTypeConstructor }) => {
  98. const newAssetIndex = meta[metaFieldName]
  99. const currentAsset = video[schemaFieldName]
  100. if (isSet(newAssetIndex)) {
  101. const asset = findAssetByIndex(assets, newAssetIndex)
  102. if (asset) {
  103. if (currentAsset) {
  104. currentAsset.unsetAt = new Date(event.blockTimestamp)
  105. await store.save<StorageDataObject>(currentAsset)
  106. }
  107. const dataObjectType = new DataObjectTypeConstructor()
  108. dataObjectType.videoId = video.id
  109. asset.type = dataObjectType
  110. video[schemaFieldName] = asset
  111. await store.save<StorageDataObject>(asset)
  112. }
  113. }
  114. })
  115. )
  116. }
  117. export async function processChannelMetadata(
  118. ctx: EventContext & StoreContext,
  119. channel: Channel,
  120. meta: DecodedMetadataObject<IChannelMetadata>,
  121. assetsParams?: StorageAssets
  122. ): Promise<Channel> {
  123. const assets = assetsParams ? await processNewAssets(ctx, assetsParams) : []
  124. integrateMeta(channel, meta, ['title', 'description', 'isPublic'])
  125. await processChannelAssets(ctx, assets, channel, meta)
  126. // prepare channel category if needed
  127. if (isSet(meta.category)) {
  128. channel.category = await processChannelCategory(ctx, channel.category, parseInt(meta.category))
  129. }
  130. // prepare language if needed
  131. if (isSet(meta.language)) {
  132. channel.language = await processLanguage(ctx, channel.language, meta.language)
  133. }
  134. return channel
  135. }
  136. export async function processVideoMetadata(
  137. ctx: EventContext & StoreContext,
  138. video: Video,
  139. meta: DecodedMetadataObject<IVideoMetadata>,
  140. assetsParams?: StorageAssets
  141. ): Promise<Video> {
  142. const assets = assetsParams ? await processNewAssets(ctx, assetsParams) : []
  143. integrateMeta(video, meta, ['title', 'description', 'duration', 'hasMarketing', 'isExplicit', 'isPublic'])
  144. await processVideoAssets(ctx, assets, video, meta)
  145. // prepare video category if needed
  146. if (meta.category) {
  147. video.category = await processVideoCategory(ctx, video.category, parseInt(meta.category))
  148. }
  149. // prepare media meta information if needed
  150. if (isSet(meta.video) || isSet(meta.mediaType) || isSet(meta.mediaPixelWidth) || isSet(meta.mediaPixelHeight)) {
  151. // prepare video file size if poosible
  152. const videoSize = extractVideoSize(assets)
  153. video.mediaMetadata = await processVideoMediaMetadata(ctx, video.mediaMetadata, meta, videoSize)
  154. }
  155. // prepare license if needed
  156. if (isSet(meta.license)) {
  157. await updateVideoLicense(ctx, video, meta.license)
  158. }
  159. // prepare language if needed
  160. if (isSet(meta.language)) {
  161. video.language = await processLanguage(ctx, video.language, meta.language)
  162. }
  163. if (isSet(meta.publishedBeforeJoystream)) {
  164. video.publishedBeforeJoystream = processPublishedBeforeJoystream(
  165. ctx,
  166. video.publishedBeforeJoystream,
  167. meta.publishedBeforeJoystream
  168. )
  169. }
  170. return video
  171. }
  172. function findAssetByIndex(assets: StorageDataObject[], index: number, name?: string): StorageDataObject | null {
  173. if (assets[index]) {
  174. return assets[index]
  175. }
  176. invalidMetadata(`Invalid${name ? ' ' + name : ''} asset index`, {
  177. numberOfAssets: assets.length,
  178. requestedAssetIndex: index,
  179. })
  180. return null
  181. }
  182. async function processVideoMediaEncoding(
  183. { store, event }: StoreContext & EventContext,
  184. existingVideoMediaEncoding: VideoMediaEncoding | undefined,
  185. metadata: DecodedMetadataObject<IMediaType>
  186. ): Promise<VideoMediaEncoding> {
  187. const encoding =
  188. existingVideoMediaEncoding ||
  189. new VideoMediaEncoding({
  190. id: deterministicEntityId(event),
  191. createdAt: new Date(event.blockTimestamp),
  192. })
  193. // integrate media encoding-related data
  194. integrateMeta(encoding, metadata, ['codecName', 'container', 'mimeMediaType'])
  195. encoding.updatedAt = new Date(event.blockTimestamp)
  196. await store.save<VideoMediaEncoding>(encoding)
  197. return encoding
  198. }
  199. async function processVideoMediaMetadata(
  200. ctx: StoreContext & EventContext,
  201. existingVideoMedia: VideoMediaMetadata | undefined,
  202. metadata: DecodedMetadataObject<IVideoMetadata>,
  203. videoSize: BN | undefined
  204. ): Promise<VideoMediaMetadata> {
  205. const { store, event } = ctx
  206. const videoMedia =
  207. existingVideoMedia ||
  208. new VideoMediaMetadata({
  209. id: deterministicEntityId(event),
  210. createdInBlock: event.blockNumber,
  211. createdAt: new Date(event.blockTimestamp),
  212. })
  213. // integrate media-related data
  214. const mediaMetadata = {
  215. size: isSet(videoSize) ? new BN(videoSize.toString()) : undefined,
  216. pixelWidth: metadata.mediaPixelWidth,
  217. pixelHeight: metadata.mediaPixelHeight,
  218. }
  219. integrateMeta(videoMedia, mediaMetadata, ['pixelWidth', 'pixelHeight', 'size'])
  220. videoMedia.updatedAt = new Date(event.blockTimestamp)
  221. videoMedia.encoding = await processVideoMediaEncoding(ctx, videoMedia.encoding, metadata.mediaType || {})
  222. await store.save<VideoMediaMetadata>(videoMedia)
  223. return videoMedia
  224. }
  225. export async function convertContentActorToChannelOwner(
  226. store: DatabaseManager,
  227. contentActor: ContentActor
  228. ): Promise<{
  229. ownerMember?: Membership
  230. ownerCuratorGroup?: CuratorGroup
  231. }> {
  232. if (contentActor.isMember) {
  233. const memberId = contentActor.asMember.toNumber()
  234. const member = await store.get(Membership, { where: { id: memberId.toString() } as FindConditions<Membership> })
  235. // ensure member exists
  236. if (!member) {
  237. return inconsistentState(`Actor is non-existing member`, memberId)
  238. }
  239. return {
  240. ownerMember: member,
  241. ownerCuratorGroup: undefined, // this will clear the field
  242. }
  243. }
  244. if (contentActor.isCurator) {
  245. const curatorGroupId = contentActor.asCurator[0].toNumber()
  246. const curatorGroup = await store.get(CuratorGroup, {
  247. where: { id: curatorGroupId.toString() } as FindConditions<CuratorGroup>,
  248. })
  249. // ensure curator group exists
  250. if (!curatorGroup) {
  251. return inconsistentState('Actor is non-existing curator group', curatorGroupId)
  252. }
  253. return {
  254. ownerMember: undefined, // this will clear the field
  255. ownerCuratorGroup: curatorGroup,
  256. }
  257. }
  258. // TODO: contentActor.isLead
  259. logger.error('Not implemented ContentActor type', { contentActor: contentActor.toString() })
  260. throw new Error('Not-implemented ContentActor type used')
  261. }
  262. function processPublishedBeforeJoystream(
  263. ctx: EventContext & StoreContext,
  264. currentValue: Date | undefined,
  265. metadata: DecodedMetadataObject<IPublishedBeforeJoystream>
  266. ): Date | undefined {
  267. if (!isSet(metadata)) {
  268. return currentValue
  269. }
  270. // Property is beeing unset
  271. if (!metadata.isPublished) {
  272. return undefined
  273. }
  274. // try to parse timestamp from publish date
  275. const timestamp = isSet(metadata.date) ? Date.parse(metadata.date) : NaN
  276. // ensure date is valid
  277. if (isNaN(timestamp)) {
  278. invalidMetadata(`Invalid date used for publishedBeforeJoystream`, {
  279. timestamp,
  280. })
  281. return currentValue
  282. }
  283. // set new date
  284. return new Date(timestamp)
  285. }
  286. async function processNewAssets(ctx: EventContext & StoreContext, assets: StorageAssets): Promise<StorageDataObject[]> {
  287. const assetsUploaded = assets.object_creation_list.length
  288. // FIXME: Ideally the runtime would provide object ids in ChannelCreated/VideoCreated/ChannelUpdated(...) events
  289. const objects = await getMostRecentlyCreatedDataObjects(ctx.store, assetsUploaded)
  290. return objects
  291. }
  292. function extractVideoSize(assets: StorageDataObject[]): BN | undefined {
  293. const mediaAsset = assets.find((a) => a.type?.isTypeOf === DataObjectTypeVideoMedia.name)
  294. return mediaAsset ? mediaAsset.size : undefined
  295. }
  296. async function processLanguage(
  297. ctx: EventContext & StoreContext,
  298. currentLanguage: Language | undefined,
  299. languageIso: string | undefined
  300. ): Promise<Language | undefined> {
  301. const { event, store } = ctx
  302. if (!isSet(languageIso)) {
  303. return currentLanguage
  304. }
  305. // ensure language string is valid
  306. if (!isValidLanguageCode(languageIso)) {
  307. invalidMetadata(`Invalid language ISO-639-1 provided`, languageIso)
  308. return currentLanguage
  309. }
  310. // load language
  311. const existingLanguage = await store.get(Language, { where: { iso: languageIso } })
  312. // return existing language if any
  313. if (existingLanguage) {
  314. return existingLanguage
  315. }
  316. // create new language
  317. const newLanguage = new Language({
  318. id: deterministicEntityId(event),
  319. iso: languageIso,
  320. createdInBlock: event.blockNumber,
  321. createdAt: new Date(event.blockTimestamp),
  322. updatedAt: new Date(event.blockTimestamp),
  323. })
  324. await store.save<Language>(newLanguage)
  325. return newLanguage
  326. }
  327. async function updateVideoLicense(
  328. ctx: StoreContext & EventContext,
  329. video: Video,
  330. licenseMetadata: ILicense | null | undefined
  331. ): Promise<void> {
  332. const { store, event } = ctx
  333. if (!isSet(licenseMetadata)) {
  334. return
  335. }
  336. const previousLicense = video.license
  337. let license: License | null = null
  338. if (!isLicenseEmpty(licenseMetadata)) {
  339. // license is meant to be created/updated
  340. license =
  341. previousLicense ||
  342. new License({
  343. id: deterministicEntityId(event),
  344. createdAt: new Date(event.blockTimestamp),
  345. })
  346. license.updatedAt = new Date(event.blockTimestamp)
  347. integrateMeta(license, licenseMetadata, ['attribution', 'code', 'customText'])
  348. await store.save<License>(license)
  349. }
  350. // Update license (and potentially remove foreign key reference)
  351. // FIXME: Note that we MUST to provide "null" here in order to unset a relation,
  352. // See: https://github.com/Joystream/hydra/issues/435
  353. video.license = license as License | undefined
  354. video.updatedAt = new Date(ctx.event.blockTimestamp)
  355. await store.save<Video>(video)
  356. // Safely remove previous license if needed
  357. if (previousLicense && !license) {
  358. await store.remove<License>(previousLicense)
  359. }
  360. }
  361. /*
  362. Checks if protobof contains license with some fields filled or is empty object (`{}` or `{someKey: undefined, ...}`).
  363. Empty object means deletion is requested.
  364. */
  365. function isLicenseEmpty(licenseObject: ILicense): boolean {
  366. const somePropertySet = Object.values(licenseObject).some((v) => isSet(v))
  367. return !somePropertySet
  368. }
  369. async function processVideoCategory(
  370. ctx: EventContext & StoreContext,
  371. currentCategory: VideoCategory | undefined,
  372. categoryId: number
  373. ): Promise<VideoCategory | undefined> {
  374. const { store } = ctx
  375. // load video category
  376. const category = await store.get(VideoCategory, {
  377. where: { id: categoryId.toString() },
  378. })
  379. // ensure video category exists
  380. if (!category) {
  381. invalidMetadata('Non-existing video category association with video requested', categoryId)
  382. return currentCategory
  383. }
  384. return category
  385. }
  386. async function processChannelCategory(
  387. ctx: EventContext & StoreContext,
  388. currentCategory: ChannelCategory | undefined,
  389. categoryId: number
  390. ): Promise<ChannelCategory | undefined> {
  391. const { store } = ctx
  392. // load video category
  393. const category = await store.get(ChannelCategory, {
  394. where: { id: categoryId.toString() },
  395. })
  396. // ensure video category exists
  397. if (!category) {
  398. invalidMetadata('Non-existing channel category association with channel requested', categoryId)
  399. return currentCategory
  400. }
  401. return category
  402. }
  403. // Needs to be done every time before data object is removed!
  404. export async function unsetAssetRelations(store: DatabaseManager, dataObject: StorageDataObject): Promise<void> {
  405. const channelAssets = ['avatarPhoto', 'coverPhoto'] as const
  406. const videoAssets = ['thumbnailPhoto', 'media'] as const
  407. // NOTE: we don't need to retrieve multiple channels/videos via `store.getMany()` because dataObject
  408. // is allowed to be associated only with one channel/video in runtime
  409. const channel = await store.get(Channel, {
  410. where: channelAssets.map((assetName) => ({
  411. [assetName]: {
  412. id: dataObject.id,
  413. },
  414. })),
  415. relations: [...channelAssets],
  416. })
  417. const video = await store.get(Video, {
  418. where: videoAssets.map((assetName) => ({
  419. [assetName]: {
  420. id: dataObject.id,
  421. },
  422. })),
  423. relations: [...videoAssets],
  424. })
  425. if (channel) {
  426. channelAssets.forEach((assetName) => {
  427. if (channel[assetName] && channel[assetName]?.id === dataObject.id) {
  428. channel[assetName] = null as any
  429. }
  430. })
  431. await store.save<Channel>(channel)
  432. // emit log event
  433. logger.info('Content has been disconnected from Channel', {
  434. channelId: channel.id.toString(),
  435. dataObjectId: dataObject.id,
  436. })
  437. }
  438. if (video) {
  439. videoAssets.forEach((assetName) => {
  440. if (video[assetName] && video[assetName]?.id === dataObject.id) {
  441. video[assetName] = null as any
  442. }
  443. })
  444. await store.save<Video>(video)
  445. // emit log event
  446. logger.info('Content has been disconnected from Video', {
  447. videoId: video.id.toString(),
  448. dataObjectId: dataObject.id,
  449. })
  450. }
  451. }