feat(torrent): remove all complete warning of torrent when the torrent H&R status to false and when the torrent be deleted

This commit is contained in:
OldHawk
2017-09-08 12:03:53 +08:00
parent 2703117331
commit aa8a8a70ac
2 changed files with 46 additions and 4 deletions

View File

@@ -14,6 +14,7 @@ var path = require('path'),
Subtitle = mongoose.model('Subtitle'),
Thumb = mongoose.model('Thumb'),
Torrent = mongoose.model('Torrent'),
Complete = mongoose.model('Complete'),
Forum = mongoose.model('Forum'),
Topic = mongoose.model('Topic'),
fs = require('fs'),
@@ -503,13 +504,33 @@ exports.toggleHnRStatus = function (req, res) {
});
} else {
res.json(torrent);
//remove the complete data and update user`s warning number when the H&R prop to false
removeTorrentHnRWarning(torrent._id);
}
});
//TODO: remove the complete data and update user`s warning number when the H&R prop to false
};
/**
* removeTorrentHnRWarning
* @param torrent
*/
function removeTorrentHnRWarning(tid) {
Complete.find({
torrent: tid
})
.populate('user')
.exec(function (err, cs) {
if (!err && cs) {
cs.forEach(function (c) {
if (c.hnr_warning) {
c.removeHnRWarning(c.user);
}
});
}
});
}
/**
* thumbsUp
* @param req
@@ -733,7 +754,11 @@ exports.delete = function (req, res) {
torrent: torrent._id
});
//TODO: remove the complete data and update user`s warning number if the torrent has H&R prop
//remove the complete data and update user`s warning number if the torrent has H&R prop
removeTorrentHnRWarning(torrent._id);
Complete.remove({
torrent: torrent._id
});
torrent.remove(function (err) {
if (err) {

View File

@@ -135,6 +135,23 @@ CompleteSchema.methods.countHnRWarning = function (u) {
}
};
/**
* removeHnRWarning
* remove H&R warning
*/
CompleteSchema.methods.removeHnRWarning = function (u) {
if (this.hnr_warning) {
this.update({
$set: {hnr_warning: false}
}).exec();
//update user warning numbers
u.update({
$inc: {hnr_warning: -1}
}).exec();
}
};
CompleteSchema.index({user: -1, createdAt: -1});
CompleteSchema.index({torrent: 1, createdAt: -1});