import { ApiPromise } from "@polkadot/api"; import { Vec, Option } from "@polkadot/types"; import { Hash } from "@joystream/types/common"; import { Entity } from "@joystream/types/content-directory"; import { ContentId, DataObject } from "@joystream/types/media"; import { getNextEntity, getEntity, getDataObject } from "./api"; import { Channel, Media } from "./types"; const CHANNEL_CLASS_iD = 1; const VIDEO_CLASS_iD = 10; const getEntities = async ( api: ApiPromise, blockHash: Hash ): Promise> => { let nrEntities: number = await getNextEntity(api, blockHash); let entities = new Map(); for (let i = 0; i < nrEntities; ++i) { const entity: Entity = await getEntity(api, blockHash, i); entities.set(i, entity); } return entities; }; export const computeUsedSpaceInMbs = async ( api: ApiPromise, contentIds: Vec ): Promise => { let space = 0; for (let contentId of contentIds) { const dataObject: Option = await getDataObject(api, contentId); space += dataObject.unwrap().size_in_bytes.toNumber(); } return space / 1024 / 1024; }; export const parseVideos = async ( entities: Map ): Promise => { let videos: Media[] = []; for (let [key, entity] of entities) { if (entity.class_id.toNumber() != VIDEO_CLASS_iD || entity.values.isEmpty) continue; let values = Array.from(entity.getField("values").entries()); if (values.length < 2 || values[2].length < 1) continue; let title = values[2][1].getValue().toString(); videos.push(new Media(key, title)); } return videos; }; const parseChannels = async ( entities: Map ): Promise => { let channels: Channel[] = []; for (let [key, entity] of entities) { if ( entity.class_id.toNumber() != CHANNEL_CLASS_iD || entity.values.isEmpty ) { continue; } let values = Array.from(entity.getField("values").entries()); let title = values[0][1].getValue().toString(); channels.push(new Channel(key, title)); } return channels; };