add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
import type { InboxRule, Mail, MailDetails, MoveMailData } from "../../api/entities/tutanota/TypeRefs.js"
|
|
|
|
|
import { createMoveMailData, MailDetailsBlobTypeRef, MailHeadersTypeRef } from "../../api/entities/tutanota/TypeRefs.js"
|
2022-12-27 15:37:40 +01:00
|
|
|
import { InboxRuleType, MailFolderType, MAX_NBR_MOVE_DELETE_MAIL_SERVICE } from "../../api/common/TutanotaConstants"
|
|
|
|
|
import { isDomainName, isRegularExpression } from "../../misc/FormatValidator"
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
import { getLegacyMailHeaders, getMailHeaders } from "../../api/common/utils/Utils"
|
|
|
|
|
import { assertNotNull, asyncFind, debounce, neverNull, ofClass, promiseMap, splitInChunks } from "@tutao/tutanota-utils"
|
2022-12-27 15:37:40 +01:00
|
|
|
import { lang } from "../../misc/LanguageViewModel"
|
|
|
|
|
import type { MailboxDetail } from "./MailModel"
|
|
|
|
|
import { LockedError, NotFoundError, PreconditionFailedError } from "../../api/common/error/RestError"
|
|
|
|
|
import type { SelectorItemList } from "../../gui/base/DropDownSelector.js"
|
|
|
|
|
import { EntityClient } from "../../api/common/EntityClient"
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
import { elementIdPart, getElementId, getListId, isSameId, listIdPart } from "../../api/common/utils/EntityUtils"
|
2022-12-27 15:37:40 +01:00
|
|
|
import { assertMainOrNode } from "../../api/common/Env"
|
2023-02-10 16:27:53 +01:00
|
|
|
import { MailFacade } from "../../api/worker/facades/lazy/MailFacade.js"
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
import { isLegacyMail } from "../../api/common/MailWrapper.js"
|
2023-01-23 15:12:59 +01:00
|
|
|
import { assertSystemFolderOfType } from "../../api/common/mail/CommonMailUtils.js"
|
2023-03-21 15:32:40 +01:00
|
|
|
import { LoginController } from "../../api/main/LoginController.js"
|
2022-01-07 15:58:30 +01:00
|
|
|
|
2017-08-15 13:54:22 +02:00
|
|
|
assertMainOrNode()
|
2020-09-28 14:50:34 +02:00
|
|
|
const moveMailDataPerFolder: MoveMailData[] = []
|
|
|
|
|
const DEBOUNCE_FIRST_MOVE_MAIL_REQUEST_MS = 200
|
|
|
|
|
let applyingRules = false // used to avoid concurrent application of rules (-> requests to locked service)
|
|
|
|
|
|
2022-03-09 17:43:29 +01:00
|
|
|
async function sendMoveMailRequest(mailFacade: MailFacade): Promise<void> {
|
2022-01-07 15:58:30 +01:00
|
|
|
if (moveMailDataPerFolder.length) {
|
|
|
|
|
const moveToTargetFolder = assertNotNull(moveMailDataPerFolder.shift())
|
|
|
|
|
const mailChunks = splitInChunks(MAX_NBR_MOVE_DELETE_MAIL_SERVICE, moveToTargetFolder.mails)
|
2022-12-27 15:37:40 +01:00
|
|
|
await promiseMap(mailChunks, (mailChunk) => {
|
2022-01-07 15:58:30 +01:00
|
|
|
moveToTargetFolder.mails = mailChunk
|
2022-03-09 17:43:29 +01:00
|
|
|
return mailFacade.moveMails(mailChunk, moveToTargetFolder.targetFolder)
|
2022-01-07 15:58:30 +01:00
|
|
|
})
|
|
|
|
|
.catch(
|
2022-12-27 15:37:40 +01:00
|
|
|
ofClass(LockedError, (e) => {
|
2022-01-07 15:58:30 +01:00
|
|
|
//LockedError should no longer be thrown!?!
|
|
|
|
|
console.log("moving mail failed", e, moveToTargetFolder)
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.catch(
|
2022-12-27 15:37:40 +01:00
|
|
|
ofClass(PreconditionFailedError, (e) => {
|
2022-01-07 15:58:30 +01:00
|
|
|
// move mail operation may have been locked by other process
|
|
|
|
|
console.log("moving mail failed", e, moveToTargetFolder)
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.finally(() => {
|
2022-03-09 17:43:29 +01:00
|
|
|
return sendMoveMailRequest(mailFacade)
|
2022-01-07 15:58:30 +01:00
|
|
|
})
|
|
|
|
|
} //We are done and unlock for future requests
|
2020-09-28 14:50:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We throttle the moveMail requests to a rate of 50ms
|
|
|
|
|
// Each target folder requires one request
|
2022-03-09 17:43:29 +01:00
|
|
|
const applyMatchingRules = debounce(DEBOUNCE_FIRST_MOVE_MAIL_REQUEST_MS, (mailFacade: MailFacade) => {
|
2022-01-07 15:58:30 +01:00
|
|
|
if (applyingRules) return
|
|
|
|
|
// We lock to avoid concurrent requests
|
|
|
|
|
applyingRules = true
|
2022-03-09 17:43:29 +01:00
|
|
|
sendMoveMailRequest(mailFacade).finally(() => {
|
2022-01-07 15:58:30 +01:00
|
|
|
applyingRules = false
|
|
|
|
|
})
|
2020-09-28 14:50:34 +02:00
|
|
|
})
|
2022-01-07 15:58:30 +01:00
|
|
|
|
2019-08-22 18:24:32 +02:00
|
|
|
export function getInboxRuleTypeNameMapping(): SelectorItemList<string> {
|
2022-01-07 15:58:30 +01:00
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
value: InboxRuleType.FROM_EQUALS,
|
|
|
|
|
name: lang.get("inboxRuleSenderEquals_action"),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: InboxRuleType.RECIPIENT_TO_EQUALS,
|
|
|
|
|
name: lang.get("inboxRuleToRecipientEquals_action"),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: InboxRuleType.RECIPIENT_CC_EQUALS,
|
|
|
|
|
name: lang.get("inboxRuleCCRecipientEquals_action"),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: InboxRuleType.RECIPIENT_BCC_EQUALS,
|
|
|
|
|
name: lang.get("inboxRuleBCCRecipientEquals_action"),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: InboxRuleType.SUBJECT_CONTAINS,
|
|
|
|
|
name: lang.get("inboxRuleSubjectContains_action"),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: InboxRuleType.MAIL_HEADER_CONTAINS,
|
|
|
|
|
name: lang.get("inboxRuleMailHeaderContains_action"),
|
|
|
|
|
},
|
|
|
|
|
]
|
2017-08-15 13:54:22 +02:00
|
|
|
}
|
2022-01-07 15:58:30 +01:00
|
|
|
|
2017-08-15 13:54:22 +02:00
|
|
|
export function getInboxRuleTypeName(type: string): string {
|
2022-12-27 15:37:40 +01:00
|
|
|
let typeNameMapping = getInboxRuleTypeNameMapping().find((t) => t.value === type)
|
2022-01-07 15:58:30 +01:00
|
|
|
return typeNameMapping != null ? typeNameMapping.name : ""
|
2017-08-15 13:54:22 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-17 16:49:56 +02:00
|
|
|
export class InboxRuleHandler {
|
|
|
|
|
constructor(private readonly mailFacade: MailFacade, private readonly entityClient: EntityClient, private readonly logins: LoginController) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks the mail for an existing inbox rule and moves the mail to the target folder of the rule.
|
|
|
|
|
* @returns true if a rule matches otherwise false
|
|
|
|
|
*/
|
|
|
|
|
async findAndApplyMatchingRule(mailboxDetail: MailboxDetail, mail: Mail, applyRulesOnServer: boolean): Promise<IdTuple | null> {
|
|
|
|
|
if (mail._errors || !mail.unread || !isInboxList(mailboxDetail, getListId(mail)) || !this.logins.getUserController().isPremiumAccount()) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
2022-01-07 15:58:30 +01:00
|
|
|
|
2023-05-17 16:49:56 +02:00
|
|
|
const inboxRule = await _findMatchingRule(this.entityClient, mail, this.logins.getUserController().props.inboxRules)
|
2022-01-07 15:58:30 +01:00
|
|
|
if (inboxRule) {
|
2022-12-16 17:18:37 +01:00
|
|
|
let targetFolder = mailboxDetail.folders.getFolderById(inboxRule.targetFolder)
|
2022-01-07 15:58:30 +01:00
|
|
|
|
2022-12-16 17:18:37 +01:00
|
|
|
if (targetFolder && targetFolder.folderType !== MailFolderType.INBOX) {
|
2022-01-07 15:58:30 +01:00
|
|
|
if (applyRulesOnServer) {
|
2022-12-27 15:37:40 +01:00
|
|
|
let moveMailData = moveMailDataPerFolder.find((folderMoveMailData) => isSameId(folderMoveMailData.targetFolder, inboxRule.targetFolder))
|
2022-01-07 15:58:30 +01:00
|
|
|
|
|
|
|
|
if (moveMailData) {
|
|
|
|
|
moveMailData.mails.push(mail._id)
|
|
|
|
|
} else {
|
|
|
|
|
moveMailData = createMoveMailData()
|
|
|
|
|
moveMailData.targetFolder = inboxRule.targetFolder
|
|
|
|
|
moveMailData.mails.push(mail._id)
|
|
|
|
|
moveMailDataPerFolder.push(moveMailData)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 16:49:56 +02:00
|
|
|
applyMatchingRules(this.mailFacade)
|
2022-01-07 15:58:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [targetFolder.mails, getElementId(mail)]
|
|
|
|
|
} else {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return null
|
|
|
|
|
}
|
2023-05-17 16:49:56 +02:00
|
|
|
}
|
2017-08-15 13:54:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Finds the first matching inbox rule for the mail and returns it.
|
|
|
|
|
* export only for testing
|
|
|
|
|
*/
|
2021-12-23 14:03:23 +01:00
|
|
|
export async function _findMatchingRule(entityClient: EntityClient, mail: Mail, rules: InboxRule[]): Promise<InboxRule | null> {
|
2022-12-27 15:37:40 +01:00
|
|
|
return asyncFind(rules, (rule) => checkInboxRule(entityClient, mail, rule)).then((v) => v ?? null)
|
2021-06-23 17:39:31 +02:00
|
|
|
}
|
2017-08-15 13:54:22 +02:00
|
|
|
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
async function getMailDetails(entityClient: EntityClient, mail: Mail): Promise<MailDetails | null> {
|
|
|
|
|
if (!isLegacyMail(mail)) {
|
|
|
|
|
try {
|
|
|
|
|
let mailDetailsBlobId = neverNull(mail.mailDetails)
|
2023-08-08 14:52:14 +02:00
|
|
|
|
|
|
|
|
const providedOwnerEncSessionKeys = new Map<Id, Uint8Array>()
|
|
|
|
|
providedOwnerEncSessionKeys.set(elementIdPart(mailDetailsBlobId), assertNotNull(mail._ownerEncSessionKey))
|
|
|
|
|
let mailDetailsBlobs = await entityClient.loadMultiple(
|
|
|
|
|
MailDetailsBlobTypeRef,
|
|
|
|
|
listIdPart(mailDetailsBlobId),
|
|
|
|
|
[elementIdPart(mailDetailsBlobId)],
|
|
|
|
|
providedOwnerEncSessionKeys,
|
|
|
|
|
)
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
return mailDetailsBlobs[0].details
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (!(e instanceof NotFoundError)) {
|
|
|
|
|
// Does the outer catch already handle this case?
|
|
|
|
|
console.error("Error processing inbox rule:", e.message)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Promise.resolve(null)
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 17:39:31 +02:00
|
|
|
async function checkInboxRule(entityClient: EntityClient, mail: Mail, inboxRule: InboxRule): Promise<boolean> {
|
2022-01-07 15:58:30 +01:00
|
|
|
const ruleType = inboxRule.type
|
|
|
|
|
try {
|
|
|
|
|
if (ruleType === InboxRuleType.FROM_EQUALS) {
|
|
|
|
|
let mailAddresses = [mail.sender.address]
|
|
|
|
|
|
|
|
|
|
if (mail.differentEnvelopeSender) {
|
|
|
|
|
mailAddresses.push(mail.differentEnvelopeSender)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _checkEmailAddresses(mailAddresses, inboxRule)
|
|
|
|
|
} else if (ruleType === InboxRuleType.RECIPIENT_TO_EQUALS) {
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
const details = await getMailDetails(entityClient, mail)
|
|
|
|
|
const toRecipients = details !== null ? details.recipients.toRecipients : mail.toRecipients
|
2022-01-07 15:58:30 +01:00
|
|
|
return _checkEmailAddresses(
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
toRecipients.map((m) => m.address),
|
2022-01-07 15:58:30 +01:00
|
|
|
inboxRule,
|
|
|
|
|
)
|
|
|
|
|
} else if (ruleType === InboxRuleType.RECIPIENT_CC_EQUALS) {
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
const details = await getMailDetails(entityClient, mail)
|
|
|
|
|
const ccRecipients = details !== null ? details.recipients.ccRecipients : mail.ccRecipients
|
2022-01-07 15:58:30 +01:00
|
|
|
return _checkEmailAddresses(
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
ccRecipients.map((m) => m.address),
|
2022-01-07 15:58:30 +01:00
|
|
|
inboxRule,
|
|
|
|
|
)
|
|
|
|
|
} else if (ruleType === InboxRuleType.RECIPIENT_BCC_EQUALS) {
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
const details = await getMailDetails(entityClient, mail)
|
|
|
|
|
const bccRecipients = details !== null ? details.recipients.ccRecipients : mail.bccRecipients
|
2022-01-07 15:58:30 +01:00
|
|
|
return _checkEmailAddresses(
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
bccRecipients.map((m) => m.address),
|
2022-01-07 15:58:30 +01:00
|
|
|
inboxRule,
|
|
|
|
|
)
|
|
|
|
|
} else if (ruleType === InboxRuleType.SUBJECT_CONTAINS) {
|
|
|
|
|
return _checkContainsRule(mail.subject, inboxRule)
|
|
|
|
|
} else if (ruleType === InboxRuleType.MAIL_HEADER_CONTAINS) {
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
if (isLegacyMail(mail) && mail.headers) {
|
2022-01-07 15:58:30 +01:00
|
|
|
return entityClient
|
|
|
|
|
.load(MailHeadersTypeRef, mail.headers)
|
2022-12-27 15:37:40 +01:00
|
|
|
.then((mailHeaders) => {
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
return _checkContainsRule(getLegacyMailHeaders(mailHeaders), inboxRule)
|
2022-01-07 15:58:30 +01:00
|
|
|
})
|
2022-12-27 15:37:40 +01:00
|
|
|
.catch((e) => {
|
2022-01-07 15:58:30 +01:00
|
|
|
if (!(e instanceof NotFoundError)) {
|
|
|
|
|
// Does the outer catch already handle this case?
|
|
|
|
|
console.error("Error processing inbox rule:", e.message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
})
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
} else if (!isLegacyMail(mail)) {
|
|
|
|
|
const details = await getMailDetails(entityClient, mail)
|
|
|
|
|
if (details?.headers != null) {
|
|
|
|
|
return _checkContainsRule(getMailHeaders(details.headers), inboxRule)
|
|
|
|
|
} else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2022-01-07 15:58:30 +01:00
|
|
|
} else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
console.warn("Unknown rule type: ", inboxRule.type)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error("Error processing inbox rule:", e.message)
|
|
|
|
|
return false
|
|
|
|
|
}
|
2017-08-15 13:54:22 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-23 17:39:31 +02:00
|
|
|
function _checkContainsRule(value: string, inboxRule: InboxRule): boolean {
|
2022-01-07 15:58:30 +01:00
|
|
|
return (isRegularExpression(inboxRule.value) && _matchesRegularExpression(value, inboxRule)) || value.includes(inboxRule.value)
|
2017-08-15 13:54:22 +02:00
|
|
|
}
|
2018-07-18 17:22:54 +02:00
|
|
|
|
2017-08-15 13:54:22 +02:00
|
|
|
/** export for test. */
|
|
|
|
|
export function _matchesRegularExpression(value: string, inboxRule: InboxRule): boolean {
|
2022-01-07 15:58:30 +01:00
|
|
|
if (isRegularExpression(inboxRule.value)) {
|
|
|
|
|
let flags = inboxRule.value.replace(/.*\/([gimsuy]*)$/, "$1")
|
|
|
|
|
let pattern = inboxRule.value.replace(new RegExp("^/(.*?)/" + flags + "$"), "$1")
|
|
|
|
|
let regExp = new RegExp(pattern, flags)
|
|
|
|
|
return regExp.test(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
2017-08-15 13:54:22 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-23 17:39:31 +02:00
|
|
|
function _checkEmailAddresses(mailAddresses: string[], inboxRule: InboxRule): boolean {
|
2022-12-27 15:37:40 +01:00
|
|
|
const mailAddress = mailAddresses.find((mailAddress) => {
|
2022-01-07 15:58:30 +01:00
|
|
|
let cleanMailAddress = mailAddress.toLowerCase().trim()
|
|
|
|
|
|
|
|
|
|
if (isRegularExpression(inboxRule.value)) {
|
|
|
|
|
return _matchesRegularExpression(cleanMailAddress, inboxRule)
|
|
|
|
|
} else if (isDomainName(inboxRule.value)) {
|
|
|
|
|
let domain = cleanMailAddress.split("@")[1]
|
|
|
|
|
return domain === inboxRule.value
|
|
|
|
|
} else {
|
|
|
|
|
return cleanMailAddress === inboxRule.value
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return mailAddress != null
|
2017-08-15 13:54:22 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-22 18:24:32 +02:00
|
|
|
export function isInboxList(mailboxDetail: MailboxDetail, listId: Id): boolean {
|
2023-01-23 15:12:59 +01:00
|
|
|
return isSameId(listId, assertSystemFolderOfType(mailboxDetail.folders, MailFolderType.INBOX).mails)
|
2022-12-27 15:37:40 +01:00
|
|
|
}
|