-
Notifications
You must be signed in to change notification settings - Fork 3
/
todoist.js
157 lines (121 loc) · 2.89 KB
/
todoist.js
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
var uuid = require('node-uuid');
var https = require('https');
var Q = require('q');
var _ = require('lodash');
var httpsGet = function (url) {
var deferred = Q.defer();
console.log("Todoist GET URL " + url);
https.get(url, function(res) {
var buf = '';
res.on('data', function(data) {
buf += data;
});
res.on('end', function(data) {
deferred.resolve(buf);
});
});
return deferred.promise;
};
function todoist(url, success, fail) {
httpsGet(url)
.then(success)
.catch(fail);
}
function todoistURL(data){
var url = "https://todoist.com/API/v6/sync";
var query = [];
for(var k in data) {
query.push(k+'='+(typeof data[k] === 'string' ? data[k] : JSON.stringify(data[k])));
}
return url + '?' + query.join('&');
};
function todoistCommandURL(type, args, token){
return todoistURL({
token: token,
commands: [{
type: type,
temp_id: uuid.v4(),
uuid: uuid.v4(),
args: args
}]});
};
function todoistItemAdd(token, content) {
var labels = get_labels_from_text(content);
var text = strip_labels_from_text(content);
return todoistGetAllPromise(token)
.then(extractLabelIDs)
.then(itemAdd)
.catch(function(err){
console.error("Caught error itemAdd:"+err);
});
function extractLabelIDs(data) {
return get_label_ids_from_labels(labels, data);
}
function itemAdd(label_ids) {
var args = {
token: token,
commands: [{
type: "item_add",
temp_id: uuid.v4(),
uuid: uuid.v4(),
args: {
content: text,
labels: label_ids
}
}]};
return httpsGet(todoistURL(args));
}
}
function strip_labels_from_text(text) {
var pattern = /\B@[a-z0-9_-]+/gi;
return text.replace(pattern, "");
}
function get_labels_from_text(text, projects) {
var pattern = /\B@[a-z0-9_-]+/gi;
var labels = text.match(pattern);
// strip @
labels = _.map(labels, function(s) { return s.substr(1); });;
return labels;
}
function get_label_ids_from_labels(labels, allData){
var acct_labels = {};
allData.Labels.forEach(function(e){
acct_labels[e.name.toLowerCase()] = e;
});
var label_ids = [];
labels.forEach(function(label){
label = label.toLowerCase();
if(label in acct_labels){
label_ids.push(acct_labels[label].id);
}
});
return label_ids;
}
module.exports = {
itemAdd : todoistItemAdd,
getToday: function(token) {
return {};
},
getWeek: function(token) {
return {};
},
getList: function(token, project) {
return {};
},
getProjects: function(token, project) {
return {};
},
getAll: todoistGetAllPromise
};
function todoistGetAllPromise(token) {
var args = {
token: token,
seq_no: 0,
resource_types: ['all']
};
return httpsGet(todoistURL(args))
.then(JSON.parse)
.catch(function(err){
console.error("todoistGetAll "+err);
});
}