forked from ranmocy/gmail-automata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadAction.ts
147 lines (121 loc) · 5.79 KB
/
ThreadAction.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
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export enum BooleanActionType {DEFAULT, ENABLE, DISABLE}
export enum InboxActionType {DEFAULT, INBOX, ARCHIVE, TRASH}
export enum ActionAfterMatchType {DEFAULT, DONE, FINISH_STAGE, NEXT_STAGE}
export default class ThreadAction {
private static ACTION_CONFIG_TYPE_FIELD_NAMES: (keyof Pick<ThreadAction, "important" | "read" | "auto_label">)[] = ["important", "read", "auto_label"];
public readonly label_names: Set<string> = new Set<string>();
public move_to: InboxActionType = InboxActionType.DEFAULT;
public important: BooleanActionType = BooleanActionType.DEFAULT;
public read: BooleanActionType = BooleanActionType.DEFAULT;
public auto_label: BooleanActionType = BooleanActionType.DEFAULT;
public action_after_match: ActionAfterMatchType = ActionAfterMatchType.DEFAULT;
hasAnyAction() {
return this.label_names.size > 0
|| this.move_to != InboxActionType.DEFAULT
|| this.important != BooleanActionType.DEFAULT
|| this.read != BooleanActionType.DEFAULT;
}
addLabels(new_label_names: string[]) {
for (const label of new_label_names) {
let remaining = label;
while (remaining) {
this.label_names.add(remaining);
const index = remaining.lastIndexOf('/');
remaining = remaining.substring(0, index);
}
}
}
mergeFrom(other: Readonly<ThreadAction>): this {
this.addLabels(Array.from(other.label_names.values()));
if (other.move_to != InboxActionType.DEFAULT) {
this.move_to = other.move_to;
}
for (const name of ThreadAction.ACTION_CONFIG_TYPE_FIELD_NAMES) {
if (other[name] != BooleanActionType.DEFAULT) {
this[name] = other[name];
}
}
return this;
}
toString() {
let result = `>${InboxActionType[this.move_to]} +L${Array.from(this.label_names.values())}`;
for (const name of ThreadAction.ACTION_CONFIG_TYPE_FIELD_NAMES) {
switch (this[name]) {
case BooleanActionType.ENABLE:
result += ` +${name[0].toUpperCase()}`;
break;
case BooleanActionType.DISABLE:
result += ` -A${name[0].toUpperCase()}`;
break;
}
}
return result;
}
static testThreadActions(it: Function, expect: Function) {
it('Adds parent labels', () => {
const labels = ['list/abc', 'bot/team1/test', 'bot/team1/alert', 'def'];
const action = new ThreadAction();
const expected = new Set(['list', 'list/abc', 'bot', 'bot/team1', 'bot/team1/test', 'bot/team1/alert', 'def'])
action.addLabels(labels);
expect(action.label_names).toEqual(expected);
});
it('Does not add parent labels for empty list', () => {
const labels: string[] = [];
const action = new ThreadAction();
action.addLabels(labels);
expect(action.label_names.size).toBe(0);
});
it('Uses Default action for rules', () => {
const thread_data_action = new ThreadAction();
expect(thread_data_action.move_to).toBe(InboxActionType.DEFAULT);
});
it('Default Actions for message', () => {
const message_action = new ThreadAction();
expect(message_action.move_to).toBe(InboxActionType.DEFAULT);
expect(message_action.important).toBe(BooleanActionType.DEFAULT);
expect(message_action.read).toBe(BooleanActionType.DEFAULT);
});
it('Merges the final Actions from a single rule', () => {
const message_action = new ThreadAction();
const rule1_action = new ThreadAction;
rule1_action.move_to = InboxActionType.ARCHIVE;
rule1_action.important = BooleanActionType.ENABLE;
rule1_action.read = BooleanActionType.ENABLE;
message_action.mergeFrom(rule1_action);
expect(message_action.move_to).toBe(InboxActionType.ARCHIVE);
expect(message_action.important).toBe(BooleanActionType.ENABLE);
expect(message_action.read).toBe(BooleanActionType.ENABLE);
});
it('Merges the final Actions from multiple rules', () => {
const message_action = new ThreadAction();
const rule1_action = new ThreadAction;
const rule2_action = new ThreadAction;
rule1_action.move_to = InboxActionType.ARCHIVE;
rule1_action.important = BooleanActionType.ENABLE;
rule1_action.read = BooleanActionType.ENABLE;
rule2_action.move_to = InboxActionType.TRASH;
rule2_action.important = BooleanActionType.DISABLE;
rule2_action.read = BooleanActionType.DISABLE;
message_action.mergeFrom(rule1_action);
message_action.mergeFrom(rule2_action);
expect(message_action.move_to).toBe(InboxActionType.TRASH);
expect(message_action.important).toBe(BooleanActionType.DISABLE);
expect(message_action.read).toBe(BooleanActionType.DISABLE);
});
}
}