From 277747d0908fe9e926a3c5ecb86b80c234ab9414 Mon Sep 17 00:00:00 2001 From: Jakob Breivik Grimstveit Date: Sun, 22 Nov 2026 15:17:14 +0100 Subject: [PATCH 1/2] mobile: remember selected filters across reloads In the mobile view, sorting, theme, and accent color persist across reloads via webui.mobile.* settings, but selected filters were only held in memory. When clicking the reload button (or refreshing the page), active filters were lost and the list reverted to displaying all torrents. Save active filters under webui.mobile.filters when committed in filterDone, and restore them at startup via plugin.storedFilters. Validate and sanitize stored filters so unknown status options and non-string values are ignored. Guard applyFilters from dropping filters before torrents are loaded, and ensure torrent labels are registered even if the desktop category panel has not yet populated on initial paint. Includes unit test coverage for storedFilters, filterDone, and applyFilters. Assisted-by: AI (Google DeepMind / Antigravity) --- plugins/mobile/init.js | 60 ++++++++++-- tests/plugins/mobile/settings.spec.js | 135 ++++++++++++++++++++++++++ 2 files changed, 189 insertions(+), 6 deletions(-) diff --git a/plugins/mobile/init.js b/plugins/mobile/init.js index 942c5db9b..365ebc85e 100644 --- a/plugins/mobile/init.js +++ b/plugins/mobile/init.js @@ -383,12 +383,14 @@ $.each(plugin.statusFilterOptions, function(i, opt) { plugin.applyFilters = function() { // Drop filters whose label/tracker no longer exists in the torrent list // (label/tracker ids are persistent, so check the current lists instead) - this.filters.label = $.grep(this.filters.label, function(name) { - return $.grep(plugin.labelList, function(o) {return o.name === name;}).length > 0; - }); - this.filters.tracker = $.grep(this.filters.tracker, function(name) { - return $.grep(plugin.trackerList, function(o) {return o.name === name;}).length > 0; - }); + if (this.torrents) { + this.filters.label = $.grep(this.filters.label, function(name) { + return $.grep(plugin.labelList, function(o) {return o.name === name;}).length > 0; + }); + this.filters.tracker = $.grep(this.filters.tracker, function(name) { + return $.grep(plugin.trackerList, function(o) {return o.name === name;}).length > 0; + }); + } // Selections within a category are ORed, categories are ANDed // (like the desktop panels), so the selector is the cartesian @@ -544,6 +546,7 @@ plugin.filtersEqual = function(a, b) { plugin.filterDone = function() { if (this.filtersSnapshot && !this.filtersEqual(this.filters, this.filtersSnapshot)) { this.scrollTop = 0; + this.storeSetting('filters', JSON.stringify(this.filters)); } this.filtersSnapshot = null; this.showList(); @@ -645,6 +648,42 @@ plugin.storedSetting = function(key, isValid, dflt) { return ((typeof v == 'string') && isValid(v)) ? v : dflt; }; +plugin.storedFilters = function() { + var dflt = {status: [], label: [], tracker: []}; + var v = theWebUI.settings['webui.mobile.filters']; + if (typeof v == 'string') { + try { + v = JSON.parse(v); + } catch (e) { + return dflt; + } + } + if (!v || (typeof v != 'object') || $.isArray(v)) { + return dflt; + } + var result = { + status: [], + label: [], + tracker: [] + }; + if ($.isArray(v.status)) { + result.status = $.grep(v.status, function(s, i) { + return (typeof s == 'string') && Object.prototype.hasOwnProperty.call(plugin.statusFilterClasses, s) && ($.inArray(s, v.status) === i); + }); + } + if ($.isArray(v.label)) { + result.label = $.grep(v.label, function(l, i) { + return (typeof l == 'string') && ($.inArray(l, v.label) === i); + }); + } + if ($.isArray(v.tracker)) { + result.tracker = $.grep(v.tracker, function(t, i) { + return (typeof t == 'string') && ($.inArray(t, v.tracker) === i); + }); + } + return result; +}; + // Re-fetch the stored settings and merge them in before saving, so this // long-lived mobile session doesn't overwrite newer settings saved from // the desktop UI (its own snapshot may be stale) -- same reason as the @@ -1893,6 +1932,14 @@ plugin.processTorrents = function(torrents, singleUpdate) { v.hash = n; v.status = theWebUI.getStatusIcon(v)[1]; torrentArray.push(v); + if (v.label) { + if (plugin.labelIds[v.label] == undefined) { + plugin.labelIds[v.label] = plugin.nextLabelId++; + } + if ($.grep(plugin.labelList, function(o) {return o.name === v.label;}).length === 0) { + plugin.labelList.push({name: v.label, count: 0}); + } + } }); torrentArray.sort(plugin.dynamicSort(plugin.sort)); @@ -2400,6 +2447,7 @@ plugin.init = function() { // plugin.sort. Restored before the first render so the list comes up // in the order the user left it this.sort = this.storedSetting('sort', this.isKnownSort, this.sort); + this.filters = this.storedFilters(); // The desktop engine keeps running underneath the mobile UI and can // auto-save the whole webui.* settings snapshot without user interaction diff --git a/tests/plugins/mobile/settings.spec.js b/tests/plugins/mobile/settings.spec.js index 7cf6425c9..5512d22be 100644 --- a/tests/plugins/mobile/settings.spec.js +++ b/tests/plugins/mobile/settings.spec.js @@ -153,4 +153,139 @@ describe("mobile plugin settings", () => { expect($("#sort_asc").prop("checked")).toBe(true); }); }); + + describe("storedFilters", () => { + it("returns the stored filters when valid JSON is stored", () => { + window.theWebUI.settings["webui.mobile.filters"] = JSON.stringify({ + status: ["downloading", "completed"], + label: ["music", ""], + tracker: ["tracker.example.com"], + }); + expect(plugin.storedFilters()).toEqual({ + status: ["downloading", "completed"], + label: ["music", ""], + tracker: ["tracker.example.com"], + }); + }); + + it("filters out unknown status options and non-string values", () => { + window.theWebUI.settings["webui.mobile.filters"] = JSON.stringify({ + status: ["downloading", "bogus_status", 123, "completed"], + label: ["music", 456, null, ""], + tracker: ["tracker.example.com", false], + }); + expect(plugin.storedFilters()).toEqual({ + status: ["downloading", "completed"], + label: ["music", ""], + tracker: ["tracker.example.com"], + }); + }); + + it("deduplicates values in filter arrays", () => { + window.theWebUI.settings["webui.mobile.filters"] = JSON.stringify({ + status: ["downloading", "downloading"], + label: ["music", "music"], + tracker: ["tracker.example.com", "tracker.example.com"], + }); + expect(plugin.storedFilters()).toEqual({ + status: ["downloading"], + label: ["music"], + tracker: ["tracker.example.com"], + }); + }); + + it("falls back to default empty filters when nothing is stored", () => { + expect(plugin.storedFilters()).toEqual({ + status: [], + label: [], + tracker: [], + }); + }); + + it("falls back to default empty filters for invalid JSON", () => { + window.theWebUI.settings["webui.mobile.filters"] = "not-a-json-string"; + expect(plugin.storedFilters()).toEqual({ + status: [], + label: [], + tracker: [], + }); + }); + + it("falls back to default empty filters for non-string / non-object values", () => { + window.theWebUI.settings["webui.mobile.filters"] = 42; + expect(plugin.storedFilters()).toEqual({ + status: [], + label: [], + tracker: [], + }); + + window.theWebUI.settings["webui.mobile.filters"] = JSON.stringify(["status", "downloading"]); + expect(plugin.storedFilters()).toEqual({ + status: [], + label: [], + tracker: [], + }); + }); + }); + + describe("filterDone", () => { + beforeEach(() => { + plugin.showList = jest.fn(); + plugin.filters = { status: [], label: [], tracker: [] }; + plugin.filtersSnapshot = null; + }); + + it("persists the filters when changed from snapshot", () => { + plugin.filtersSnapshot = { status: [], label: [], tracker: [] }; + plugin.filters = { status: ["downloading"], label: ["music"], tracker: [] }; + + plugin.filterDone(); + + expect(saveCount).toBe(1); + expect(JSON.parse(storedOnServer["webui.mobile.filters"])).toEqual({ + status: ["downloading"], + label: ["music"], + tracker: [], + }); + expect(plugin.filtersSnapshot).toBeNull(); + expect(plugin.showList).toHaveBeenCalled(); + }); + + it("does not re-save when filters are unchanged from snapshot", () => { + plugin.filtersSnapshot = { status: ["downloading"], label: ["music"], tracker: [] }; + plugin.filters = { status: ["downloading"], label: ["music"], tracker: [] }; + + plugin.filterDone(); + + expect(saveCount).toBe(0); + expect(plugin.filtersSnapshot).toBeNull(); + expect(plugin.showList).toHaveBeenCalled(); + }); + }); + + describe("applyFilters with saved filters", () => { + it("does not drop saved label and tracker filters before torrents are loaded", () => { + plugin.torrents = null; + plugin.labelList = []; + plugin.trackerList = []; + plugin.filters = { status: ["downloading"], label: ["music"], tracker: ["tr.example.com"] }; + + plugin.applyFilters(); + + expect(plugin.filters.label).toEqual(["music"]); + expect(plugin.filters.tracker).toEqual(["tr.example.com"]); + }); + + it("prunes absent label and tracker filters once torrents are loaded", () => { + plugin.torrents = { h1: { name: "test", size: 100 } }; + plugin.labelList = [{ name: "other", count: 1 }]; + plugin.trackerList = [{ name: "tr2.example.com", count: 1 }]; + plugin.filters = { status: ["downloading"], label: ["music"], tracker: ["tr.example.com"] }; + + plugin.applyFilters(); + + expect(plugin.filters.label).toEqual([]); + expect(plugin.filters.tracker).toEqual([]); + }); + }); }); From 06298be3d3d3699101b7712e460641a29954ca07 Mon Sep 17 00:00:00 2001 From: xirvik Date: Sun, 22 Nov 2026 20:53:03 +0000 Subject: [PATCH 2/2] mobile: test that stored filters are restored on load --- tests/plugins/mobile/settings.spec.js | 51 +++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/plugins/mobile/settings.spec.js b/tests/plugins/mobile/settings.spec.js index 5512d22be..e10563cc6 100644 --- a/tests/plugins/mobile/settings.spec.js +++ b/tests/plugins/mobile/settings.spec.js @@ -288,4 +288,55 @@ describe("mobile plugin settings", () => { expect(plugin.filters.tracker).toEqual([]); }); }); + + describe("init", () => { + const label = 'tv "4k" \\ archive'; + let ajaxSpy; + + beforeEach(() => { + window.theUILang.All = "All"; + window.theUILang.No_label = "No label"; + window.theUILang.Total = "Total"; + window.theUILang.Stopped = "Stopped"; + window.thePlugins = { get: () => null }; + window.cacheBust = (url) => url; + // The page markup arrives over ajax; this spec only needs what init + // restores before that request + ajaxSpy = jest.spyOn($, "ajax").mockImplementation(() => {}); + $("body").append( + '
' + + '
' + + '
' + ); + plugin.filters = { status: [], label: [], tracker: [] }; + }); + afterEach(() => { + ajaxSpy.mockRestore(); + $(window).off("hashchange"); + $("#initFilterUi").remove(); + }); + + it("restores the stored filters on load", () => { + window.theWebUI.settings["webui.mobile.filters"] = JSON.stringify({ + status: ["stopped"], + label: [label], + tracker: [], + }); + + plugin.init(); + + expect(plugin.filters).toEqual({ status: ["stopped"], label: [label], tracker: [] }); + + plugin.torrents = null; + plugin.labelList = [{ name: "other", count: 1 }, { name: label, count: 1 }]; + plugin.trackerList = []; + plugin.updateFilterButton($()); + plugin.renderFilterPage(); + + expect($("#filterText").text()).toBe("Stopped ยท " + label); + expect($("#filterStatusList .active").text()).toBe("0Stopped"); + expect($("#filterLabelsList .active").text()).toBe("1" + label); + expect($("#filterTrackersList .active").text()).toBe("0All"); + }); + }); });