import { showMessage } from './actions/message' ; import { addTweetToTimeline , addTweetsToTimeline , addMentions , addRejectedUserIds , addFriends , removeFriends , resetFriends , removeRejectedUserIds , addNoRetweetUserIds , addSeparator , setCurrentUser , updateCurrentUser , deleteStatusInTimeline , statusLiked , } from './actions/timeline' ; import Store from './store' ; import log from './log' ; import Tweet , { TwitterUser } from './item/tweet' ; import DB from './database/db' ; import { Twitter , Options } from 'twit' ; import TwitterRestApi from './twitter/rest_api' ; const { ipcRenderer : ipc } = global . require ( 'electron' ) ; interface Listeners { [ c ] : Electron . IpcRendererEventListener ; } export default class IpcChannelProxy { private listeners : Listeners ; constructor ( ) { this . listeners = { } ; } subscribe ( c , cb ) { const callback = ( _ , ... args ) => { log . debug ( '--->' , c , ... args ) ; cb . apply ( this , args ) ; } ; ipc . on ( c , callback ) ; this . listeners [ c ] = callback ; } start ( ) { this . subscribe ( 'yf:auth-tokens' , ( options ) => { TwitterRestApi . setupClient ( options ) ; return Promise . all ( [ TwitterRestApi . verifyCredentials ( ) , TwitterRestApi . rejectedIds ( ) , TwitterRestApi . noRetweetIds ( ) , TwitterRestApi . homeTimeline ( { include_entities : true , count : 20 , } ) , TwitterRestApi . mentionTimeline ( ) , ] ) . then ( ( [ my_account , rejected_ids , no_retweet_ids , tweets , mentions ] ) => { Store . dispatch ( setCurrentUser ( new TwitterUser ( my_account ) ) ) ; Store . dispatch ( addRejectedUserIds ( rejected_ids ) ) ; Store . dispatch ( addNoRetweetUserIds ( no_retweet_ids ) ) ; Store . dispatch ( addTweetsToTimeline ( tweets . reverse ( ) . map ( tw => new Tweet ( tw ) ) ) ) ; DB . accounts . storeAccountsInTweets ( tweets ) ; DB . hashtags . storeHashtagsInTweets ( tweets ) ; Store . dispatch ( addMentions ( mentions . map ( j => new Tweet ( j ) ) ) ) ; DB . accounts . storeAccountsInTweets ( mentions ) ; DB . hashtags . storeHashtagsInTweets ( mentions ) ; ipc . send ( 'yf:start-user-stream' as ChannelFromRenderer ) ; } ) ; } ) ; this . subscribe ( 'yf:tweet' , ( json ) => { if ( json . retweeted_status && ! json . retweeted_status . retweeted && Store . getState ( ) . timeline . user . id === json . user . id ) { log . warn ( 'Incorrect response of retweeted status via user stream. Will correct "retweeted"' , json ) ; json . retweeted = true ; json . retweeted_status . retweeted = true ; } const tw = new Tweet ( json ) ; Store . dispatch ( addTweetToTimeline ( tw ) ) ; DB . accounts . storeAccountsInTweet ( json ) ; DB . hashtags . storeHashtagsInTweet ( json ) ; } ) ; this . subscribe ( 'yf:connection-failure' , ( ) => { Store . dispatch ( addSeparator ( ) ) ; } ) ; this . subscribe ( 'yf:api-failure' , ( msg ) => { Store . dispatch ( showMessage ( 'API error: ' + msg , 'error' ) ) ; } ) ; this . subscribe ( 'yf:all-friend-ids' , ( ids ) => { Store . dispatch ( resetFriends ( ids ) ) ; } ) ; this . subscribe ( 'yf:my-account-update' , ( json ) => { Store . dispatch ( updateCurrentUser ( json ) ) ; } ) ; this . subscribe ( 'yf:delete-status' , ( status ) => { Store . dispatch ( deleteStatusInTimeline ( status . id_str ) ) ; } ) ; this . subscribe ( 'yf:liked-status' , ( status , from_user ) => { Store . dispatch ( statusLiked ( new Tweet ( status ) , new TwitterUser ( from_user ) ) ) ; } ) ; this . subscribe ( 'yf:rejected-ids' , ( ids ) => { Store . dispatch ( addRejectedUserIds ( ids ) ) ; } ) ; this . subscribe ( 'yf:unrejected-ids' , ( ids ) => { Store . dispatch ( removeRejectedUserIds ( ids ) ) ; } ) ; this . subscribe ( 'yf:follow' , ( source , target ) => { if ( Store . getState ( ) . timeline . user . id === source . id ) { Store . dispatch ( addFriends ( [ target . id ] ) ) ; Store . dispatch ( setCurrentUser ( new TwitterUser ( source ) ) ) ; } else { Store . dispatch ( setCurrentUser ( new TwitterUser ( target ) ) ) ; } } ) ; this . subscribe ( 'yf:unfollow' , ( target ) => { Store . dispatch ( removeFriends ( [ target . id ] ) ) ; } ) ; log . debug ( 'Started to receive messages' ) ; return this ; } terminate ( ) { for ( const c in this . listeners ) { ipc . removeListener ( c , this . listeners [ c ] ) ; } this . listeners = { } ; log . debug ( 'Terminated receivers' ) ; } }