forked from ranmocy/gmail-automata
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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).
- Loading branch information
Matt Diehl
committed
Aug 14, 2022
1 parent
45411b5
commit 9640012
Showing
2 changed files
with
34 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import {Rule} from './Rule'; | |
export class Processor { | ||
|
||
private static processThread(session_data: SessionData, thread_data: ThreadData) { | ||
let thread_matched_a_rule = false; | ||
for (const message_data of thread_data.message_data_list) { | ||
// Apply each rule until matching a rule with a DONE action or matching a rule with | ||
// FINISH_STAGE and then exhausting all other rules in that stage. | ||
|
@@ -37,8 +38,10 @@ export class Processor { | |
break; | ||
} | ||
if (rule.condition.match(message_data)) { | ||
thread_matched_a_rule = true; | ||
console.log(`rule ${rule} matches message ${message_data}, apply action ${rule.thread_action}`); | ||
thread_data.thread_action.mergeFrom(rule.thread_action); | ||
console.log(`Thread action after merging: ${thread_data.thread_action}`); | ||
let endThread = false; | ||
switch (rule.thread_action.action_after_match) { | ||
case ActionAfterMatchType.DONE: | ||
|
@@ -69,7 +72,13 @@ export class Processor { | |
// } | ||
|
||
} | ||
thread_data.validateActions(); | ||
if (!thread_matched_a_rule) { | ||
const last_message = thread_data.getLatestMessage(); | ||
const from = last_message.getFrom(); | ||
const to = last_message.getTo(); | ||
const first_message_subject = thread_data.getFirstMessageSubject(); | ||
throw `Thread "${first_message_subject}" from ${from} to ${to} has no action, does it match any rule?`; | ||
} | ||
} | ||
|
||
public static processAllUnprocessedThreads() { | ||
|
@@ -171,19 +180,23 @@ export class Processor { | |
expect(thread_data.thread_action.move_to).toBe(InboxActionType.DEFAULT); | ||
expect(thread_data.thread_action.read).toBe(BooleanActionType.DEFAULT); | ||
}) | ||
it('Throws error when message matches action but has no actions', () => { | ||
expect(() => { | ||
test_proc([ | ||
{ | ||
conditions: '(sender [email protected])', | ||
stage: '5', | ||
}, | ||
], [ | ||
{ | ||
getFrom: () => '[email protected]', | ||
} | ||
]) | ||
}).toThrow(); | ||
it('Does nothing to message that matches rule with no actions', () => { | ||
const thread_data = test_proc([ | ||
{ | ||
conditions: '(sender [email protected])', | ||
stage: '5', | ||
}, | ||
], [ | ||
{ | ||
getFrom: () => '[email protected]', | ||
} | ||
]); | ||
|
||
expect(thread_data.thread_action.action_after_match).toBe(ActionAfterMatchType.DEFAULT); | ||
expect(thread_data.thread_action.important).toBe(BooleanActionType.DEFAULT); | ||
expect(thread_data.thread_action.label_names).toEqual(new Set()); | ||
expect(thread_data.thread_action.move_to).toBe(InboxActionType.DEFAULT); | ||
expect(thread_data.thread_action.read).toBe(BooleanActionType.DEFAULT); | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters