Skip to content

Commit

Permalink
Purge old recs on thread refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
am1t committed Jul 20, 2018
1 parent d19e08d commit 1fdd7c2
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 14 deletions.
2 changes: 1 addition & 1 deletion helper/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const remove_fetched = function(items, onComplete = null) {
items.forEach(item => {
let errors = [];

Post.count({'post_id': item.id}, function(error, count){
Post.countDocuments({'post_id': item.id}, function(error, count){
if(error){
errors.push("Failed to fetch thread " + error);
res.render('/', {
Expand Down
4 changes: 4 additions & 0 deletions models/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const PostSchema = new Schema({
post_id: {
type: String,
required: true
},
date:{
type: Date,
default: Date.now
}
});

Expand Down
6 changes: 5 additions & 1 deletion models/Recommendation.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ const RecommendationSchema = new Schema({
author:{
type: String,
required: true
}
},
date:{
type: Date,
default: Date.now
}
})

mongoose.model('recommendations', RecommendationSchema);
6 changes: 5 additions & 1 deletion models/Thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const ThreadSchema = new Schema({
type: String,
required: true
},
created_by: {
type: String,
required: false
},
discover: {
type: Boolean,
default: false,
Expand All @@ -22,7 +26,7 @@ const ThreadSchema = new Schema({
date:{
type: Date,
default: Date.now
}
}
})

mongoose.model('threads', ThreadSchema);
88 changes: 77 additions & 11 deletions routes/threads.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const { fetch_links, fetch_title, remove_fetched } = require('../helper/utils');
require('../models/Thread');
const Thread = mongoose.model('threads');

// Load Post Model
require('../models/Post');
const Post = mongoose.model('posts');

// Load Recommendation Model
require('../models/Recommendation');
const Recommendation = mongoose.model('recommendations');
Expand Down Expand Up @@ -80,8 +84,65 @@ router.get('/edit', ensureAuthenticated, (req, res) => {
})
});

const purge_posts = function(thread_id){
var date = new Date();
var daysToDeletion = 120;
var deletionDate = new Date(date.setDate(date.getDate() - daysToDeletion));

return new Promise((resolve, reject) => {
try {
Post.deleteMany({thread_id:thread_id, insertDate : {$lt : deletionDate}}, function(err){
if(err) reject(err);
else {
resolve("success");
}
});

} catch (error) {
reject(error);
}
});
}

const purge_recommendations = function(thread_id){
var date = new Date();
var daysToDeletion = 120;
var deletionDate = new Date(date.setDate(date.getDate() - daysToDeletion));

return new Promise((resolve, reject) => {
try {
Recommendation.deleteMany({thread_id:thread_id, insertDate : {$lt : deletionDate}}, function(err){
if(err) reject(err);
else {
resolve("success");
}
});

} catch (error) {
reject(error);
}
});
}

const refresh_thread_date = function(thread_id){
return new Promise((resolve, reject) => {
try {
Thread.findById(thread_id)
.then(thread => {
thread.date = Date.now();
thread.save()
.then(thread => {
resolve("success");
});
});
} catch (error) {
reject(error);
}
});
}

//Edit Threads Form for refresh
router.get('/refresh/:id', ensureAuthenticated, (req, res) => {
router.get('/refresh/:id', ensureAuthenticated, async (req, res) => {
try {
Thread.findById(req.params.id)
.sort({date: 'desc'})
Expand Down Expand Up @@ -109,14 +170,17 @@ router.get('/refresh/:id', ensureAuthenticated, (req, res) => {
errors: errors
})
} else {
remove_fetched(thread_items.items, function(items){
remove_fetched(thread_items.items, async function(items){
if(items.length == 0){
await Promise.all([purge_recommendations(thread.id)
, purge_posts(thread.id), refresh_thread_date(thread.id)]);
console.log("No items to be parsed for recommendations");
req.flash('info_msg', 'No new recommendations posted yet');
res.redirect('/micro/threads/' + thread.id + '/recommendations');
}
fetch_links(items, thread.id, function(recs){
var recommendations = [];
let is_error = false;
recs.forEach((link, linkIndex) => {
fetch_title(link.url, function(output){
var temp_title = '';
Expand All @@ -133,21 +197,23 @@ router.get('/refresh/:id', ensureAuthenticated, (req, res) => {
}

new Recommendation(newRecommendation)
.save()
.then(rec => {
.save(async function(err, rec){
if(err) {
is_error = true;
}
recommendations.push(rec);
if(recommendations.length == recs.length){
await Promise.all([purge_recommendations(thread.id)
, purge_posts(thread.id), refresh_thread_date(thread.id)]);

console.log("Parsed " + recs.length + " items successfully as recommendations");
req.flash('success_msg', 'Thread Refreshed Successfully');

if(is_error) req.flash('info_msg', 'Thread Refreshed Successfully, with some failures');
else req.flash('success_msg', 'Thread Refreshed Successfully');

res.redirect('/micro/threads/' + thread.id + '/recommendations');
}
})
.catch(function(error){
console.warn("Some links details cannot be fetched, possible reason - no title.");
req.flash('info_msg', 'Thread Refreshed Successfully, with some failures');
res.redirect('/micro/threads/' + thread.id + '/recommendations');
});;

});
});
});
Expand Down

0 comments on commit 1fdd7c2

Please sign in to comment.