|
|
import MailosaurClient from 'mailosaur'; |
|
|
import { SecretsManager } from './secrets'; |
|
|
import { envVariables } from '.'; |
|
|
import type { Message, Link } from 'mailosaur/lib/models'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class EmailClient { |
|
|
private client; |
|
|
private startTimestamp: Date; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor() { |
|
|
this.client = new MailosaurClient( SecretsManager.secrets.mailosaur.apiKey ); |
|
|
this.startTimestamp = new Date(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getTestEmailAddress( inboxId: string ) { |
|
|
return this.client.servers.generateEmailAddress( inboxId ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async getLastMatchingMessage( { |
|
|
inboxId, |
|
|
receivedAfter, |
|
|
sentTo, |
|
|
sentFrom, |
|
|
subject, |
|
|
body, |
|
|
}: { |
|
|
inboxId: string; |
|
|
receivedAfter?: Date; |
|
|
sentTo?: string; |
|
|
sentFrom?: string; |
|
|
subject?: string; |
|
|
body?: string; |
|
|
} ): Promise< Message > { |
|
|
const searchCriteria = { |
|
|
sentTo: sentTo !== undefined ? sentTo : '', |
|
|
sentFrom: sentFrom !== undefined ? sentFrom : '', |
|
|
subject: subject !== undefined ? subject : '', |
|
|
body: body !== undefined ? body : '', |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const message = await this.client.messages.get( inboxId, searchCriteria, { |
|
|
receivedAfter: receivedAfter !== undefined ? receivedAfter : this.startTimestamp, |
|
|
timeout: 120 * 1000, |
|
|
} ); |
|
|
return message; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async getLinksFromMessage( message: Message ): Promise< string[] > { |
|
|
if ( ! message.html ) { |
|
|
throw new Error( 'Message did not contain a body.' ); |
|
|
} |
|
|
|
|
|
const links = message.html.links as Link[]; |
|
|
if ( ! links || links.length === 0 ) { |
|
|
throw new Error( 'Message did not contain any links.' ); |
|
|
} |
|
|
|
|
|
const results = new Set< string >(); |
|
|
for ( const link of links ) { |
|
|
if ( link.href ) { |
|
|
results.add( link.href ); |
|
|
} |
|
|
} |
|
|
return Array.from( results ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getLinkFromMessageByKey( message: Message, key: string ): string | null { |
|
|
if ( ! message.html ) { |
|
|
throw new Error( 'Message did not contain a body.' ); |
|
|
} |
|
|
|
|
|
const links = message.html.links as Link[]; |
|
|
|
|
|
for ( const link of links ) { |
|
|
if ( link.text && link.text.trim().includes( key.trim() ) ) { |
|
|
return link.href as string; |
|
|
} |
|
|
} |
|
|
return null; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getMagicLink( message: Message ): URL { |
|
|
const link = message.text?.links?.shift(); |
|
|
|
|
|
if ( ! link ) { |
|
|
throw new Error( 'Message did not contain text links. ' ); |
|
|
} |
|
|
|
|
|
const magicLinkURL = new URL( link?.href as string ); |
|
|
const baseURL = new URL( envVariables.CALYPSO_BASE_URL ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new URL( magicLinkURL.pathname + magicLinkURL.search, baseURL.origin ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get2FACodeFromMessage( message: Message ): string { |
|
|
if ( ! message.text ) { |
|
|
throw new Error( 'Message is not defined.' ); |
|
|
} |
|
|
|
|
|
if ( message.text.codes?.length === 0 ) { |
|
|
throw new Error( 'Message has no OTP code.' ); |
|
|
} |
|
|
return message.text.codes?.at( 0 )?.value as string; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async deleteMessage( message: Message ): Promise< void > { |
|
|
if ( ! message.id ) { |
|
|
throw new Error( 'Message ID not found.' ); |
|
|
} |
|
|
return await this.client.messages.del( message.id ); |
|
|
} |
|
|
} |
|
|
|