@@ -656,31 +656,35 @@ Server.prototype._onRequest = function (params, cb) {
656656
657657Server . prototype . _onAnnounce = function ( params , cb ) {
658658 var self = this
659+
660+ if ( self . _filter ) {
661+ self . _filter ( params . info_hash , params , function ( err ) {
662+ // Presence of `err` means that this announce request is disallowed
663+ if ( err ) return cb ( err )
659664
660- self . getSwarm ( params . info_hash , function ( err , swarm ) {
661- if ( err ) return cb ( err )
662-
663- if ( self . _filter ) {
664- self . _filter ( params . info_hash , params , function ( err ) {
665- // Precense of err means that this torrent or user is disallowd
666- if ( err ) cb ( err )
667- else {
668- if ( swarm ) announce ( swarm )
669- else createSwarm ( )
670- }
665+ getOrCreateSwarm ( function ( err , swarm ) {
666+ if ( err ) return cb ( err )
667+ announce ( swarm )
671668 } )
672- } else {
673- if ( swarm ) announce ( swarm )
674- else createSwarm ( )
675- }
676- } )
677-
678- function createSwarm ( ) {
679- self . createSwarm ( params . info_hash , function ( err , swarm ) {
669+ } )
670+ } else {
671+ getOrCreateSwarm ( function ( err , swarm ) {
680672 if ( err ) return cb ( err )
681673 announce ( swarm )
682674 } )
683675 }
676+
677+ // Get existing swarm, or create one if one does not exist
678+ function getOrCreateSwarm ( cb ) {
679+ self . getSwarm ( params . info_hash , function ( err , swarm ) {
680+ if ( err ) return cb ( err )
681+ if ( swarm ) return cb ( null , swarm )
682+ self . createSwarm ( params . info_hash , function ( err , swarm ) {
683+ if ( err ) return cb ( err )
684+ cb ( null , swarm )
685+ } )
686+ } )
687+ }
684688
685689 function announce ( swarm ) {
686690 if ( ! params . event || params . event === 'empty' ) params . event = 'update'
0 commit comments