-
Notifications
You must be signed in to change notification settings - Fork 0
/
posts.js
100 lines (87 loc) · 2.66 KB
/
posts.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
const dsteem = require('dsteem');
const rpc_node = 'https://gtg.steem.house:8090';
const client = new dsteem.Client(rpc_node);
function loadPosts(account, callback) {
return getPosts(account, -1, [], new Set(), callback);
}
function getPosts(account, start, posts, visitedPermlinks, callback) {
let last_trans = start;
console.log(`Fetching account history for ${account} at ${start}`);
client.database.call('get_account_history', [account, start, (start < 0) ? 10000 : Math.min(start, 10000)]).then((result) => {
let hasError = false;
if (!hasError) {
result.reverse();
result.forEach(function(trans) {
var op = trans[1].op;
if(op[0] == 'comment') {
/*
if (posts.length <= 2) {
console.log(trans);
console.log(op);
console.log(op[1]);
console.log(trans[1].timestamp);
console.log(JSON.parse(op[1].json_metadata).tags);
}
*/
const post = op[1];
if (post.parent_author === '' && post.author === account && post.title) {
if (!visitedPermlinks.has(post.permlink)) {
visitedPermlinks.add(post.permlink);
const jsonMetadata = JSON.parse(post.json_metadata);
//console.log(post);
posts.push({
author: account,
timestamp: trans[1].timestamp,
title: post.title,
permlink: post.permlink,
tags: jsonMetadata.tags,
});
}
}
}
// Save the ID of the last transaction that was processed.
last_trans = trans[0];
});
if(last_trans > 0 && last_trans != start) {
return getPosts(account, last_trans, posts, visitedPermlinks, callback);
} else {
if (last_trans > 0) {
console.log('Missing account history.... last trans: ' + last_trans);
hasError = true;
}
}
}
if (hasError) {
// add indication that loading failed
posts.unshift({
author: account,
timestamp: 'Error while loading',
title: '',
permlink: '',
tags: '',
});
}
callback(posts.reverse());
}, (err) => {
let hasError = false;
if (err) {
console.log('error');
console.log(err);
hasError = true;
}
if (hasError) {
// add indication that loading failed
posts.unshift({
author: account,
timestamp: 'Error while loading',
title: '',
permlink: '',
tags: '',
});
}
callback(posts.reverse());
});
}
module.exports = {
loadPosts: loadPosts,
};