import Action , { ThunkAction } from './type' ; import Item from '../item/item' ; import Tweet , { TwitterUser } from '../item/tweet' ; import log from '../log' ; import TwitterRestApi from '../twitter/rest_api' ; import KeymapTransition from '../keybinds/keymap_transition' ; export function openUserTimeline ( user ) { return dispatch => { KeymapTransition . enterSlaveTimeline ( ) ; dispatch ( { type : 'OpenUserTimeline' , user , } ) ; } ; } function mergeHigherOrder ( left , right ) { const ret = [ ] as Tweet [ ] ; const push = Array . prototype . push ; while ( true ) { if ( left . length === 0 ) { push . apply ( ret , right ) ; return ret ; } else if ( right . length === 0 ) { push . apply ( ret , left ) ; return ret ; } const l = left [ 0 ] ; const r = right [ 0 ] ; const cmp = l . compareId ( r ) ; if ( cmp < 0 ) { ret . push ( right . shift ( ) ! ) ; } else if ( cmp > 0 ) { ret . push ( left . shift ( ) ! ) ; } else { ret . push ( left . shift ( ) ! ) ; right . shift ( ) ; } } } export function gatherConversationStatuses ( status ) { return Promise . all ( [ TwitterRestApi . conversationStatuses ( status . id , status . user . screen_name ) . then ( json => json . map ( j => new Tweet ( j ) ) ) , Promise . resolve ( status . getChainedRelatedStatuses ( ) ) , ] ) . then ( ( [ from_search , from_related ] ) => { const merged = mergeHigherOrder ( from_search , from_related ) ; log . debug ( 'Merged search/tweets with related tweets:' , merged ) ; merged . push ( status ) ; while ( status . in_reply_to_status !== null ) { merged . push ( status . in_reply_to_status ) ; status = status . in_reply_to_status ; } return merged ; } ) ; } export function openConversationTimeline ( status ) { return dispatch => { gatherConversationStatuses ( status ) . then ( statuses => { KeymapTransition . enterSlaveTimeline ( ) ; dispatch ( { type : 'OpenConversationTimeline' , statuses , } ) ; } ) ; } ; } export function closeSlaveTimeline ( ) { return ( dispatch , getState ) => { KeymapTransition . escapeFromCurrentKeymaps ( getState ( ) ) ; dispatch ( { type : 'CloseSlaveTimeline' , } ) ; } ; } export function backSlaveTimeline ( ) { return { type : 'BackSlaveTimeline' , } ; } export function addUserTweets ( user_id , statuses ) { return { type : 'AddUserTweets' , user_id , statuses , } ; } export function appendPastItems ( user_id , items ) { return { type : 'AppendPastItems' , user_id , items , } ; } export function focusSlaveNext ( ) { return { type : 'FocusSlaveNext' , } ; } export function focusSlavePrev ( ) { return { type : 'FocusSlavePrev' , } ; } export function focusSlaveTop ( ) { return { type : 'FocusSlaveTop' , } ; } export function focusSlaveBottom ( ) { return { type : 'FocusSlaveBottom' , } ; } export function focusSlaveOn ( index ) { return { type : 'FocusSlaveOn' , index , } ; } export function blurSlaveTimeline ( ) { return { type : 'BlurSlaveTimeline' , } ; }