forked from mailchain/mailchain-web
/
inbox-compose.component.ts
640 lines (557 loc) · 19.3 KB
/
inbox-compose.component.ts
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
import { Component, OnInit, Output, Input, EventEmitter, ViewChild } from '@angular/core';
import { Mail } from 'src/app/models/mail';
import { OutboundMail } from 'src/app/models/outbound-mail';
import { MailchainService } from 'src/app/services/mailchain/mailchain.service';
import { SendService } from 'src/app/services/mailchain/messages/send.service';
import { PublicKeyService } from 'src/app/services/mailchain/public-key/public-key.service';
import { AddressesService } from 'src/app/services/mailchain/addresses/addresses.service';
import { EnvelopeService } from 'src/app/services/mailchain/envelope/envelope.service';
import { NameserviceService } from 'src/app/services/mailchain/nameservice/nameservice.service';
import { BalanceService } from 'src/app/services/mailchain/addresses/balance.service';
import { Subject, of, Observable } from 'rxjs';
import { debounceTime, distinctUntilChanged, mergeMap } from "rxjs/operators";
import { BsModalService, BsModalRef } from 'ngx-bootstrap/modal';
import { ModalConnectivityErrorComponent } from 'src/app/modals/modal-connectivity-error/modal-connectivity-error.component';
import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import { CKEditorComponent } from '@ckeditor/ckeditor5-angular';
import { getCurrencySymbol } from '@angular/common';
@Component({
selector: '[inbox-compose]',
templateUrl: './inbox-compose.component.html',
styleUrls: ['./inbox-compose.component.scss']
})
export class InboxComposeComponent implements OnInit {
@Input() currentAccount: string;
@Input() currentNetwork: string;
@Input() currentProtocol: string;
@Input() currentMessage: any;
@Output() openMessage = new EventEmitter();
@Output() goToInboxMessages = new EventEmitter();
public model = new Mail()
public fromAddresses: Array<any> = []
public envelopes: Array<any> = []
public balance: number = 0
public currency: string = ""
public fees: string = ""
public sendMessagesDisabled: boolean = false;
private subscription
public currentRecipientValue
public recipientAddressChanged = new Subject<string>();
public recipientLoadingIcon = ""
public recipientLoadingText = ""
public messageToField = ""
public errorTitle: string = ""
public errorMessage: string = ""
public modalConnectivityError: BsModalRef;
public Editor = ClassicEditor;
public inputContentType = "html"
public contentTypeSwitchLabel: string = ""
public envelopeType
public envelopeDescription
public isReply: boolean = false
@ViewChild('editor') public editorComponent: CKEditorComponent;
constructor(
private mailchainService: MailchainService,
private sendService: SendService,
private publicKeyService: PublicKeyService,
private addressesService: AddressesService,
private envelopeService: EnvelopeService,
private nameserviceService: NameserviceService,
private balanceService: BalanceService,
private modalService: BsModalService,
) {
this.initMail()
}
/**
* Initialize empty values for the message model
*/
private initMail() {
this.model.to = ""
this.model.from = ""
this.model.replyTo = ""
this.model.subject = ""
this.model.body = ""
}
/**
* initEditor - initiates CKeditor
*/
private initEditor() {
let editor = this.editorComponent.editorInstance
// Enable nested blockquotes
if (editor && editor.model && editor.model.schema) {
editor.model.schema.on('checkChild', (evt, args) => {
const context = args[0];
const childDefinition = args[1];
if (context.endsWith('blockQuote') && childDefinition.name == 'blockQuote') {
// Prevent next listeners from being called.
evt.stop();
// Set the checkChild()'s return value.
evt.return = true;
}
}, { priority: 'highest' });
};
// Put CKeditor in focus
if (editor && editor.editing) {
editor.editing.view.focus()
}
}
/**
* Sets the available 'from' addresses
*/
private async setFromAddressList() {
this.fromAddresses = await this.addressesService.getAddresses(this.currentProtocol, this.currentNetwork);
}
/**
* Sets the available envelopes
*/
private async setEnvelopeList() {
this.envelopes = await this.envelopeService.getEnvelope();
}
/**
* updates the balance whenver address dropdwon value is changed
*/
public async updateBalanceForAddress(address) {
this.balance = await this.balanceService.getBalance(address, this.currentProtocol, this.currentNetwork);
}
private async setBalance() {
if (this.currentAccount != undefined) {
this.balance = await this.balanceService.getBalance(this.currentAccount, this.currentProtocol, this.currentNetwork);
}
}
/**
* Sets the currency
*/
private async setCurrency() {
this.currency = await this.mailchainService.getCurrencyForProtocol(this.currentProtocol)
}
/**
* Returns the identicon for the an address
* @param address the address
*/
public generateIdenticon(address) {
let icon = this.mailchainService.generateIdenticon(this.currentProtocol, address);
return icon == "" ? "assets/question-circle-regular.svg" : icon
}
/**
* Resolves the recipient field to ENS address
* (or validates public address).
* Leverages debounce (in case of user still typing).
*
* @param event the event from keyup
*/
public recipientResolve(event) {
let eventVal = event.target.value.trim();
if (eventVal == "") {
this.setRecipientLoadingIcon("clear")
this.setRecipientLoadingText()
this.resetModelToField()
} else if (this.currentRecipientValue != eventVal) {
this.setRecipientLoadingIcon("loading")
this.setRecipientLoadingText()
this.resetModelToField()
}
this.currentRecipientValue = eventVal
this.recipientAddressChanged.next(eventVal);
}
/**
* Sets the recipientLoadingIcon to the following:
* loading: spinner
* valid: tick
* invalid: cross
* clear: blank
* @param status ['loading' | 'valid' | 'invalid' | 'clear' ]
*/
public setRecipientLoadingIcon(status) {
switch (status) {
case 'loading':
this.recipientLoadingIcon = "fa fa-spinner fa-pulse"
break;
case 'valid':
this.recipientLoadingIcon = "fa fa-check-circle text-success"
break;
case 'invalid':
this.recipientLoadingIcon = "fa fa-times-circle text-danger"
break;
case 'clear':
this.recipientLoadingIcon = ""
break;
}
}
/**
* Sets the recipientLoadingText to the input text
* @param text the text to display; if blank, clears text
*/
public setRecipientLoadingText(text: string = "") {
this.recipientLoadingText = text
}
/**
* Sets up a subscription to handle user entering an ENS name or Ethereum address in the To field.
* Includes debounce handling and will validate a name or address.
*/
private setupRecipientAddressLookupSubscription() {
this.subscription = this.recipientAddressChanged.pipe(
debounceTime(1500),
distinctUntilChanged(),
mergeMap(searchVal => {
return this.resolveAddress(searchVal)
})
).subscribe((res) => {
res.subscribe(val => {
let address = val['body']['address']
if (
(this.currentProtocol == 'algorand' && this.mailchainService.validateAlgorandAddress(address)) ||
(this.currentProtocol == 'ethereum' && this.mailchainService.validateEthAddress(address)) ||
(this.currentProtocol == 'substrate' && this.mailchainService.validateSubstrateAddress(address))
) {
this.model.to = address
this.setRecipientLoadingIcon('valid')
this.setRecipientLoadingText('valid address')
} else {
this.setRecipientLoadingIcon('invalid')
this.setRecipientLoadingText('invalid address')
}
}, err => {
this.setRecipientLoadingIcon('invalid')
this.setRecipientLoadingText(err['error']['message'])
})
})
};
/**
* resetModelToField resets the to field in the model
*/
public resetModelToField() {
this.model.to = ""
}
/**
* resetModelReplyToField resets the reply-to field in the model
*/
public resetModelReplyToField() {
this.model.replyTo = ""
}
/**
* Determines whether an address is:
* * a public address,
* * an ENS address
*
* Returns observable with body containing address, e,g.
* {body: {address: "0x1234567891234567891234567891234567891234"} }
*/
public async resolveAddress(value) {
let returnObs
if (this.currentProtocol == 'ethereum') {
if (this.mailchainService.validateEnsName(value)) {
returnObs = await this.nameserviceService.resolveName(
this.currentProtocol,
this.currentNetwork,
value
)
} else if (this.mailchainService.validateEthAddress(value)) {
returnObs = of(
{ body: { address: value } }
)
} else {
returnObs = of(
{ body: { address: '' } }
)
}
} else if (this.currentProtocol == 'substrate') {
if (this.mailchainService.validateSubstrateAddress(value)) {
returnObs = of(
{ body: { address: value } }
)
} else {
returnObs = of(
{ body: { address: '' } }
)
}
} else if (this.currentProtocol == 'algorand') {
if (this.mailchainService.validateAlgorandAddress(value)) {
returnObs = of(
{ body: { address: value } }
)
} else {
returnObs = of(
{ body: { address: '' } }
)
}
} else { // return the address anyway
returnObs = of(
{ body: { address: value } }
)
};
return returnObs
}
/**
* Go back to the inbox-messages view
*/
public returnToInboxMessages(): void {
this.goToInboxMessages.emit('');
}
/**
* If there is a currentMessage, call the openMessage.emit to go back to the inbox-messages view; Otherwise, call the functin to return to inbox view
*/
public returnToMessage(): void {
if (this.currentMessage == undefined) {
this.returnToInboxMessages();
} else {
this.openMessage.emit(this.currentMessage);
}
}
/**
* Surpress propagation of events to extensions etc.
*/
public supressEnterPropagation($event: KeyboardEvent): void {
$event.stopPropagation()
}
/**
* Sets the currentAccount as the address in the `from` dropdown
* -or-
* When replying, sets the from address as the address the message was sent to
*/
private setCurrentAccountInFromAddressDropdown() {
if (this.currentAccount != undefined) {
this.model.replyTo = this.model.from = this.currentAccount
}
}
/**
* Sets the envelope in the `envelope` dropdown
* By default takes the first value returned from the /envelop endpoint
*/
private setFirstEnvelopeInEnvelopeDropdown() {
if (this.envelopes != undefined) {
this.envelopeType = this.envelopes[0]["type"]
this.envelopeDescription = this.envelopes[0]["description"]
}
}
/**
* Initialise the message.
* If it's a new message, fields will be blank.
* -or-
* If it's a reply, the correct fields are mapped to build the response.
*/
async ngOnInit(): Promise<void> {
await this.setFromAddressList()
await this.setEnvelopeList()
this.setContentTypeForView()
this.initEditor()
this.setCurrentAccountInFromAddressDropdown()
this.setFirstEnvelopeInEnvelopeDropdown()
this.evaluateReply()
this.handleReplyFields()
this.setupRecipientAddressLookupSubscription()
await this.setBalance()
await this.setCurrency()
}
/**
* Handles content-type in the view
*/
private setContentTypeForView() {
if (this.currentMessage) {
let ct = this.currentMessage.headers["content-type"]
this.inputContentType = this.mailchainService.getContentTypeForView(ct)
}
this.contentTypeSwitchLabel = this.setContentTypeSwitchLabel()
}
/**
* handleReplyInPlaintext prepares message fields for a plaintext reply based on the currentMessage
*/
private handleReplyInPlaintext() {
var messageFrom: string = "",
messageReplyTo: string = "",
messageDate: string = "",
messageTo: string = "",
messageSubject: string = "",
messageBody: string = ""
if (this.currentMessage.headers["from"]) {
messageFrom = '\r\n\r\n>From: ' + this.currentMessage.headers["from"] + "\r\n"
}
if (this.currentMessage.headers["reply-to"]) {
messageReplyTo = '>Reply To: ' + this.currentMessage.headers["reply-to"] + "\r\n"
}
if (this.currentMessage.headers["date"]) {
messageDate = '>Date: ' + this.currentMessage.headers["date"] + "\r\n"
}
if (this.currentMessage.headers["to"]) {
messageTo = '>To: ' + this.currentMessage.headers["to"] + "\r\n"
}
if (this.currentMessage["subject"]) {
messageSubject = '>Subject: ' + this.currentMessage["subject"] + "\r\n" + ">" + "\r\n"
} else {
messageSubject = ""
}
if (this.currentMessage["body"]) {
messageBody = '>' + this.currentMessage["body"]
}
messageBody = messageBody.replace(/\r\n/g, '\r\n>');
this.model.body = messageFrom +
messageReplyTo +
messageDate +
messageTo +
messageSubject +
messageBody
}
/**
* handleReplyInHtml prepares message fields for a html reply based on the currentMessage
*/
private handleReplyInHtml() {
var messageFrom: string = "",
messageReplyTo: string = "",
messageDate: string = "",
messageTo: string = "",
messageSubject: string = "",
messageBody: string = ""
if (this.currentMessage.headers["from"]) {
messageFrom = '<strong>From:</strong> ' + this.currentMessage.headers["from"] + "<br>"
}
if (this.currentMessage.headers["reply-to"]) {
messageReplyTo = '<strong>Reply To:</strong> ' + this.currentMessage.headers["reply-to"] + "<br>"
}
if (this.currentMessage.headers["date"]) {
messageDate = '<strong>Date:</strong> ' + this.currentMessage.headers["date"] + "<br>"
}
if (this.currentMessage.headers["to"]) {
messageTo = '<strong>To:</strong> ' + this.currentMessage.headers["to"] + "<br>"
}
if (this.currentMessage["subject"]) {
messageSubject = '<strong>Subject:</strong> ' + this.currentMessage["subject"]
} else {
messageSubject = ""
}
if (this.currentMessage["body"]) {
messageBody = '<blockquote>' + this.currentMessage["body"] + '<br></blockquote>'
}
this.model.body = "<p></p><p>" +
messageFrom +
messageReplyTo +
messageDate +
messageTo +
messageSubject +
"</p>" +
messageBody
}
/**
* Evalute if the message is a reply or a new message
*/
evaluateReply() {
this.isReply = (this.currentMessage && this.currentMessage.headers) ? true : false
}
/**
* Set the fields for a reply
*/
private handleReplyFields() {
if (this.currentMessage && this.currentMessage.headers) {
if (this.inputContentType == "html") {
this.handleReplyInHtml()
} else if (this.inputContentType == "plaintext") {
this.handleReplyInPlaintext()
}
if (this.currentMessage.headers["reply-to"] && this.currentMessage.headers["reply-to"].length) {
this.model.to = this.mailchainService.parseAddressFromMailchain(this.currentProtocol, this.currentMessage.headers["reply-to"])
} else {
this.model.to = this.mailchainService.parseAddressFromMailchain(this.currentProtocol, this.currentMessage.headers["from"])
}
this.messageToField = this.currentRecipientValue = this.model.to
this.model.replyTo = this.model.from = this.mailchainService.parseAddressFromMailchain(this.currentProtocol, this.currentMessage.headers.to)
this.model.subject = this.addRePrefixToSubject(this.currentMessage["subject"])
}
}
/**
* Checks for 'Re: ' on the subject and adds it if it is not there already.
* @param subject the message subject
*/
private addRePrefixToSubject(subject: string) {
if (subject.startsWith("Re: ")) {
return subject
} else {
return "Re: " + subject
}
};
/**
* onSubmit sends email to the local service. It includes the reset form to handle errors and resets.
*/
public onSubmit() {
this.sendMessagesDisabled = true
var self = this
this.publicKeyService.getPublicKeyFromAddress(
this.model.to,
this.currentNetwork
).subscribe(res => {
this.model.publicKey = res["body"]["public-key"]
this.model.publicKeyEncoding = res["body"]["public-key-encoding"]
this.model.publicKeyKind = res["body"]["public-key-kind"]
this.model.supportedEncryptionTypes = res["body"]["supported-encryption-types"]
var outboundMail = this.generateMessage(this.model, this.inputContentType, this.envelopeType, this.currentProtocol)
this.sendMessage(outboundMail).subscribe(res => {
self.initMail();
self.returnToInboxMessages();
},
err => {
this.handleErrorOnPage(
`Error Code: ${err["error"]["code"]}`,
`<p>${err["error"]["message"]}</p><p>Please visit <a href="https://docs.mailchain.xyz/troubleshooting/common-inbox-errors" target="_blank">Docs: common inbox errors</a> to see how to fix this.</p>`,
)
this.sendMessagesDisabled = false;
this.resetErrorOnPage();
});
})
}
/**
* Builds the OutboundMail object for sending
* @param mailObj The form Mail object
*/
private generateMessage(mailObj: Mail, inputContentType: string, envelope: string, protocol: string): OutboundMail {
return this.mailchainService.generateMail(mailObj, inputContentType, envelope, protocol)
}
/**
* Sends the OutboundMail object on the currently selected network
* @param outboundMail The OutboundMail object
*/
private sendMessage(outboundMail: OutboundMail) {
let protocol = this.currentProtocol
let network = this.currentNetwork
return this.sendService.sendMail(outboundMail, protocol, network)
}
/**
* handleErrorOnPage
*/
public handleErrorOnPage(errorTitle, errorMessage) {
if (this.errorTitle.length == 0 && this.errorMessage.length == 0) {
this.errorTitle = errorTitle
this.errorMessage = errorMessage
const initialState = {
errorTitle: errorTitle,
errorMessage: errorMessage,
};
this.modalConnectivityError = this.modalService.show(ModalConnectivityErrorComponent, { initialState });
this.modalConnectivityError.content.closeBtnName = 'Close'
}
}
/**
* resetErrorOnPage
*/
private resetErrorOnPage() {
this.errorTitle = ""
this.errorMessage = ""
}
public convertToPlainText() {
let res = confirm("Are you sure? This will remove formatting and cannot be changed back to HTML.")
if (res == true) {
let text = document.getElementsByClassName('ck-content')[0]["innerText"]
this.model.body = text
this.inputContentType = "plaintext"
this.setContentTypeSwitchLabel()
} else {
document.getElementById('contentTypeSwitch')["checked"] = false
}
};
/**
* Sets the contentTypeSwitch label
*/
private setContentTypeSwitchLabel() {
if (this.inputContentType == "plaintext") {
return this.contentTypeSwitchLabel = "Plain Text"
} else {
return this.contentTypeSwitchLabel = "Convert to Plain Text"
}
}
}