diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ee2601c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +gte-process-inbox + diff --git a/cmd/process-inbox/main.go b/cmd/gte-process-inbox/main.go similarity index 100% rename from cmd/process-inbox/main.go rename to cmd/gte-process-inbox/main.go diff --git a/internal/task/date.go b/internal/task/date.go index 4dd2cc5..734b071 100644 --- a/internal/task/date.go +++ b/internal/task/date.go @@ -97,7 +97,7 @@ func NewDateFromString(date string) Date { } daysToAdd := int(newWeekday) - weekday - if daysToAdd < 0 { + if daysToAdd <= 0 { daysToAdd += 7 } diff --git a/internal/task/recurring.go b/internal/task/recur.go similarity index 100% rename from internal/task/recurring.go rename to internal/task/recur.go diff --git a/internal/task/task.go b/internal/task/task.go index cfd6064..0c63176 100644 --- a/internal/task/task.go +++ b/internal/task/task.go @@ -32,6 +32,7 @@ const ( FIELD_ACTION = "action" FIELD_PROJECT = "project" FIELD_DUE = "due" + FIELD_RECUR = "recur" ) var ( @@ -59,7 +60,9 @@ type Task struct { Action string Project string Due Date + Recur Recurrer + //Message is the underlying message Message *mstore.Message // Current indicates whether the task represents an existing message in the mstore @@ -110,7 +113,13 @@ func New(msg *mstore.Message) *Task { // Due dueStr, d := FieldFromBody(FIELD_DUE, msg.Body) - if dueStr == "" || d { + if dueStr == "" { + dueStr = FieldFromSubject(FIELD_DUE, msg.Subject) + if dueStr != "" { + dirty = true + } + } + if d { dirty = true } due := NewDateFromString(dueStr) @@ -134,6 +143,12 @@ func New(msg *mstore.Message) *Task { // Project project, d := FieldFromBody(FIELD_PROJECT, msg.Body) + if project == "" { + project = FieldFromSubject(FIELD_PROJECT, msg.Subject) + if project != "" { + dirty = true + } + } if d { dirty = true } @@ -242,11 +257,22 @@ func FieldFromBody(field, body string) (string, bool) { } func FieldFromSubject(field, subject string) string { - if field != FIELD_ACTION { - return "" - } terms := strings.Split(subject, SUBJECT_SEPARATOR) + switch field { + case FIELD_ACTION: + return terms[len(terms)-1] + case FIELD_PROJECT: + if len(terms) < 2 { + return "" + } + return terms[len(terms)-2] + case FIELD_DUE: + if len(terms) < 3 { + return "" + } + return terms[len(terms)-3] + } - return terms[len(terms)-1] + return "" } diff --git a/internal/task/task_test.go b/internal/task/task_test.go index 8f3c83b..637f04b 100644 --- a/internal/task/task_test.go +++ b/internal/task/task_test.go @@ -126,7 +126,7 @@ action: %s }, }, { - name: "action in subject takes precedence", + name: "action in body takes precedence", message: &mstore.Message{ Folder: task.FOLDER_PLANNED, Subject: "some other action", @@ -161,6 +161,45 @@ action: %s Dirty: true, }, }, + { + name: "project in body takes precedence", + message: &mstore.Message{ + Folder: task.FOLDER_PLANNED, + Subject: fmt.Sprintf("old project - %s", action), + Body: fmt.Sprintf(` +id: %s +due: no date +version: %d +action: %s +project: %s + `, id, version, action, project), + }, + hasId: true, + hasVersion: true, + exp: &task.Task{ + Id: id, + Version: version, + Folder: task.FOLDER_PLANNED, + Action: action, + Project: project, + }, + }, + { + name: "project from subject if not present in body", + message: &mstore.Message{ + Folder: task.FOLDER_PLANNED, + Subject: fmt.Sprintf("%s - %s", project, action), + Body: fmt.Sprintf(`id: %s`, id), + }, + hasId: true, + exp: &task.Task{ + Id: id, + Folder: task.FOLDER_PLANNED, + Action: action, + Project: project, + Dirty: true, + }, + }, { name: "quoted fields", message: &mstore.Message{ @@ -406,6 +445,12 @@ field: valuea func TestFieldFromSubject(t *testing.T) { action := "action" + project := "project" + due := "due" + subjectOne := fmt.Sprintf("%s", action) + subjectTwo := fmt.Sprintf("%s - %s", project, action) + subjectThree := fmt.Sprintf("%s - %s - %s", due, project, action) + for _, tc := range []struct { name string field string @@ -423,26 +468,59 @@ func TestFieldFromSubject(t *testing.T) { { name: "unknown field", field: "unknown", - subject: action, + subject: subjectOne, }, { - name: "known field", + name: "action with one", field: task.FIELD_ACTION, - subject: action, + subject: subjectOne, exp: action, }, { - name: "with project", + name: "action with with two", field: task.FIELD_ACTION, - subject: fmt.Sprintf("project - %s", action), + subject: subjectTwo, exp: action, }, { - name: "with due and project", + name: "action with three", field: task.FIELD_ACTION, - subject: fmt.Sprintf("due - project - %s", action), + subject: subjectThree, exp: action, }, + { + name: "project with one", + field: task.FIELD_PROJECT, + subject: subjectOne, + }, + { + name: "project with with two", + field: task.FIELD_PROJECT, + subject: subjectTwo, + exp: project, + }, + { + name: "project with three", + field: task.FIELD_PROJECT, + subject: subjectThree, + exp: project, + }, + { + name: "due with one", + field: task.FIELD_DUE, + subject: subjectOne, + }, + { + name: "due with with two", + field: task.FIELD_DUE, + subject: subjectTwo, + }, + { + name: "due with three", + field: task.FIELD_DUE, + subject: subjectThree, + exp: due, + }, } { t.Run(tc.name, func(t *testing.T) { test.Equals(t, tc.exp, task.FieldFromSubject(tc.field, tc.subject)) diff --git a/pkg/mstore/email.go b/pkg/mstore/imap.go similarity index 100% rename from pkg/mstore/email.go rename to pkg/mstore/imap.go