-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathitem.go
266 lines (233 loc) · 6.25 KB
/
item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package todoist
import (
"context"
"regexp"
"strings"
"time"
)
var (
linkRegex = regexp.MustCompile(`\[(.*?)\]\((.*?)\)`)
)
const (
RFC3339Date = "2006-01-02"
RFC3339DateTime = "2006-01-02T15:04:05"
RFC3339DateTimeWithTimeZone = "2006-01-02T15:04:05Z07:00"
)
type Due struct {
Date string `json:"date"`
TimeZone string `json:"timezone"`
IsRecurring bool `json:"is_recurring"`
String string `json:"string"`
Lang string `json:"lang"`
}
type BaseItem struct {
HaveID
HaveProjectID
Content string `json:"content"`
UserID string `json:"user_id"`
}
func (bitem BaseItem) GetContent() string {
return bitem.Content
}
type CompletedItem struct {
BaseItem
CompletedData string `json:"completed_at"`
MetaData interface{} `json:"meta_data"`
TaskID string `json:"task_id"`
}
func (item CompletedItem) DateTime() time.Time {
t, _ := time.Parse(time.RFC3339, item.CompletedData)
return t
}
func (item CompletedItem) GetProjectID() string {
return item.ProjectID
}
func (item CompletedItem) GetLabelNames() []string {
return []string{}
}
type CompletedItems []CompletedItem
type Item struct {
BaseItem
HaveParentID
HaveIndent
HaveSectionID
ChildItem *Item `json:"-"`
BrotherItem *Item `json:"-"`
AllDay bool `json:"all_day"`
AssignedByUID string `json:"assigned_by_uid"`
Checked bool `json:"checked"`
Collapsed bool `json:"collapsed"`
DateAdded string `json:"added_at"`
DateLang string `json:"date_lang"`
DateString string `json:"date_string"`
DayOrder int `json:"day_order"`
Due *Due `json:"due"`
HasMoreNotes bool `json:"has_more_notes"`
IsArchived int `json:"is_archived"`
IsDeleted bool `json:"is_deleted"`
ItemOrder int `json:"item_order"`
LabelNames []string `json:"labels"`
Priority int `json:"priority"`
AutoReminder bool `json:"auto_reminder"`
ResponsibleUID interface{} `json:"responsible_uid"`
SyncID interface{} `json:"sync_id"`
}
type Items []Item
func (a Items) Len() int { return len(a) }
func (a Items) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Items) Less(i, j int) bool { return a[i].ID < a[j].ID }
func (a Items) At(i int) IDCarrier { return a[i] }
func (item Item) DateTime() time.Time {
var date string
if item.Due == nil {
date = ""
} else {
date = item.Due.Date
}
//2020-03-03T14:00:00
//2020-01-17T23:00:00Z
t, err := time.ParseInLocation(RFC3339DateTimeWithTimeZone, date, time.Local)
if err != nil {
t, err = time.ParseInLocation(RFC3339DateTime, date, time.Local)
}
if err != nil {
t, _ = time.ParseInLocation(RFC3339Date, date, time.Local)
}
return t
}
func (item Item) GetProjectID() string {
return item.ProjectID
}
func (item Item) GetLabelNames() []string {
return item.LabelNames
}
// interface for Eval actions
type AbstractItem interface {
DateTime() time.Time
GetProjectID() string
GetLabelNames() []string
}
func GetContentTitle(item ContentCarrier) string {
return linkRegex.ReplaceAllString(item.GetContent(), "$1")
}
func GetContentURL(item ContentCarrier) []string {
if HasURL(item) {
matches := linkRegex.FindAllStringSubmatch(item.GetContent(), -1)
if matches != nil {
urls := make([]string, len(matches))
for i, match := range matches {
urls[i] = match[2]
}
return urls
}
}
return []string{}
}
func HasURL(item ContentCarrier) bool {
return linkRegex.MatchString(item.GetContent())
}
func (item Item) AddParam() interface{} {
param := map[string]interface{}{}
if item.Content != "" {
param["content"] = item.Content
}
if item.DateString != "" {
param["date_string"] = item.DateString
}
if len(item.LabelNames) != 0 {
param["labels"] = item.LabelNames
}
if item.Priority != 0 {
param["priority"] = item.Priority
}
if item.ProjectID != "" {
param["project_id"] = item.ProjectID
}
if item.Due != nil {
param["due"] = item.Due
}
param["auto_reminder"] = item.AutoReminder
return param
}
func (item Item) UpdateParam() interface{} {
param := map[string]interface{}{}
if item.ID != "" {
param["id"] = item.ID
}
if item.Content != "" {
param["content"] = item.Content
}
if item.DateString != "" {
param["date_string"] = item.DateString
}
// TODO: more cool
if item.DateString == "null" {
param["date_string"] = ""
}
if len(item.LabelNames) != 0 {
param["labels"] = item.LabelNames
}
if item.Priority != 0 {
param["priority"] = item.Priority
}
if item.Due != nil {
param["due"] = item.Due
}
return param
}
func (item *Item) MoveParam(projectId string) interface{} {
param := map[string]interface{}{
"id": item.ID,
"project_id": projectId,
}
return param
}
func (item Item) LabelsString(store *Store) string {
var b strings.Builder
labelIDs := []string{}
for _, labelName := range item.LabelNames {
labelIDs = append(labelIDs, store.Labels.GetIDByName(labelName))
}
for i, labelId := range labelIDs {
label := store.FindLabel(labelId)
b.WriteString("@" + label.Name)
if i < len(labelIDs)-1 {
b.WriteString(",")
}
}
return b.String()
}
func (c *Client) AddItem(ctx context.Context, item Item) error {
commands := Commands{
NewCommand("item_add", item.AddParam()),
}
return c.ExecCommands(ctx, commands)
}
func (c *Client) UpdateItem(ctx context.Context, item Item) error {
commands := Commands{
NewCommand("item_update", item.UpdateParam()),
}
return c.ExecCommands(ctx, commands)
}
func (c *Client) CloseItem(ctx context.Context, ids []string) error {
var commands Commands
for _, id := range ids {
command := NewCommand("item_close", map[string]interface{}{"id": id})
commands = append(commands, command)
}
return c.ExecCommands(ctx, commands)
}
func (c *Client) DeleteItem(ctx context.Context, ids []string) error {
var commands Commands
for _, id := range ids {
command := NewCommand("item_delete", map[string]interface{}{"id": id})
commands = append(commands, command)
}
return c.ExecCommands(ctx, commands)
}
func (c *Client) MoveItem(ctx context.Context, item *Item, projectId string) error {
commands := Commands{
NewCommand("item_move", item.MoveParam(projectId)),
}
return c.ExecCommands(ctx, commands)
}