forked from malcolmocean/opusfluxus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
396 lines (377 loc) · 11 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
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
var Q = require('q')
request = require('request')
var querystring = require('querystring')
var uuidv4 = require('uuid/v4')
var utils = {
getTimestamp: function(meta) {
return Math.floor((Date.now() - meta.projectTreeData.mainProjectTreeInfo.dateJoinedTimestampInSeconds) / 60)
},
makePollId: function() {
return (Math.random() + 1).toString(36).substr(2, 8)
},
httpAbove299toError: function (arg) {
var body, error, resp
resp = arg[0], body = arg[1]
var status = resp.statusCode
if(!((status === 302) && (resp.headers.location === "https://workflowy.com/" || resp.headers.location === "/"))) {
if ((300 <= status && status < 600)) {
return Q.reject({status: status, message: "Error with request " + resp.request.uri.href + ": " + status})
}
if (error = body.error) {
return Q.reject({status: status, message: "Error with request " + resp.request.uri.href + ": " + error})
}
}
return arg
},
}
module.exports = Workflowy = (function() {
Workflowy.clientVersion = 18
Workflowy.urls = {
login: 'https://workflowy.com/ajax_login',
// login: 'https://workflowy.com/accounts/login/',
meta: "https://workflowy.com/get_initialization_data?client_version=" + Workflowy.clientVersion,
update: 'https://workflowy.com/push_and_poll'
}
function Workflowy(auth, jar) {
var self = this
this.jar = jar ? request.jar(jar) : request.jar()
this.request = request.defaults({
jar: this.jar,
json: true
})
this.sessionid = auth.sessionid
if (!this.sessionid) {
this.username = auth.username
this.password = auth.password
this._lastTransactionId = null
this._login = Q.ninvoke(this.request, 'post', {
url: Workflowy.urls.login,
form: {
username: this.username,
password: this.password
}
}).then(utils.httpAbove299toError)
.then(function (arg) {
var body = arg[1]
if (/Please enter a correct username and password./.test(body)) {
return Q.reject({status: 403, message: "Incorrect login info"})
}
}).then(function(arg) {
var jar = self.jar._jar.toJSON()
for (c in jar.cookies) {
if (jar.cookies[c].key === 'sessionid') {
self.sessionid = jar.cookies[c].value
break
}
}
}, function (err) {
return Q.reject(err)
})
}
this.refresh()
}
Workflowy.prototype.refresh = function() {
function meta (_this) {
return function() {
var opts = {
url: Workflowy.urls.meta
}
if (_this.sessionid) {
opts.headers = {
Cookie: 'sessionid='+_this.sessionid
}
}
return Q.ninvoke(_this.request, 'get', opts)
.then(utils.httpAbove299toError)
.then(function(arg) {
var body = arg[1]
return body
}).fail(function(err) {
err.message = "Error fetching document root: " + err.message
return Q.reject(err)
})
}
}
if (this.sessionid) {
this.meta = Q.when(true, meta(this))
} else {
this.meta = this._login.then(meta(this))
}
this.outline = this.meta.then((function(_this) {
return function(body) {
var meta
meta = body.projectTreeData.mainProjectTreeInfo
_this._lastTransactionId = meta.initialMostRecentOperationTransactionId
return meta.rootProjectChildren
}
})(this))
return this.nodes = this.outline.then((function(_this) {
return function(outline) {
var addChildren, result
result = []
addChildren = function(arr, parentId, parentCompleted) {
var child, children, j, len
result.push.apply(result, arr)
for (j = 0, len = arr.length; j < len; j++) {
child = arr[j]
child.parentId = parentId
child.pcp = parentCompleted
child.prio = j
if (children = child.ch) {
addChildren(children, child.id, child.cp || child.pcp)
}
}
}
addChildren(outline, 'None', false)
return result
}
})(this))
}
Workflowy.prototype._update = function(operations) {
return this.meta.then((function(_this) {
return function(meta) {
var clientId, j, len, operation, timestamp
timestamp = utils.getTimestamp(meta)
clientId = meta.projectTreeData.clientId
for (j = 0, len = operations.length; j < len; j++) {
operation = operations[j]
operation.client_timestamp = 140101232 //timestamp
}
return Q.ninvoke(_this.request, 'post', {
url: Workflowy.urls.update,
form: {
client_id: clientId,
client_version: Workflowy.clientVersion,
push_poll_id: utils.makePollId(),
push_poll_data: JSON.stringify([
{
most_recent_operation_transaction_id: _this._lastTransactionId,
operations: operations
}
])
},
headers: {
Cookie: 'sessionid='+_this.sessionid
}
})
.then(utils.httpAbove299toError)
.then(function(arg) {
var resp = arg[0]
var body = arg[1]
_this._lastTransactionId = body.results[0].new_most_recent_operation_transaction_id
return [resp, body, timestamp]
})
}
})(this))
}
/*
* @search [optional]
* @returns an array of nodes that match the given string, regex or function
*/
Workflowy.prototype.find = function(search, completed, parentCompleted) {
var condition, deferred, originalCondition, originalCondition2
if (!search) {
} else if (typeof search == 'function') {
condition = search
} else if (typeof search == 'string') {
condition = function(node) {
return node.nm === search
}
} else if (search instanceof RegExp) {
condition = function(node) {
return search.test(node.nm)
}
} else {
(deferred = Q.defer()).reject(new Error('unknown search type'))
return deferred
}
if (typeof completed == 'boolean') {
originalCondition = condition
condition = function(node) {
return (Boolean(node.cp) === !!completed) && originalCondition(node)
}
}
if (typeof parentCompleted == 'boolean') {
originalCondition2 = condition
condition = function(node) {
return Boolean(node.pcp) === !!parentCompleted && originalCondition2(node)
}
}
return this.nodes.then(function(nodes) {
if (condition) {
nodes = nodes.filter(condition)
}
return nodes
})
}
Workflowy.prototype.delete = function(nodes) {
var node, operations
if (Array.isArray(nodes)) {
nodes = [nodes]
}
operations = (function() {
var j, len, results
results = []
for (j = 0, len = nodes.length; j < len; j++) {
node = nodes[j]
results.push({
type: 'delete',
data: {
projectid: node.id
},
undo_data: {
previous_last_modified: node.lm,
parentid: node.parentId,
priority: node.prio
}
})
}
return results
})()
return this._update(operations).then((function(_this) {
return function() {
_this.refresh()
}
})(this))
}
Workflowy.prototype.complete = function(nodes, tf) {
console.log(nodes)
console.log(tf)
var node, operations
if (tf == null) {
tf = true
}
if (!Array.isArray(nodes)) {
nodes = [nodes]
}
operations = (function() {
var j, len, results
results = []
for (j = 0, len = nodes.length; j < len; j++) {
node = nodes[j]
results.push({
type: tf ? 'complete' : 'uncomplete',
data: {
projectid: node.id
},
undo_data: {
previous_last_modified: node.lm,
previous_completed: tf ? false : node.cp
}
})
}
console.log(results)
return results
})()
return this._update(operations).then((function(_this) {
console.log(_this)
return function(arg) {
var body, i, j, len, resp, timestamp
resp = arg[0], body = arg[1], timestamp = arg[2]
for (i = j = 0, len = nodes.length; j < len; i = ++j) {
node = nodes[i]
if (tf) {
console.log(timestamp)
node.cp = timestamp
} else {
delete node.cp
}
}
}
})(this))
}
Workflowy.prototype.create = function (parentid, name, priority, note) {
var projectid = uuidv4()
var operations = [
{
type: "create",
data: {
projectid: projectid,
parentid: parentid,
priority: priority || 0 // 0 adds as first child, 1 as second, etc
},
},
{
type: "edit",
data: {
projectid: projectid,
name: name,
description: note,
}
}
]
return this._update(operations)
.then(utils.httpAbove299toError)
.then((function(_this) {
return function(arg) {
// TODO: for a local workflowy client
// we'll want to update the local node
}
})(this))
}
Workflowy.prototype.update = function(nodes, newNames) {
var i, node, operations
if (!Array.isArray(nodes)) {
nodes = [nodes]
newNames = [newNames]
}
operations = (function() {
var j, len, results
results = []
for (i = j = 0, len = nodes.length; j < len; i = ++j) {
node = nodes[i]
results.push({
type: 'edit',
data: {
projectid: node.id,
name: newNames[i]
},
undo_data: {
previous_last_modified: node.lm,
previous_name: node.nm
}
})
}
return results
})()
return this._update(operations).then((function(_this) {
return function(arg) {
var body, j, len, resp, timestamp
resp = arg[0], body = arg[1], timestamp = arg[2]
for (i = j = 0, len = nodes.length; j < len; i = ++j) {
node = nodes[i]
node.nm = newNames[i]
node.lm = timestamp
}
}
})(this))
}
Workflowy.prototype.move = function(node, newParentid, priority) {
var operations = [
{
type: 'move',
data: {
projectid: node.id,
parentid: newParentid,
priority: priority
},
undo_data: {
previous_parentid: node.parentId,
previous_priority: node.prio
}
}
]
return this._update(operations)
.then(utils.httpAbove299toError)
.then((function(_this) {
return function(arg) {
// TODO: for a local workflowy client
// we'll want to update the local node
}
})(this))
}
return Workflowy
})()
// ---
// generated by coffee-script 1.9.2
// code adapted from https://github.com/ruxi/workflowy