diff --git a/todoist/client.go b/todoist/client.go index 3ee5081..33b251c 100644 --- a/todoist/client.go +++ b/todoist/client.go @@ -27,6 +27,7 @@ type Client struct { Label *LabelClient Project *ProjectClient Relation *RelationClient + Note *NoteClient queue []Command } @@ -81,6 +82,7 @@ func NewClient(endpoint, token, sync_token, cache_dir string, logger *log.Logger c.Label = &LabelClient{c, &labelCache{&c.syncState.Labels}} c.Project = &ProjectClient{c, &projectCache{&c.syncState.Projects}} c.Relation = &RelationClient{c} + c.Note = &NoteClient{¬eCache{&c.syncState.Notes}} return c, nil } @@ -201,6 +203,12 @@ func (c *Client) updateState(state *SyncState) { for _, project := range state.Projects { c.Project.cache.store(project) } + for _, note := range state.Notes { + c.Note.cache.store(note) + } + for _, note := range state.ProjectNotes { + c.Note.cache.store(note) + } c.syncState = state } diff --git a/todoist/id.go b/todoist/id.go index 8f79c95..1629b8f 100644 --- a/todoist/id.go +++ b/todoist/id.go @@ -69,7 +69,8 @@ func (i *ID) UnmarshalJSON(b []byte) (err error) { } func GenerateTempID() ID { - return ID(uuid.NewV4().String()) + u, _ := uuid.NewV4() + return ID(u.String()) } func IsTempID(id ID) bool { @@ -82,7 +83,8 @@ func IsTempID(id ID) bool { type UUID string func GenerateUUID() UUID { - return UUID(uuid.NewV4().String()) + u, _ := uuid.NewV4() + return UUID(u.String()) } func (i UUID) MarshalJSON() ([]byte, error) { diff --git a/todoist/note.go b/todoist/note.go index 0ae328a..fdecef8 100644 --- a/todoist/note.go +++ b/todoist/note.go @@ -17,3 +17,57 @@ type Note struct { IsArchived int `json:"is_archived"` Posted Time `json:"posted"` } + +// NoteClient encapsulate client operations for notes. +type NoteClient struct { + cache *noteCache +} + +// GetAllForItem returns all the cached notes that belong to the given item. +func (c NoteClient) GetAllForItem(itemID ID) []Note { + var res []Note + for _, n := range c.cache.getAll() { + if n.ItemID == itemID { + res = append(res, n) + } + } + return res +} + +// GetAllForProject returns all the cached notes that belong to the given project. +func (c NoteClient) GetAllForProject(projectID ID) []Note { + var res []Note + for _, n := range c.cache.getAll() { + if n.ProjectID == projectID && n.ItemID == "" { + res = append(res, n) + } + } + return res +} + +type noteCache struct { + cache *[]Note +} + +func (c *noteCache) getAll() []Note { + return *c.cache +} + +func (c *noteCache) store(note Note) { + var res []Note + isNew := true + for _, n := range *c.cache { + if n.Equal(note) { + if !note.IsDeleted { + res = append(res, note) + } + isNew = false + } else { + res = append(res, n) + } + } + if isNew && !note.IsDeleted.Bool() { + res = append(res, note) + } + c.cache = &res +} diff --git a/todoist/sync.go b/todoist/sync.go index e632bb1..001c119 100644 --- a/todoist/sync.go +++ b/todoist/sync.go @@ -4,12 +4,12 @@ type SyncState struct { SyncToken string `json:"sync_token"` FullSync bool `json:"full_sync"` // User User `json:"user"` - Projects []Project `json:"projects"` - // ProjectNotes []interface{} `json:"project_notes"` - Items []Item `json:"items"` - Notes []Note `json:"notes"` - Labels []Label `json:"labels"` - Filters []Filter `json:"filters"` + Projects []Project `json:"projects"` + ProjectNotes []Note `json:"project_notes"` + Items []Item `json:"items"` + Notes []Note `json:"notes"` + Labels []Label `json:"labels"` + Filters []Filter `json:"filters"` // DayOrders struct {} `json:"day_orders"` // DayOrdersTimestamp string `json:"day_orders_timestamp"` Reminders []Reminder `json:"reminders"`