import * as request from 'request' ; import * as Consts from './../consts' ; import SkypeAccount from './../skype_account' ; import Utils from "./../utils" ; import * as http from 'http' ; import { CookieJar } from "request" ; import { Login } from "../login" ; import { EventEmitter } from "../utils" ; export class Poll { private requestWithJar : any ; private cookieJar : CookieJar ; private eventEmitter : EventEmitter ; constructor ( cookieJar , eventEmitter ) { this . requestWithJar = request . defaults ( { jar : cookieJar } ) ; this . eventEmitter = eventEmitter ; this . cookieJar = cookieJar ; } public pollAll ( skypeAccount , messagesCallback ) { setTimeout ( ( ) => { this . requestWithJar . post ( Consts . SKYPEWEB_HTTPS + skypeAccount . messagesHost + '/v1/users/ME/endpoints/SELF/subscriptions/0/poll' , { headers : { 'RegistrationToken' : skypeAccount . registrationTokenParams . raw } } , ( error , response , body ) => { if ( ! error && response . statusCode === 200 ) { Poll . parsePollResult ( JSON . parse ( body ) , messagesCallback ) ; } else if ( body && body . errorCode === 729 ) { new Login ( this . cookieJar , this . eventEmitter ) . doLogin ( skypeAccount ) . then ( this . pollAll . bind ( this , skypeAccount , messagesCallback ) ) ; return ; } else { this . eventEmitter . fire ( 'error' , 'Failed to poll messages.' + '.\n Error code: ' + ( response && response . statusCode ? response . statusCode : 'none' ) + '.\n Error: ' + error + '.\n Body: ' + body ) ; } this . pollAll ( skypeAccount , messagesCallback ) ; } ) ; } , 1000 ) ; } private static parsePollResult ( pollResult , messagesCallback ) { if ( pollResult . eventMessages ) { var messages = pollResult . eventMessages . filter ( ( item ) => { return item . resourceType === 'NewMessage' ; } ) ; if ( messages . length ) { messagesCallback ( messages ) ; } } } } export default Poll ;