Skip to content

Commit 5416823

Browse files
author
Matt Diehl
committed
Allow rules with no actions.
Rows in the rules sheet can match a mail, but do not need to specify actions. This is a different way of doing PR ranmocy#29, instead of adding a new InboxActionType we just decide to not worry about if a thread has any actions to do (as long as it matched a rule).
1 parent 9a7a0d9 commit 5416823

2 files changed

Lines changed: 34 additions & 22 deletions

File tree

Processor.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {Rule} from './Rule';
2424
export class Processor {
2525

2626
private static processThread(session_data: SessionData, thread_data: ThreadData) {
27+
let thread_matched_a_rule = false;
2728
for (const message_data of thread_data.message_data_list) {
2829
// Apply each rule until matching a rule with a DONE action or matching a rule with
2930
// FINISH_STAGE and then exhausting all other rules in that stage.
@@ -37,8 +38,10 @@ export class Processor {
3738
break;
3839
}
3940
if (rule.condition.match(message_data)) {
41+
thread_matched_a_rule = true;
4042
console.log(`rule ${rule} matches message ${message_data}, apply action ${rule.thread_action}`);
4143
thread_data.thread_action.mergeFrom(rule.thread_action);
44+
console.log(`Thread action after merging: ${thread_data.thread_action}`);
4245
let endThread = false;
4346
switch (rule.thread_action.action_after_match) {
4447
case ActionAfterMatchType.DONE:
@@ -69,7 +72,13 @@ export class Processor {
6972
// }
7073

7174
}
72-
thread_data.validateActions();
75+
if (!thread_matched_a_rule) {
76+
const last_message = thread_data.getLatestMessage();
77+
const from = last_message.getFrom();
78+
const to = last_message.getTo();
79+
const first_message_subject = thread_data.getFirstMessageSubject();
80+
throw `Thread "${first_message_subject}" from ${from} to ${to} has no action, does it match any rule?`;
81+
}
7382
}
7483

7584
public static processAllUnprocessedThreads() {
@@ -171,19 +180,23 @@ export class Processor {
171180
expect(thread_data.thread_action.move_to).toBe(InboxActionType.DEFAULT);
172181
expect(thread_data.thread_action.read).toBe(BooleanActionType.DEFAULT);
173182
})
174-
it('Throws error when message matches action but has no actions', () => {
175-
expect(() => {
176-
test_proc([
177-
{
178-
conditions: '(sender xyz@gmail.com)',
179-
stage: '5',
180-
},
181-
], [
182-
{
183-
getFrom: () => 'xyz@gmail.com',
184-
}
185-
])
186-
}).toThrow();
183+
it('Does nothing to message that matches rule with no actions', () => {
184+
const thread_data = test_proc([
185+
{
186+
conditions: '(sender xyz@gmail.com)',
187+
stage: '5',
188+
},
189+
], [
190+
{
191+
getFrom: () => 'xyz@gmail.com',
192+
}
193+
]);
194+
195+
expect(thread_data.thread_action.action_after_match).toBe(ActionAfterMatchType.DEFAULT);
196+
expect(thread_data.thread_action.important).toBe(BooleanActionType.DEFAULT);
197+
expect(thread_data.thread_action.label_names).toEqual(new Set());
198+
expect(thread_data.thread_action.move_to).toBe(InboxActionType.DEFAULT);
199+
expect(thread_data.thread_action.read).toBe(BooleanActionType.DEFAULT);
187200
})
188201
}
189202
}

ThreadData.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,13 @@ export class ThreadData {
121121
}
122122
}
123123

124-
validateActions() {
125-
if (!this.thread_action.hasAnyAction()) {
126-
const messages = this.raw.getMessages();
127-
const last_message = messages[messages.length - 1];
128-
const from = last_message.getFrom();
129-
const to = last_message.getTo();
130-
throw `Thread "${this.raw.getFirstMessageSubject()}" from ${from} to ${to} has no action, does it match any rule?`;
131-
}
124+
getLatestMessage(): GoogleAppsScript.Gmail.GmailMessage {
125+
const messages = this.raw.getMessages();
126+
return messages[messages.length -1];
127+
}
128+
129+
getFirstMessageSubject(): string {
130+
return this.raw.getFirstMessageSubject();
132131
}
133132

134133
static applyAllActions(session_data: SessionData, all_thread_data: ThreadData[]) {

0 commit comments

Comments
 (0)