'use strict' ; import { Api , ProgressCbs , ReqMethod } from '../../shared/api.js' ; import { Dict , Str } from '../../../core/common.js' ; import { GOOGLE_API_HOST } from '../../../core/const.js' ; import { GmailRes } from './gmail-parser.js' ; import { GoogleAuth } from './google-auth.js' ; import { Serializable } from '../../../platform/store/abstract-store.js' ; export class Google { public static webmailUrl = ( acctEmail ) => { return `https://mail.google.com/mail/u/${ acctEmail }` ; } ; public static gmailCall = async < RT > ( acctEmail , method , path , params , progress ? , contentType ? ) : Promise < RT > => { progress = progress || { } ; let data , url ; if ( typeof progress . upload === 'function' ) { url = `${ GOOGLE_API_HOST }/upload/gmail/v1/users/me/${ path }?uploadType=multipart` ; data = params ; } else { url = `${ GOOGLE_API_HOST }/gmail/v1/users/me/${ path }` ; if ( method === 'GET' || method === 'DELETE' ) { data = params ; } else { data = JSON . stringify ( params ) ; } } contentType = contentType || 'application/json; charset=UTF-8' ; const headers = { 'Authorization' : await GoogleAuth . googleApiAuthHeader ( acctEmail ) } ; const xhr = Api . getAjaxProgressXhrFactory ( progress ) ; const request = { xhr , url , method , data , headers , crossDomain : true , contentType , async : true } ; return await GoogleAuth . apiGoogleCallRetryAuthErrorOneTime ( acctEmail , request ) as RT ; } ; public static contactsGet = async ( acctEmail , query ? , progress ? , max = 10 , start = 0 ) => { progress = progress || { } ; const method = 'GET' ; const contentType = 'application/json; charset=UTF-8' ; const url = `${ GOOGLE_API_HOST }/m8/feeds/contacts/default/thin` ; const data = { 'alt' : "json" , 'q' : query , 'v' : '3.0' , 'max-results' : max , 'start-index' : start } ; const xhr = Api . getAjaxProgressXhrFactory ( progress ) ; const headers = { 'Authorization' : await GoogleAuth . googleApiAuthHeader ( acctEmail ) } ; const contacts = await GoogleAuth . apiGoogleCallRetryAuthErrorOneTime ( acctEmail , { xhr , url , method , data , headers , contentType , crossDomain : true , async : true } ) as GmailRes . GoogleContacts ; return contacts . feed . entry && contacts . feed . entry . filter ( entry => ! ! ( entry . gd$email || [ ] ) . find ( email => email . primary === "true" ) ) . map ( e => ( { email : ( e . gd$email || [ ] ) . find ( e => e . primary === "true" ) ! . address , name : e . gd$name && e . gd$name . gd$fullName && e . gd$name . gd$fullName . $t } ) ) ; } ; public static encodeAsMultipartRelated = ( parts ) => { const boundary = 'the_boundary_is_' + Str . sloppyRandom ( 10 ) ; let body = '' ; for ( const type of Object . keys ( parts ) ) { body += '--' + boundary + '\n' ; body += 'Content-Type: ' + type + '\n' ; if ( type . includes ( 'json' ) ) { body += '\n' + parts [ type ] + '\n\n' ; } else { body += 'Content-Transfer-Encoding: base64\n' ; body += '\n' + btoa ( parts [ type ] ) + '\n\n' ; } } body += '--' + boundary + '--' ; return { contentType : 'multipart/related; boundary=' + boundary , body } ; } ; }