import { NgZone , Injectable } from '@angular/core' ; import { Client } from './api' ; import { ConfigsService } from '../common/services/configs.service' ; import { Session } from './session' ; @ Injectable ( ) export class ThirdPartyNetworksService { inProgress : boolean = false ; readonly siteUrl : string ; private status : any = { } ; private integrations : any ; private statusReady : Promise < any > ; constructor ( private client , private zone , configs , private session ) { this . siteUrl = configs . get ( 'site_url' ) ; this . integrations = configs . get ( 'thirdpartynetworks' ) ; } getStatus ( refresh = false ) { if ( ! this . statusReady || refresh ) { this . statusReady = this . client . get ( 'api/v1/thirdpartynetworks/status' ) . then ( ( response ) => { this . overrideStatus ( response . thirdpartynetworks ) ; } ) ; } return this . statusReady ; } setStatusKey ( network , value ) { this . getStatus ( ) . then ( ( ) => { this . status [ network ] = value ; } ) ; } overrideStatus ( statusResponse ) { this . status = statusResponse ; } connect ( network ) { switch ( network ) { case 'facebook' : return this . connectFb ( ) ; case 'twitter' : return this . connectTw ( ) ; } throw new Error ( 'Unknown Third-Party Network' ) ; } disconnect ( network ) { switch ( network ) { case 'facebook' : return this . removeFb ( ) ; case 'twitter' : return this . removeTw ( ) ; } throw new Error ( 'Unknown Third-Party Network' ) ; } getStatusKey ( network , key = null ) { if ( ! this . status || ! this . status [ network ] ) { return null ; } if ( key === null ) { return this . status [ network ] ; } return this . status [ network ] [ key ] || null ; } isConnected ( network ) { return this . getStatusKey ( network , 'connected' ) ; } getIntegrations ( ) { return this . integrations ; } hasIntegration ( network ) { return this . integrations && this . integrations [ network ] ; } removeFbLogin ( ) { this . inProgress = true ; return this . client . delete ( 'api/v1/thirdpartynetworks/facebook/login' ) . then ( ( ) => { this . inProgress = false ; if ( this . session . getLoggedInUser ( ) ) { this . session . getLoggedInUser ( ) . signup_method = 'ex-facebook' ; } } ) . catch ( e => { this . inProgress = false ; } ) ; } private connectFb ( ) { this . inProgress = true ; return new Promise < void > ( ( resolve , reject ) => { window . onSuccessCallback = ( ) => this . zone . run ( ( ) => { this . getStatus ( true ) . then ( ( ) => { resolve ( ) ; this . inProgress = false ; } ) ; } ) ; window . onErrorCallback = reason => this . zone . run ( ( ) => { this . inProgress = false ; reject ( reason ) ; } ) ; window . open ( `${ this . siteUrl }api/v1/thirdpartynetworks/facebook/link?no_pages=1` , 'Login with Facebook' , 'toolbar=no, location=no, directories=no, status=no, menubar=no, copyhistory=no, width=600, height=400, top=100, left=100' ) ; } ) ; } private removeFb ( ) { this . inProgress = true ; return this . client . delete ( 'api/v1/thirdpartynetworks/facebook' ) . then ( ( ) => { this . inProgress = false ; this . setStatusKey ( 'facebook' , { connected : false } ) ; } ) . catch ( e => { this . inProgress = false ; } ) ; } private connectTw ( ) { this . inProgress = true ; return new Promise < void > ( ( resolve , reject ) => { window . onSuccessCallback = ( ) => this . zone . run ( ( ) => { this . getStatus ( true ) . then ( ( ) => { resolve ( ) ; this . inProgress = false ; } ) ; } ) ; window . onErrorCallback = reason => this . zone . run ( ( ) => { this . inProgress = false ; reject ( reason ) ; } ) ; window . open ( `${ this . siteUrl }api/v1/thirdpartynetworks/twitter/link` , 'Login with Twitter' , 'toolbar=no, location=no, directories=no, status=no, menubar=no, copyhistory=no, width=600, height=400, top=100, left=100' ) ; } ) ; } private removeTw ( ) { this . inProgress = true ; return this . client . delete ( 'api/v1/thirdpartynetworks/twitter' ) . then ( ( ) => { this . inProgress = false ; this . setStatusKey ( 'twitter' , { connected : false } ) ; } ) . catch ( e => { this . inProgress = false ; } ) ; } }