-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopularPostHandler.js
More file actions
136 lines (131 loc) · 4.54 KB
/
popularPostHandler.js
File metadata and controls
136 lines (131 loc) · 4.54 KB
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
const User = require('./models/User');
const ImagePost = require('./models/ImagePost')
const PopularPosts = require('./models/PopularPosts')
const { generateTwoDigitDate } = require('./generateTwoDigitDate')
const hoursPerRefresh = 1;
function decidePopularPosts(callback) {
const now = new Date();
const last24hours = now.getTime() - (24 * 60 * 60 * 1000);
ImagePost.aggregate([
{
$lookup: {
from: 'upvotes',
let: { postId: '$_id' },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ['$postId', '$$postId'] },
{ $gte: ['$interactionDate', last24hours] }
]
}
}
}
],
as: 'upvotes'
}
},
{
$lookup: {
from: 'downvotes',
let: { postId: '$_id' },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ['$postId', '$$postId'] },
{ $gte: ['$interactionDate', last24hours] }
]
}
}
}
],
as: 'downvotes'
}
},
{
$addFields: {
upvotes: {
$size: '$upvotes'
},
downvotes: {
$size: '$downvotes'
},
score: {
$subtract: [
{$size: '$upvotes'},
{$size: '$downvotes'}
]
}
}
},
{
$sort: {
score: -1
}
},
{
$limit: 100
},
{
$project: {
upvotes: 0,
downvotes: 0,
score: 0
}
}
]).then(posts => {
console.log('Popular Posts:', posts)
callback(posts)
}).catch(error => {
console.error('An error occured whil getting popular posts')
console.error(error)
})
}
function popularPostHandler() {
try {
var dateNow = Date.now()
PopularPosts.findOne({}, {lastUpdated: 1}).then(popularPosts => {
if (popularPosts) {
var hoursDiff = Math.floor((dateNow-popularPosts.lastUpdated)/3600000)
console.log("hourDiff: " + hoursDiff)
if (hoursDiff >= hoursPerRefresh) {
decidePopularPosts(function(decidedPosts) {
PopularPosts.findOneAndUpdate({}, {lastUpdated: dateNow, popularPosts: decidedPosts}).then(() => {
console.log("Updated popular posts.");
return "SUCCESS"; // this might not return where I want it to
}).catch(err => {
console.log("Error while updating popular posts.");
console.log(err)
return "FAILED"; // this might not return where I want it to
})
})
}
} else {
console.log("Couldn't find popular posts?");
decidePopularPosts(function(decidedPosts) {
updatePosts = new PopularPosts({lastUpdated: dateNow, popularPosts: decidedPosts})
updatePosts.save().then(results => {
console.log("Updated popular posts.");
return "SUCCESS"; // this might not return where I want it to
}).catch(err => {
console.log("Error while updating popular posts.");
console.log(err)
return "FAILED"; // this might not return where I want it to
})
})
}
}).catch(err => {
console.log("Error finding popularPost document.");
console.log(err);
return "FAILED";
})
} catch (err) {
console.log("Error in popular post handler");
console.log(err);
return "FAILED";
}
}
exports.popularPostHandler = popularPostHandler;