import fs = require ( 'fs-extra' ) ; import { uniq , pull } from 'lodash-es' ; import { Subscribe , Subscriber , CQError } from '@/models' ; import { getUsernameFromUID , getAllFollowings } from './helper' ; import { globalCache , SUBSCRIBE_LIST } from '@/db' ; import { getBiliDynamic } from './dynamic' ; import { getVupAndVtuberList } from './dd' ; ( async ( ) => { let list = await getSubscribeList ( ) ; list = uniq ( list ) ; for ( let i = 0 ; i < list . length ; i ++ ) { const e = list [ i ] ; if ( ! e . userName ) { e . userName = await getUsernameFromUID ( e . userId ) ; } else { const key = `bili-username-from-uid-${ e . userId }` ; await globalCache . set ( key , e . userName , 3600 * 24 * 7 ) ; } SUBSCRIBE_LIST . push ( e ) ; } } ) ( ) ; author CaoMeiYouRen
 *  date export async function saveSubscribeList ( list ) { if ( ! await fs . pathExists ( 'data' ) ) { await fs . mkdir ( 'data' ) ; } await fs . writeFile ( 'data/subscribeList.json' , JSON . stringify ( list , null , 4 ) ) ; } author CaoMeiYouRen
 *  date returns { Promise < Subscribe [ ] > } export async function getSubscribeList ( ) { if ( ! await fs . pathExists ( 'data/subscribeList.json' ) ) { return [ ] ; } return fs . readJSON ( 'data/subscribeList.json' ) ; } author CaoMeiYouRen
 *  date export param { number } userId param { number } subId param { string } subType param { string } userName export async function subscribeUp ( userId , subId , subType , userName ? ) { let sub = SUBSCRIBE_LIST . find ( ( e ) => e . userId === userId ) ; if ( ! sub ) { if ( ! userName ) { userName = await getUsernameFromUID ( userId ) ; } sub = new Subscribe ( { userId , userName , lastDynamic : Date . now ( ) , lastLive : Date . now ( ) , subscribers : [ { subId , subType , } , ] , } ) ; SUBSCRIBE_LIST . push ( sub ) ; await saveSubscribeList ( SUBSCRIBE_LIST ) ; return sub ; } let suber = sub . subscribers . find ( ( e ) => e . subId === subId && e . subType === subType ) ; if ( suber ) { throw new CQError ( '该 up 已订阅，请勿重复订阅' ) ; } suber = new Subscriber ( { subId , subType } ) ; sub . subscribers . push ( suber ) ; await saveSubscribeList ( SUBSCRIBE_LIST ) ; return sub ; } author CaoMeiYouRen
 *  date export param { number } userId param { number } subId param { string } subType param { number } tag export async function transferSubscribeUp ( userId , subId , subType , tag ? ) { const followings = await getAllFollowings ( userId , tag ) ; for ( let i = 0 ; i < followings . length ; i ++ ) { const e = followings [ i ] ; try { await subscribeUp ( e . mid , subId , subType , e . uname ) ; } catch ( error ) { } } return followings . length ; } author CaoMeiYouRen
 *  date export param { number } userId param { number } subId param { string } subType export async function unsubscribeUp ( userId , subId , subType ) { const sub = SUBSCRIBE_LIST . find ( ( e ) => e . userId === userId ) ; if ( ! sub ) { throw new CQError ( '该 up 未被任何用户订阅' ) ; } const suber = sub . subscribers . find ( ( e ) => e . subId === subId && e . subType === subType ) ; if ( suber ) { pull ( sub . subscribers , suber ) ; if ( sub . subscribers . length === 0 ) { pull ( SUBSCRIBE_LIST , sub ) ; } await saveSubscribeList ( SUBSCRIBE_LIST ) ; return sub ; } throw new CQError ( '您尚未订阅该 up' ) ; } author CaoMeiYouRen
 *  date export param { number } subId param { string } subType export async function unsubscribeAllUp ( subId , subType ) { const subs = SUBSCRIBE_LIST . filter ( ( e ) => e . subscribers . find ( ( f ) => f . subId === subId && f . subType === subType ) ) ; if ( subs . length === 0 ) { throw new CQError ( '您尚未订阅任何 up' ) ; } for ( let i = 0 ; i < subs . length ; i ++ ) { const sub = subs [ i ] ; try { await unsubscribeUp ( sub . userId , subId , subType ) ; } catch ( error ) { } } return true ; } author CaoMeiYouRen
 *  date export param { number } subId param { string } subType param { number } limit returns export async function oneClickDD ( subId , subType , limit = 20 ) { const vups = await getVupAndVtuberList ( limit ) ; for ( let i = 0 ; i < vups . length ; i ++ ) { const e = vups [ i ] ; try { await subscribeUp ( e . mid , subId , subType , e . uname ) ; } catch ( error ) { } } return vups . length ; } author CaoMeiYouRen
 *  date export param { number } subId param { string } subType export function querySubscribe ( subId , subType ) { return SUBSCRIBE_LIST . filter ( ( e ) => e . subscribers . find ( ( f ) => f . subId === subId && f . subType === subType ) ) ; } author CaoMeiYouRen
 *  date export param { number } userId param { number } lastDynamic param { number } limit returns export async function getNotPushDynamic ( userId , lastDynamic , limit = 3 ) { const channel = await getBiliDynamic ( userId ) ; if ( ! channel ) { return [ ] ; } return channel ?. item . filter ( ( e ) => { if ( ! e . pubDate ) { return false ; } const pubDate = new Date ( e . pubDate ) . getTime ( ) ; return pubDate > lastDynamic && Date . now ( ) - pubDate < 1000 * 60 * 60 * 24 ; } ) . reverse ( ) . slice ( 0 , limit ) ; }