Skip to content

Commit

Permalink
Merge pull request #1 from idrarig/notes
Browse files Browse the repository at this point in the history
add accessors for cached notes
  • Loading branch information
kobtea committed Jan 29, 2018
2 parents 169f904 + 529ecee commit 7274fc6
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
8 changes: 8 additions & 0 deletions todoist/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Client struct {
Label *LabelClient
Project *ProjectClient
Relation *RelationClient
Note *NoteClient
queue []Command
}

Expand Down Expand Up @@ -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{&noteCache{&c.syncState.Notes}}
return c, nil
}

Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 4 additions & 2 deletions todoist/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
54 changes: 54 additions & 0 deletions todoist/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 6 additions & 6 deletions todoist/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 7274fc6

Please sign in to comment.