File size: 5,702 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import MailosaurClient from 'mailosaur';
import { SecretsManager } from './secrets';
import { envVariables } from '.';
import type { Message, Link } from 'mailosaur/lib/models';

/**
 * Wrapper client around the Mailosaur.io Node.JS client.
 */
export class EmailClient {
	private client;
	private startTimestamp: Date;

	/**
	 * Construct and instance of the EmailClient.
	 */
	constructor() {
		this.client = new MailosaurClient( SecretsManager.secrets.mailosaur.apiKey );
		this.startTimestamp = new Date();
	}

	/**
	 * Returns a test email address.
	 *
	 * @param {string} inboxId Inbox ID to use for the test email address.
	 */
	getTestEmailAddress( inboxId: string ) {
		return this.client.servers.generateEmailAddress( inboxId );
	}

	/**
	 * Given an inbox ID and sentTo (phone/email), retrieves the latest message.
	 *
	 * @param param0 Keyed parameter object.
	 * @param {string} param0.inboxId ID of the inbox to look into. Also known as serverId in Mailosaur parlance.
	 * @param {Date} param0.receivedAfter Timestamp marking the earliest point the search should look up.
	 * @param {string} param0.sentTo Recipient email or phone number.
	 * @param {string} param0.sentFrom Sender email or phone number.
	 * @param {string} param0.subject Subject of the message.
	 * @param {string} param0.body Body of the message.
	 * @returns {Message} Message object returned by Mailosaur client.
	 */
	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 : '',
		};

		// Get messages received after either when the client was
		// initialized, or a specified timestamp.
		const message = await this.client.messages.get( inboxId, searchCriteria, {
			receivedAfter: receivedAfter !== undefined ? receivedAfter : this.startTimestamp,
			timeout: 120 * 1000, // Sometimes email is slow to be received.
		} );
		return message;
	}

	/**
	 * Extracts and returns all links from a message.
	 *
	 * @param {Message} message Representing the message.
	 * @returns {Promise<string[]} Array of links contained in the message.
	 * @throws {Error} If the message did not contain a body or no links were found.
	 */
	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 );
	}

	/**
	 * Given an email message and a key, returns a URL if the link text
	 * matches the key.
	 *
	 * If no matching links are found, this method returns `null`.
	 *
	 * If the email message is not valid HTML, this method throws.
	 *
	 * @param {Message} message Representing the message.
	 * @param {string} key Link text.
	 * @returns {string|null} If a link text matching the supplied key is found
	 * 	the link URL is returned. Otherwise, returns null.
	 * @throws {Error} If the message is not valid HTML.
	 */
	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;
	}

	/**
	 * Specialized method to return human-friendly magic login link.
	 *
	 * Also performs normalization of the link.
	 * (eg. target calypso.live if run from that environment.)
	 *
	 * @param {Message} message Representing the message.
	 * @returns {URL} URL object for the magic link.
	 * @throws {Error} IF the message did not have any links.
	 */
	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 );

		// Returns a new URL object with normalized magic link.
		// Useful when running tests against environments other than the default
		// CALYPSO_BASE_URL.
		// Example: https://wordpress.com -> https://container-something.calypso.live.
		return new URL( magicLinkURL.pathname + magicLinkURL.search, baseURL.origin );
	}

	/**
	 * Extracts and returns a 2FA code from the message.
	 *
	 * @param {Message} message Instance of a Mailosaur message.
	 * @returns {string} 2FA code.
	 * @throws {Error} If the message did not contain a 2FA code.
	 */
	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;
	}

	/**
	 * Given a Message object, permanently deletes the message from the server.
	 *
	 * @param {Message} message E-mail message to delete.
	 */
	async deleteMessage( message: Message ): Promise< void > {
		if ( ! message.id ) {
			throw new Error( 'Message ID not found.' );
		}
		return await this.client.messages.del( message.id );
	}
}