This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
191 lines (156 loc) · 5.2 KB
/
index.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
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
const axios = require('axios');
const uuidv4 = require('uuid/v4');
async function getTaskCompletions(token) {
const url = 'https://todoist.com/api/v8/activity/get';
const params = {token: token, limit: 100};
const response = await axios.post(url, params);
if (response.status != 200) {
console.log('Unexpected response when retrieving activity: ' + response);
throw 'Unable to retrieve activity';
}
const result = [];
const idsSeen = new Set();
response.data.events.forEach(entry => {
if (entry.object_type != 'item' || entry.event_type != 'completed' || idsSeen.has(entry.object_id)) {
return;
}
idsSeen.add(entry.object_id);
if (!entry.extra_data || !entry.extra_data.content) {
return;
}
result.push({id: entry.object_id, content: entry.extra_data.content});
});
return result;
}
async function getTasksById(token) {
const url = 'https://todoist.com/api/v8/sync';
const params = {token: token, sync_token: '*', resource_types: '["items"]'};
const response = await axios.post(url, params);
if (response.status != 200) {
console.log('Unexpected response when retrieving items: ' + response);
throw 'Unable to retrieve items';
}
const tasksById = {};
response.data.items.forEach(item => {
tasksById[item.id] = item;
});
return tasksById;
}
async function getTaskRotationNotes(token) {
const url = 'https://todoist.com/api/v8/sync';
const params = {token: token, sync_token: '*', resource_types: '["notes"]'};
const response = await axios.post(url, params);
if (response.status != 200) {
console.log('Unexpected response when retrieving notes: ' + response);
throw 'Unable to retrieve notes';
}
const rotationNotesByTaskId = {};
response.data.notes.forEach(note => {
if (note.is_deleted) {
return;
}
if (note.content.startsWith('ROTATION:\n')) {
rotationNotesByTaskId[note.item_id] = note;
}
});
return rotationNotesByTaskId;
}
async function writeUpdates(token, commands) {
if (commands.length < 1) {
return;
}
const url = 'https://todoist.com/api/v8/sync';
const params = {token: token, sync_token: '*', commands: JSON.stringify(commands)};
const response = await axios.post(url, params);
if (response.status != 200) {
console.log('Unexpected response when writing updates: ' + response);
throw 'Unable to write updates';
}
const statuses = response.data.sync_status;
Object.keys(statuses).forEach(uuid => {
const status = statuses[uuid];
if (status != 'ok') {
console.log('An update failed: ' + status);
}
});
}
function filterTasksChangedSinceCompletion(taskCompletions, tasksById) {
return taskCompletions.filter(({id, content}) => {
const task = tasksById[id];
if (!task) {
console.log('Found activity for ' + id + ' but no task with that ID');
return false;
}
if (task.content != content) {
console.log('Task ' + id + ' has had its content changed from [' + content + '] to [' + task.content + '] since it was completed.');
return false;
}
return true;
});
}
function parseRotationNote(note) {
let lines = note.content.split("\n");
if (lines[0] != 'ROTATION:') {
console.log('Cannot parse rotation note, does not contain correct start line: ' + note);
return null;
}
lines.shift();
lines.map(line => line.trim()).filter(line => !!line);
if (lines.length < 1) {
console.log('Cannot parse rotation note, does not contain any entries: ' + note);
return null;
}
return lines;
}
function getNextTaskContent(lastContent, rotation) {
const nextIndex = rotation.indexOf(lastContent) + 1;
if (!nextIndex) {
console.log('Cannot find most recent content [' + lastContent + '] in rotation [' + rotation + ']');
return null;
}
if (nextIndex == rotation.length) {
return rotation[0];
}
return rotation[nextIndex];
}
function buildTaskUpdates(taskCompletions, taskRotationNotes) {
return taskCompletions.map(({id, content}) => {
const note = taskRotationNotes[id];
if (!note) {
return null;
}
const rotation = parseRotationNote(note);
if (!note) {
return null;
}
const nextContent = getNextTaskContent(content, rotation);
if (!nextContent) {
return null;
}
return ({id: id, content: nextContent});
}).filter(update => !!update);
}
function buildTaskUpdateCommands(taskUpdates) {
return taskUpdates.map(({id, content}) => ({
type: 'item_update',
uuid: uuidv4(),
args: { id, content }
}));
}
module.exports = async () => {
const token = process.env.TODOIST_API_TOKEN;
if (!token) {
throw 'Environment variable TODOIST_API_TOKEN not set';
}
const taskCompletions = await getTaskCompletions(token);
const tasksById = await getTasksById(token);
const filteredCompletions = filterTasksChangedSinceCompletion(taskCompletions, tasksById);
const taskRotationNotes = await getTaskRotationNotes(token);
const taskUpdates = buildTaskUpdates(filteredCompletions, taskRotationNotes);
if (taskUpdates.length > 0) {
console.log('Will perform updates:');
console.log(taskUpdates);
const taskUpdateCommands = buildTaskUpdateCommands(taskUpdates);
writeUpdates(token, taskUpdateCommands);
}
}