-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathupdater-posts.js
88 lines (77 loc) · 3.37 KB
/
updater-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
import Post from './server/models/post.model';
import config from './config/config';
import steemApi from './server/steemAPI';
const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
mongoose.connect(`${config.mongo.host}`);
const conn = mongoose.connection;
conn.once('open', function ()
{
// updating only posts created in last 15 days
const activeSince = new Date((new Date().getTime() - (15 * 24 * 60 * 60 * 1000)));
const query = {
created:
{
$gte: activeSince.toISOString()
}
};
Post
.countAll({ query })
.then(count => {
if (count === 0) {
console.log(`NO POSTS TO UPDATE. ENDED.`);
process.exit(0);
} else {
console.log(`${count} ACTIVE POSTS. CHECKING AND UPDATING`);
}
Post
.list({ skip: 0, limit: count, query })
.then(posts => {
if(posts.length > 0) {
posts.forEach((post, index) => {
steemApi.getContent(post.author, post.permlink, (err, updatedPost) => {
if (!err) {
console.log(`---- NOW CHECKING POST ${post.permlink} by ${post.author} ----\n`);
updatedPost.json_metadata = JSON.parse(updatedPost.json_metadata);
// @UTOPIAN @TODO bad patches. Needs to have a specific place where the put the utopian data so it does not get overwritten
if (!updatedPost.json_metadata.type && post.json_metadata.type) {
updatedPost.json_metadata.type = post.json_metadata.type;
}
if (updatedPost.json_metadata.app !== 'utopian/1.0.0') updatedPost.json_metadata.app = 'utopian/1.0.0';
if (updatedPost.json_metadata.community !== 'utopian') updatedPost.json_metadata.community = 'utopian';
// making sure the repository does not get deleted
if (!updatedPost.json_metadata.repository) updatedPost.json_metadata.repository = post.json_metadata.repository;
if (!updatedPost.json_metadata.platform) updatedPost.json_metadata.platform = post.json_metadata.platform;
for (var prop in updatedPost) {
if (updatedPost[prop] !== post[prop]) {
post[prop] = updatedPost[prop];
console.log(`UPDATED PROP ${prop} was ${JSON.stringify(post[prop])} now is ${JSON.stringify(updatedPost[prop])}\n`);
}
}
post.save()
.then(() => console.log(`POST UPDATED SUCCESSFULLY\n`))
.catch(e => {
console.log(`ERROR UPDATING POST ${e}\n`);
next(e);
})
.finally(() => {
if ((index + 1) === count) {
conn.close();
process.exit(0);
}
})
} else {
console.log(`CANNOT RETRIEVE POST - STEEM ERROR ${err}\n`);
if ((index + 1) === count) {
conn.close();
process.exit(0);
}
}
});
});
}
})
.catch(e => console.log(`CANNOT RETRIEVE POSTS FROM MONGO ${e}\n`));
})
.catch(e => console.log(`CANNOT COUNT ACTIVE POSTS IN MONGO ${e}\n`));
});