Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 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,6 +38,7 @@ 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);
let endThread = false;
Expand Down Expand Up @@ -69,7 +71,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 +179,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 xyz@gmail.com)',
stage: '5',
},
], [
{
getFrom: () => 'xyz@gmail.com',
}
])
}).toThrow();
it('Does nothing to message that matches rule with no actions', () => {
const thread_data = test_proc([
{
conditions: '(sender xyz@gmail.com)',
stage: '5',
},
], [
{
getFrom: () => 'xyz@gmail.com',
}
]);

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