tutanota/src/mail/editor/SendMailModel.ts

1035 lines
36 KiB
TypeScript
Raw Normal View History

import { ApprovalStatus, ConversationType, MailFolderType, MailMethod, MAX_ATTACHMENT_SIZE, OperationType, ReplyType } from "../../api/common/TutanotaConstants"
import {
2021-12-28 13:53:11 +01:00
AccessBlockedError,
LockedError,
NotAuthorizedError,
NotFoundError,
PayloadTooLargeError,
PreconditionFailedError,
TooManyRequestsError,
2021-02-03 17:13:38 +01:00
} from "../../api/common/error/RestError"
import { UserError } from "../../api/main/UserError"
import { getPasswordStrengthForUser, isSecurePassword, PASSWORD_MIN_SECURE_VALUE } from "../../misc/passwords/PasswordUtils"
import {
cleanMatch,
deduplicate,
downcast,
findAndRemove,
getFromMap,
LazyLoaded,
neverNull,
noOp,
ofClass,
promiseMap,
remove,
typedValues,
} from "@tutao/tutanota-utils"
import { checkAttachmentSize, getDefaultSender, getTemplateLanguages } from "../model/MailUtils"
import type { EncryptedMailAddress, File as TutanotaFile, Mail, MailboxProperties } from "../../api/entities/tutanota/TypeRefs.js"
import { ContactTypeRef, ConversationEntryTypeRef, File, FileTypeRef, MailboxPropertiesTypeRef, MailTypeRef } from "../../api/entities/tutanota/TypeRefs.js"
import { FileNotFoundError } from "../../api/common/error/FileNotFoundError"
import type { LoginController } from "../../api/main/LoginController"
import type { MailboxDetail, MailModel } from "../model/MailModel"
import { RecipientNotResolvedError } from "../../api/common/error/RecipientNotResolvedError"
import stream from "mithril/stream"
import Stream from "mithril/stream"
import type { EntityUpdateData } from "../../api/main/EventController"
import { EventController, isUpdateForTypeRef } from "../../api/main/EventController"
import { isMailAddress } from "../../misc/FormatValidator"
import type { ContactModel } from "../../contacts/model/ContactModel"
import type { Language, TranslationKey, TranslationText } from "../../misc/LanguageViewModel"
import { getAvailableLanguageCode, getSubstitutedLanguageCode, lang, languages } from "../../misc/LanguageViewModel"
import type { UserController } from "../../api/main/UserController"
import { RecipientsNotFoundError } from "../../api/common/error/RecipientsNotFoundError"
import { checkApprovalStatus } from "../../misc/LoginUtils"
import { EntityClient } from "../../api/common/EntityClient"
import { getContactDisplayName } from "../../contacts/model/ContactUtils"
import { getListId, isSameId, stringToCustomId } from "../../api/common/utils/EntityUtils"
import { CustomerPropertiesTypeRef } from "../../api/entities/sys/TypeRefs.js"
import type { InlineImages } from "../view/MailViewer"
import { cloneInlineImages, revokeInlineImages } from "../view/MailGuiUtils"
import { MailBodyTooLargeError } from "../../api/common/error/MailBodyTooLargeError"
import type { MailFacade } from "../../api/worker/facades/lazy/MailFacade.js"
import { assertMainOrNode } from "../../api/common/Env"
import { DataFile } from "../../api/common/DataFile"
import { FileReference } from "../../api/common/utils/FileUtils"
import { PartialRecipient, Recipient, RecipientList, Recipients, RecipientType } from "../../api/common/recipients/Recipient"
import { RecipientsModel, ResolvableRecipient, ResolveMode } from "../../api/main/RecipientsModel"
import { createApprovalMail } from "../../api/entities/monitor/TypeRefs"
import { DateProvider } from "../../api/common/DateProvider.js"
import { RecipientField } from "../model/MailUtils.js"
import { getSenderName } from "../../misc/MailboxPropertiesUtils.js"
import { isLegacyMail, MailWrapper } from "../../api/common/MailWrapper.js"
2021-12-28 13:53:11 +01:00
assertMainOrNode()
2021-12-23 14:03:23 +01:00
export const TOO_MANY_VISIBLE_RECIPIENTS = 10
2021-12-28 13:53:11 +01:00
export type Attachment = TutanotaFile | DataFile | FileReference
export type InitAsResponseArgs = {
2021-12-28 13:53:11 +01:00
previousMail: Mail
conversationType: ConversationType
senderMailAddress: string
recipients: Recipients
2021-12-28 13:53:11 +01:00
attachments: TutanotaFile[]
subject: string
bodyText: string
replyTos: RecipientList
}
2022-03-17 16:18:17 +01:00
type InitArgs = {
conversationType: ConversationType
subject: string
bodyText: string
recipients: Recipients
confidential: boolean | null
draft?: Mail | null
2022-03-17 16:18:17 +01:00
senderMailAddress?: string
attachments?: ReadonlyArray<Attachment>
replyTos?: RecipientList
previousMail?: Mail | null
previousMessageId?: string | null
initialChangedState: boolean | null
2022-03-17 16:18:17 +01:00
}
/**
* Model which allows sending mails interactively - including resolving of recipients and handling of drafts.
*/
export class SendMailModel {
onMailChanged: Stream<null> = stream(null)
onRecipientDeleted: Stream<{ field: RecipientField; recipient: Recipient } | null> = stream(null)
2022-03-17 16:18:17 +01:00
onBeforeSend: () => void = noOp
loadedInlineImages: InlineImages = new Map()
// Isn't private because used by MinimizedEditorOverlay, refactor?
2022-03-17 16:18:17 +01:00
draft: Mail | null = null
private conversationType: ConversationType = ConversationType.NEW
private subject: string = ""
private body: string = ""
private recipients: Map<RecipientField, Array<ResolvableRecipient>> = new Map()
2022-03-17 16:18:17 +01:00
private senderAddress: string
private confidential: boolean
// contains either Files from Tutanota or DataFiles of locally loaded files. these map 1:1 to the _attachmentButtons
private attachments: Array<Attachment> = []
private replyTos: Array<ResolvableRecipient> = []
2022-03-17 16:18:17 +01:00
// only needs to be the correct value if this is a new email. if we are editing a draft, conversationType is not used
private previousMessageId: Id | null = null
private previousMail: Mail | null = null
private selectedNotificationLanguage: string
private availableNotificationTemplateLanguages: Array<Language> = []
private mailChangedAt: number = 0
private mailSavedAt: number = 1
2022-03-17 16:18:17 +01:00
private passwords: Map<string, string> = new Map()
2021-12-28 13:53:11 +01:00
// The promise for the draft currently being saved
private currentSavePromise: Promise<void> | null = null
// If saveDraft is called while the previous call is still running, then flag to call again afterwards
private doSaveAgain: boolean = false
private recipientsResolved = new LazyLoaded<void>(async () => {})
2021-12-28 13:53:11 +01:00
/**
* creates a new empty draft message. calling an init method will fill in all the blank data
*/
constructor(
2022-03-17 16:18:17 +01:00
public readonly mailFacade: MailFacade,
public readonly entity: EntityClient,
public readonly logins: LoginController,
public readonly mailModel: MailModel,
public readonly contactModel: ContactModel,
private readonly eventController: EventController,
public readonly mailboxDetails: MailboxDetail,
private readonly recipientsModel: RecipientsModel,
private readonly dateProvider: DateProvider,
private mailboxProperties: MailboxProperties,
2021-12-28 13:53:11 +01:00
) {
const userProps = logins.getUserController().props
2022-03-17 16:18:17 +01:00
this.senderAddress = this.getDefaultSender()
this.confidential = !userProps.defaultUnconfidential
2021-12-28 13:53:11 +01:00
2022-03-17 16:18:17 +01:00
this.selectedNotificationLanguage = getAvailableLanguageCode(userProps.notificationMailLanguage || lang.code)
this.updateAvailableNotificationTemplateLanguages()
2021-12-28 13:53:11 +01:00
2023-02-22 17:31:36 +01:00
this.eventController.addEntityListener(this.entityEventReceived)
2022-03-17 16:18:17 +01:00
}
2023-02-22 17:31:36 +01:00
private readonly entityEventReceived = async (updates: ReadonlyArray<EntityUpdateData>) => {
for (const update of updates) {
2022-03-17 16:18:17 +01:00
await this.handleEntityEvent(update)
}
2021-12-28 13:53:11 +01:00
}
/**
* Sort list of all languages alphabetically
* then we see if the user has custom notification templates
* in which case we replace the list with just the templates that the user has specified
*/
2022-03-17 16:18:17 +01:00
private async updateAvailableNotificationTemplateLanguages(): Promise<void> {
this.availableNotificationTemplateLanguages = languages.slice().sort((a, b) => lang.get(a.textId).localeCompare(lang.get(b.textId)))
const filteredLanguages = await getTemplateLanguages(this.availableNotificationTemplateLanguages, this.entity, this.logins)
if (filteredLanguages.length > 0) {
2022-12-27 15:37:40 +01:00
const languageCodes = filteredLanguages.map((l) => l.code)
2022-03-17 16:18:17 +01:00
this.selectedNotificationLanguage =
getSubstitutedLanguageCode(this.logins.getUserController().props.notificationMailLanguage || lang.code, languageCodes) || languageCodes[0]
this.availableNotificationTemplateLanguages = filteredLanguages
}
2021-12-28 13:53:11 +01:00
}
user(): UserController {
2022-03-17 16:18:17 +01:00
return this.logins.getUserController()
2021-12-28 13:53:11 +01:00
}
getPreviousMail(): Mail | null {
2022-03-17 16:18:17 +01:00
return this.previousMail
2021-12-28 13:53:11 +01:00
}
getConversationType(): ConversationType {
2022-03-17 16:18:17 +01:00
return this.conversationType
2021-12-28 13:53:11 +01:00
}
setPassword(mailAddress: string, password: string) {
this.onMailChanged(null)
2022-03-17 16:18:17 +01:00
this.passwords.set(mailAddress, password)
2021-12-28 13:53:11 +01:00
}
getPassword(mailAddress: string): string {
2022-03-17 16:18:17 +01:00
return this.passwords.get(mailAddress) || ""
2021-12-28 13:53:11 +01:00
}
getSubject(): string {
2022-03-17 16:18:17 +01:00
return this.subject
2021-12-28 13:53:11 +01:00
}
setSubject(subject: string) {
this.markAsChangedIfNecessary(subject !== this.subject)
2022-03-17 16:18:17 +01:00
this.subject = subject
2021-12-28 13:53:11 +01:00
}
getBody(): string {
2022-03-17 16:18:17 +01:00
return this.body
2021-12-28 13:53:11 +01:00
}
setBody(body: string) {
this.markAsChangedIfNecessary(this.body !== body)
2022-03-17 16:18:17 +01:00
this.body = body
2021-12-28 13:53:11 +01:00
}
/**
* set the mail address used to send the mail.
* @param senderAddress the mail address that will show up lowercased in the sender field of the sent mail.
*/
2021-12-28 13:53:11 +01:00
setSender(senderAddress: string) {
// we can (and should) do this because we lowercase all addresses on signup and when creating aliases.
senderAddress = senderAddress.toLowerCase()
this.markAsChangedIfNecessary(this.senderAddress !== senderAddress)
2022-03-17 16:18:17 +01:00
this.senderAddress = senderAddress
2021-12-28 13:53:11 +01:00
}
getSender(): string {
2022-03-17 16:18:17 +01:00
return this.senderAddress
2021-12-28 13:53:11 +01:00
}
/**
* Returns the strength indicator for the recipients password
* @returns value between 0 and 100
*/
getPasswordStrength(recipient: PartialRecipient): number {
return getPasswordStrengthForUser(this.getPassword(recipient.address), recipient, this.mailboxDetails, this.logins)
2021-12-28 13:53:11 +01:00
}
hasMailChanged(): boolean {
return this.mailChangedAt > this.mailSavedAt
2021-12-28 13:53:11 +01:00
}
/**
* update the changed state of the mail.
* will only be reset when saving.
*/
markAsChangedIfNecessary(hasChanged: boolean) {
if (!hasChanged) return
this.mailChangedAt = this.dateProvider.now()
// if this method is called wherever state gets changed, onMailChanged should function properly
this.onMailChanged(null)
2021-12-28 13:53:11 +01:00
}
/**
*
* @param recipients
* @param subject
* @param bodyText
* @param attachments
* @param confidential
* @param senderMailAddress
* @param initialChangedState
2021-12-28 13:53:11 +01:00
* @returns {Promise<SendMailModel>}
*/
initWithTemplate(
recipients: Recipients,
subject: string,
bodyText: string,
attachments?: ReadonlyArray<Attachment>,
confidential?: boolean,
senderMailAddress?: string,
2022-12-27 15:37:40 +01:00
initialChangedState?: boolean,
2021-12-28 13:53:11 +01:00
): Promise<SendMailModel> {
2022-03-17 16:18:17 +01:00
return this.init({
2021-12-28 13:53:11 +01:00
conversationType: ConversationType.NEW,
subject,
bodyText,
recipients,
attachments,
confidential: confidential ?? null,
2021-12-28 13:53:11 +01:00
senderMailAddress,
2022-12-27 15:37:40 +01:00
initialChangedState: initialChangedState ?? null,
2021-12-28 13:53:11 +01:00
})
}
async initAsResponse(args: InitAsResponseArgs, inlineImages: InlineImages): Promise<SendMailModel> {
const { previousMail, conversationType, senderMailAddress, recipients, attachments, subject, bodyText, replyTos } = args
2021-12-28 13:53:11 +01:00
let previousMessageId: string | null = null
2022-03-17 16:18:17 +01:00
await this.entity
.load(ConversationEntryTypeRef, previousMail.conversationEntry)
.then((ce) => {
previousMessageId = ce.messageId
})
.catch(
ofClass(NotFoundError, (e) => {
console.log("could not load conversation entry", e)
}),
)
2021-12-28 13:53:11 +01:00
// if we reuse the same image references, changing the displayed mail in mail view will cause the minimized draft to lose
// that reference, because it will be revoked
this.loadedInlineImages = cloneInlineImages(inlineImages)
2022-03-17 16:18:17 +01:00
return this.init({
2021-12-28 13:53:11 +01:00
conversationType,
subject,
bodyText,
recipients,
senderMailAddress,
confidential: previousMail.confidential,
attachments,
replyTos,
previousMail,
previousMessageId,
2022-12-27 15:37:40 +01:00
initialChangedState: false,
2021-12-28 13:53:11 +01:00
})
}
async initWithDraft(attachments: File[], mailWrapper: MailWrapper, inlineImages: InlineImages): Promise<SendMailModel> {
2021-12-28 13:53:11 +01:00
let previousMessageId: string | null = null
let previousMail: Mail | null = null
const draft = mailWrapper.getMail()
2022-03-17 16:18:17 +01:00
const conversationEntry = await this.entity.load(ConversationEntryTypeRef, draft.conversationEntry)
const conversationType = downcast<ConversationType>(conversationEntry.conversationType)
if (conversationEntry.previous) {
try {
2022-03-17 16:18:17 +01:00
const previousEntry = await this.entity.load(ConversationEntryTypeRef, conversationEntry.previous)
previousMessageId = previousEntry.messageId
if (previousEntry.mail) {
2022-03-17 16:18:17 +01:00
previousMail = await this.entity.load(MailTypeRef, previousEntry.mail)
}
} catch (e) {
if (e instanceof NotFoundError) {
// ignore
} else {
throw e
}
2021-12-28 13:53:11 +01:00
}
}
2021-12-28 13:53:11 +01:00
// if we reuse the same image references, changing the displayed mail in mail view will cause the minimized draft to lose
// that reference, because it will be revoked
this.loadedInlineImages = cloneInlineImages(inlineImages)
const { confidential, sender, subject } = draft
let recipients: Recipients
let replyTos: EncryptedMailAddress[]
if (!isLegacyMail(draft) && mailWrapper) {
const { toRecipients, ccRecipients, bccRecipients } = mailWrapper.getDetails().recipients
recipients = {
to: toRecipients,
cc: ccRecipients,
bcc: bccRecipients,
}
replyTos = mailWrapper.getReplyTos()
} else {
const { toRecipients, ccRecipients, bccRecipients } = draft
recipients = {
to: toRecipients,
cc: ccRecipients,
bcc: bccRecipients,
}
replyTos = draft.replyTos
2021-12-28 13:53:11 +01:00
}
const bodyText = mailWrapper.getMailBodyText()
2022-03-17 16:18:17 +01:00
return this.init({
2021-12-28 13:53:11 +01:00
conversationType: conversationType,
subject,
bodyText,
recipients,
draft,
senderMailAddress: sender.address,
confidential,
attachments,
replyTos,
previousMail,
previousMessageId,
2022-12-27 15:37:40 +01:00
initialChangedState: false,
2021-12-28 13:53:11 +01:00
})
}
2022-12-27 15:37:40 +01:00
private async init({
conversationType,
subject,
bodyText,
draft,
recipients,
senderMailAddress,
confidential,
attachments,
replyTos,
previousMail,
previousMessageId,
initialChangedState,
}: InitArgs): Promise<SendMailModel> {
2022-03-17 16:18:17 +01:00
this.conversationType = conversationType
this.subject = subject
this.body = bodyText
this.draft = draft || null
2021-12-28 13:53:11 +01:00
let to: RecipientList
let cc: RecipientList
let bcc: RecipientList
2021-12-28 13:53:11 +01:00
if (recipients instanceof Array) {
to = recipients
cc = []
bcc = []
} else {
to = recipients.to ?? []
cc = recipients.cc ?? []
bcc = recipients.bcc ?? []
2021-12-28 13:53:11 +01:00
}
2022-12-27 15:37:40 +01:00
const recipientsFilter = (recipientList: Array<PartialRecipient>) =>
deduplicate(
recipientList.filter((r) => isMailAddress(r.address, false)),
(a, b) => a.address === b.address,
)
// Making it LazyLoaded() will allow us to retry it in case it fails.
// It is very important that we insert the recipients here synchronously. Even though it is inside the async function it will call insertRecipient()
// right away when we call getAsync() below
this.recipientsResolved = new LazyLoaded(async () => {
await Promise.all([
promiseMap(recipientsFilter(to), async (r) => this.insertRecipient(RecipientField.TO, r)),
promiseMap(recipientsFilter(cc), async (r) => this.insertRecipient(RecipientField.CC, r)),
promiseMap(recipientsFilter(bcc), async (r) => this.insertRecipient(RecipientField.BCC, r)),
])
})
// noinspection ES6MissingAwait
this.recipientsResolved.getAsync()
2021-12-28 13:53:11 +01:00
// .toLowerCase because all our aliases and accounts are lowercased on creation
this.senderAddress = senderMailAddress?.toLowerCase() || this.getDefaultSender()
this.confidential = confidential ?? !this.user().props.defaultUnconfidential
2022-03-17 16:18:17 +01:00
this.attachments = []
2021-12-28 13:53:11 +01:00
if (attachments) {
this.attachFiles(attachments)
}
2022-12-27 15:37:40 +01:00
this.replyTos = recipientsFilter(replyTos ?? []).map((recipient) => this.recipientsModel.resolve(recipient, ResolveMode.Eager))
2022-03-17 16:18:17 +01:00
this.previousMail = previousMail || null
this.previousMessageId = previousMessageId || null
this.mailChangedAt = this.dateProvider.now()
// Determine if we should have this mail already be detected as modified so it saves.
if (initialChangedState) {
this.onMailChanged(null)
this.mailSavedAt = this.mailChangedAt - 1
} else {
this.mailSavedAt = this.mailChangedAt + 1
}
2022-03-17 16:18:17 +01:00
return this
2021-12-28 13:53:11 +01:00
}
2022-03-17 16:18:17 +01:00
private getDefaultSender(): string {
return getDefaultSender(this.logins, this.mailboxDetails)
2021-12-28 13:53:11 +01:00
}
getRecipientList(type: RecipientField): Array<ResolvableRecipient> {
2022-03-17 16:18:17 +01:00
return getFromMap(this.recipients, type, () => [])
2021-12-28 13:53:11 +01:00
}
toRecipients(): Array<ResolvableRecipient> {
return this.getRecipientList(RecipientField.TO)
2021-12-28 13:53:11 +01:00
}
toRecipientsResolved(): Promise<Array<Recipient>> {
2022-12-27 15:37:40 +01:00
return Promise.all(this.toRecipients().map((recipient) => recipient.resolved()))
}
ccRecipients(): Array<ResolvableRecipient> {
return this.getRecipientList(RecipientField.CC)
2021-12-28 13:53:11 +01:00
}
ccRecipientsResolved(): Promise<Array<Recipient>> {
2022-12-27 15:37:40 +01:00
return Promise.all(this.ccRecipients().map((recipient) => recipient.resolved()))
}
bccRecipients(): Array<ResolvableRecipient> {
return this.getRecipientList(RecipientField.BCC)
2021-12-28 13:53:11 +01:00
}
bccRecipientsResolved(): Promise<Array<Recipient>> {
2022-12-27 15:37:40 +01:00
return Promise.all(this.bccRecipients().map((recipient) => recipient.resolved()))
}
2021-12-28 13:53:11 +01:00
replyTosResolved(): Promise<Array<Recipient>> {
2022-12-27 15:37:40 +01:00
return Promise.all(this.replyTos.map((r) => r.resolved()))
2021-12-28 13:53:11 +01:00
}
/**
* add a recipient to the recipient list without updating the saved state of the draft.
* if the recipient is already inserted, it will wait for it to resolve before returning.
*
* @returns whether the list was actually changed.
*/
private async insertRecipient(
fieldType: RecipientField,
{ address, name, type, contact }: PartialRecipient,
2022-12-27 15:37:40 +01:00
resolveMode: ResolveMode = ResolveMode.Eager,
): Promise<boolean> {
2022-12-27 15:37:40 +01:00
let recipient = this.getRecipientList(fieldType).find((recipient) => recipient.address === address)
// Only add a recipient if it doesn't exist
if (!recipient) {
2022-12-27 15:37:40 +01:00
recipient = this.recipientsModel.resolve(
{
address,
name,
type,
contact,
},
2022-12-27 15:37:40 +01:00
resolveMode,
)
this.getRecipientList(fieldType).push(recipient)
2021-12-28 13:53:11 +01:00
recipient.resolved().then(({ address, contact }) => {
if (!this.passwords.has(address) && contact != null) {
this.setPassword(address, contact.presharedPassword ?? "")
} else {
// always notify listeners after we finished resolving the recipient, even if email itself didn't change
this.onMailChanged(null)
}
2021-12-28 13:53:11 +01:00
})
await recipient.resolved()
return true
2021-12-28 13:53:11 +01:00
}
await recipient.resolved()
return false
}
/**
* Add a new recipient, this method resolves when the recipient resolves.
* will notify of a changed draft state after the recipient was inserted
*/
async addRecipient(fieldType: RecipientField, partialRecipient: PartialRecipient, resolveMode: ResolveMode = ResolveMode.Eager): Promise<void> {
const wasAdded = await this.insertRecipient(fieldType, partialRecipient, resolveMode)
this.markAsChangedIfNecessary(wasAdded)
}
2021-12-28 13:53:11 +01:00
getRecipient(type: RecipientField, address: string): ResolvableRecipient | null {
2022-12-27 15:37:40 +01:00
return this.getRecipientList(type).find((recipient) => recipient.address === address) ?? null
2021-12-28 13:53:11 +01:00
}
removeRecipientByAddress(address: string, type: RecipientField, notify: boolean = true) {
2022-12-27 15:37:40 +01:00
const recipient = this.getRecipientList(type).find((recipient) => recipient.address === address)
if (recipient) {
this.removeRecipient(recipient, type, notify)
}
}
/**
* remove recipient from the recipient list
* @return true if the recipient was removed
*/
removeRecipient(recipient: Recipient, type: RecipientField, notify: boolean = true): boolean {
const recipients = this.recipients.get(type) ?? []
2022-12-27 15:37:40 +01:00
const didRemove = findAndRemove(recipients, (r) => r.address === recipient.address)
this.markAsChangedIfNecessary(didRemove)
2021-12-28 13:53:11 +01:00
if (didRemove && notify) {
this.onRecipientDeleted({
field: type,
recipient,
})
}
return didRemove
}
dispose() {
2022-03-17 16:18:17 +01:00
this.eventController.removeEntityListener(this.entityEventReceived)
2021-12-28 13:53:11 +01:00
revokeInlineImages(this.loadedInlineImages)
}
/**
* @throws UserError in the case that any files were too big to attach. Small enough files will still have been attached
*/
getAttachments(): Array<Attachment> {
2022-03-17 16:18:17 +01:00
return this.attachments
2021-12-28 13:53:11 +01:00
}
/** @throws UserError in case files are too big to add */
attachFiles(files: ReadonlyArray<Attachment>): void {
2022-03-17 16:18:17 +01:00
let sizeLeft = MAX_ATTACHMENT_SIZE - this.attachments.reduce((total, file) => total + Number(file.size), 0)
2021-12-28 13:53:11 +01:00
const sizeCheckResult = checkAttachmentSize(files, sizeLeft)
2022-03-17 16:18:17 +01:00
this.attachments.push(...sizeCheckResult.attachableFiles)
this.markAsChangedIfNecessary(sizeCheckResult.attachableFiles.length > 0)
2021-12-28 13:53:11 +01:00
if (sizeCheckResult.tooBigFiles.length > 0) {
throw new UserError(() => lang.get("tooBigAttachment_msg") + "\n" + sizeCheckResult.tooBigFiles.join("\n"))
}
}
removeAttachment(file: Attachment): void {
this.markAsChangedIfNecessary(remove(this.attachments, file))
2021-12-28 13:53:11 +01:00
}
getSenderName(): string {
return getSenderName(this.mailboxProperties, this.senderAddress) ?? ""
2021-12-28 13:53:11 +01:00
}
getDraft(): Readonly<Mail> | null {
return this.draft
2021-12-28 13:53:11 +01:00
}
private async updateDraft(body: string, attachments: ReadonlyArray<Attachment> | null, draft: Mail): Promise<Mail> {
2022-03-17 16:18:17 +01:00
return this.mailFacade
.updateDraft({
subject: this.getSubject(),
body: body,
senderMailAddress: this.senderAddress,
senderName: this.getSenderName(),
toRecipients: await this.toRecipientsResolved(),
ccRecipients: await this.ccRecipientsResolved(),
bccRecipients: await this.bccRecipientsResolved(),
attachments: attachments,
confidential: this.isConfidential(),
draft: draft,
})
.catch(
ofClass(LockedError, (e) => {
console.log("updateDraft: operation is still active", e)
throw new UserError("operationStillActive_msg")
}),
)
.catch(
ofClass(NotFoundError, (e) => {
console.log("draft has been deleted, creating new one")
return this.createDraft(body, attachments, downcast(draft.method))
}),
)
2022-12-27 15:37:40 +01:00
}
private async createDraft(body: string, attachments: ReadonlyArray<Attachment> | null, mailMethod: MailMethod): Promise<Mail> {
return this.mailFacade.createDraft({
subject: this.getSubject(),
bodyText: body,
senderMailAddress: this.senderAddress,
senderName: this.getSenderName(),
toRecipients: await this.toRecipientsResolved(),
ccRecipients: await this.ccRecipientsResolved(),
bccRecipients: await this.bccRecipientsResolved(),
conversationType: this.conversationType,
previousMessageId: this.previousMessageId,
attachments: attachments,
confidential: this.isConfidential(),
replyTos: await this.replyTosResolved(),
method: mailMethod,
})
2021-12-28 13:53:11 +01:00
}
isConfidential(): boolean {
2022-03-17 16:18:17 +01:00
return this.confidential || !this.containsExternalRecipients()
2021-12-28 13:53:11 +01:00
}
isConfidentialExternal(): boolean {
2022-03-17 16:18:17 +01:00
return this.confidential && this.containsExternalRecipients()
2021-12-28 13:53:11 +01:00
}
setConfidential(confidential: boolean): void {
this.markAsChangedIfNecessary(this.confidential !== confidential)
2022-03-17 16:18:17 +01:00
this.confidential = confidential
2021-12-28 13:53:11 +01:00
}
containsExternalRecipients(): boolean {
2022-12-27 15:37:40 +01:00
return this.allRecipients().some((r) => r.type === RecipientType.EXTERNAL)
2021-12-28 13:53:11 +01:00
}
getExternalRecipients(): Array<Recipient> {
2022-12-27 15:37:40 +01:00
return this.allRecipients().filter((r) => r.type === RecipientType.EXTERNAL)
2021-12-28 13:53:11 +01:00
}
/**
* @reject {RecipientsNotFoundError}
* @reject {TooManyRequestsError}
* @reject {AccessBlockedError}
* @reject {FileNotFoundError}
* @reject {PreconditionFailedError}
* @reject {LockedError}
* @reject {UserError}
* @param mailMethod
* @param getConfirmation: A callback to get user confirmation
* @param waitHandler: A callback to allow UI blocking while the mail is being sent. it seems like wrapping the send call in showProgressDialog causes the confirmation dialogs not to be shown. We should fix this, but this works for now
2021-12-28 13:53:11 +01:00
* @param tooManyRequestsError
* @return true if the send was completed, false if it was aborted (by getConfirmation returning false
*/
async send(
mailMethod: MailMethod,
2022-12-27 15:37:40 +01:00
getConfirmation: (arg0: TranslationText) => Promise<boolean> = (_) => Promise.resolve(true),
waitHandler: (arg0: TranslationText, arg1: Promise<any>) => Promise<any> = (_, p) => p,
tooManyRequestsError: TranslationKey = "tooManyMails_msg",
2021-12-28 13:53:11 +01:00
): Promise<boolean> {
await this.recipientsResolved.getAsync()
2021-12-28 13:53:11 +01:00
this.onBeforeSend()
if (this.allRecipients().length === 1 && this.allRecipients()[0].address.toLowerCase().trim() === "approval@tutao.de") {
2022-03-17 16:18:17 +01:00
await this.sendApprovalMail(this.getBody())
2021-12-28 13:53:11 +01:00
return true
}
if (this.toRecipients().length === 0 && this.ccRecipients().length === 0 && this.bccRecipients().length === 0) {
throw new UserError("noRecipients_msg")
}
const numVisibleRecipients = this.toRecipients().length + this.ccRecipients().length
// Many recipients is a warning
if (numVisibleRecipients >= TOO_MANY_VISIBLE_RECIPIENTS && !(await getConfirmation("manyRecipients_msg"))) {
return false
}
// Empty subject is a warning
if (this.getSubject().length === 0 && !(await getConfirmation("noSubject_msg"))) {
return false
}
// The next check depends on contacts being available
// So we need to wait for our recipients here
const recipients = await this.waitForResolvedRecipients()
2021-12-28 13:53:11 +01:00
// No password in external confidential mail is an error
2022-12-27 15:37:40 +01:00
if (this.isConfidentialExternal() && this.getExternalRecipients().some((r) => !this.getPassword(r.address))) {
2021-12-28 13:53:11 +01:00
throw new UserError("noPreSharedPassword_msg")
}
// Weak password is a warning
if (this.isConfidentialExternal() && this.hasInsecurePasswords() && !(await getConfirmation("presharedPasswordNotStrongEnough_msg"))) {
return false
}
const doSend = async () => {
await this.saveDraft(true, mailMethod)
await this.updateContacts(recipients)
await this.mailFacade.sendDraft(neverNull(this.draft), recipients, this.selectedNotificationLanguage)
2022-03-17 16:18:17 +01:00
await this.updatePreviousMail()
await this.updateExternalLanguage()
2021-12-28 13:53:11 +01:00
return true
}
return waitHandler(this.isConfidential() ? "sending_msg" : "sendingUnencrypted_msg", doSend())
.catch(
ofClass(LockedError, () => {
2022-03-17 16:18:17 +01:00
throw new UserError("operationStillActive_msg")
}),
) // catch all of the badness
.catch(
ofClass(RecipientNotResolvedError, () => {
2022-03-17 16:18:17 +01:00
throw new UserError("tooManyAttempts_msg")
}),
)
.catch(
2022-12-27 15:37:40 +01:00
ofClass(RecipientsNotFoundError, (e) => {
2022-03-17 16:18:17 +01:00
if (mailMethod === MailMethod.ICAL_CANCEL) {
// in case of calendar event termination we will remove invalid recipients and then delete the event without sending updates
2022-03-17 16:18:17 +01:00
throw e
} else {
let invalidRecipients = e.message
throw new UserError(
() => lang.get("tutanotaAddressDoesNotExist_msg") + " " + lang.get("invalidRecipients_msg") + "\n" + invalidRecipients,
)
}
}),
)
.catch(
ofClass(TooManyRequestsError, () => {
2022-03-17 16:18:17 +01:00
throw new UserError(tooManyRequestsError)
}),
)
.catch(
2022-12-27 15:37:40 +01:00
ofClass(AccessBlockedError, (e) => {
2022-03-17 16:18:17 +01:00
// special case: the approval status is set to SpamSender, but the update has not been received yet, so use SpamSender as default
return checkApprovalStatus(this.logins, true, ApprovalStatus.SPAM_SENDER).then(() => {
console.log("could not send mail (blocked access)", e)
return false
})
}),
)
.catch(
ofClass(FileNotFoundError, () => {
2022-03-17 16:18:17 +01:00
throw new UserError("couldNotAttachFile_msg")
}),
)
.catch(
ofClass(PreconditionFailedError, () => {
2022-03-17 16:18:17 +01:00
throw new UserError("operationStillActive_msg")
}),
)
2021-12-28 13:53:11 +01:00
}
/**
* Whether any of the external recipients have an insecure password.
* We don't consider empty passwords, because an empty password will disallow and encrypted email from sending, whereas an insecure password
* can still be used
* @returns {boolean}
*/
hasInsecurePasswords(): boolean {
const minimalPasswordStrength = this.allRecipients()
.filter((r) => this.getPassword(r.address) !== "")
.reduce((min, recipient) => Math.min(min, this.getPasswordStrength(recipient)), PASSWORD_MIN_SECURE_VALUE)
2021-12-28 13:53:11 +01:00
return !isSecurePassword(minimalPasswordStrength)
}
2022-12-27 15:37:40 +01:00
saveDraft(saveAttachments: boolean, mailMethod: MailMethod): Promise<void> {
if (this.currentSavePromise == null) {
this.currentSavePromise = Promise.resolve().then(async () => {
try {
await this.doSaveDraft(saveAttachments, mailMethod)
} finally {
// If there is an error, we still need to reset currentSavePromise
this.currentSavePromise = null
}
if (this.hasMailChanged() && this.doSaveAgain) {
this.doSaveAgain = false
await this.saveDraft(saveAttachments, mailMethod)
}
})
} else {
this.doSaveAgain = true
}
return this.currentSavePromise
}
2021-12-28 13:53:11 +01:00
/**
* Saves the draft.
* @param saveAttachments True if also the attachments shall be saved, false otherwise.
* @param mailMethod
2021-12-28 13:53:11 +01:00
* @returns {Promise} When finished.
* @throws FileNotFoundError when one of the attachments could not be opened
* @throws PreconditionFailedError when the draft is locked
*/
2022-12-27 15:37:40 +01:00
private async doSaveDraft(saveAttachments: boolean, mailMethod: MailMethod): Promise<void> {
try {
2022-03-17 16:18:17 +01:00
const attachments = saveAttachments ? this.attachments : null
// We also want to create new drafts for drafts edited from trash or spam folder
2022-12-27 15:37:40 +01:00
this.draft =
this.draft == null || (await this.isMailInTrashOrSpam(this.draft))
? await this.createDraft(this.getBody(), attachments, mailMethod)
: await this.updateDraft(this.getBody(), attachments, this.draft)
const attachmentIds = await this.mailFacade.getAttachmentIds(this.draft)
const newAttachments = await promiseMap(attachmentIds, (fileId) => this.entity.load<TutanotaFile>(FileTypeRef, fileId), {
2022-12-27 15:37:40 +01:00
concurrency: 5,
})
2022-03-17 16:18:17 +01:00
this.attachments = [] // attachFiles will push to existing files but we want to overwrite them
this.attachFiles(newAttachments)
// Allow any changes that might occur while the mail is being saved to be accounted for
// if saved is called before this has completed
this.mailSavedAt = this.dateProvider.now()
} catch (e) {
if (e instanceof PayloadTooLargeError) {
throw new UserError("requestTooLarge_msg")
} else if (e instanceof MailBodyTooLargeError) {
throw new UserError("mailBodyTooLarge_msg")
} else if (e instanceof FileNotFoundError) {
throw new UserError("couldNotAttachFile_msg")
} else if (e instanceof PreconditionFailedError) {
throw new UserError("operationStillActive_msg")
} else {
throw e
}
}
}
private async isMailInTrashOrSpam(draft: Mail): Promise<boolean> {
2022-03-17 16:18:17 +01:00
const folders = await this.mailModel.getMailboxFolders(draft)
const mailFolder = folders?.getFolderByMailListId(getListId(draft))
return !!mailFolder && (mailFolder.folderType === MailFolderType.TRASH || mailFolder.folderType === MailFolderType.SPAM)
2021-12-28 13:53:11 +01:00
}
2022-03-17 16:18:17 +01:00
private sendApprovalMail(body: string): Promise<unknown> {
2021-12-28 13:53:11 +01:00
const listId = "---------c--"
const m = createApprovalMail({
2022-03-17 16:18:17 +01:00
_id: [listId, stringToCustomId(this.senderAddress)],
2021-12-28 13:53:11 +01:00
_ownerGroup: this.user().user.userGroup.group,
text: `Subject: ${this.getSubject()}<br>${body}`,
})
2022-12-27 15:37:40 +01:00
return this.entity.setup(listId, m).catch(ofClass(NotAuthorizedError, (e) => console.log("not authorized for approval message")))
2021-12-28 13:53:11 +01:00
}
getAvailableNotificationTemplateLanguages(): Array<Language> {
2022-03-17 16:18:17 +01:00
return this.availableNotificationTemplateLanguages
2021-12-28 13:53:11 +01:00
}
getSelectedNotificationLanguageCode(): string {
2022-03-17 16:18:17 +01:00
return this.selectedNotificationLanguage
2021-12-28 13:53:11 +01:00
}
setSelectedNotificationLanguageCode(code: string) {
this.markAsChangedIfNecessary(this.selectedNotificationLanguage !== code)
2022-03-17 16:18:17 +01:00
this.selectedNotificationLanguage = code
this.markAsChangedIfNecessary(true)
2021-12-28 13:53:11 +01:00
}
2022-03-17 16:18:17 +01:00
private updateExternalLanguage() {
2021-12-28 13:53:11 +01:00
let props = this.user().props
2022-03-17 16:18:17 +01:00
if (props.notificationMailLanguage !== this.selectedNotificationLanguage) {
props.notificationMailLanguage = this.selectedNotificationLanguage
2021-12-28 13:53:11 +01:00
2022-03-17 16:18:17 +01:00
this.entity.update(props)
2021-12-28 13:53:11 +01:00
}
}
2022-03-17 16:18:17 +01:00
private updatePreviousMail(): Promise<void> {
if (this.previousMail) {
if (this.previousMail.replyType === ReplyType.NONE && this.conversationType === ConversationType.REPLY) {
this.previousMail.replyType = ReplyType.REPLY
} else if (this.previousMail.replyType === ReplyType.NONE && this.conversationType === ConversationType.FORWARD) {
this.previousMail.replyType = ReplyType.FORWARD
} else if (this.previousMail.replyType === ReplyType.FORWARD && this.conversationType === ConversationType.REPLY) {
this.previousMail.replyType = ReplyType.REPLY_FORWARD
} else if (this.previousMail.replyType === ReplyType.REPLY && this.conversationType === ConversationType.FORWARD) {
this.previousMail.replyType = ReplyType.REPLY_FORWARD
2021-12-28 13:53:11 +01:00
} else {
return Promise.resolve()
}
2022-03-17 16:18:17 +01:00
return this.entity.update(this.previousMail).catch(ofClass(NotFoundError, noOp))
2021-12-28 13:53:11 +01:00
} else {
return Promise.resolve()
}
}
/**
* If contacts have had their passwords changed, we update them before sending
*/
private async updateContacts(resolvedRecipients: Recipient[]): Promise<any> {
for (const { address, contact, type } of resolvedRecipients) {
if (contact == null) {
continue
}
const isExternalAndConfidential = type === RecipientType.EXTERNAL && this.isConfidential()
if (!contact._id && (!this.user().props.noAutomaticContacts || isExternalAndConfidential)) {
if (isExternalAndConfidential) {
contact.presharedPassword = this.getPassword(address).trim()
}
const listId = await this.contactModel.contactListId()
await this.entity.setup(listId, contact)
} else if (contact._id && isExternalAndConfidential && contact.presharedPassword !== this.getPassword(address).trim()) {
contact.presharedPassword = this.getPassword(address).trim()
await this.entity.update(contact)
}
}
2021-12-28 13:53:11 +01:00
}
allRecipients(): Array<ResolvableRecipient> {
2021-12-28 13:53:11 +01:00
return this.toRecipients().concat(this.ccRecipients()).concat(this.bccRecipients())
}
/**
* Makes sure the recipient type and contact are resolved.
*/
async waitForResolvedRecipients(): Promise<Recipient[]> {
await this.recipientsResolved.getAsync()
2022-12-27 15:37:40 +01:00
return Promise.all(this.allRecipients().map((recipient) => recipient.resolved())).catch(
ofClass(TooManyRequestsError, () => {
throw new RecipientNotResolvedError("")
}),
)
2021-12-28 13:53:11 +01:00
}
async handleEntityEvent(update: EntityUpdateData): Promise<void> {
const { operation, instanceId, instanceListId } = update
2021-12-28 13:53:11 +01:00
let contactId: IdTuple = [neverNull(instanceListId), instanceId]
let changed = false
2021-12-28 13:53:11 +01:00
if (isUpdateForTypeRef(ContactTypeRef, update)) {
await this.recipientsResolved.getAsync()
2021-12-28 13:53:11 +01:00
if (operation === OperationType.UPDATE) {
2022-12-27 15:37:40 +01:00
this.entity.load(ContactTypeRef, contactId).then((contact) => {
for (const fieldType of typedValues(RecipientField)) {
2022-12-27 15:37:40 +01:00
const matching = this.getRecipientList(fieldType).filter(
(recipient) => recipient.contact && isSameId(recipient.contact._id, contact._id),
)
matching.forEach((recipient) => {
2021-12-28 13:53:11 +01:00
// if the mail address no longer exists on the contact then delete the recipient
2022-12-27 15:37:40 +01:00
if (!contact.mailAddresses.find((ma) => cleanMatch(ma.address, recipient.address))) {
changed = changed || this.removeRecipient(recipient, fieldType, true)
2021-12-28 13:53:11 +01:00
} else {
// else just modify the recipient
recipient.setName(getContactDisplayName(contact))
recipient.setContact(contact)
changed = true
2021-12-28 13:53:11 +01:00
}
})
}
})
} else if (operation === OperationType.DELETE) {
for (const fieldType of typedValues(RecipientField)) {
2021-12-28 13:53:11 +01:00
const recipients = this.getRecipientList(fieldType)
2022-12-27 15:37:40 +01:00
const toDelete = recipients.filter((recipient) => (recipient.contact && isSameId(recipient.contact._id, contactId)) || false)
2021-12-28 13:53:11 +01:00
for (const r of toDelete) {
changed = changed || this.removeRecipient(r, fieldType, true)
2021-12-28 13:53:11 +01:00
}
}
}
this.markAsChangedIfNecessary(true)
2021-12-28 13:53:11 +01:00
} else if (isUpdateForTypeRef(CustomerPropertiesTypeRef, update)) {
this.updateAvailableNotificationTemplateLanguages()
} else if (isUpdateForTypeRef(MailboxPropertiesTypeRef, update) && operation === OperationType.UPDATE) {
this.mailboxProperties = await this.entity.load(MailboxPropertiesTypeRef, update.instanceId)
2021-12-28 13:53:11 +01:00
}
this.markAsChangedIfNecessary(changed)
2021-12-28 13:53:11 +01:00
return Promise.resolve()
}
setOnBeforeSendFunction(fun: () => unknown) {
this.onBeforeSend = fun
}
}