import { Injectable } from '@angular/core' ; import { of , OperatorFunction } from 'rxjs' ; import { ApiResponse , ApiService } from '../../../common/api/api.service' ; import { catchError , debounceTime , map , switchAll , tap } from 'rxjs/operators' ; import { createUrlRegex } from '../../../helpers/url-regex' ; /**
 * Rich embed structure
 */ export interface RichEmbed { url : string ; entityGuid : string | null ; title ? : string ; description ? : string ; thumbnail ? : string ; } /**
 * Service which helps resolving URLs onto rich embeds
 */ @ Injectable ( ) export class RichEmbedService { param api constructor ( protected api ) { } param body extract ( body ) { const matches = body . match ( createUrlRegex ( ) ) ; return matches && typeof matches [ 0 ] !== 'undefined' ? matches [ 0 ] : null ; } /**
     * Resolves a URL onto a rich embed using engine's preview endpoint
     */ resolve ( debounceMs ) { return input$ => input$ . pipe ( debounceTime ( debounceMs ) , map ( urlOrRichEmbed => { if ( ! urlOrRichEmbed ) { return of ( null ) ; } else if ( typeof urlOrRichEmbed !== 'string' ) { return of ( urlOrRichEmbed ) ; } return this . api . get ( 'api/v1/newsfeed/preview' , { url : urlOrRichEmbed , } ) . pipe ( map ( ( response ) : RichEmbed => { const richEmbed = { url : urlOrRichEmbed , entityGuid : null , title : response . meta . title || '' , description : response . meta . description || '' , } ; if ( response . links && response . links . thumbnail && response . links . thumbnail [ 0 ] ) { richEmbed . thumbnail = response . links . thumbnail [ 0 ] . href ; } return richEmbed ; } ) , catchError ( _ => of ( null ) ) ) ; } ) , switchAll ( ) ) ; } }