-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.js
396 lines (368 loc) · 12.4 KB
/
sync.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"use strict";
const _ = require("lodash"),
moment = require("moment"),
jsonFile = require("jsonfile");
module.exports = class Sync {
constructor(todoist, habitica, logger, config) {
this.priorityMap = {
1: 0.1,
2: 1,
3: 1.5,
4: 2,
};
this.habitica = habitica;
this.logger = logger;
this.todoist = todoist;
this.config = config;
}
async sync(lastRun) {
this.logger.info("*** SYNC STARTED ***");
this.config.append = function (key, obj) {
this[key] = obj;
return this;
};
const config = this.config;
lastRun = lastRun || {};
config.lastRun = lastRun;
return this.getHabiticaTasks(config)
.then(() => this.getProjects(config))
.then((config) => this.getSyncData(config, lastRun.syncToken))
.then((config) => this.scoreCompletedTasks(config))
.then((config) => this.updateDailies(config))
.then((config) => this.updateTasks(config))
.then((config) => this.checkDailyGoal(config))
.then((config) => this.syncChecklistItems(config));
}
getHabiticaTasks(config) {
this.logger.info("Getting habitica tasks");
return this.habitica
.listTasks()
.then((tasks) => {
this.logger.info("Loaded tasks");
config.habiticaTasks = tasks;
config.aliases = _.reduce(
tasks,
(acc, task) => {
acc[task.alias] = task._id;
return acc;
},
{},
);
})
.then(() => {
return this.habitica.listDailies().then((dailies) => {
this.logger.info("Loaded dailies");
config.habiticaDailies = dailies;
});
})
.then(() => {
return this.habitica.listTasks("habits").then((habits) => {
this.logger.info("Loaded habits");
config.habiticaHabits = habits;
});
})
.then(() => {
this.logger.info("Finished loading habitica tasks");
return config;
});
}
getProjects(config) {
return this.todoist
.listProjects()
.then((projects) => config.append("projects", projects));
}
getSyncData(config, syncToken) {
config.todoistLookup = {};
return this.todoist
.sync(syncToken)
.then((sync) => {
config.sync = sync;
config.sync.checklistItems = sync.items.filter(
(item) => item.parent_id,
);
// TODO: normalize the data structure so it confirms with the task list below
config.sync.items.forEach((i) => (config.todoistLookup[i.id] = i));
return config;
})
.then((config) => {
this.logger.info("Fetching all todoist tasks");
return this.todoist.listTasks(syncToken).then((tasks) => {
tasks.forEach((i) => (config.todoistLookup[i.id] = i));
config.todoistTasks = tasks;
return config;
});
});
}
async scoreCompletedTasks(config) {
const sync = config.sync;
const items = sync.items
.filter((item) => !item.parent_id)
.filter((item) => item.checked);
for (const item of items) {
this.logger.info("Scoring task", item.id, item.content);
try {
await this.habitica.scoreTask(item.id);
} catch (e) {
console.warn("Failed to score a task: " + item.content);
}
}
return config.append(
"items",
sync.items.filter((item) => !item.checked),
);
}
updateDailies(config) {
const isProjectAllowed = this.filterIgnoredProjects(config);
return Promise.all(
config.items
.filter(isProjectAllowed)
.filter(this.todoist.isTaskRecurring)
.map((item) => {
// if the recurring task's due date is in the future, this means it was probably completed
// unless it was simply created with a future due date
const dueDate = _.get(item, "due.date");
if (dueDate && moment(dueDate).isAfter(moment())) {
// try to find a matching "daily" in habitica and score it
const task = config.habiticaDailies.find(
(i) =>
i.text.toLowerCase().trim() ===
item.content.toLowerCase().trim(),
);
if (task) {
this.logger.info("Scoring daily task", task.text);
return this.habitica
.scoreTask(task._id)
.catch((e) =>
console.warn("Failed to score a task: " + item.content),
);
} else if (config.todoist.unmatchedDailyTask) {
return this.scoreTaskByName(
config,
config.todoist.unmatchedDailyTask,
).catch((e) => {
this.logger.error("Failed to score unmatched daily task:" + e);
});
} else {
this.logger.warn(
`Recurring task completed but no daily could be found in habitica called [${item.content}]`,
);
}
} else {
this.logger.info(`Skipping newly-created daily [${item.content}]`);
}
}),
).then(() => config);
}
async updateTasks(config) {
const isProjectAllowed = this.filterIgnoredProjects(config);
let items = config.items
.filter(isProjectAllowed)
.filter((t) => !t.parent_id)
.filter((t) => !this.todoist.isTaskRecurring(t));
for (const item of items) {
const aliases = config.habiticaTasks.map((t) => t.alias);
if (item.is_deleted) {
let checklistItem = null;
let habiticaTask = null;
this.config.habiticaTasks.forEach((t) => {
const checklistPrefix = `[${item.id}]`;
const found =
t.checklist &&
t.checklist.find((li) => li.text.includes(checklistPrefix));
if (found) {
checklistItem = found;
habiticaTask = t;
return false;
}
});
if (checklistItem) {
await this.habitica.deleteChecklistItem(
habiticaTask.id,
checklistItem.id,
);
} else {
await this.deleteTask(item);
}
} else if (_.includes(aliases, item.id + "")) {
await this.updateTask(item, config.aliases[item.id]);
} else {
if (!item.parent_id) {
// don't create new tasks in habitica which are subtasks in todoist = those should be checklist items!
try {
const newItem = await this.createTask(item);
this.config.habiticaTasks.push(newItem);
} catch (e) {
this.logger.warn("failed to create task: " + item.content);
}
} else {
this.logger.info("skipping child task", item.content);
}
}
}
return config;
}
checkDailyGoal(config) {
return this.todoist
.getStats()
.then((stats) => {
const lastRun = config.lastRun;
const goal = stats.goals.daily_goal;
const today = moment(stats["days_items"][0].date);
const lastDailyGoal = lastRun.lastDailyGoal;
const completed = stats["days_items"][0].total_completed;
this.logger.info(
"Daily goal: [" + goal + "] Completed: [" + completed + "]",
);
if (
completed >= goal &&
(!lastDailyGoal || today.isAfter(lastRun.lastDailyGoal, "d"))
) {
return this.scoreDailyGoalTask(config, today);
}
})
.then(() => config);
}
createHabiticaTask(todoistTask) {
const todoistDate = todoistTask.due_date_utc;
const dueDate = todoistDate && moment(todoistDate).format();
return {
type: "todo",
text: todoistTask.content,
alias: todoistTask.id,
priority: this.priorityMap[todoistTask.priority],
date: dueDate,
};
}
createTask(todoistTask) {
this.logger.info("Creating new habitica task: " + todoistTask.content);
return this.habitica.createTask(this.createHabiticaTask(todoistTask));
}
deleteTask(task) {
this.logger.info("Deleting habitica task: " + task.content);
return this.habitica
.deleteTask(task.id)
.catch((e) =>
console.warn(`Task ${task.id} wasn't deleted because it doesn't exist`),
);
}
filterIgnoredProjects(config) {
const ignoreProjectIds = config.ignoreProjects.map((projectName) => {
return _.keyBy(config.projects, "name")[projectName].id;
});
return function (item) {
const projectId = item.project_id;
return !_.includes(ignoreProjectIds, projectId);
};
}
findRootTask(todoistTask) {
let task = todoistTask;
let parentId = todoistTask.parent || todoistTask.parent_id;
while (parentId) {
task = this.config.todoistLookup[parentId];
if (!task) {
throw Error(`Task not found for parentId [${parentId}]`);
}
parentId = task.parent || task.parent_id;
}
return task;
}
syncChecklistItems(config) {
const checklistItems = config.sync.checklistItems || [];
return Promise.all(
checklistItems.map((todoistItem) => {
const parentTask = this.findRootTask(todoistItem);
const taskId = parentTask.id;
const habiticaTask = config.habiticaTasks.find(
(i) => i && i.alias == parentTask.id,
);
if (!habiticaTask) {
this.logger.warn(
"Could not find a habitica task for todoist item",
parentTask.id,
);
return;
}
const checklist = habiticaTask.checklist;
const content = this.createChecklistItem(todoistItem);
const habiticaChecklistItem = checklist.find((item) =>
item.text.includes(`[${todoistItem.id}]`),
);
if (habiticaChecklistItem) {
const itemId = habiticaChecklistItem.id;
if (todoistItem.is_deleted) {
return this.habitica.deleteChecklistItem(taskId, itemId);
} else {
this.logger.info("Updating checklist item");
return this.habitica
.updateChecklistItem(taskId, itemId, content)
.then(() => {
if (todoistItem.checked && !habiticaChecklistItem.completed) {
return this.habitica.scoreChecklistItem(taskId, itemId);
}
});
}
} else {
console.log("creating checklist item");
const staleTask = config.habiticaTasks.find(
(t) => t.alias == todoistItem.id,
);
// if the task exists, delete it
if (staleTask) {
// found a task that was converted to a checklist item, so we need to delete it
this.logger.info(
`Detected task that was converted to a subtask - deleting (${staleTask._id}).`,
);
return (
todoistItem.is_deleted ||
this.habitica
.deleteTask(staleTask._id)
.then(() => this.habitica.createChecklistItem(taskId, content))
);
} else {
return (
todoistItem.is_deleted ||
this.habitica.createChecklistItem(taskId, content)
);
}
}
}),
).then(() => config);
}
scoreDailyGoalTask(config, today) {
// TODO: make this configurable
const taskName = "Todoist: Daily Goal";
return this.scoreTaskByName(config, taskName)
.then((task) => {
this.logger.info(`Daily goal reached! Scored ${taskName}`, task._id);
config.lastRun.lastDailyGoal = today;
})
.catch((e) => this.logger.info(e));
}
scoreTaskByName(config, taskName) {
const task =
config.habiticaDailies.find((t) => t.text === taskName) ||
config.habiticaHabits.find((t) => t.text === taskName);
if (task) {
return this.habitica.scoreTask(task._id).then((ignored) => task);
} else {
return Promise.reject(`${taskName} not found`);
}
}
/**
* Generates the checklist item text for habitica from a todoist task
*
* The text will be in the form "[id] content", e.g., "[2342342342] walk the dog"
*/
createChecklistItem(todoistTask) {
return `[${todoistTask.id}] ${todoistTask.content}`;
}
updateTask(todoistTask) {
this.logger.info("Updating habitica task:" + todoistTask.content);
if (!!todoistTask.parent) {
// delete tasks in habitica which have become child tasks in todoist
return this.deleteTask(todoistTask);
} else {
return this.habitica.updateTask(this.createHabiticaTask(todoistTask));
}
}
};