quoted forwards

This commit is contained in:
Erik Winter 2021-01-29 17:48:22 +01:00
parent 32fad4414a
commit e5e98cfc2c
2 changed files with 68 additions and 19 deletions

View File

@ -17,6 +17,7 @@ const (
FOLDER_INBOX = "INBOX" FOLDER_INBOX = "INBOX"
FOLDER_NEW = "New" FOLDER_NEW = "New"
QUOTE_PREFIX = ">"
FIELD_SEPARATOR = ":" FIELD_SEPARATOR = ":"
FIELD_ID = "id" FIELD_ID = "id"
FIELD_ACTION = "action" FIELD_ACTION = "action"
@ -50,19 +51,25 @@ type Task struct {
// This enables updating a task by forwarding a topposted message whith new values for fields that the user wants to update. // This enables updating a task by forwarding a topposted message whith new values for fields that the user wants to update.
func New(msg *mstore.Message) *Task { func New(msg *mstore.Message) *Task {
dirty := false dirty := false
id := FieldFromBody(FIELD_ID, msg.Body) id, d := FieldFromBody(FIELD_ID, msg.Body)
if id == "" { if id == "" {
id = uuid.New().String() id = uuid.New().String()
dirty = true dirty = true
} }
if d {
dirty = true
}
action := FieldFromBody(FIELD_ACTION, msg.Body) action, d := FieldFromBody(FIELD_ACTION, msg.Body)
if action == "" { if action == "" {
action = FieldFromSubject(FIELD_ACTION, msg.Subject) action = FieldFromSubject(FIELD_ACTION, msg.Subject)
if action != "" { if action != "" {
dirty = true dirty = true
} }
} }
if d {
dirty = true
}
folder := msg.Folder folder := msg.Folder
if folder == FOLDER_INBOX { if folder == FOLDER_INBOX {
@ -111,19 +118,28 @@ func (t *Task) FormatBody() string {
return body return body
} }
func FieldFromBody(field, body string) string { func FieldFromBody(field, body string) (string, bool) {
value := ""
dirty := false
lines := strings.Split(body, "\n") lines := strings.Split(body, "\n")
for _, line := range lines { for _, line := range lines {
parts := strings.SplitN(line, FIELD_SEPARATOR, 2) parts := strings.SplitN(line, FIELD_SEPARATOR, 2)
if len(parts) < 2 { if len(parts) < 2 {
continue continue
} }
if strings.ToLower(strings.TrimSpace(parts[0])) == field {
return strings.TrimSpace(parts[1]) fieldName := strings.ToLower(strings.TrimSpace(strings.TrimPrefix(parts[0], QUOTE_PREFIX)))
if fieldName == field {
if value == "" {
value = strings.TrimSpace(parts[1])
} else {
dirty = true
}
} }
} }
return "" return value, dirty
} }
func FieldFromSubject(field, subject string) string { func FieldFromSubject(field, subject string) string {

View File

@ -69,6 +69,7 @@ id: %s
action: %s action: %s
`, id, action), `, id, action),
}, },
hasId: true,
exp: &task.Task{ exp: &task.Task{
Id: id, Id: id,
Folder: folder, Folder: folder,
@ -82,6 +83,27 @@ action: %s
Subject: action, Subject: action,
Body: fmt.Sprintf(`id: %s`, id), Body: fmt.Sprintf(`id: %s`, id),
}, },
hasId: true,
exp: &task.Task{
Id: id,
Folder: folder,
Action: action,
Dirty: true,
},
},
{
name: "quoted fields",
message: &mstore.Message{
Folder: folder,
Body: fmt.Sprintf(`
action: %s
Forwarded message:
> id: %s
> action: old action
`, action, id),
},
hasId: true,
exp: &task.Task{ exp: &task.Task{
Id: id, Id: id,
Folder: folder, Folder: folder,
@ -162,10 +184,11 @@ action: an action
func TestFieldFromBody(t *testing.T) { func TestFieldFromBody(t *testing.T) {
for _, tc := range []struct { for _, tc := range []struct {
name string name string
field string field string
body string body string
exp string expValue string
expDirty bool
}{ }{
{ {
name: "empty field", name: "empty field",
@ -190,7 +213,7 @@ fielda: valuea
fieldb: valueb fieldb: valueb
fieldc: valuec fieldc: valuec
`, `,
exp: "valueb", expValue: "valueb",
}, },
{ {
name: "present twice", name: "present twice",
@ -199,29 +222,39 @@ fieldc: valuec
field: valuea field: valuea
field: valueb field: valueb
`, `,
exp: "valuea", expValue: "valuea",
expDirty: true,
}, },
{ {
name: "with colons", name: "with colons",
field: "field", field: "field",
body: "field:: val:ue", body: "field:: val:ue",
exp: ": val:ue", expValue: ": val:ue",
}, },
{ {
name: "trim field", name: "trim field",
field: "field", field: "field",
body: " field : value", body: " field : value",
exp: "value", expValue: "value",
}, },
{ {
name: "trim value", name: "trim value",
field: "field", field: "field",
body: "field: value ", body: "field: value ",
exp: "value", expValue: "value",
},
{
name: "quoted",
field: "field",
body: "> field: value",
expValue: "value",
}, },
} { } {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
test.Equals(t, tc.exp, task.FieldFromBody(tc.field, tc.body)) actValue, actDirty := task.FieldFromBody(tc.field, tc.body)
test.Equals(t, tc.expValue, actValue)
test.Equals(t, tc.expDirty, actDirty)
}) })
} }
} }