diff --git a/internal/process/inbox.go b/internal/process/inbox.go index e26b1f7..f3eb36b 100644 --- a/internal/process/inbox.go +++ b/internal/process/inbox.go @@ -44,6 +44,22 @@ func (inbox *Inbox) Process() (*InboxResult, error) { return &InboxResult{}, fmt.Errorf("%w: %v", ErrInboxProcess, err) } + // deduplicate + taskKeys := map[string]*task.Task{} + for _, newT := range tasks { + existingT, ok := taskKeys[newT.Id] + switch { + case !ok: + taskKeys[newT.Id] = newT + case newT.Version >= existingT.Version: + taskKeys[newT.Id] = newT + } + } + tasks = []*task.Task{} + for _, t := range taskKeys { + tasks = append(tasks, t) + } + // split them doneTasks, updateTasks := []*task.Task{}, []*task.Task{} for _, t := range tasks { diff --git a/internal/process/inbox_test.go b/internal/process/inbox_test.go index 54d8f1c..c425426 100644 --- a/internal/process/inbox_test.go +++ b/internal/process/inbox_test.go @@ -35,15 +35,15 @@ func TestInboxProcess(t *testing.T) { }, { Subject: "to recurring", - Body: "recur: 2021-05-14, daily\nid: xxx-xxx\nversion: 1", + Body: "recur: 2021-05-14, daily\nid: xxx-xxx-a\nversion: 1", }, { Subject: "to planned", - Body: "due: 2021-05-14\nid: xxx-xxx\nversion: 1", + Body: "due: 2021-05-14\nid: xxx-xxx-b\nversion: 1", }, { Subject: "to unplanned", - Body: "id: xxx-xxx\nversion: 1", + Body: "id: xxx-xxx-c\nversion: 1", }, }, }, @@ -110,6 +110,30 @@ func TestInboxProcess(t *testing.T) { task.FOLDER_UNPLANNED: {}, }, }, + { + name: "deduplicate", + messages: map[string][]*mstore.Message{ + task.FOLDER_INBOX: { + { + Subject: "version 2", + Body: "id: xxx-xxx\nversion: 1\n", + }, + { + Subject: "version 2b", + Body: "id: xxx-xxx\nversion: 1\n", + }, + }, + task.FOLDER_UNPLANNED: {{ + Subject: "the task", + Body: "id: xxx-xxx\nversion: 1\n", + }}, + }, + expCount: 1, + expMsgs: map[string][]*mstore.Message{ + task.FOLDER_INBOX: {}, + task.FOLDER_UNPLANNED: {{Subject: "version 2b"}}, + }, + }, } { t.Run(tc.name, func(t *testing.T) { mstorer, err := mstore.NewMemory([]string{