mirror of
https://github.com/taobataoma/meanTorrent.git
synced 2026-05-06 10:26:05 +02:00
opt(announce): optimization announce data
This commit is contained in:
@@ -269,6 +269,7 @@ exports.announce = function (req, res) {
|
||||
done(174);
|
||||
} else {
|
||||
req.torrent = t;
|
||||
t.updateSeedLeechNumbers();
|
||||
done(null);
|
||||
}
|
||||
});
|
||||
@@ -988,6 +989,7 @@ exports.userByPasskey = function (req, res, next, pk) {
|
||||
req.passkeyuser.status = 'idle';
|
||||
}
|
||||
|
||||
u.updateSeedLeechNumbers();
|
||||
} else {
|
||||
req.passkeyuser = undefined;
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ exports.getMySeeding = function (req, res) {
|
||||
Peer.find({
|
||||
user: req.user._id,
|
||||
peer_status: PEERSTATE_SEEDER,
|
||||
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval * 2}
|
||||
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval - 60 * 1000}
|
||||
}).sort('-peer_uploaded')
|
||||
.populate({
|
||||
path: 'torrent',
|
||||
@@ -58,7 +58,7 @@ exports.getMyDownloading = function (req, res) {
|
||||
Peer.find({
|
||||
user: req.user._id,
|
||||
peer_status: PEERSTATE_LEECHER,
|
||||
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval * 2}
|
||||
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval - 60 * 1000}
|
||||
}).sort('-peer_downloaded')
|
||||
.populate({
|
||||
path: 'torrent',
|
||||
|
||||
@@ -1785,7 +1785,7 @@ exports.getSeederUsers = function (req, res) {
|
||||
Peer.count({
|
||||
torrent: req.torrent._id,
|
||||
peer_status: PEERSTATE_SEEDER,
|
||||
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval * 2}
|
||||
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval - 60 * 1000}
|
||||
}, function (err, count) {
|
||||
if (err) {
|
||||
callback(err, null);
|
||||
@@ -1800,7 +1800,7 @@ exports.getSeederUsers = function (req, res) {
|
||||
Peer.find({
|
||||
torrent: req.torrent._id,
|
||||
peer_status: PEERSTATE_SEEDER,
|
||||
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval * 2}
|
||||
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval - 60 * 1000}
|
||||
})
|
||||
.sort('-peer_uploaded')
|
||||
.populate('user', 'username displayName profileImageURL isVip')
|
||||
@@ -1846,7 +1846,7 @@ exports.getLeecherUsers = function (req, res) {
|
||||
Peer.count({
|
||||
torrent: req.torrent._id,
|
||||
peer_status: PEERSTATE_LEECHER,
|
||||
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval * 2}
|
||||
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval - 60 * 1000}
|
||||
}, function (err, count) {
|
||||
if (err) {
|
||||
callback(err, null);
|
||||
@@ -1861,7 +1861,7 @@ exports.getLeecherUsers = function (req, res) {
|
||||
Peer.find({
|
||||
torrent: req.torrent._id,
|
||||
peer_status: PEERSTATE_LEECHER,
|
||||
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval * 2}
|
||||
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval - 60 * 1000}
|
||||
})
|
||||
.sort('-peer_uploaded')
|
||||
.populate('user', 'username displayName profileImageURL isVip')
|
||||
|
||||
@@ -4,8 +4,16 @@
|
||||
* Module dependencies
|
||||
*/
|
||||
var mongoose = require('mongoose'),
|
||||
path = require('path'),
|
||||
config = require(path.resolve('./config/config')),
|
||||
Peer = mongoose.model('Peer'),
|
||||
Schema = mongoose.Schema;
|
||||
|
||||
var announceConfig = config.meanTorrentConfig.announce;
|
||||
|
||||
const PEERSTATE_SEEDER = 'seeder';
|
||||
const PEERSTATE_LEECHER = 'leecher';
|
||||
|
||||
/**
|
||||
* Sub Comment Schema
|
||||
*/
|
||||
@@ -232,6 +240,47 @@ function writeIsSaling(torrent) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* updateSeedLeechNumbers
|
||||
*/
|
||||
TorrentSchema.methods.updateSeedLeechNumbers = function () {
|
||||
var torrent = this;
|
||||
|
||||
Peer.aggregate({
|
||||
$match: {
|
||||
torrent: torrent._id,
|
||||
last_announce_at: {$gt: new Date(Date.now() - announceConfig.announceInterval - 60 * 1000)}
|
||||
}
|
||||
}, {
|
||||
$group: {
|
||||
_id: '$peer_status',
|
||||
count: {$sum: 1}
|
||||
}
|
||||
}).exec(function (err, counts) {
|
||||
if (!err) {
|
||||
var sc = 0;
|
||||
var lc = 0;
|
||||
counts.forEach(function (c) {
|
||||
switch (c._id) {
|
||||
case PEERSTATE_SEEDER:
|
||||
sc = c.count;
|
||||
break;
|
||||
case PEERSTATE_LEECHER:
|
||||
lc = c.count;
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
torrent.update({
|
||||
$set: {
|
||||
torrent_seeds: sc,
|
||||
torrent_leechers: lc
|
||||
}
|
||||
}).exec();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* globalUpdateMethod
|
||||
*/
|
||||
@@ -261,7 +310,8 @@ TorrentSchema.index({
|
||||
torrent_recommended: 1,
|
||||
orderedat: -1,
|
||||
createdat: -1,
|
||||
'_peers.id': 1});
|
||||
'_peers.id': 1
|
||||
});
|
||||
|
||||
TorrentSchema.index({
|
||||
user: 1,
|
||||
|
||||
@@ -515,7 +515,7 @@ exports.getUserSeeding = function (req, res) {
|
||||
Peer.find({
|
||||
user: req.model._id,
|
||||
peer_status: PEERSTATE_SEEDER,
|
||||
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval * 2}
|
||||
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval - 60 * 1000}
|
||||
}).sort('-peer_uploaded')
|
||||
.populate({
|
||||
path: 'torrent',
|
||||
@@ -544,7 +544,7 @@ exports.getUserLeeching = function (req, res) {
|
||||
Peer.find({
|
||||
user: req.model._id,
|
||||
peer_status: PEERSTATE_LEECHER,
|
||||
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval * 2}
|
||||
last_announce_at: {$gt: Date.now() - announceConfig.announceInterval - 60 * 1000}
|
||||
}).sort('-peer_downloaded')
|
||||
.populate({
|
||||
path: 'torrent',
|
||||
|
||||
@@ -12,10 +12,15 @@ var mongoose = require('mongoose'),
|
||||
generatePassword = require('generate-password'),
|
||||
owasp = require('owasp-password-strength-test'),
|
||||
moment = require('moment'),
|
||||
Peer = mongoose.model('Peer'),
|
||||
chalk = require('chalk');
|
||||
|
||||
owasp.config(config.shared.owasp);
|
||||
|
||||
var announceConfig = config.meanTorrentConfig.announce;
|
||||
|
||||
const PEERSTATE_SEEDER = 'seeder';
|
||||
const PEERSTATE_LEECHER = 'leecher';
|
||||
|
||||
/**
|
||||
* A Validation function for local strategy properties
|
||||
@@ -383,6 +388,47 @@ UserSchema.methods.globalUpdateMethod = function () {
|
||||
this.save();
|
||||
};
|
||||
|
||||
/**
|
||||
* updateSeedLeechNumbers
|
||||
*/
|
||||
UserSchema.methods.updateSeedLeechNumbers = function () {
|
||||
var user = this;
|
||||
|
||||
Peer.aggregate({
|
||||
$match: {
|
||||
user: user._id,
|
||||
last_announce_at: {$gt: new Date(Date.now() - announceConfig.announceInterval - 60 * 1000)}
|
||||
}
|
||||
}, {
|
||||
$group: {
|
||||
_id: '$peer_status',
|
||||
count: {$sum: 1}
|
||||
}
|
||||
}).exec(function (err, counts) {
|
||||
if (!err) {
|
||||
var sc = 0;
|
||||
var lc = 0;
|
||||
counts.forEach(function (c) {
|
||||
switch (c._id) {
|
||||
case PEERSTATE_SEEDER:
|
||||
sc = c.count;
|
||||
break;
|
||||
case PEERSTATE_LEECHER:
|
||||
lc = c.count;
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
user.update({
|
||||
$set: {
|
||||
seeded: sc,
|
||||
leeched: lc
|
||||
}
|
||||
}).exec();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* update user last client_agent
|
||||
* @param ip
|
||||
|
||||
Reference in New Issue
Block a user