import db from '@inrixia/db' ; import Video from './Video' ; import type { BlogPost } from 'floatplane/creator' ; import type { ChannelOptions } from './types' ; import type Subscription from './Subscription' ; export type VideoDBEntry = { episodeNo : number ; expectedSize ? : number ; } ; export type ChannelDB = { videos : { [ key ] : VideoDBEntry ; } ; nextEpisodeNo : number ; } ; export default class Channel { public title : ChannelOptions [ 'title' ] ; public identifiers : ChannelOptions [ 'identifiers' ] ; public skip : ChannelOptions [ 'skip' ] ; public consoleColor : ChannelOptions [ 'consoleColor' ] ; public subscription : Subscription ; public _db : ChannelDB ; param { ChannelOptions } channel constructor ( channel , subscription ) { this . subscription = subscription ; this . title = channel . title ; this . identifiers = channel . identifiers ; this . skip = channel . skip ; this . consoleColor = channel . consoleColor ; const databaseFilePath = `./db/channels/${ subscription . creatorId }/${ channel . title }.json` ; try { this . _db = db < ChannelDB > ( databaseFilePath , { template : { videos : { } , nextEpisodeNo : 1 } } ) ; } catch { throw new Error ( `Cannot load Channel database file ${ databaseFilePath }! Please delete the file or fix it!` ) ; } } public lookupVideoDB = ( guid ) : VideoDBEntry => this . _db . videos [ guid ] ; public markVideoCompleted ( guid , releaseDate ) { if ( this . _db . videos [ guid ] === undefined ) throw new Error ( `Cannot mark unknown video ${ guid } as completed. Video does not exist in channel database.` ) ; this . subscription . updateLastSeenVideo ( { guid , releaseDate } ) ; } public addVideo ( video ) { this . _db . videos [ video . guid ] ??= { episodeNo : this . _db . nextEpisodeNo ++ } ; return new Video ( video , this ) ; } }