-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
523 lines (422 loc) · 14.1 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
var path = require('path');
var async = require('async');
var mysql = require('mysql');
var moment = require('moment');
var _ = require('underscore');
var fs = require('fs-extra');
var noop = function(){};
var pkg = fs.readJsonSync(path.join(__dirname, '/package.json'));
var name = pkg.name.replace(/nodebb-plugin-import-/, '');
var logPrefix = '[' + pkg.name + ']';
(function(Exporter) {
Exporter.setup = function(config, callback) {
Exporter.log('setup');
// mysql db only config
// extract them from the configs passed by the nodebb-plugin-import adapter
var _config = {
host: config.dbhost || config.host || 'localhost',
user: config.dbuser || config.user || 'root',
password: config.dbpass || config.pass || config.password || '',
port: config.dbport || config.port || 3306,
database: config.dbname || config.name || config.database || name
};
Exporter.config(_config);
Exporter.config('prefix', config.prefix || config.tablePrefix || '');
Exporter.connection = mysql.createConnection(_config);
Exporter.connection.connect();
callback(null, Exporter.config());
};
// usually that's unecessary, but IP.Board's schema is a little different
var getGroups = function(config, callback) {
if (_.isFunction(config)) {
callback = config;
config = {};
}
callback = !_.isFunction(callback) ? noop : callback;
if (!Exporter.connection) {
Exporter.setup(config);
}
var prefix = Exporter.config('prefix');
var query = 'SELECT '
+ prefix + 'groups.g_id as _gid, '
+ prefix + 'groups.g_view_board as _notbanned, ' // not sure, just making an assumption
+ prefix + 'groups.g_post_new_topics as _pending, ' // not sure, just making an assumption
+ prefix + 'groups.g_open_close_posts as _administrator, ' // not sure, just making an assumption
+ prefix + 'groups.g_is_supmod as _moderator ' // not sure, just making an assumption
+ 'FROM ' + prefix + 'groups ';
Exporter.connection.query(query,
function(err, rows) {
if (err) {
Exporter.error(err);
return callback(err);
}
var map = {};
rows.forEach(function(row) {
map[row._gid] = row;
});
// keep a copy of the users in memory here
Exporter._groups = map;
callback(null, map);
});
};
Exporter.getUsers = function(callback) {
return Exporter.getPaginatedUsers(0, -1, callback);
};
Exporter.getPaginatedUsers = function(start, limit, callback) {
callback = !_.isFunction(callback) ? noop : callback;
if (!Exporter.connection) {
Exporter.setup(config);
}
var prefix = Exporter.config('prefix');
var startms = +new Date();
var query = 'SELECT '
+ prefix + 'members.member_id as _uid, '
+ prefix + 'members.name as _username, '
+ prefix + 'members.members_display_name as _alternativeUsername, '
+ prefix + 'members.email as _email, '
+ prefix + 'members.member_group_id as _gid, '
+ prefix + 'members.joined as _joindate, '
+ prefix + 'members.title as _badge, '
+ prefix + 'members.members_profile_views as _profileviews, '
+ prefix + 'members.hide_email as _showemail, '
+ prefix + 'members.member_banned as _banned, '
+ prefix + 'members.last_activity as _lastposttime, '
+ prefix + 'members.last_visit as _lastonline, '
+ 'CONCAT(' + prefix + 'members.bday_month, \'/\', ' + prefix + 'members.bday_day, \'/\', ' + prefix + 'members.bday_year)' + ' as _birthday '
+ 'FROM ' + prefix + 'members '
+ (start >= 0 && limit >= 0 ? 'LIMIT ' + start + ',' + limit : '');
// _banned and _level are determined below
getGroups(function(err, groups) {
Exporter.connection.query(query,
function(err, rows) {
if (err) {
Exporter.error(err);
return callback(err);
}
//normalize here
var map = {};
rows.forEach(function(row) {
row._joindate = ((row._joindate || 0) * 1000) || startms;
row._email = (row._email || '').toLowerCase();
row._birthday = moment(row._birthday, 'MM/DD/YYYY').isValid() ? row._birthday : '';
row._picture = Exporter.validateUrl(row._picture);
row._website = Exporter.validateUrl(row._website);
// let's check the group that this user belongs to and set the right privileges
row._level = groups[row._gid]._administrator > 0 && groups[row._gid]._moderator > 0 ? 'administrator' : groups[row._gid]._moderator > 0 ? 'moderator' : 'member';
row._banned = row._banned ? 1 : groups[row._gid]._notbanned > 0 ? 0 : 1;
row._showemail = !row._showemail;
row._lastposttime = row._lastposttime ? row._lastposttime * 1000 : null;
row._lastonline = row._lastonline ? row._lastonline * 1000 : null;
map[row._uid] = row;
});
callback(null, map);
});
});
};
Exporter.getCategories = function(callback) {
return Exporter.getPaginatedCategories(0, -1, callback);
};
Exporter.getPaginatedCategories = function(start, limit, callback) {
callback = !_.isFunction(callback) ? noop : callback;
if (!Exporter.connection) {
Exporter.setup(config);
}
var prefix = Exporter.config('prefix');
var startms = +new Date();
var query = 'SELECT '
+ prefix + 'forums.id as _cid, '
+ prefix + 'forums.name as _name, '
+ prefix + 'forums.description as _description, '
+ prefix + 'forums.position as _order, '
+ prefix + 'forums.parent_id as _parentCid '
+ 'FROM ' + prefix + 'forums '
+ (start >= 0 && limit >= 0 ? 'LIMIT ' + start + ',' + limit : '');
Exporter.connection.query(query,
function(err, rows) {
if (err) {
Exporter.error(err);
return callback(err);
}
//normalize here
var map = {};
rows.forEach(function(row) {
row._name = row._name || 'Untitled Category';
row._description = row._description || 'No decsciption available';
map[row._cid] = row;
});
callback(null, map);
});
};
Exporter.getTopics = function(callback) {
return Exporter.getPaginatedTopics(0, -1, callback);
};
Exporter.getPaginatedTopics = function(start, limit, callback) {
callback = !_.isFunction(callback) ? noop : callback;
if (!Exporter.connection) {
Exporter.setup(config);
}
var prefix = Exporter.config('prefix');
var startms = +new Date();
var query = 'SELECT '
+ prefix + 'topics.tid as _tid, '
+ prefix + 'topics.starter_id as _uid, '
+ prefix + 'topics.forum_id as _cid, '
+ prefix + 'posts.post as _content, '
+ prefix + 'topics.title as _title, '
+ prefix + 'topics.start_date as _timestamp, '
+ prefix + 'topics.views as _viewcount, '
+ prefix + 'topics.state as _state, '
+ prefix + 'topics.pinned as _pinned, '
+ prefix + 'posts.edit_time as _edited, '
+ prefix + 'posts.ip_address as _ip, '
+ prefix + 'topics.approved as _approved '
+ 'FROM ' + prefix + 'topics '
+ 'JOIN ' + prefix + 'posts ON ' + prefix + 'posts.pid = ' + prefix + 'topics.topic_firstpost '
+ (start >= 0 && limit >= 0 ? 'LIMIT ' + start + ',' + limit : '');
Exporter.connection.query(query,
function(err, rows) {
if (err) {
Exporter.error(err);
return callback(err);
}
//normalize here
var map = {};
rows.forEach(function(row) {
row._title = row._title ? row._title[0].toUpperCase() + row._title.substr(1) : 'Untitled';
row._timestamp = ((row._timestamp || 0) * 1000) || startms;
row._locked = row._state == "closed" ? 1 : 0;
delete row._state;
row._edited = row._edited ? row._edited * 1000 : null;
map[row._tid] = row;
});
callback(null, map);
});
};
Exporter.getPosts = function(callback) {
return Exporter.getPaginatedPosts(0, -1, callback);
};
Exporter.getPaginatedPosts = function(start, limit, callback) {
callback = !_.isFunction(callback) ? noop : callback;
if (!Exporter.connection) {
Exporter.setup(config);
}
var prefix = Exporter.config('prefix');
var startms = +new Date();
var query = 'SELECT '
+ prefix + 'posts.pid as _pid, '
+ prefix + 'posts.topic_id as _tid, '
+ prefix + 'posts.author_id as _uid, '
+ prefix + 'posts.author_name as _guest, '
+ prefix + 'posts.post as _content, '
+ prefix + 'posts.post_date as _timestamp, '
+ prefix + 'posts.ip_address as _ip, '
+ prefix + 'posts.post_parent as _toPid, '
+ prefix + 'posts.edit_time as _edited '
+ 'FROM ' + prefix + 'posts '
+ 'WHERE ' + prefix + 'posts.new_topic = 0 '
+ (start >= 0 && limit >= 0 ? 'LIMIT ' + start + ',' + limit : '');
Exporter.connection.query(query,
function(err, rows) {
if (err) {
Exporter.error(err);
return callback(err);
}
//normalize here
var map = {};
rows.forEach(function(row) {
row._content = row._content || '';
row._timestamp = ((row._timestamp || 0) * 1000) || startms;
row._edited = row._edited ? row._edited * 1000 : null;
map[row._pid] = row;
});
callback(null, map);
});
};
// todo: possible memory issues
function getConversations (callback) {
callback = !_.isFunction(callback) ? noop : callback;
if (!Exporter.connection) {
Exporter.setup(config);
}
var prefix = Exporter.config('prefix');
var startms = +new Date();
var query = 'SELECT '
+ prefix + 'message_topic_user_map.map_topic_id as _cvid, '
+ 'group_concat(' + prefix + 'message_topic_user_map.map_user_id separator \',\') as _uids '
+ 'FROM ' + prefix + 'message_topic_user_map GROUP BY ' + prefix + 'message_topic_user_map.map_topic_id'
var parse = function(v) { return parseInt(v, 10); };
Exporter.connection.query(query,
function(err, rows) {
if (err) {
Exporter.error(err);
return callback(err);
}
//normalize here
var map = {};
rows.forEach(function(row) {
if (!row._uids) {
return;
}
var _uids = row._uids.split(',').map(parse);
row._uids = {};
row._uids[_uids[0]] = _uids[1]
row._uids[_uids[1]] = _uids[0]
map[row._cvid] = row;
});
callback(null, map);
});
}
Exporter.getMessages = function(callback) {
return Exporter.getPaginatedMessages(0, -1, callback);
};
Exporter.getPaginatedMessages = function(start, limit, callback) {
callback = !_.isFunction(callback) ? noop : callback;
if (!Exporter.connection) {
Exporter.setup(config);
}
var prefix = Exporter.config('prefix');
var startms = +new Date();
var query = 'SELECT '
+ prefix + 'message_posts.msg_id as _mid, '
+ prefix + 'message_posts.msg_post as _content, '
+ prefix + 'message_posts.msg_author_id as _fromuid, '
+ prefix + 'message_posts.msg_topic_id as _cvid, '
+ prefix + 'message_posts.msg_date as _timestamp '
+ 'FROM ' + prefix + 'message_posts '
+ (start >= 0 && limit >= 0 ? 'LIMIT ' + start + ',' + limit : '');
var logged = 0;
getConversations(function(err, conversations) {
if (err) {
return callback(err);
}
Exporter.connection.query(query,
function(err, rows) {
if (err) {
Exporter.error(err);
return callback(err);
}
//normalize here
var map = {};
rows.forEach(function(row) {
var conversation = conversations[row._cvid];
if (!conversation) {
return;
}
row._touid = conversation._uids[row._fromuid];
if (!row._touid) {
return;
}
row._content = row._content || '';
row._timestamp = ((row._timestamp || 0) * 1000) || startms;
delete row._cvid;
map[row._mid] = row;
});
callback(null, map);
});
});
};
Exporter.teardown = function(callback) {
Exporter.log('teardown');
Exporter.connection.end();
Exporter.log('Done');
if (_.isFunction(callback)) {
callback();
}
};
Exporter.testrun = function(config, callback) {
async.series([
function(next) {
Exporter.setup(config, next);
},
function(next) {
Exporter.getUsers(next);
},
function(next) {
Exporter.getCategories(next);
},
function(next) {
Exporter.getTopics(next);
},
function(next) {
Exporter.getPosts(next);
},
function(next) {
Exporter.getMessages(next);
},
function(next) {
Exporter.teardown(next);
}
], callback);
};
Exporter.paginatedTestrun = function(config, callback) {
async.series([
function(next) {
Exporter.setup(config, next);
},
function(next) {
Exporter.getPaginatedUsers(0, 1000, next);
},
function(next) {
Exporter.getPaginatedCategories(0, 1000, next);
},
function(next) {
Exporter.getPaginatedTopics(0, 1000, next);
},
function(next) {
Exporter.getPaginatedPosts(1001, 2000, next);
},
function(next) {
Exporter.getPaginatedMessages(1001, 2000, next);
},
function(next) {
Exporter.teardown(next);
}
], callback);
};
Exporter.warn = function() {
var args = _.toArray(arguments);
args.unshift(logPrefix);
console.warn.apply(console, args);
};
Exporter.log = function() {
var args = _.toArray(arguments);
args.unshift(logPrefix);
console.log.apply(console, args);
};
Exporter.error = function() {
var args = _.toArray(arguments);
args.unshift(logPrefix);
console.error.apply(console, args);
};
Exporter.config = function(config, val) {
if (config != null) {
if (typeof config === 'object') {
Exporter._config = config;
} else if (typeof config === 'string') {
if (val != null) {
Exporter._config = Exporter._config || {};
Exporter._config[config] = val;
}
return Exporter._config[config];
}
}
return Exporter._config;
};
// from Angular https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L11
Exporter.validateUrl = function(url) {
var pattern = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/;
return url && url.length < 2083 && url.match(pattern) ? url : '';
};
Exporter.truncateStr = function(str, len) {
if (typeof str != 'string') return str;
len = _.isNumber(len) && len > 3 ? len : 20;
return str.length <= len ? str : str.substr(0, len - 3) + '...';
};
Exporter.whichIsFalsy = function(arr) {
for (var i = 0; i < arr.length; i++) {
if (!arr[i])
return i;
}
return null;
};
})(module.exports);