import { Injectable } from '@angular/core' ; import { HttpClient , HttpHeaders } from '@angular/common/http' ; import { environment } from '../../../../../environments/environment' ; import { Email } from '../models/email' ; import { Observable } from 'rxjs' ; import { shareReplay } from 'rxjs/operators' ; import { TokenRequest } from '../models/token-request' ; import { User } from 'carey-user' ; import { EmailIntegration } from '../models/email-integration' ; import { AuthenticationService } from 'carey-auth' ; const baseUrl = environment . baseEmailServiceUrl ; const gmailRedirectUrl = environment . gmailRedirectUrl ; const textHeaders = new HttpHeaders ( ) . set ( 'Content-Type' , 'text/plain; charset=utf-8' ) ; const replyToHtmlSeparator = '<br/><br/><br/>-----<br/><br/>' ; @ Injectable ( { providedIn : 'root' } ) export class EmailService { private emailMessagesRequest$ : Observable < Email [ ] > = null ; constructor ( private http , private authenticationService ) { } updateUserEmailChoice ( user , choice ) { let url = `${ baseUrl }/user/${ user . id }` ; let emailIntegration = { integrationType : choice } as EmailIntegration ; return this . http . patch ( url , emailIntegration ) ; } getGoogleAuthCodeFlowUrl ( ) { let url = `${ baseUrl }/email/authorizationCode?redirectUrl=${ gmailRedirectUrl }` ; return this . http . get ( url , { headers : textHeaders , responseType : 'text' } ) ; } fetchToken ( code ) { console . log ( "code is " + code ) ; let tokenRequest = { code : code , redirectUrl : gmailRedirectUrl } as TokenRequest ; let url = `${ baseUrl }/email/token` ; console . log ( "tokenrequest is " , tokenRequest ) ; return this . http . post ( url , tokenRequest , { responseType : 'text' } ) ; } fetchInbox ( refresh ? ) { let url = `${ baseUrl }/email/inbox` ; console . log ( "Fetch inbox URL is " + url ) ; if ( ! this . emailMessagesRequest$ || refresh ) { this . emailMessagesRequest$ = this . http . get < Email [ ] > ( url ) . pipe ( shareReplay ( 1 ) ) ; } return this . emailMessagesRequest$ ; } fetchMessageById ( id ) { let url = `${ baseUrl }/email/messages/${ id }` ; console . log ( "Fetch message URL is " + url ) ; return this . http . get < Email > ( url ) ; } getDisplayableFrom ( from ) { let cleanText = from . replace ( /<\/?[^>]+(>|$)/g , "" ) ; if ( cleanText . length > 20 ) { cleanText = cleanText . substring ( 0 , 21 ) + '...' ; } return cleanText ; } sendEmail ( email ) { let url = `${ baseUrl }/email/messages` ; console . log ( "Send email URL is " + url ) ; return this . http . post < Email > ( url , email ) ; } getEmailHistory ( replyToEmail ) { let emailHistory = '' ; if ( replyToEmail ) { if ( replyToEmail . html ) { emailHistory = replyToHtmlSeparator + replyToEmail . html ; console . log ( replyToEmail . html ) ; } else { emailHistory = replyToHtmlSeparator + replyToEmail . plainText ; } } return emailHistory ; } getEmailAddressFromString ( emailAddress ) { let justEmail = emailAddress ; if ( emailAddress && emailAddress . indexOf ( "<" ) > - 1 ) { let regex = /<(.*)>/g ; let matches = regex . exec ( emailAddress ) ; if ( matches . length > 0 ) justEmail = matches [ 1 ] ; } return justEmail ; } getReplySubject ( replyToEmail ) { let subject = '' ; if ( replyToEmail && replyToEmail . subject ) { let oldSubject = replyToEmail . subject ; if ( ! oldSubject . toLowerCase ( ) . startsWith ( 're:' ) ) { subject = 'Re: ' + oldSubject ; } } return subject ; } clearObservables ( ) { this . emailMessagesRequest$ = null ; } }