snappymail/dev/Model/Filter.js

277 lines
6.1 KiB
JavaScript
Raw Normal View History

2016-07-07 05:03:30 +08:00
import ko from 'ko';
2021-07-22 03:34:17 +08:00
import { arrayLength, pString } from 'Common/Utils';
import { delegateRunOnDestroy } from 'Common/UtilsUser';
2019-07-05 03:19:24 +08:00
import { i18n } from 'Common/Translator';
import { getFolderFromCacheList } from 'Common/Cache';
2016-07-07 05:03:30 +08:00
import { AccountUserStore } from 'Stores/User/Account';
2019-07-05 03:19:24 +08:00
import { FilterConditionModel } from 'Model/FilterCondition';
import { AbstractModel } from 'Knoin/AbstractModel';
2016-07-07 05:03:30 +08:00
2021-01-25 05:58:06 +08:00
/**
* @enum {string}
*/
export const FilterAction = {
None: 'None',
MoveTo: 'MoveTo',
Discard: 'Discard',
Vacation: 'Vacation',
Reject: 'Reject',
Forward: 'Forward'
};
/**
* @enum {string}
*/
const FilterRulesType = {
All: 'All',
Any: 'Any'
};
2021-01-22 23:32:08 +08:00
export class FilterModel extends AbstractModel {
2016-07-16 05:29:42 +08:00
constructor() {
2020-10-19 01:19:45 +08:00
super();
2016-07-07 05:03:30 +08:00
this.id = '';
2020-10-25 18:46:58 +08:00
this.addObservables({
enabled: true,
askDelete: false,
2020-10-25 18:46:58 +08:00
canBeDeleted: true,
2016-07-07 05:03:30 +08:00
2020-10-25 18:46:58 +08:00
name: '',
nameError: false,
nameFocused: false,
2016-07-07 05:03:30 +08:00
2020-10-25 18:46:58 +08:00
conditionsType: FilterRulesType.Any,
2016-07-07 05:03:30 +08:00
2020-10-25 18:46:58 +08:00
// Actions
actionValue: '',
actionValueError: false,
2016-07-07 05:03:30 +08:00
2020-10-25 18:46:58 +08:00
actionValueSecond: '',
actionValueThird: '',
2016-07-07 05:03:30 +08:00
2020-10-25 18:46:58 +08:00
actionValueFourth: '',
actionValueFourthError: false,
2016-07-07 05:03:30 +08:00
2020-10-25 18:46:58 +08:00
actionMarkAsRead: false,
2016-07-07 05:03:30 +08:00
2020-10-25 18:46:58 +08:00
actionKeep: true,
actionNoStop: false,
2016-07-07 05:03:30 +08:00
2021-01-25 05:58:06 +08:00
actionType: FilterAction.MoveTo
2016-07-07 05:03:30 +08:00
});
this.conditions = ko.observableArray();
2020-10-25 18:46:58 +08:00
2021-11-30 17:19:43 +08:00
const fGetRealFolderName = (folderFullName) => {
const folder = getFolderFromCacheList(folderFullName);
return folder ? folder.fullName.replace('.' === folder.delimiter ? /\./ : /[\\/]+/, ' / ') : folderFullName;
2016-07-07 05:03:30 +08:00
};
2020-10-25 21:14:14 +08:00
this.addComputables({
nameSub: () => {
let result = '';
const actionValue = this.actionValue(), root = 'SETTINGS_FILTERS/SUBNAME_';
2020-10-25 21:14:14 +08:00
switch (this.actionType()) {
2021-01-25 05:58:06 +08:00
case FilterAction.MoveTo:
result = i18n(root + 'MOVE_TO', {
2020-10-25 21:14:14 +08:00
FOLDER: fGetRealFolderName(actionValue)
});
break;
2021-01-25 05:58:06 +08:00
case FilterAction.Forward:
result = i18n(root + 'FORWARD_TO', {
2020-10-25 21:14:14 +08:00
EMAIL: actionValue
});
break;
2021-01-25 05:58:06 +08:00
case FilterAction.Vacation:
result = i18n(root + 'VACATION_MESSAGE');
2020-10-25 21:14:14 +08:00
break;
2021-01-25 05:58:06 +08:00
case FilterAction.Reject:
result = i18n(root + 'REJECT');
2020-10-25 21:14:14 +08:00
break;
2021-01-25 05:58:06 +08:00
case FilterAction.Discard:
result = i18n(root + 'DISCARD');
2020-10-25 21:14:14 +08:00
break;
// no default
}
return result ? '(' + result + ')' : '';
},
actionTemplate: () => {
const result = 'SettingsFiltersAction';
2020-10-25 21:14:14 +08:00
switch (this.actionType()) {
2021-01-25 05:58:06 +08:00
case FilterAction.Forward:
return result + 'Forward';
2021-01-25 05:58:06 +08:00
case FilterAction.Vacation:
return result + 'Vacation';
2021-01-25 05:58:06 +08:00
case FilterAction.Reject:
return result + 'Reject';
2021-01-25 05:58:06 +08:00
case FilterAction.None:
return result + 'None';
2021-01-25 05:58:06 +08:00
case FilterAction.Discard:
return result + 'Discard';
2021-01-25 05:58:06 +08:00
case FilterAction.MoveTo:
2020-10-25 21:14:14 +08:00
default:
return result + 'MoveToFolder';
2020-10-25 21:14:14 +08:00
}
2016-07-07 05:03:30 +08:00
}
});
2020-10-25 18:46:58 +08:00
this.addSubscribables({
name: sValue => this.nameError(!sValue),
actionValue: sValue => this.actionValueError(!sValue),
actionType: () => {
this.actionValue('');
this.actionValueError(false);
this.actionValueSecond('');
this.actionValueThird('');
this.actionValueFourth('');
this.actionValueFourthError(false);
}
});
2016-07-07 05:03:30 +08:00
}
generateID() {
this.id = Jua.randomId();
2016-07-07 05:03:30 +08:00
}
verify() {
if (!this.name()) {
2020-10-25 18:46:58 +08:00
this.nameError(true);
2016-07-07 05:03:30 +08:00
return false;
}
if (this.conditions.length && this.conditions.find(cond => cond && !cond.verify())) {
2020-10-25 18:46:58 +08:00
return false;
2016-07-07 05:03:30 +08:00
}
if (!this.actionValue()) {
if ([
2021-01-25 05:58:06 +08:00
FilterAction.MoveTo,
FilterAction.Forward,
FilterAction.Reject,
FilterAction.Vacation
].includes(this.actionType())
2019-07-05 03:19:24 +08:00
) {
2020-10-25 18:46:58 +08:00
this.actionValueError(true);
2016-07-07 05:03:30 +08:00
return false;
}
}
2021-01-25 05:58:06 +08:00
if (FilterAction.Forward === this.actionType() && !this.actionValue().includes('@')) {
2020-10-25 18:46:58 +08:00
this.actionValueError(true);
2016-07-07 05:03:30 +08:00
return false;
}
2019-07-05 03:19:24 +08:00
if (
2021-01-25 05:58:06 +08:00
FilterAction.Vacation === this.actionType() &&
this.actionValueFourth() &&
!this.actionValueFourth().includes('@')
2019-07-05 03:19:24 +08:00
) {
2020-10-25 18:46:58 +08:00
this.actionValueFourthError(true);
2016-07-07 05:03:30 +08:00
return false;
}
2020-10-25 18:46:58 +08:00
this.nameError(false);
this.actionValueError(false);
2016-07-07 05:03:30 +08:00
return true;
}
toJson() {
return {
// '@Object': 'Object/Filter',
2016-07-07 05:03:30 +08:00
ID: this.id,
2021-03-13 06:54:47 +08:00
Enabled: this.enabled() ? 1 : 0,
2016-07-07 05:03:30 +08:00
Name: this.name(),
Conditions: this.conditions.map(item => item.toJson()),
ConditionsType: this.conditionsType(),
2016-07-07 05:03:30 +08:00
ActionType: this.actionType(),
2016-07-07 05:03:30 +08:00
ActionValue: this.actionValue(),
ActionValueSecond: this.actionValueSecond(),
ActionValueThird: this.actionValueThird(),
ActionValueFourth: this.actionValueFourth(),
2021-03-13 06:54:47 +08:00
Keep: this.actionKeep() ? 1 : 0,
Stop: this.actionNoStop() ? 0 : 1,
MarkAsRead: this.actionMarkAsRead() ? 1 : 0
2016-07-07 05:03:30 +08:00
};
}
addCondition() {
this.conditions.push(new FilterConditionModel());
}
removeCondition(oConditionToDelete) {
this.conditions.remove(oConditionToDelete);
delegateRunOnDestroy(oConditionToDelete);
}
setRecipients() {
this.actionValueFourth(AccountUserStore.getEmailAddresses().join(', '));
2016-07-07 05:03:30 +08:00
}
/**
* @static
* @param {FetchJsonFilter} json
* @returns {?FilterModel}
*/
static reviveFromJson(json) {
const filter = super.reviveFromJson(json);
if (filter) {
filter.id = pString(json.ID);
filter.conditions([]);
2016-07-07 05:03:30 +08:00
2021-07-22 03:34:17 +08:00
if (arrayLength(json.Conditions)) {
filter.conditions(
json.Conditions.map(aData => FilterConditionModel.reviveFromJson(aData)).filter(v => v)
2019-07-05 03:19:24 +08:00
);
2016-07-07 05:03:30 +08:00
}
2021-03-14 19:15:48 +08:00
filter.actionKeep(0 != json.Keep);
2021-03-13 06:54:47 +08:00
filter.actionNoStop(0 == json.Stop);
filter.actionMarkAsRead(1 == json.MarkAsRead);
2016-07-07 05:03:30 +08:00
}
return filter;
2016-07-07 05:03:30 +08:00
}
cloneSelf() {
const filter = new FilterModel();
2016-07-07 05:03:30 +08:00
filter.id = this.id;
filter.enabled(this.enabled());
filter.name(this.name());
2020-10-25 18:46:58 +08:00
filter.nameError(this.nameError());
2016-07-07 05:03:30 +08:00
filter.conditionsType(this.conditionsType());
filter.actionMarkAsRead(this.actionMarkAsRead());
filter.actionType(this.actionType());
filter.actionValue(this.actionValue());
2020-10-25 18:46:58 +08:00
filter.actionValueError(this.actionValueError());
2016-07-07 05:03:30 +08:00
filter.actionValueSecond(this.actionValueSecond());
filter.actionValueThird(this.actionValueThird());
filter.actionValueFourth(this.actionValueFourth());
filter.actionKeep(this.actionKeep());
filter.actionNoStop(this.actionNoStop());
filter.conditions(this.conditions.map(item => item.cloneSelf()));
2016-07-07 05:03:30 +08:00
return filter;
}
}