/* eslint-disable @typescript-eslint/naming-convention */ import { EventContext, StoreContext } from '@joystream/hydra-common' import { In } from 'typeorm' import { Content } from '../generated/types' import { deserializeMetadata, inconsistentState, logger } from '../common' import { processVideoMetadata } from './utils' import { Channel, Video, VideoCategory } from 'query-node/dist/model' import { VideoMetadata, VideoCategoryMetadata } from '@joystream/metadata-protobuf' import { integrateMeta } from '@joystream/metadata-protobuf/utils' import _ from 'lodash' export async function content_VideoCategoryCreated({ store, event }: EventContext & StoreContext): Promise { // read event data const [, videoCategoryId, videoCategoryCreationParameters] = new Content.VideoCategoryCreatedEvent(event).params // read metadata const metadata = (await deserializeMetadata(VideoCategoryMetadata, videoCategoryCreationParameters.meta)) || {} // 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(event.blockTimestamp), updatedAt: new Date(event.blockTimestamp), }) integrateMeta(videoCategory, metadata, ['name']) // save video category await store.save(videoCategory) // emit log event logger.info('Video category has been created', { id: videoCategoryId }) } export async function content_VideoCategoryUpdated({ store, event }: EventContext & StoreContext): Promise { // read event data const [, videoCategoryId, videoCategoryUpdateParameters] = new Content.VideoCategoryUpdatedEvent(event).params // load video category const videoCategory = await store.get(VideoCategory, { where: { id: videoCategoryId.toString() }, }) // ensure video category exists if (!videoCategory) { return inconsistentState('Non-existing video category update requested', videoCategoryId) } // read metadata const newMeta = deserializeMetadata(VideoCategoryMetadata, videoCategoryUpdateParameters.new_meta) || {} integrateMeta(videoCategory, newMeta, ['name']) // set last update time videoCategory.updatedAt = new Date(event.blockTimestamp) // save video category await store.save(videoCategory) // emit log event logger.info('Video category has been updated', { id: videoCategoryId }) } export async function content_VideoCategoryDeleted({ store, event }: EventContext & StoreContext): Promise { // read event data const [, videoCategoryId] = new Content.VideoCategoryDeletedEvent(event).params // load video category const videoCategory = await store.get(VideoCategory, { where: { id: videoCategoryId.toString() }, }) // ensure video category exists if (!videoCategory) { return inconsistentState('Non-existing video category deletion requested', videoCategoryId) } // remove video category await store.remove(videoCategory) // emit log event logger.info('Video category has been deleted', { id: videoCategoryId }) } /// //////////////// Video ////////////////////////////////////////////////////// export async function content_VideoCreated(ctx: EventContext & StoreContext): Promise { const { store, event } = ctx // read event data const [, channelId, videoId, videoCreationParameters] = new Content.VideoCreatedEvent(event).params // load channel const channel = await store.get(Channel, { where: { id: channelId.toString() } }) // ensure channel exists if (!channel) { return inconsistentState('Trying to add video to non-existing channel', channelId) } const video = new Video({ id: videoId.toString(), channel, isCensored: false, isFeatured: false, createdInBlock: event.blockNumber, createdAt: new Date(event.blockTimestamp), updatedAt: new Date(event.blockTimestamp), }) // deserialize & process metadata if (videoCreationParameters.meta.isSome) { const metadata = deserializeMetadata(VideoMetadata, videoCreationParameters.meta.unwrap()) || {} await processVideoMetadata(ctx, video, metadata, videoCreationParameters.assets.unwrapOr(undefined)) } // save video await store.save