moving stuff, all fields from subject

This commit is contained in:
Erik Winter 2021-01-31 08:22:31 +01:00
parent 204c660d34
commit e50d624b9c
7 changed files with 120 additions and 14 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
gte-process-inbox

View File

@ -97,7 +97,7 @@ func NewDateFromString(date string) Date {
} }
daysToAdd := int(newWeekday) - weekday daysToAdd := int(newWeekday) - weekday
if daysToAdd < 0 { if daysToAdd <= 0 {
daysToAdd += 7 daysToAdd += 7
} }

View File

@ -32,6 +32,7 @@ const (
FIELD_ACTION = "action" FIELD_ACTION = "action"
FIELD_PROJECT = "project" FIELD_PROJECT = "project"
FIELD_DUE = "due" FIELD_DUE = "due"
FIELD_RECUR = "recur"
) )
var ( var (
@ -59,7 +60,9 @@ type Task struct {
Action string Action string
Project string Project string
Due Date Due Date
Recur Recurrer
//Message is the underlying message
Message *mstore.Message Message *mstore.Message
// Current indicates whether the task represents an existing message in the mstore // Current indicates whether the task represents an existing message in the mstore
@ -110,7 +113,13 @@ func New(msg *mstore.Message) *Task {
// Due // Due
dueStr, d := FieldFromBody(FIELD_DUE, msg.Body) 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 dirty = true
} }
due := NewDateFromString(dueStr) due := NewDateFromString(dueStr)
@ -134,6 +143,12 @@ func New(msg *mstore.Message) *Task {
// Project // Project
project, d := FieldFromBody(FIELD_PROJECT, msg.Body) project, d := FieldFromBody(FIELD_PROJECT, msg.Body)
if project == "" {
project = FieldFromSubject(FIELD_PROJECT, msg.Subject)
if project != "" {
dirty = true
}
}
if d { if d {
dirty = true dirty = true
} }
@ -242,11 +257,22 @@ func FieldFromBody(field, body string) (string, bool) {
} }
func FieldFromSubject(field, subject string) string { func FieldFromSubject(field, subject string) string {
if field != FIELD_ACTION {
return ""
}
terms := strings.Split(subject, SUBJECT_SEPARATOR) terms := strings.Split(subject, SUBJECT_SEPARATOR)
switch field {
case FIELD_ACTION:
return terms[len(terms)-1] 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 ""
} }

View File

@ -126,7 +126,7 @@ action: %s
}, },
}, },
{ {
name: "action in subject takes precedence", name: "action in body takes precedence",
message: &mstore.Message{ message: &mstore.Message{
Folder: task.FOLDER_PLANNED, Folder: task.FOLDER_PLANNED,
Subject: "some other action", Subject: "some other action",
@ -161,6 +161,45 @@ action: %s
Dirty: true, 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", name: "quoted fields",
message: &mstore.Message{ message: &mstore.Message{
@ -406,6 +445,12 @@ field: valuea
func TestFieldFromSubject(t *testing.T) { func TestFieldFromSubject(t *testing.T) {
action := "action" 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 { for _, tc := range []struct {
name string name string
field string field string
@ -423,26 +468,59 @@ func TestFieldFromSubject(t *testing.T) {
{ {
name: "unknown field", name: "unknown field",
field: "unknown", field: "unknown",
subject: action, subject: subjectOne,
}, },
{ {
name: "known field", name: "action with one",
field: task.FIELD_ACTION, field: task.FIELD_ACTION,
subject: action, subject: subjectOne,
exp: action, exp: action,
}, },
{ {
name: "with project", name: "action with with two",
field: task.FIELD_ACTION, field: task.FIELD_ACTION,
subject: fmt.Sprintf("project - %s", action), subject: subjectTwo,
exp: action, exp: action,
}, },
{ {
name: "with due and project", name: "action with three",
field: task.FIELD_ACTION, field: task.FIELD_ACTION,
subject: fmt.Sprintf("due - project - %s", action), subject: subjectThree,
exp: action, 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) { t.Run(tc.name, func(t *testing.T) {
test.Equals(t, tc.exp, task.FieldFromSubject(tc.field, tc.subject)) test.Equals(t, tc.exp, task.FieldFromSubject(tc.field, tc.subject))