mirror of
https://github.com/logos-storage/bittorrent-tracker.git
synced 2026-01-05 22:43:08 +00:00
server: Improve style of announce/filter logic
- Filter before potentially creating a swarm that is not needed - Unify getSwarm()/createSwarm() into a getOrCreateSwarm() function
This commit is contained in:
parent
c98c40abe5
commit
65c02dd153
38
server.js
38
server.js
@ -657,29 +657,33 @@ Server.prototype._onRequest = function (params, cb) {
|
||||
Server.prototype._onAnnounce = function (params, cb) {
|
||||
var self = this
|
||||
|
||||
self.getSwarm(params.info_hash, function (err, swarm) {
|
||||
if (err) return cb(err)
|
||||
|
||||
if (self._filter) {
|
||||
self._filter(params.info_hash, params, function (err) {
|
||||
// Precense of err means that this torrent or user is disallowd
|
||||
if (err) cb(err)
|
||||
else {
|
||||
if (swarm) announce(swarm)
|
||||
else createSwarm()
|
||||
}
|
||||
})
|
||||
} else {
|
||||
if (swarm) announce(swarm)
|
||||
else createSwarm()
|
||||
}
|
||||
})
|
||||
// Presence of `err` means that this announce request is disallowed
|
||||
if (err) return cb(err)
|
||||
|
||||
function createSwarm () {
|
||||
self.createSwarm(params.info_hash, function (err, swarm) {
|
||||
getOrCreateSwarm(function (err, swarm) {
|
||||
if (err) return cb(err)
|
||||
announce(swarm)
|
||||
})
|
||||
})
|
||||
} else {
|
||||
getOrCreateSwarm(function (err, swarm) {
|
||||
if (err) return cb(err)
|
||||
announce(swarm)
|
||||
})
|
||||
}
|
||||
|
||||
// Get existing swarm, or create one if one does not exist
|
||||
function getOrCreateSwarm (cb) {
|
||||
self.getSwarm(params.info_hash, function (err, swarm) {
|
||||
if (err) return cb(err)
|
||||
if (swarm) return cb(null, swarm)
|
||||
self.createSwarm(params.info_hash, function (err, swarm) {
|
||||
if (err) return cb(err)
|
||||
cb(null, swarm)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function announce (swarm) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user