import BN from 'bn.js' import { fixBlockTimestamp } from '../eventFix' import { SubstrateEvent } from '@dzlzv/hydra-common' import { DatabaseManager } from '@dzlzv/hydra-db-utils' import { FindConditions, In } from 'typeorm' import { Content } from '../../../generated/types' import { inconsistentState, logger, getNextId } from '../common' import { convertContentActorToDataObjectOwner, readProtobuf, readProtobufWithAssets, RawVideoMetadata } from './utils' import { AssetAvailability, Channel, License, Video, VideoCategory, VideoMediaEncoding, VideoMediaMetadata, } from 'query-node' // Joystream types import { ChannelId } from '@joystream/types/augment' // eslint-disable-next-line @typescript-eslint/naming-convention export async function content_VideoCategoryCreated(db: DatabaseManager, event: SubstrateEvent) { // read event data const { videoCategoryId, videoCategoryCreationParameters, contentActor } = new Content.VideoCategoryCreatedEvent( event ).data // read metadata const protobufContent = await readProtobuf(new VideoCategory(), { metadata: videoCategoryCreationParameters.meta, db, event, }) // create new video category const videoCategory = new VideoCategory({ // main data id: videoCategoryId.toString(), videos: [], createdInBlock: event.blockNumber, // fill in auto-generated fields createdAt: new Date(fixBlockTimestamp(event.blockTimestamp).toNumber()), updatedAt: new Date(fixBlockTimestamp(event.blockTimestamp).toNumber()), // integrate metadata ...protobufContent, }) // save video category await db.save(videoCategory) // emit log event logger.info('Video category has been created', { id: videoCategoryId }) } // eslint-disable-next-line @typescript-eslint/naming-convention export async function content_VideoCategoryUpdated(db: DatabaseManager, event: SubstrateEvent) { // read event data const { videoCategoryId, videoCategoryUpdateParameters, contentActor } = new Content.VideoCategoryUpdatedEvent( event ).data // load video category const videoCategory = await db.get(VideoCategory, { where: { id: videoCategoryId.toString() } as FindConditions, }) // ensure video category exists if (!videoCategory) { return inconsistentState('Non-existing video category update requested', videoCategoryId) } // read metadata const protobufContent = await readProtobuf(new VideoCategory(), { metadata: videoCategoryUpdateParameters.new_meta, db, event, }) // update all fields read from protobuf for (const [key, value] of Object.entries(protobufContent)) { videoCategory[key] = value } // set last update time videoCategory.updatedAt = new Date(fixBlockTimestamp(event.blockTimestamp).toNumber()) // save video category await db.save(videoCategory) // emit log event logger.info('Video category has been updated', { id: videoCategoryId }) } // eslint-disable-next-line @typescript-eslint/naming-convention export async function content_VideoCategoryDeleted(db: DatabaseManager, event: SubstrateEvent) { // read event data const { videoCategoryId } = new Content.VideoCategoryDeletedEvent(event).data // load video category const videoCategory = await db.get(VideoCategory, { where: { id: videoCategoryId.toString() } as FindConditions, }) // ensure video category exists if (!videoCategory) { return inconsistentState('Non-existing video category deletion requested', videoCategoryId) } // remove video category await db.remove(videoCategory) // emit log event logger.info('Video category has been deleted', { id: videoCategoryId }) } /// ///////////////// Video ////////////////////////////////////////////////////// // eslint-disable-next-line @typescript-eslint/naming-convention export async function content_VideoCreated(db: DatabaseManager, event: SubstrateEvent) { // read event data const { channelId, videoId, videoCreationParameters, contentActor } = new Content.VideoCreatedEvent(event).data // read metadata const protobufContent = await readProtobufWithAssets(new Video(), { metadata: videoCreationParameters.meta, db, event, assets: videoCreationParameters.assets, ChannelOwner: convertContentActorToDataObjectOwner(contentActor, channelId.toNumber()), }) // load channel const channel = await db.get(Channel, { where: { id: channelId.toString() } as FindConditions }) // ensure channel exists if (!channel) { return inconsistentState('Trying to add video to non-existing channel', channelId) } // prepare video media metadata (if any) const fixedProtobuf = await integrateVideoMediaMetadata(db, null, protobufContent, event) const licenseIsEmpty = fixedProtobuf.license && !Object.keys(fixedProtobuf.license).length if (licenseIsEmpty) { // license deletion was requested - ignore it and consider it empty delete fixedProtobuf.license } // create new video const video = new Video({ // main data id: videoId.toString(), isCensored: false, channel, createdInBlock: event.blockNumber, isFeatured: false, // default values for properties that might or might not be filled by metadata thumbnailPhotoUrls: [], thumbnailPhotoAvailability: AssetAvailability.INVALID, mediaUrls: [], mediaAvailability: AssetAvailability.INVALID, // fill in auto-generated fields createdAt: new Date(fixBlockTimestamp(event.blockTimestamp).toNumber()), updatedAt: new Date(fixBlockTimestamp(event.blockTimestamp).toNumber()), // integrate metadata ...fixedProtobuf, }) // save video await db.save