tutanota/src/mail/InboxRuleHandler.js

147 lines
5.5 KiB
JavaScript
Raw Normal View History

2017-08-15 13:54:22 +02:00
//@flow
import {createMoveMailData} from "../api/entities/tutanota/MoveMailData"
import {load, serviceRequestVoid} from "../api/main/Entity"
2017-08-15 13:54:22 +02:00
import {TutanotaService} from "../api/entities/tutanota/Services"
import {InboxRuleType} from "../api/common/TutanotaConstants"
2019-02-13 13:38:51 +01:00
import {isDomainName, isRegularExpression} from "../misc/FormatValidator"
import {HttpMethod, isSameId} from "../api/common/EntityFunctions"
import {neverNull, noOp} from "../api/common/utils/Utils"
2017-08-15 13:54:22 +02:00
import {assertMainOrNode} from "../api/Env"
import {lang} from "../misc/LanguageViewModel"
import {MailHeadersTypeRef} from "../api/entities/tutanota/MailHeaders"
import {logins} from "../api/main/LoginController"
2017-11-24 15:14:56 +01:00
import {getInboxFolder} from "./MailUtils"
import type {MailboxDetail} from "./MailModel"
import {NotFoundError, PreconditionFailedError} from "../api/common/error/RestError"
2017-08-15 13:54:22 +02:00
assertMainOrNode()
2018-07-18 17:22:54 +02:00
export function getInboxRuleTypeNameMapping(): {value: string, name: string}[] {
2017-08-15 13:54:22 +02: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")}
]
}
export function getInboxRuleTypeName(type: string): string {
2018-07-26 14:25:29 +02:00
let typeNameMapping = getInboxRuleTypeNameMapping().find(t => t.value === type)
2017-12-21 12:12:05 +01:00
return typeNameMapping != null ? typeNameMapping.name : ""
2017-08-15 13:54:22 +02:00
}
/**
* 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
*/
2017-11-24 15:14:56 +01:00
export function findAndApplyMatchingRule(mailboxDetail: MailboxDetail, mail: Mail): Promise<boolean> {
2018-07-18 17:22:54 +02:00
if (mail._errors || !mail.unread || !isInboxList(mailboxDetail, mail._id[0])
|| !logins.getUserController().isPremiumAccount()) {
2017-08-15 13:54:22 +02:00
return Promise.resolve(false)
}
return _findMatchingRule(mail).then(inboxRule => {
if (inboxRule) {
2017-11-24 15:14:56 +01:00
let targetFolder = mailboxDetail.folders.find(folder => isSameId(folder._id, neverNull(inboxRule).targetFolder));
2017-08-15 13:54:22 +02:00
if (targetFolder) {
let moveMailData = createMoveMailData()
moveMailData.targetFolder = inboxRule.targetFolder
moveMailData.mails.push(mail._id)
// execute move mail in parallel
serviceRequestVoid(TutanotaService.MoveMailService, HttpMethod.POST, moveMailData).catch(PreconditionFailedError, e => {
// move mail operation may have been locked by other process
})
2017-08-15 13:54:22 +02:00
return true
} else {
return false
}
} else {
return false
}
})
}
/**
* Finds the first matching inbox rule for the mail and returns it.
* export only for testing
*/
export function _findMatchingRule(mail: Mail): Promise<?InboxRule> {
return Promise.reduce(logins.getUserController().props.inboxRules, (resultInboxRule, inboxRule) => {
if (resultInboxRule) {
//console.log("rule matches", resultInboxRule)
return resultInboxRule
}
//console.log("find matching rule", inboxRule.value)
let ruleType = inboxRule.type;
2018-07-18 17:22:54 +02:00
if (ruleType === InboxRuleType.FROM_EQUALS) {
2017-08-15 13:54:22 +02:00
return _checkEmailAddresses([mail.sender], inboxRule)
2018-07-18 17:22:54 +02:00
} else if (ruleType === InboxRuleType.RECIPIENT_TO_EQUALS) {
2017-08-15 13:54:22 +02:00
return _checkEmailAddresses(mail.toRecipients, inboxRule)
2018-07-18 17:22:54 +02:00
} else if (ruleType === InboxRuleType.RECIPIENT_CC_EQUALS) {
2017-08-15 13:54:22 +02:00
return _checkEmailAddresses(mail.ccRecipients, inboxRule)
2018-07-18 17:22:54 +02:00
} else if (ruleType === InboxRuleType.RECIPIENT_BCC_EQUALS) {
2017-08-15 13:54:22 +02:00
return _checkEmailAddresses(mail.bccRecipients, inboxRule)
2018-07-18 17:22:54 +02:00
} else if (ruleType === InboxRuleType.SUBJECT_CONTAINS) {
2017-08-15 13:54:22 +02:00
return _checkContainsRule(mail.subject, inboxRule)
2018-07-18 17:22:54 +02:00
} else if (ruleType === InboxRuleType.MAIL_HEADER_CONTAINS) {
2017-08-15 13:54:22 +02:00
if (mail.headers) {
return load(MailHeadersTypeRef, mail.headers)
.then(mailHeaders => {
return _checkContainsRule(mailHeaders.headers, inboxRule)
})
.catch(NotFoundError, noOp)
2017-08-15 13:54:22 +02:00
} else {
return null
}
} else {
return null
}
}, null)
}
function _checkContainsRule(value: string, inboxRule: InboxRule): ?InboxRule {
if (isRegularExpression(inboxRule.value) && _matchesRegularExpression(value, inboxRule)) {
return inboxRule
} else if (value.indexOf(inboxRule.value) >= 0) {
return inboxRule
} else {
return null
}
}
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 {
if (isRegularExpression(inboxRule.value)) {
let regExp = new RegExp(inboxRule.value.substring(1, inboxRule.value.length - 1));
return regExp.test(value)
}
return false
}
function _checkEmailAddresses(mailAddresses: MailAddress[], inboxRule: InboxRule): ?InboxRule {
let mailAddress = mailAddresses.find(mailAddress => {
let cleanMailAddress = mailAddress.address.toLowerCase().trim();
if (isRegularExpression(inboxRule.value)) {
return _matchesRegularExpression(cleanMailAddress, inboxRule)
} else if (isDomainName(inboxRule.value)) {
let domain = cleanMailAddress.split("@")[1];
2018-07-18 17:22:54 +02:00
return domain === inboxRule.value
2017-08-15 13:54:22 +02:00
} else {
2018-07-18 17:22:54 +02:00
return cleanMailAddress === inboxRule.value
2017-08-15 13:54:22 +02:00
}
})
if (mailAddress) {
return inboxRule
} else {
return null
}
}
2017-11-24 15:14:56 +01:00
export function isInboxList(mailboxDetail: MailboxDetail, listId: Id) {
return isSameId(listId, getInboxFolder(mailboxDetail.folders).mails)
2017-08-15 13:54:22 +02:00
}