Skip to content

Commit

Permalink
Merge pull request ajnart#1534 from InterN0te/master-showActiveTorrent
Browse files Browse the repository at this point in the history
Add an option to show active torrents when completed torrents are hidden in Torrent Queue widget
  • Loading branch information
SeDemal authored Nov 6, 2023
2 parents f4a4b3c + 390d4e7 commit 5dcdeab
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 19 deletions.
6 changes: 6 additions & 0 deletions public/locales/en/modules/torrents-status.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"displayCompletedTorrents": {
"label": "Display completed torrents"
},
"displayActiveTorrents": {
"label": "Display active torrents"
},
"speedLimitOfActiveTorrents": {
"label": "Upload speed to consider a torrent as active (kB/s)"
},
"displayStaleTorrents": {
"label": "Display stale torrents"
},
Expand Down
88 changes: 70 additions & 18 deletions src/widgets/torrent/TorrentTile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ describe('TorrentTile', () => {
labelFilter: [],
labelFilterIsWhitelist: false,
displayCompletedTorrents: true,
displayActiveTorrents: true,
speedLimitOfActiveTorrents: 10,
displayStaleTorrents: false,
},
};
const torrents: NormalizedTorrent[] = [
constructTorrent('ABC', 'Nice Torrent', false, 672),
constructTorrent('HH', 'I am completed', true, 0),
constructTorrent('HH', 'I am stale', false, 0),
constructTorrent('ABC', 'Nice Torrent', false, 672, 672),
constructTorrent('HH', 'I am completed', true, 0, 0),
constructTorrent('HH', 'I am stale', false, 0, 0),
];

// act
Expand Down Expand Up @@ -55,13 +57,15 @@ describe('TorrentTile', () => {
labelFilter: [],
labelFilterIsWhitelist: false,
displayCompletedTorrents: true,
displayActiveTorrents: true,
speedLimitOfActiveTorrents: 10,
displayStaleTorrents: true,
},
};
const torrents: NormalizedTorrent[] = [
constructTorrent('ABC', 'Nice Torrent', false, 672),
constructTorrent('HH', 'I am completed', true, 0),
constructTorrent('HH', 'I am stale', false, 0),
constructTorrent('ABC', 'Nice Torrent', false, 672, 672),
constructTorrent('HH', 'I am completed', true, 0, 0),
constructTorrent('HH', 'I am stale', false, 0, 0),
];

// act
Expand All @@ -74,7 +78,7 @@ describe('TorrentTile', () => {
expect(filtered.includes(torrents[2])).toBe(true);
});

it('filter when completed', () => {
it('filter when completed without active torrent', () => {
// arrange
const widget: ITorrent = {
id: 'abc',
Expand All @@ -90,13 +94,15 @@ describe('TorrentTile', () => {
labelFilter: [],
labelFilterIsWhitelist: false,
displayCompletedTorrents: false,
displayActiveTorrents: false,
speedLimitOfActiveTorrents: 10,
displayStaleTorrents: true,
},
};
const torrents: NormalizedTorrent[] = [
constructTorrent('ABC', 'Nice Torrent', false, 672),
constructTorrent('HH', 'I am completed', true, 0),
constructTorrent('HH', 'I am stale', false, 0),
constructTorrent('ABC', 'Nice Torrent', false, 672, 672),
constructTorrent('HH', 'I am completed', true, 0, 672),
constructTorrent('HH', 'I am stale', false, 0, 0),
];

// act
Expand All @@ -109,6 +115,47 @@ describe('TorrentTile', () => {
expect(filtered.at(1)).toBe(torrents[2]);
});

it('filter when completed with active torrent', () => {
// arrange
const widget: ITorrent = {
id: 'abc',
area: {
type: 'sidebar',
properties: {
location: 'left',
},
},
shape: {},
type: 'torrents-status',
properties: {
labelFilter: [],
labelFilterIsWhitelist: false,
displayCompletedTorrents: false,
displayActiveTorrents: true,
speedLimitOfActiveTorrents: 10,
displayStaleTorrents: true,
},
};
const torrents: NormalizedTorrent[] = [
constructTorrent('ABC', 'Nice Torrent', false, 672, 672),
constructTorrent('HH', 'I am completed and uploading less than 10 ko/s (10239 ≈ 9.99ko/s)', true, 0, 10239),
constructTorrent('HH', 'I am completed and uploading more than 10 ko/s (10241 ≈ 10.01ko/s)', true, 0, 10241),
constructTorrent('HH', 'I am completed', true, 0, 0),
constructTorrent('HH', 'I am stale', false, 0, 0),
];

// act
const filtered = filterTorrents(widget, torrents);

// assert
expect(filtered.length).toBe(3);
expect(filtered.at(0)).toBe(torrents[0]);
expect(filtered.includes(torrents[1])).toBe(false);
expect(filtered.at(1)).toBe(torrents[2]);
expect(filtered.includes(torrents[3])).toBe(false);
expect(filtered.at(2)).toBe(torrents[4]);
});

it('filter by label when whitelist', () => {
// arrange
const widget: ITorrent = {
Expand All @@ -125,13 +172,15 @@ describe('TorrentTile', () => {
labelFilter: ['music', 'movie'],
labelFilterIsWhitelist: true,
displayCompletedTorrents: true,
displayActiveTorrents: true,
speedLimitOfActiveTorrents: 10,
displayStaleTorrents: true,
},
};
const torrents: NormalizedTorrent[] = [
constructTorrent('1', 'A sick drop', false, 672, 'music'),
constructTorrent('2', 'I cried', true, 0, 'movie'),
constructTorrent('3', 'Great Animations', false, 0, 'anime'),
constructTorrent('1', 'A sick drop', false, 672, 672, 'music'),
constructTorrent('2', 'I cried', true, 0, 0, 'movie'),
constructTorrent('3', 'Great Animations', false, 0, 0, 'anime'),
];

// act
Expand Down Expand Up @@ -160,13 +209,15 @@ describe('TorrentTile', () => {
labelFilter: ['music', 'movie'],
labelFilterIsWhitelist: false,
displayCompletedTorrents: false,
displayActiveTorrents: false,
speedLimitOfActiveTorrents: 10,
displayStaleTorrents: true,
},
};
const torrents: NormalizedTorrent[] = [
constructTorrent('ABC', 'Nice Torrent', false, 672, 'anime'),
constructTorrent('HH', 'I am completed', true, 0, 'movie'),
constructTorrent('HH', 'I am stale', false, 0, 'tv'),
constructTorrent('ABC', 'Nice Torrent', false, 672, 672, 'anime'),
constructTorrent('HH', 'I am completed', true, 0, 0, 'movie'),
constructTorrent('HH', 'I am stale', false, 0, 0, 'tv'),
];

// act
Expand All @@ -184,7 +235,8 @@ const constructTorrent = (
id: string,
name: string,
isCompleted: boolean,
downloadSpeed: number,
downloadSpeed: number, // Bytes per second in @ctrl/shared-torrent
uploadSpeed: number, // Bytes per second in @ctrl/shared-torrent
label?: string
): NormalizedTorrent => ({
id,
Expand All @@ -208,6 +260,6 @@ const constructTorrent = (
totalSize: 839539535,
totalSelected: 0,
totalUploaded: 378535535,
uploadSpeed: 8349,
uploadSpeed,
label,
});
10 changes: 9 additions & 1 deletion src/widgets/torrent/TorrentTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ const definition = defineWidget({
type: 'switch',
defaultValue: true,
},
displayActiveTorrents: {
type: 'switch',
defaultValue: true,
},
speedLimitOfActiveTorrents: { // Unit : kB/s
type: 'number',
defaultValue: 10,
},
displayStaleTorrents: {
type: 'switch',
defaultValue: true,
Expand Down Expand Up @@ -194,7 +202,7 @@ function TorrentTile({ widget }: TorrentTileProps) {
export const filterTorrents = (widget: ITorrent, torrents: NormalizedTorrent[]) => {
let result = torrents;
if (!widget.properties.displayCompletedTorrents) {
result = result.filter((torrent) => !torrent.isCompleted);
result = result.filter((torrent) => !torrent.isCompleted || (widget.properties.displayActiveTorrents && torrent.uploadSpeed > widget.properties.speedLimitOfActiveTorrents * 1024));
}

if (widget.properties.labelFilter.length > 0) {
Expand Down

0 comments on commit 5dcdeab

Please sign in to comment.