import TelegramBot = require ( 'node-telegram-bot-api' ) ; import dlDetails = require ( './detail' ) ; export class DlManager { private static instance : DlManager ; private allDls : any = { } ; private activeDls : any = { } ; /**
     * Stores all general status messages. General status messages show the status
     * of all downloads. Each chat can have at most 1 general status message.
     * Key: Chat ID: number
     * Value: Status message: TelegramBot.Message
     */ private statusAll : any = { } ; private statusLock : any = { } ; private cancelledMessages : any = { } ; private cancelledDls : any = { } ; private constructor ( ) { } static getInstance ( ) { if ( ! DlManager . instance ) { DlManager . instance = new DlManager ( ) ; } return DlManager . instance ; } addDownload ( gid , dlDir , msg , isTar ) { var detail = new dlDetails . DlVars ( gid , msg , isTar , dlDir ) ; this . allDls [ gid ] = detail ; } getDownloadByGid ( gid ) { return this . allDls [ gid ] ; } param dlDetails moveDownloadToActive ( dlDetails ) { dlDetails . isDownloading = true ; dlDetails . isUploading = false ; this . activeDls [ dlDetails . gid ] = dlDetails ; } param oldGid param newGid changeDownloadGid ( oldGid , newGid ) { var dlDetails = this . getDownloadByGid ( oldGid ) ; this . deleteDownload ( oldGid ) ; dlDetails . gid = newGid ; dlDetails . isDownloading = false ; this . allDls [ newGid ] = dlDetails ; } param msg getDownloadByMsgId ( msg ) { for ( var dl of Object . keys ( this . allDls ) ) { var download = this . allDls [ dl ] ; if ( download . tgChatId === msg . chat . id && ( download . tgMessageId === msg . message_id ) ) { return download ; } } return null ; } deleteDownload ( gid ) { delete this . allDls [ gid ] ; delete this . activeDls [ gid ] ; } param callback forEachDownload ( callback ) { for ( var key of Object . keys ( this . allDls ) ) { var details = this . allDls [ key ] ; callback ( details ) ; } } deleteStatus ( chatId ) { delete this . statusAll [ chatId ] ; } param chatId returns { TelegramBot . Message } getStatus ( chatId ) { return this . statusAll [ chatId ] ; } addStatus ( msg , lastStatus ) { this . statusAll [ msg . chat . id ] = { msg : msg , lastStatus : lastStatus } ; } param callback forEachStatus ( callback ) { for ( var key of Object . keys ( this . statusAll ) ) { callback ( this . statusAll [ key ] ) ; } } param msg param toCall setStatusLock ( msg , toCall ) { if ( ! this . statusLock [ msg . chat . id ] ) { this . statusLock [ msg . chat . id ] = Promise . resolve ( ) ; } this . statusLock [ msg . chat . id ] = this . statusLock [ msg . chat . id ] . then ( ( ) => { return toCall ( msg , true ) ; } ) ; } addCancelled ( dlDetails ) { this . cancelledDls [ dlDetails . gid ] = dlDetails ; var message = this . cancelledMessages [ dlDetails . tgChatId ] ; if ( message ) { if ( this . checkUnique ( dlDetails . tgUsername , message ) ) { message . push ( dlDetails . tgUsername ) ; } } else { message = [ dlDetails . tgUsername ] ; } this . cancelledMessages [ dlDetails . tgChatId ] = message ; } forEachCancelledDl ( callback ) { for ( var key of Object . keys ( this . cancelledDls ) ) { callback ( this . cancelledDls [ key ] ) ; } } forEachCancelledChat ( callback ) { for ( var key of Object . keys ( this . cancelledMessages ) ) { callback ( this . cancelledMessages [ key ] , key ) ; } } removeCancelledMessage ( chatId ) { delete this . cancelledMessages [ chatId ] ; } removeCancelledDls ( gid ) { delete this . cancelledDls [ gid ] ; } private checkUnique ( toFind , src ) { for ( var item of src ) { if ( item === toFind ) { return false ; } } return true ; } } interface StatusAll { msg : TelegramBot . Message ; lastStatus : string ; }