Revision 326439363933 () - Diff

Link to this snippet: https://friendpaste.com/3uiUREvtzHp6O02qe9p7Qn
Embed:
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
/* Map function */
function(doc) { if (doc.kind == "comment") { emit(doc.link_id, doc); }}

/* Rereduce function */
function (keys, values, rereduce) {
var idx_comments = {};
var comments = [];

if (rereduce) {
return values;
} else {
for (var i=0; i<values.length; i++) {
if (values[i]) {
log({"_id": values[i]["_id"]});
idx_comments[values[i]['_id']] = values[i];
if (!values[i]['parents']) {
comments=comments.concat(values[i]);
}
}
}
log("bouh!");

function iter_comments(lcomments, idx) {
for (var i=0; i<lcomments.length; i++) {
var comment = lcomments[i] || [];
var thread=[];
if (comment['children'] && comment['children'].length > 0) {
for (var j=0; j<comment['children'].length; j++) {
if (!idx[comment['children'][j]]) {
log({"don't exist?": comment['children'][j]});
}
thread = thread.concat(idx[comment['children'][j]]);
}
comment['thread'] = iter_comments(thread, idx);
}
lcomments[i] = comment;
}
return lcomments;
}
tmp = iter_comments(comments, idx_comments);
return tmp;
}
}