Skip to content

Commit

Permalink
Allow rules with no actions.
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
41 changes: 27 additions & 14 deletions Processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
})
}
}
15 changes: 7 additions & 8 deletions ThreadData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,13 @@ export class ThreadData {
}
}

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

getFirstMessageSubject(): string {
return this.raw.getFirstMessageSubject();
}

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

0 comments on commit 9640012

Please sign in to comment.