123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- /*
- eslint-disable @typescript-eslint/naming-convention
- */
- import { EventContext, StoreContext } from '@joystream/hydra-common'
- import { Content } from '../generated/types'
- import { convertContentActorToChannelOwner, processChannelMetadata } from './utils'
- import { Channel, ChannelCategory, StorageDataObject, Membership } from 'query-node/dist/model'
- import { deserializeMetadata, inconsistentState, logger } from '../common'
- import { ChannelCategoryMetadata, ChannelMetadata } from '@joystream/metadata-protobuf'
- import { integrateMeta } from '@joystream/metadata-protobuf/utils'
- import { In } from 'typeorm'
- import { removeDataObject } from '../storage/utils'
- export async function content_ChannelCreated(ctx: EventContext & StoreContext): Promise<void> {
- const { store, event } = ctx
- // read event data
- const [contentActor, channelId, , channelCreationParameters] = new Content.ChannelCreatedEvent(event).params
- // create entity
- const channel = new Channel({
- // main data
- id: channelId.toString(),
- isCensored: false,
- videos: [],
- createdInBlock: event.blockNumber,
- rewardAccount: channelCreationParameters.reward_account.unwrapOr(undefined)?.toString(),
- // fill in auto-generated fields
- createdAt: new Date(event.blockTimestamp),
- updatedAt: new Date(event.blockTimestamp),
- // prepare channel owner (handles fields `ownerMember` and `ownerCuratorGroup`)
- ...(await convertContentActorToChannelOwner(store, contentActor)),
- collaborators: Array.from(channelCreationParameters.collaborators).map(
- (id) => new Membership({ id: id.toString() })
- ),
- })
- // deserialize & process metadata
- if (channelCreationParameters.meta.isSome) {
- const metadata = deserializeMetadata(ChannelMetadata, channelCreationParameters.meta.unwrap()) || {}
- await processChannelMetadata(ctx, channel, metadata, channelCreationParameters.assets.unwrapOr(undefined))
- }
- // save entity
- await store.save<Channel>(channel)
- // emit log event
- logger.info('Channel has been created', { id: channel.id })
- }
- export async function content_ChannelUpdated(ctx: EventContext & StoreContext): Promise<void> {
- const { store, event } = ctx
- // read event data
- const [, channelId, , channelUpdateParameters] = new Content.ChannelUpdatedEvent(event).params
- // load channel
- const channel = await store.get(Channel, { where: { id: channelId.toString() } })
- // ensure channel exists
- if (!channel) {
- return inconsistentState('Non-existing channel update requested', channelId)
- }
- // prepare changed metadata
- const newMetadataBytes = channelUpdateParameters.new_meta.unwrapOr(null)
- // update metadata if it was changed
- if (newMetadataBytes) {
- const newMetadata = deserializeMetadata(ChannelMetadata, newMetadataBytes) || {}
- await processChannelMetadata(
- ctx,
- channel,
- newMetadata,
- channelUpdateParameters.assets_to_upload.unwrapOr(undefined)
- )
- }
- // prepare changed reward account
- const newRewardAccount = channelUpdateParameters.reward_account.unwrapOr(null)
- // reward account change happened?
- if (newRewardAccount) {
- // this will change the `channel`!
- channel.rewardAccount = newRewardAccount.unwrapOr(undefined)?.toString()
- }
- const newCollaborators = channelUpdateParameters.collaborators.unwrapOr(undefined)
- if (newCollaborators) {
- channel.collaborators = Array.from(newCollaborators).map((id) => new Membership({ id: id.toString() }))
- }
- // set last update time
- channel.updatedAt = new Date(event.blockTimestamp)
- // save channel
- await store.save<Channel>(channel)
- // emit log event
- logger.info('Channel has been updated', { id: channel.id })
- }
- export async function content_ChannelAssetsRemoved({ store, event }: EventContext & StoreContext): Promise<void> {
- const [, , dataObjectIds] = new Content.ChannelAssetsRemovedEvent(event).params
- const assets = await store.getMany(StorageDataObject, {
- where: {
- id: In(Array.from(dataObjectIds).map((item) => item.toString())),
- },
- })
- await Promise.all(assets.map((a) => removeDataObject(store, a)))
- logger.info('Channel assets have been removed', { ids: dataObjectIds.toJSON() })
- }
- export async function content_ChannelCensorshipStatusUpdated({
- store,
- event,
- }: EventContext & StoreContext): Promise<void> {
- // read event data
- const [, channelId, isCensored] = new Content.ChannelCensorshipStatusUpdatedEvent(event).params
- // load event
- const channel = await store.get(Channel, { where: { id: channelId.toString() } })
- // ensure channel exists
- if (!channel) {
- return inconsistentState('Non-existing channel censoring requested', channelId)
- }
- // update channel
- channel.isCensored = isCensored.isTrue
- // set last update time
- channel.updatedAt = new Date(event.blockTimestamp)
- // save channel
- await store.save<Channel>(channel)
- // emit log event
- logger.info('Channel censorship status has been updated', { id: channelId, isCensored: isCensored.isTrue })
- }
- /// //////////////// ChannelCategory ////////////////////////////////////////////
- export async function content_ChannelCategoryCreated({ store, event }: EventContext & StoreContext): Promise<void> {
- // read event data
- const [channelCategoryId, , channelCategoryCreationParameters] = new Content.ChannelCategoryCreatedEvent(event).params
- // read metadata
- const metadata = deserializeMetadata(ChannelCategoryMetadata, channelCategoryCreationParameters.meta) || {}
- // create new channel category
- const channelCategory = new ChannelCategory({
- // main data
- id: channelCategoryId.toString(),
- channels: [],
- createdInBlock: event.blockNumber,
- // fill in auto-generated fields
- createdAt: new Date(event.blockTimestamp),
- updatedAt: new Date(event.blockTimestamp),
- })
- integrateMeta(channelCategory, metadata, ['name'])
- // save channel
- await store.save<ChannelCategory>(channelCategory)
- // emit log event
- logger.info('Channel category has been created', { id: channelCategory.id })
- }
- export async function content_ChannelCategoryUpdated({ store, event }: EventContext & StoreContext): Promise<void> {
- // read event data
- const [, channelCategoryId, channelCategoryUpdateParameters] = new Content.ChannelCategoryUpdatedEvent(event).params
- // load channel category
- const channelCategory = await store.get(ChannelCategory, {
- where: {
- id: channelCategoryId.toString(),
- },
- })
- // ensure channel exists
- if (!channelCategory) {
- return inconsistentState('Non-existing channel category update requested', channelCategoryId)
- }
- // read metadata
- const newMeta = deserializeMetadata(ChannelCategoryMetadata, channelCategoryUpdateParameters.new_meta) || {}
- integrateMeta(channelCategory, newMeta, ['name'])
- // set last update time
- channelCategory.updatedAt = new Date(event.blockTimestamp)
- // save channel category
- await store.save<ChannelCategory>(channelCategory)
- // emit log event
- logger.info('Channel category has been updated', { id: channelCategory.id })
- }
- export async function content_ChannelCategoryDeleted({ store, event }: EventContext & StoreContext): Promise<void> {
- // read event data
- const [, channelCategoryId] = new Content.ChannelCategoryDeletedEvent(event).params
- // load channel category
- const channelCategory = await store.get(ChannelCategory, {
- where: {
- id: channelCategoryId.toString(),
- },
- })
- // ensure channel category exists
- if (!channelCategory) {
- return inconsistentState('Non-existing channel category deletion requested', channelCategoryId)
- }
- // delete channel category
- await store.remove<ChannelCategory>(channelCategory)
- // emit log event
- logger.info('Channel category has been deleted', { id: channelCategory.id })
- }
- export async function content_ChannelDeleted({ store, event }: EventContext & StoreContext): Promise<void> {
- const [, channelId] = new Content.ChannelDeletedEvent(event).params
- await store.remove<Channel>(new Channel({ id: channelId.toString() }))
- }
|