@@ -116,6 +116,7 @@ let setTagFilter = function() {};
116
116
const TRACKERS_ALL = 1 ;
117
117
const TRACKERS_TRACKERLESS = 2 ;
118
118
119
+ /** @type Map<number, {host: string, trackerTorrentMap: Map<string, string[]>}> **/
119
120
const trackerList = new Map ( ) ;
120
121
121
122
let selectedTracker = LocalPreferences . get ( 'selected_tracker' , TRACKERS_ALL ) ;
@@ -623,11 +624,20 @@ window.addEventListener("DOMContentLoaded", function() {
623
624
624
625
// Sort trackers by hostname
625
626
const sortedList = [ ] ;
626
- trackerList . forEach ( ( tracker , hash ) => sortedList . push ( {
627
- trackerHost : getHost ( tracker . url ) ,
628
- trackerHash : hash ,
629
- trackerCount : tracker . torrents . length
630
- } ) ) ;
627
+ trackerList . forEach ( ( { host, trackerTorrentMap } , hash ) => {
628
+ const uniqueTorrents = new Set ( ) ;
629
+ for ( const torrents of trackerTorrentMap . values ( ) ) {
630
+ for ( const torrent of torrents ) {
631
+ uniqueTorrents . add ( torrent ) ;
632
+ }
633
+ }
634
+
635
+ sortedList . push ( {
636
+ trackerHost : host ,
637
+ trackerHash : hash ,
638
+ trackerCount : uniqueTorrents . size ,
639
+ } ) ;
640
+ } ) ;
631
641
sortedList . sort ( ( left , right ) => window . qBittorrent . Misc . naturalSortCollator . compare ( left . trackerHost , right . trackerHost ) ) ;
632
642
for ( const { trackerHost, trackerHash, trackerCount } of sortedList )
633
643
trackerFilterList . appendChild ( createLink ( trackerHash , ( trackerHost + ' (%1)' ) , trackerCount ) ) ;
@@ -760,40 +770,28 @@ window.addEventListener("DOMContentLoaded", function() {
760
770
updateTags = true ;
761
771
}
762
772
if ( response [ 'trackers' ] ) {
763
- for ( const tracker in response [ 'trackers' ] ) {
764
- const torrents = response [ 'trackers' ] [ tracker ] ;
765
- const hash = window . qBittorrent . Client . genHash ( getHost ( tracker ) ) ;
766
-
767
- // the reason why we need the merge here is because the WebUI api returned trackers may have different url for the same tracker host.
768
- // for example, some private trackers use diff urls for each torrent from the same tracker host.
769
- // then we got the response of `trackers` from qBittorrent api will like:
770
- // {
771
- // "trackers": {
772
- // "https://example.com/announce?passkey=identify_info1": ["hash1"],
773
- // "https://example.com/announce?passkey=identify_info2": ["hash2"],
774
- // "https://example.com/announce?passkey=identify_info3": ["hash3"]
775
- // }
776
- // }
777
- // after getHost(), those torrents all belongs to `example.com`
778
- let merged_torrents = torrents ;
779
- if ( trackerList . has ( hash ) ) {
780
- merged_torrents = trackerList . get ( hash ) . torrents . concat ( torrents ) ;
781
- // deduplicate is needed when the webui opens in multi tabs
782
- merged_torrents = merged_torrents . filter ( ( item , pos ) => merged_torrents . indexOf ( item ) === pos ) ;
773
+ for ( const [ tracker , torrents ] of Object . entries ( response [ 'trackers' ] ) ) {
774
+ const host = getHost ( tracker ) ;
775
+ const hash = window . qBittorrent . Client . genHash ( host ) ;
776
+
777
+ let trackerListItem = trackerList . get ( hash ) ;
778
+ if ( trackerListItem === undefined ) {
779
+ trackerListItem = { host : host , trackerTorrentMap : new Map ( ) } ;
780
+ trackerList . set ( hash , trackerListItem ) ;
783
781
}
784
782
785
- trackerList . set ( hash , {
786
- url : tracker ,
787
- torrents : merged_torrents
788
- } ) ;
783
+ trackerListItem . trackerTorrentMap . set ( tracker , [ ...torrents ] ) ;
789
784
}
790
785
updateTrackers = true ;
791
786
}
792
787
if ( response [ 'trackers_removed' ] ) {
793
788
for ( let i = 0 ; i < response [ 'trackers_removed' ] . length ; ++ i ) {
794
789
const tracker = response [ 'trackers_removed' ] [ i ] ;
795
790
const hash = window . qBittorrent . Client . genHash ( getHost ( tracker ) ) ;
796
- trackerList . delete ( hash ) ;
791
+ const trackerListEntry = trackerList . get ( hash ) ;
792
+ if ( trackerListEntry ) {
793
+ trackerListEntry . trackerTorrentMap . delete ( tracker ) ;
794
+ }
797
795
}
798
796
updateTrackers = true ;
799
797
}
0 commit comments